structure packing and misaligned members

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

structure packing and misaligned members

edA-qa mort-ora-y
I'm trying to determine how to do my packed structure compilation and am
a bit unclear about how packed structures are handled. Consider this
structure:

%struct.packed = type <{ i8, i32 }>

Now if I get a pointer to the i32 element the pointer will not be
properly aligned for a typical i32. On my current machine (x86_64) the
cpu doesn't seem to care, but I suspect this isn't true on all platforms
(not is true on this platform if I do atomic operations). Yet the
pointer for this i32 which I get is the same as a normal i32 pointer.

Is there supposed to be a special marker for possibly misaligned
pointers? Or how I am supposed to track such misaligned pointers and
ensure they aren't used incorrectly?


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (269 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: structure packing and misaligned members

Duncan Sands
Hi edA-qa mort-ora-y,

On 12/05/13 17:13, edA-qa mort-ora-y wrote:

> I'm trying to determine how to do my packed structure compilation and am
> a bit unclear about how packed structures are handled. Consider this
> structure:
>
> %struct.packed = type <{ i8, i32 }>
>
> Now if I get a pointer to the i32 element the pointer will not be
> properly aligned for a typical i32. On my current machine (x86_64) the
> cpu doesn't seem to care, but I suspect this isn't true on all platforms
> (not is true on this platform if I do atomic operations). Yet the
> pointer for this i32 which I get is the same as a normal i32 pointer.
>
> Is there supposed to be a special marker for possibly misaligned
> pointers? Or how I am supposed to track such misaligned pointers and
> ensure they aren't used incorrectly?

in LLVM, pointer types don't have an alignment associated with them.
Instead it is pointer operations that have an alignment (when relevant),
for example load and store instructions have an optional alignment.  So
you need to put the alignment information onto the loads and stores that
you generate in your front-end.

Ciao, Duncan.

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: structure packing and misaligned members

edA-qa mort-ora-y
On 12/05/13 17:50, Duncan Sands wrote:
> in LLVM, pointer types don't have an alignment associated with them.
> Instead it is pointer operations that have an alignment (when relevant),
> for example load and store instructions have an optional alignment.  So
> you need to put the alignment information onto the loads and stores that
> you generate in your front-end.

Okay, so I will track that on my own. Though I'm a bit confused then by
what clang is emitted for packed C structures. It appears an "int*" is
always loaded with "align 8" regardless of where it comes from. I know C
says you have to be responsible for your own alignment, so is this
result from clang correct?


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (269 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: structure packing and misaligned members

Tim Northover-2
Hi,

> Okay, so I will track that on my own. Though I'm a bit confused then by
> what clang is emitted for packed C structures. It appears an "int*" is
> always loaded with "align 8" regardless of where it comes from. I know C
> says you have to be responsible for your own alignment, so is this
> result from clang correct?

Yes. The various packed-struct extensions aren't specified very well
since they're not in the standard, but in general I think you only get
(guaranteed) sane behaviour if you access their members through an
lvalue whose type is related to that packed struct. So given:

struct __attribute__((packed)) Mine { char a; int b; };

the following works:

int foo(struct Mine *m) {
  return m->b;
}

but this won't necessarily work:

int foo(struct Mine *m) {
  int *b_pointer = &m->b
  return *b_pointer;
}

Tim.
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev