edA-qa mort-ora-y
2013-May-23 03:41 UTC
[LLVMdev] creating an empty struct field / maximally aligned end of struct
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. 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. -- edA-qa mort-ora-y -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Sign: Please digitally sign your emails. Encrypt: I'm also happy to receive encrypted mail. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 261 bytes Desc: OpenPGP digital signature URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130523/a0cde387/attachment.sig>
Duncan Sands
2013-May-23 09:09 UTC
[LLVMdev] creating an empty struct field / maximally aligned end of struct
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.