[llvm-dev] Constant unions are incredibly hard to work with - do they need to be?

Christoffer Lernö via llvm-dev llvm-dev at lists.llvm.org
Thu Sep 16 12:54:39 PDT 2021


I just cleaned up another bug in my constant initializer code. Basically I forgot that when I have an extremely simple thing like this:

struct Baz
{
    char c;
    union
    {
        int a;
        double b;
    };
    int z;
};

And I initialize it like this:

struct Baz baz = { .a = 3, .z = 4 };

In this case I get  the ”wrong” automatic offset for a, so I need to add some extra padding.

This is not the only thing I’ve overlooked in this code. I likely have quite a few more that I need to handle.

But this begs the question: why isn’t LLVM offer a better interface for this, at least the case of creating a constant union value.

Clang has a lot of code just trying to do this well. Code that every other frontend that has unions must duplicate. Would it be complex to push this into LLVM?


More information about the llvm-dev mailing list