Is a global without definitive initializer not "sized"?

Why do we require a definitive initializer for global variables to be able to determine their size?

The code in question is in `SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable`,
`llvm/lib/Analysis/MemoryBuiltins.cpp`.

~ Johannes

Pragmatically it's probably because we allow different modules to
declare globals with different types, as long as two don't claim to
provide canonical yet different versions. Once you decide that's
meaningful you pretty much have to say that a global that could end up
with a different type has unknown size (or you might speculate an
invalid load for example).

I don't know why that decision was made though, it seems very old.
I've never personally seen it exploited. From C, I *think* you might
be able to legitimately get the situation with a `static` variable
inside a C99 `inline` function. It'd be weird code though, and doesn't
actually motivate the size assumption part itself.

Cheers.

Tim.

Why do we require a definitive initializer for global variables to be
able to determine their size?

Pragmatically it's probably because we allow different modules to
declare globals with different types, as long as two don't claim to
provide canonical yet different versions. Once you decide that's
meaningful you pretty much have to say that a global that could end up
with a different type has unknown size (or you might speculate an
invalid load for example).

I don't know why that decision was made though, it seems very old.
I've never personally seen it exploited. From C, I *think* you might
be able to legitimately get the situation with a `static` variable
inside a C99 `inline` function. It'd be weird code though, and doesn't
actually motivate the size assumption part itself.

Unsure how the static variable has different sizes then but that is
beyond the point actually.

It is just unfortunate that we have such a restriction to (potentially)
support some corner cases. Sizes of a gobal is something we use
not only to do load speculation but also in alias analysis queries,
e.g., something that is at least X bytes big cannot alias something
that is at most Y bytes if x > Y. I've seen this to be hampered by the
definitive initializer quite a bit :frowning:

~ Johannes