[LLVMdev] Target backend not converting char* to struct properly.

Eli Friedman eli.friedman at gmail.com
Fri Oct 12 13:01:40 PDT 2012


On Fri, Oct 12, 2012 at 10:43 AM, Stephen McGruer
<stephen.mcgruer at gmail.com> wrote:
> I'm having trouble getting my backend to properly treat the situation where
> a char* is converted into a struct, i.e. something like:
>
> char* pointer_representation = ...;
> MyStruct* my_struct = (MyStruct*) pointer_representation;
> my_struct->an_int_field = 5;
>
> When this occurs, LLVM seems to fold the struct and char* into one assembly
> 'object', which is perfectly fine. However, it also keeps the 1-alignment of
> the char array, which is not ok, as then the assignment (store) to the
> 'an_int_field' gets converted into a 4-byte aligned store instruction -
> which of course fails when run.
>
> My test case can be seen at http://pastebin.com/zPTi2GRW. Does anyone know
> what I might need to do in my backend to make this work properly - perhaps
> correct the definition of my 'store' instructions, or set a data-layout for
> a struct type somewhere?

Your code has undefined behavior; you're just getting lucky if it
works anywhere.  (I can look up the standard reference if you're
interested.)  Try __attribute((aligned)) on the array.

-Eli



More information about the llvm-dev mailing list