This is an archive of the discontinued LLVM Phabricator instance.

[LangRef] Describe linkage types, allocation size of declarations for global variables
ClosedPublic

Authored by NeoRaider on Apr 27 2020, 12:21 PM.

Details

Summary

Linkage type was only referenced for functions, not for global variables.

Clarify that LLVM doesn't make assumption about the allocation size when no definitive initializer for a global variable is known.

Diff Detail

Event Timeline

NeoRaider created this revision.Apr 27 2020, 12:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2020, 12:21 PM
nikic added a subscriber: nikic.Apr 27 2020, 12:23 PM

I don't want to take this too far off track, but there are some additional caveats which might not be obvious to someone reading this rule:

  1. Global variables are not allowed to overlap. (This follows from the pointer aliasing rules.)
  2. The alignment does have to match the declared alignment. And the "declared" alignment is implicitly computed based on the type if it isn't explicitly specified.

Not sure if it makes sense to modify the language here.

llvm/docs/LangRef.rst
691

Not sure the italics are necessary.

  1. Global variables are not allowed to overlap. (This follows from the pointer aliasing rules.)

Makes sense, I'll add this.

  1. The alignment does have to match the declared alignment. And the "declared" alignment is implicitly computed based on the type if it isn't explicitly specified.

Question regarding alignment: I would expect that a variable declaration with lower alignment than the definition it refers to should be fine. For example, when declaring an extern global variable of a forward-declared struct type in C, clang will use an opaque type with alignment 1 for the declaration.

spatel added inline comments.Apr 28 2020, 5:13 AM
llvm/docs/LangRef.rst
693

Vocabulary nit: the LangRef has mostly avoided legalese to remain readable and approachable even for non-native English speakers, so I'd reduce this to something like:
"...LLVM makes no assumptions about the allocation sizes of the variables."

Question regarding alignment: I would expect that a variable declaration with lower alignment than the definition it refers to should be fine. For example, when declaring an extern global variable of a forward-declared struct type in C, clang will use an opaque type with alignment 1 for the declaration.

Sure, the alignment specified on a declaration can be lower than the alignment on the definition.

  • Changed wording as suggested by review
  • Mention overlap
  • Mention alignment
This revision is now accepted and ready to land.Apr 28 2020, 3:19 PM

Is there something left for me to do to have this change applied?

I'll commit it for you. How should I credit you on the git "Author" line?

Author: Matthias Schiffer <mschiffer@universe-factory.net>

This revision was automatically updated to reflect the committed changes.
nikic added a comment.Jan 31 2023, 1:50 AM

In getPointerDereferenceableBytes(), we currently assume that non-extern_weak globals are dereferenceable up to their type store size: https://github.com/llvm/llvm-project/blob/2153544865a9733b06579823814c981f735e4201/llvm/lib/IR/Value.cpp#L907

In ConstantFoldGetElementPtr(), we currently assume that GEPs without notional overindexing are inbounds of non-extern_weak globals: https://github.com/llvm/llvm-project/blob/2153544865a9733b06579823814c981f735e4201/llvm/lib/IR/ConstantFold.cpp#L2283 (Note that this code is broken for other reasons, it's missing a pointee type check -- this is why I'm looking into this in the first place.)

In getObjectSize() we do check for a definitive initializer: https://github.com/llvm/llvm-project/blob/a6f66d57e1d626a9b676f6e3ec0e084bdca3a934/llvm/lib/Analysis/MemoryBuiltins.cpp#L821

So I wanted to double check what the intended semantics here are. For a global without definitive initializer, do we assume that the global is *at least* as large as the value type, or do we allow it to be smaller? If the latter, then we need to fix our dereferencability assumptions.

Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 1:50 AM

I've put up D142970 as an incomplete patch to remove the dereferencability assumption. Not sure this is a good idea.