[LLVMdev] creating an empty struct field / maximally aligned end of struct

Duncan Sands baldrick at free.fr
Thu May 23 02:09:02 PDT 2013

Hi edA-qa mort-ora-y,

On 23/05/13 05:41, edA-qa mort-ora-y wrote:
> I have a variable length struct where an arbitrary struct will be at the
> end of the first one. I'm trying to find the best way to deal with this
> at the LLVM API level without needing to do all the alignment
> calculations myself.

when you create an object of the type (a global variable or a stack variable)
you should just explicitly specify the alignment you want.

> My first approach is to just put a pointer at the end of the structure,
> but then I can't calculate the "base" size from this structure. The
> other approach is to leave off this final field and then calculate the
> maximum alignment offset myself (it works, but doesn't take advantage of
> LLVM's struct calculations).
> Is there perhaps a way to add a maximally aligned zero-sized element to
> the struct? I could add this to the end and then get what I want: the
> size would be of the base, and the address of this final element would
> be the variable part.

In my opinion it is a mistake to try to attach the desired alignment to the
type (eg by playing games with zero sized objects).  It is like trying to
attach signed/unsigned info to LLVM's integer types when instead signedness
information should go on operations.  Analogously, you should instead specify
the alignment you want when you create objects of the type.

Ciao, Duncan.

More information about the llvm-dev mailing list