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

Erik de Castro Lopo mle+cl at mega-nerd.com
Tue Sep 7 06:22:14 PDT 2010


Chris Lattner wrote:

> I removed unions from mainline in r112356.

Sorry for reviving this old thread, but I think the removal of
unions is a real pity.

I use Haskell to generate LLVM code using David Terei's LLVM
code from the GHC compiler (the compiler I'm working on is also
written in Haskell). Once I've generated LLVM IR code I use llc
to generate object code.

I'm currently use llvm-2.7 and have been using unions, not being
aware that they are going to be removed. The use case is for 
forcing field alignments in a packed struct to be correct for
32 and 64 bits. In particular I have a struct with an i32 tag
field followed by a pointer.

When generating 32 bit code the struct looks like:

     <{ i32, pointer }>

and for 64 bit code:

     <{ union { i32, i64 }, pointer }>

The nice thing about this is that in my LLVM code generator,
I have a function offsetOf which can get me the byte offset of
and element in a struct. In the case above,

    offsetOf (1)

returns 4 when generating 32 bit code and 8 when generating 64
bit code.

If there's another of guaranteeing struct alignment as well as
and easy way to get struct field offsets I'd like hear of it.
Otherwise, I'd like to know what needs to be done to get unions
back in LLVM.

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/



More information about the llvm-dev mailing list