Skip to content

Commit ff2fb2a

Browse files
authored
[TSan] Fix atomicrmw xchg with pointer and floats (#85228)
atomicrmw xchg also accepts pointer and floating-point values. To handle those, insert necessary casts to and from integer. This is what we do for cmpxchg as well. Fixes #85226.
1 parent 0a739eb commit ff2fb2a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,12 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
752752
const unsigned ByteSize = 1U << Idx;
753753
const unsigned BitSize = ByteSize * 8;
754754
Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
755-
Value *Args[] = {Addr,
756-
IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
755+
Value *Val = RMWI->getValOperand();
756+
Value *Args[] = {Addr, IRB.CreateBitOrPointerCast(Val, Ty),
757757
createOrdering(&IRB, RMWI->getOrdering())};
758-
CallInst *C = CallInst::Create(F, Args);
759-
ReplaceInstWithInst(I, C);
758+
Value *C = IRB.CreateCall(F, Args);
759+
I->replaceAllUsesWith(IRB.CreateBitOrPointerCast(C, Val->getType()));
760+
I->eraseFromParent();
760761
} else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
761762
Value *Addr = CASI->getPointerOperand();
762763
Type *OrigOldValTy = CASI->getNewValOperand()->getType();

llvm/test/Instrumentation/ThreadSanitizer/atomic.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ entry:
7878
; CHECK-LABEL: atomic8_xchg_monotonic
7979
; CHECK: call i8 @__tsan_atomic8_exchange(ptr %a, i8 0, i32 0), !dbg
8080

81+
define void @atomic8_xchg_monotonic_ptr(ptr %a, ptr %b) nounwind uwtable {
82+
entry:
83+
atomicrmw xchg ptr %a, ptr %b monotonic, !dbg !7
84+
ret void, !dbg !7
85+
}
86+
; CHECK-LABEL: atomic8_xchg_monotonic_ptr
87+
; CHECK: [[ARG:%.*]] = ptrtoint ptr %b to i64, !dbg
88+
; CHECK: [[RES:%.*]] = call i64 @__tsan_atomic64_exchange(ptr %a, i64 [[ARG]], i32 0), !dbg
89+
; CHECK: [[CAST:%.*]] = inttoptr i64 [[RES]] to ptr, !dbg
90+
91+
define void @atomic8_xchg_monotonic_float(ptr %a, float %b) nounwind uwtable {
92+
entry:
93+
atomicrmw xchg ptr %a, float %b monotonic, !dbg !7
94+
ret void, !dbg !7
95+
}
96+
; CHECK-LABEL: atomic8_xchg_monotonic_float
97+
; CHECK: [[ARG:%.*]] = bitcast float %b to i32, !dbg
98+
; CHECK: [[RES:%.*]] = call i32 @__tsan_atomic32_exchange(ptr %a, i32 [[ARG]], i32 0), !dbg
99+
; CHECK: [[CAST:%.*]] = bitcast i32 [[RES]] to float, !dbg
100+
81101
define void @atomic8_add_monotonic(ptr %a) nounwind uwtable {
82102
entry:
83103
atomicrmw add ptr %a, i8 0 monotonic, !dbg !7

0 commit comments

Comments
 (0)