[llvm-dev] Is a global without definitive initializer not "sized"?

Johannes Doerfert via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 10 11:56:51 PST 2021


On 3/10/21 1:46 PM, Tim Northover wrote:
> On Wed, 10 Mar 2021 at 17:16, Johannes Doerfert via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
>> 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 :(

~ Johannes


> Cheers.
>
> Tim.


More information about the llvm-dev mailing list