Last time I looked at the union stuff, I was trying to decide how to implement TargetData.cpp for unions, and whether or not to copy the way structs handled memory layout. Currently structs have an auxilliary data structure (StructLayout) that is used to cache the overall size of the struct and the offset of each member. In the case of unions, it doesn’t need the offsets, since they are always zero, but it does need to calculate the overall size (with alignment considerations). The alternative is to simply recalculate the union size each time it is needed – it does seem like a lot of extra complexity to have a whole cache just for the size.
At the moment, my LLVM-related project is on hiatus - After two years of working on it I decided to take a few months off to recharge my creative batteries before tackling the next phase (which is garbage collection - I did make pretty good progress generating the tables for direct probing of stack roots, without needing shadow-stack.)
As a first implementation it doesn't seem like too much to recalculate
the union size every time, usually unions have less than a few dozen
I don't know how the lowering do the struct (and I'm on holiday now,
so hard to check), but an extra method on UnionType (or whatever
reflects on the lowering code) seems simple enough to return the size,
calculated on the c-tor or dynamically, via getPrimitiveSizeInBits()
method on each field.
BTW, I don’t know if I mentioned this but thank you for the patch. Do you know if you will be doing any further work on unions? I’m trying to decide / understand what to work on next - unfortunately I’m not all that familiar with the LLVM backends and code generators, I’m mostly a front end guy.