[LLVMdev] Suggestion: Support union types in IR
Jon Harrop
jon at ffconsultancy.com
Fri Jan 2 14:29:57 PST 2009
On Friday 02 January 2009 20:48:25 Chris Lattner wrote:
> On Jan 1, 2009, at 6:25 AM, Jon Harrop wrote:
> >> Exactly. I'm not especially interested in C-style unions, I'm
> >> interested in discriminated unions. But the actual discriminator field is
> >> easily represented in LLVM IR already, so there's no need to extend the
> >> IR to support them. That's why I am only asking for C-style union
> >> support - it's a lower-level primitive that you can use to build
> >> discriminated unions on top of.
> >
> > I don't think you would want to build discriminated unions on top of
> > C-style unions though.
>
> Why?
Uniformity when nesting and space efficiency. Users of a language front-end
will want to nest discriminated unions, e.g. to manipulate trees. C-style
unions are designed to be unboxed so they are useless for that unless you
introduce pointer indirections but if you store all unions by reference then
the uniformity of the 1-word pointer size obviates the need to use same
(largest) size union cases so you would drop C-style unions in favor of a
more space-efficient alternative anyway. Otherwise, you might opt for a
non-uniform representation where local unions are unboxed but unions stored
within them are boxed, which is needless complexity but it might buy a little
performance.
So I think most LLVM users will represent their discriminated unions as
something like:
{i32, {..} *}
or:
{i32, {..}} *
but not as C-style unions.
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
More information about the llvm-dev
mailing list