Skip to content

[GlobalOpt] Do not promote malloc if there are atomic loads/stores #137158

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 1 commit into from
Apr 24, 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
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,14 @@ static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
const Value *P = Worklist.pop_back_val();
for (const auto *U : P->users()) {
if (auto *LI = dyn_cast<LoadInst>(U)) {
if (!LI->isSimple())
return false;
SmallPtrSet<const PHINode *, 8> PHIs;
if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
return false;
} else if (auto *SI = dyn_cast<StoreInst>(U)) {
if (!SI->isSimple())
return false;
// Ignore stores to the global.
if (SI->getPointerOperand() != P)
return false;
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/Transforms/GlobalOpt/malloc-promote-atomic.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes=globalopt -S < %s | FileCheck %s

@g = internal global ptr null, align 8

define void @init() {
; CHECK-LABEL: define void @init() local_unnamed_addr {
; CHECK-NEXT: [[ALLOC:%.*]] = call ptr @malloc(i64 48)
; CHECK-NEXT: store atomic ptr [[ALLOC]], ptr @g seq_cst, align 8
; CHECK-NEXT: ret void
;
%alloc = call ptr @malloc(i64 48)
store atomic ptr %alloc, ptr @g seq_cst, align 8
ret void
}

define i1 @check() {
; CHECK-LABEL: define i1 @check() local_unnamed_addr {
; CHECK-NEXT: [[VAL:%.*]] = load atomic ptr, ptr @g seq_cst, align 8
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[VAL]], null
; CHECK-NEXT: ret i1 [[CMP]]
;
%val = load atomic ptr, ptr @g seq_cst, align 8
%cmp = icmp eq ptr %val, null
ret i1 %cmp
}

declare ptr @malloc(i64) allockind("alloc,uninitialized") allocsize(0)
Loading