Skip to content

Commit 5470dff

Browse files
authored
[Attributor] Do not optimize away externally_initialized loads. (#128170)
Fixes SWDEV-515029
1 parent 5030105 commit 5470dff

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
@@ -259,11 +259,15 @@ AA::getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA,
259259
if (!Initializer)
260260
return nullptr;
261261
} else {
262-
if (!GV->hasLocalLinkage() &&
263-
(GV->isInterposable() || !(GV->isConstant() && GV->hasInitializer())))
264-
return nullptr;
265-
if (!GV->hasInitializer())
266-
return UndefValue::get(&Ty);
262+
if (!GV->hasLocalLinkage()) {
263+
// Externally visible global that's either non-constant,
264+
// or a constant with an uncertain initializer.
265+
if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
266+
return nullptr;
267+
}
268+
269+
// Globals with local linkage are always initialized.
270+
assert(!GV->hasLocalLinkage() || GV->hasInitializer());
267271

268272
if (!Initializer)
269273
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
@@ -1651,6 +1653,23 @@ define i32 @readWeakOdrConst() {
16511653
ret i32 %l
16521654
}
16531655

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

0 commit comments

Comments
 (0)