[LLVMdev] Union type, is it really used or necessary?

Dan Gohman gohman at apple.com
Wed Sep 8 11:38:58 PDT 2010


On Sep 8, 2010, at 4:49 AM, Erik de Castro Lopo wrote:

> David Given wrote:
> 
>> On 07/09/10 14:22, Erik de Castro Lopo wrote:
>> [...]
>>> When generating 32 bit code the struct looks like:
>>> 
>>>     <{ i32, pointer }>
>>> 
>>> and for 64 bit code:
>>> 
>>>     <{ union { i32, i64 }, pointer }>
>> 
>> Surely LLVM will cause the first structure to be correctly aligned on
>> 64-bit platforms by automatically inserting padding? Is explicit
>> alignment by the user really necessary?
> 
> You missed the point. a struct defined with just parentheses like
> 
>     { i32, pointer }
> 
> may be padded. A struct defined with angle brackets and parentheses
> 
>     <{ i32, pointer }>
> 
> is a packed struct, guaranteed not to have padding.
> 
> See:
> 
>    http://llvm.org/docs/LangRef.html#t_pstruct

>From what you've described so far, regular non-packed structs
would be a much better approach than packed structs with unions,
even in LLVM versions which support unions. The output would be
more readable and you'd get better optimization.

I don't know how much work it would involve for you to change
whatever assumptions you have in your code about packed structs, but
if you're considering doing work in LLVM to re-introduce and finish
unions, it's something to consider.

Dan





More information about the llvm-dev mailing list