[llvm-commits] RFC: initial union syntax support

Nick Lewycky nicholas at mxc.ca
Wed May 20 20:09:45 PDT 2009


Duncan Sands wrote:
> Hi Nick,
> 
>> Then it would seem I misunderstood the purpose of unions. I thought the 
>> problem was that it was impossible to declare a type which would be "as 
>> large as the largest of any of these" without having accurate 
>> TargetData. The union type was supposed to do that and nothing more.
> 
> I sent an example earlier showing that you can do this already without
> union types.

Close. Your trick does perform a ptrtoint which requires knowing what
int size is large enough. Fortunately in your case it's indexing off of
null so it's very unlikely that it won't fit in 16 bits or less, but
it's still not as good as a first-class union.

I've been thinking about the original suggestion and the reasons I 
objected to it. It seems that the original suggest was to think about a 
union as a structure where the offset into each element is zero instead 
of being contiguous to each other. That makes the original proposal make 
a whole lot more sense to me than it did originally.

Despite Chris' message to the contrary, I still think u{i32, i32} 
shouldn't be allowed (rather, it should be folded to u{i32} by the 
getter). We could provide an accessor that returns the element number 
for a given Type* and the only drawback is that it means doing an extra 
lookup through a small list. Allowing GEP makes sense, and unions should 
certainly be first class aggregates.

Nick




More information about the llvm-commits mailing list