Skip to content

Commit a8ec6e8

Browse files
authored
[IR] Require that global value initializers are sized (#137358)
While external globals can be unsized, I don't think an unsized initializer makes sense. It seems like the backend currently ends up treating this as a zero-size global. If people want that behavior, they should declare it as such.
1 parent 140c2b6 commit a8ec6e8

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

llvm/docs/LangRef.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ Global Variables
700700
Global variables define regions of memory allocated at compilation time
701701
instead of run-time.
702702

703-
Global variable definitions must be initialized.
703+
Global variable definitions must be initialized with a sized value.
704704

705705
Global variables in other translation units can also be declared, in which
706706
case they don't have an initializer.

llvm/lib/IR/Verifier.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
835835
"Global variable initializer type does not match global "
836836
"variable type!",
837837
&GV);
838+
Check(GV.getInitializer()->getType()->isSized(),
839+
"Global variable initializer must be sized", &GV);
838840
// If the global has common linkage, it must have a zero initializer and
839841
// cannot be constant.
840842
if (GV.hasCommonLinkage()) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
3+
@g = global target("opaque") undef
4+
5+
; CHECK: Global variable initializer must be sized

llvm/test/Verifier/scalable-global-vars.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

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

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

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

0 commit comments

Comments
 (0)