Skip to content

Commit 05c40eb

Browse files
author
Jin Huang
committed
[AA] A conservative fix for atomic instruciton.
1 parent 5bca8f2 commit 05c40eb

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

llvm/lib/Analysis/AliasAnalysis.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,18 @@ ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
421421
const MemoryLocation &Loc,
422422
AAQueryInfo &AAQI) {
423423
// Be conservative in the face of atomic.
424-
if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
424+
if (isStrongerThan(L->getOrdering(), AtomicOrdering::Monotonic))
425425
return ModRefInfo::ModRef;
426426

427+
// For Monotonic and unordered atomic loads, if the locations are not NoAlias,
428+
// we must be conservative and return ModRef to prevent unsafe reordering of
429+
// accesses to the same memory.
430+
if (L->isAtomic()){
431+
if (Loc.Ptr &&
432+
alias(MemoryLocation::get(L), Loc, AAQI, L) != AliasResult::NoAlias)
433+
return ModRefInfo::ModRef;
434+
}
435+
427436
// If the load address doesn't alias the given address, it doesn't read
428437
// or write the specified memory.
429438
if (Loc.Ptr) {
@@ -439,7 +448,7 @@ ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
439448
const MemoryLocation &Loc,
440449
AAQueryInfo &AAQI) {
441450
// Be conservative in the face of atomic.
442-
if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
451+
if (isStrongerThan(S->getOrdering(), AtomicOrdering::Monotonic))
443452
return ModRefInfo::ModRef;
444453

445454
if (Loc.Ptr) {
@@ -458,7 +467,7 @@ ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
458467
}
459468

460469
// Otherwise, a store just writes.
461-
return ModRefInfo::Mod;
470+
return ModRefInfo::ModRef;
462471
}
463472

464473
ModRefInfo AAResults::getModRefInfo(const FenceInst *S,

llvm/test/Transforms/DeadStoreElimination/atomic-todo.ll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
; XFAIL: *
21
; RUN: opt -passes=dse -S < %s | FileCheck %s
32

43
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
@@ -11,7 +10,7 @@ target triple = "x86_64-apple-macosx10.7.0"
1110
@x = common global i32 0, align 4
1211
@y = common global i32 0, align 4
1312

14-
; DSE across monotonic load (allowed as long as the eliminated store isUnordered)
13+
; DSE across monotonic load (allowed if the monotonic load's address is NoAlias)
1514
define i32 @test9() {
1615
; CHECK-LABEL: test9
1716
; CHECK-NOT: store i32 0
@@ -21,3 +20,11 @@ define i32 @test9() {
2120
store i32 1, ptr @x
2221
ret i32 %x
2322
}
23+
24+
; DSE across monotonic load (blocked if the atomic load's address isn't NoAlias)
25+
define i32 @test9a() {
26+
store i32 0, ptr @x
27+
%x = load atomic i32, ptr @ptr monotonic, align 4
28+
store i32 1, ptr @x
29+
ret i32 %x
30+
}

0 commit comments

Comments
 (0)