[LLVMdev] Structure memory layout

Renato Golin renato.golin at arm.com
Tue Oct 19 06:38:27 PDT 2010


On 19 October 2010 13:28, Duncan Sands <baldrick at free.fr> wrote:
> Hi Jin Gu Kang
>
>>    5 %0 = type { i3, i4, i3, i2 }
>
> each of the above struct fields occupies 1 byte.  If you were hoping that an
> i3 field would occupy only 3 bits then I am sorry, it doesn't work that way.

Hi Duncan,

I'm not sure he meant that, but you are right, the container type is
lost in the process.

Jim,

Consider the examples:

struct testChar { char a:3;  char b:4;  char c:3;  char d:2; };
struct testInt { int a:3;  int b:4;  int c:3;  int d:2; };

In your IR, both would be represented by:

%0 = type { i3, i4, i3, i2 }

But the first will actually be a 2-byte structure (as you can see on
LLVM's IR) and the second a 2/4/8-byte structure (depending on the
target). How would you distinguish them?

If LLVM types had alignment, you could represent as:

%Char = type { i3 align 1, i4 align 1, i3 align 1, i2 align 1 }
%Int = type { i3 align 4, i4 align 4, i3 align 4, i2 align 4 }

and even

%Short = type { i3 align 2, i4 align 2, i3 align 2, i2 align 2 }

for short fields. That would tell the codegen to fit as many i** into
the group of fields with the same alignment. It'd also help with
implementing zero sized bit-fields:

struct zero { int :0; short i; }
to:
%zero = type { i16 } align 32;

Type alignment would also help to remove the burden on carrying
alignment information throughout the life of a variable, as you can
only (and have to) emit it on instructions.

But wait, a word of caution, I haven't thought through properly to
assure you it's the best way, not even if it's safe. Others on the
list might know better.

cheers,
--renato




More information about the llvm-dev mailing list