Skip to content

[Attributor] Do not optimize away externally_initialized loads. #128170

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 5 commits into from
Mar 3, 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
14 changes: 9 additions & 5 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,15 @@ AA::getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA,
if (!Initializer)
return nullptr;
} else {
if (!GV->hasLocalLinkage() &&
(GV->isInterposable() || !(GV->isConstant() && GV->hasInitializer())))
return nullptr;
if (!GV->hasInitializer())
return UndefValue::get(&Ty);
if (!GV->hasLocalLinkage()) {
// Externally visible global that's either non-constant,
// or a constant with an uncertain initializer.
if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
return nullptr;
}

// Globals with local linkage are always initialized.
assert(!GV->hasLocalLinkage() || GV->hasInitializer());

if (!Initializer)
Initializer = GV->getInitializer();
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/Transforms/Attributor/value-simplify.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ declare ptr @llvm.call.preallocated.arg(token, i32)
@ConstPtr = constant i32 0, align 4
@ConstWeakPtr = weak constant i32 0, align 4
@ConstWeakODRPtr = weak_odr constant i32 0, align 4
@ExtInitZeroInit = externally_initialized constant i32 zeroinitializer, align 4

;.
; CHECK: @str = private unnamed_addr addrspace(4) constant [1 x i8] zeroinitializer, align 1
; CHECK: @ConstAS3Ptr = addrspace(3) global i32 0, align 4
; CHECK: @ConstPtr = constant i32 0, align 4
; CHECK: @ConstWeakPtr = weak constant i32 0, align 4
; CHECK: @ConstWeakODRPtr = weak_odr constant i32 0, align 4
; CHECK: @ExtInitZeroInit = externally_initialized constant i32 0, align 4
; CHECK: @S = external global %struct.X
; CHECK: @g = internal constant { [2 x ptr] } { [2 x ptr] [ptr @f1, ptr @f2] }
; CHECK: @x = external global i32
Expand Down Expand Up @@ -1651,6 +1653,23 @@ define i32 @readWeakOdrConst() {
ret i32 %l
}

define i32 @readExtInitZeroInit() {
; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
; TUNIT-LABEL: define {{[^@]+}}@readExtInitZeroInit
; TUNIT-SAME: () #[[ATTR2]] {
; TUNIT-NEXT: [[L:%.*]] = load i32, ptr @ExtInitZeroInit, align 4
; TUNIT-NEXT: ret i32 [[L]]
;
; CGSCC: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
; CGSCC-LABEL: define {{[^@]+}}@readExtInitZeroInit
; CGSCC-SAME: () #[[ATTR1]] {
; CGSCC-NEXT: [[L:%.*]] = load i32, ptr @ExtInitZeroInit, align 4
; CGSCC-NEXT: ret i32 [[L]]
;
%l = load i32, ptr @ExtInitZeroInit
ret i32 %l
}

;.
; TUNIT: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn }
; TUNIT: attributes #[[ATTR1]] = { memory(readwrite, argmem: none) }
Expand Down