[LLVMdev] StructLayout

Chris Lattner clattner at apple.com
Tue Sep 22 13:47:12 PDT 2009


On Sep 22, 2009, at 1:27 PM, David Greene wrote:

> How are bitfields handled in StructLayout?  In LLVM 2.5 the
> struct is unambiguously size by:
>
>    StructSize += TD.getTypePaddedSize(Ty); // Consume space for this  
> data
>
> In LLVM 2.6 it's getTypeAllocSize, which does the same thing.
>
> Unfortunately, this is not correct for bitfields.  For example,
> LLVM reports this struct:
>
> typedef struct test1 {
> 	short f0 :  10;
> 	char f1 :  5;
> 	long f2 :  1;
> 	long f3 :  45;
> } test1_t;
>
> which in LLVM is:
>
> %test1 = type { i10, i5, i1, i45 }
>
> to have size 12 on x86-64 whien it should have size 8.

Fields like "i10" are not bitfields in the C sense.  they get  
individually padded out to the nearest byte, not packed together  
densely.  The C frontend is expected to lower the struct type as  
appropriate.  For example, clang lowers that type to:

%struct.test1 = type { [2 x i8], [6 x i8] }

> Does this need to be handled in the frontend, to munge the struct type
> and add the necessary masking and shifting so it works correctly?

Yep,

-Chris




More information about the llvm-dev mailing list