[cfe-dev] Struct initializer creating anon type

Richard Smith richard at metafoo.co.uk
Thu Jul 31 14:15:32 PDT 2014


On Wed, Jul 30, 2014 at 2:08 PM, Ghitulete Razvan <
razvan.ghitulete at gmail.com> wrote:

> Hi,
>
> I have an llvm pass, that goes through all the GlobalVariable objects
> inside a file and tries to match them to the corresponding type (at some
> point). And I have noticed the following behavior. For
>
> struct v {
>    int x:9;
>    ....
> };
> struct v var1 = {.x=0};
> struct v var2;
>
> The LLVM IR generated will declare variable var1 of an anonymous type
> instead of type struct v, while var2 is okay.
>
> %struct.v = type { [2 x i8], [6 x i8] }
> @var1 = global { i8, i8, [6 x i8] } { i8 0, i8 0, [6 x i8] undef }, align 8
> @var2 = common global %struct.v zeroinitializer, align 8
>
> This actually happens always when the variable is statically initialized,
> and the bitfield is longer than 8 bits.
>
> Does anyone have any idea why this is occurring,
>

It happens essentially because it's more convenient for us to generate IR
that way. There are no guarantees that IR struct types will match
source-level struct types in any way, or be consistent from use to use.
Another example:

typedef union { int n; float f; } U;
U u1 = { .n = 123 };
U u2 = { .f = 1.0f };

... gives ...

@u1 = global %union.U { i32 123 }, align 4
@u2 = global { float } { float 1.000000e+00 }, align 4

or more exactly where the code responsible for generating this part is?
>

lib/CodeGen/CGExprConstant.cpp -- look for ConstStructBuilder::Finalize.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140731/94897363/attachment.html>


More information about the cfe-dev mailing list