Skip to content

Commit 1cf8c77

Browse files
nikictstellar
authored andcommitted
[GlobalOpt] Do not promote malloc if there are atomic loads/stores (llvm#137158)
When converting a malloc stored to a global into a global, we will introduce an i1 flag to track whether the global has been initialized. In case of atomic loads/stores, this will result in verifier failures, because atomic ops on i1 are illegal. Even if we changed this to i8, I don't think it is a good idea to change atomic types in that way. Instead, bail out of the transform is we encounter any atomic loads/stores of the global. Fixes llvm#137152. (cherry picked from commit 57530c2)
1 parent 24805c2 commit 1cf8c77

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,14 @@ static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
719719
const Value *P = Worklist.pop_back_val();
720720
for (const auto *U : P->users()) {
721721
if (auto *LI = dyn_cast<LoadInst>(U)) {
722+
if (!LI->isSimple())
723+
return false;
722724
SmallPtrSet<const PHINode *, 8> PHIs;
723725
if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
724726
return false;
725727
} else if (auto *SI = dyn_cast<StoreInst>(U)) {
728+
if (!SI->isSimple())
729+
return false;
726730
// Ignore stores to the global.
727731
if (SI->getPointerOperand() != P)
728732
return false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=globalopt -S < %s | FileCheck %s
3+
4+
@g = internal global ptr null, align 8
5+
6+
define void @init() {
7+
; CHECK-LABEL: define void @init() local_unnamed_addr {
8+
; CHECK-NEXT: [[ALLOC:%.*]] = call ptr @malloc(i64 48)
9+
; CHECK-NEXT: store atomic ptr [[ALLOC]], ptr @g seq_cst, align 8
10+
; CHECK-NEXT: ret void
11+
;
12+
%alloc = call ptr @malloc(i64 48)
13+
store atomic ptr %alloc, ptr @g seq_cst, align 8
14+
ret void
15+
}
16+
17+
define i1 @check() {
18+
; CHECK-LABEL: define i1 @check() local_unnamed_addr {
19+
; CHECK-NEXT: [[VAL:%.*]] = load atomic ptr, ptr @g seq_cst, align 8
20+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[VAL]], null
21+
; CHECK-NEXT: ret i1 [[CMP]]
22+
;
23+
%val = load atomic ptr, ptr @g seq_cst, align 8
24+
%cmp = icmp eq ptr %val, null
25+
ret i1 %cmp
26+
}
27+
28+
declare ptr @malloc(i64) allockind("alloc,uninitialized") allocsize(0)

0 commit comments

Comments
 (0)