Skip to content

Commit d6895cd

Browse files
committed
[IR] Require that global value initializers are sized
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 b3529ed commit d6895cd

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
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()) {

llvm/test/Assembler/2005-05-05-OpaqueUndefValues.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
; RUN: verify-uselistorder %s
33

44
%t = type opaque
5-
@x = global %t undef
5+
6+
define void @test() {
7+
%sel = select i1 true, %t undef, %t poison
8+
ret void
9+
}

llvm/test/Transforms/InstSimplify/gv-alloca-cmp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define i1 @cmp_gv_weak_alloca() {
4646
}
4747

4848
%opaque = type opaque
49-
@gv_unsized = weak global %opaque zeroinitializer, align 16
49+
@gv_unsized = external global %opaque, align 16
5050

5151
define i1 @cmp_gv_unsized_alloca() {
5252
; CHECK-LABEL: define i1 @cmp_gv_unsized_alloca() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
3+
%t = type opaque
4+
@g = global %t undef
5+
6+
; 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)