Skip to content

Commit 7f95a73

Browse files
authored
(cherry-pick) [Attributor] Do not optimize away externally_initialized loads. (llvm#128170) (llvm#1078)
2 parents a84753e + e0a3e71 commit 7f95a73

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,15 @@ AA::getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA,
260260
if (!Initializer)
261261
return nullptr;
262262
} else {
263-
if (!GV->hasLocalLinkage() &&
264-
(GV->isInterposable() || !(GV->isConstant() && GV->hasInitializer())))
265-
return nullptr;
266-
if (!GV->hasInitializer())
267-
return UndefValue::get(&Ty);
263+
if (!GV->hasLocalLinkage()) {
264+
// Externally visible global that's either non-constant,
265+
// or a constant with an uncertain initializer.
266+
if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
267+
return nullptr;
268+
}
269+
270+
// Globals with local linkage are always initialized.
271+
assert(!GV->hasLocalLinkage() || GV->hasInitializer());
268272

269273
if (!Initializer)
270274
Initializer = GV->getInitializer();

llvm/test/Transforms/Attributor/value-simplify.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ declare ptr @llvm.call.preallocated.arg(token, i32)
1212
@ConstPtr = constant i32 0, align 4
1313
@ConstWeakPtr = weak constant i32 0, align 4
1414
@ConstWeakODRPtr = weak_odr constant i32 0, align 4
15+
@ExtInitZeroInit = externally_initialized constant i32 zeroinitializer, align 4
1516

1617
;.
1718
; CHECK: @str = private unnamed_addr addrspace(4) constant [1 x i8] zeroinitializer, align 1
1819
; CHECK: @ConstAS3Ptr = addrspace(3) global i32 0, align 4
1920
; CHECK: @ConstPtr = constant i32 0, align 4
2021
; CHECK: @ConstWeakPtr = weak constant i32 0, align 4
2122
; CHECK: @ConstWeakODRPtr = weak_odr constant i32 0, align 4
23+
; CHECK: @ExtInitZeroInit = externally_initialized constant i32 0, align 4
2224
; CHECK: @S = external global %struct.X
2325
; CHECK: @g = internal constant { [2 x ptr] } { [2 x ptr] [ptr @f1, ptr @f2] }
2426
; CHECK: @x = external global i32
@@ -1652,6 +1654,23 @@ define i32 @readWeakOdrConst() {
16521654
ret i32 %l
16531655
}
16541656

1657+
define i32 @readExtInitZeroInit() {
1658+
; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
1659+
; TUNIT-LABEL: define {{[^@]+}}@readExtInitZeroInit
1660+
; TUNIT-SAME: () #[[ATTR2]] {
1661+
; TUNIT-NEXT: [[L:%.*]] = load i32, ptr @ExtInitZeroInit, align 4
1662+
; TUNIT-NEXT: ret i32 [[L]]
1663+
;
1664+
; CGSCC: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
1665+
; CGSCC-LABEL: define {{[^@]+}}@readExtInitZeroInit
1666+
; CGSCC-SAME: () #[[ATTR1]] {
1667+
; CGSCC-NEXT: [[L:%.*]] = load i32, ptr @ExtInitZeroInit, align 4
1668+
; CGSCC-NEXT: ret i32 [[L]]
1669+
;
1670+
%l = load i32, ptr @ExtInitZeroInit
1671+
ret i32 %l
1672+
}
1673+
16551674
;.
16561675
; TUNIT: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn }
16571676
; TUNIT: attributes #[[ATTR1]] = { memory(readwrite, argmem: none) }

0 commit comments

Comments
 (0)