Skip to content

[IR] Require that global value initializers are sized #137358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ Global Variables
Global variables define regions of memory allocated at compilation time
instead of run-time.

Global variable definitions must be initialized.
Global variable definitions must be initialized with a sized value.

Global variables in other translation units can also be declared, in which
case they don't have an initializer.
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
"Global variable initializer type does not match global "
"variable type!",
&GV);
Check(GV.getInitializer()->getType()->isSized(),
"Global variable initializer must be sized", &GV);
// If the global has common linkage, it must have a zero initializer and
// cannot be constant.
if (GV.hasCommonLinkage()) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/Verifier/global-initializer-sized.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; RUN: not llvm-as < %s 2>&1 | FileCheck %s

@g = global target("opaque") undef

; CHECK: Global variable initializer must be sized
6 changes: 3 additions & 3 deletions llvm/test/Verifier/scalable-global-vars.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

; CHECK-NEXT: Globals cannot contain scalable types
; CHECK-NEXT: ptr @ScalableVecStructGlobal
@ScalableVecStructGlobal = global { i32, <vscale x 4 x i32> } zeroinitializer
@ScalableVecStructGlobal = external global { i32, <vscale x 4 x i32> }

; CHECK-NEXT: Globals cannot contain scalable types
; CHECK-NEXT: ptr @StructTestGlobal
Expand All @@ -23,9 +23,9 @@
; CHECK-NEXT: Globals cannot contain scalable types
; CHECK-NEXT: ptr @StructArrayTestGlobal
%struct.array.test = type { [2 x <vscale x 1 x double>] }
@StructArrayTestGlobal = global %struct.array.test zeroinitializer
@StructArrayTestGlobal = external global %struct.array.test

; CHECK-NEXT: Globals cannot contain scalable types
; CHECK-NEXT: ptr @StructTargetTestGlobal
%struct.target.test = type { target("aarch64.svcount"), target("aarch64.svcount") }
@StructTargetTestGlobal = global %struct.target.test zeroinitializer
@StructTargetTestGlobal = external global %struct.target.test