Skip to content

Commit 44e875a

Browse files
committed
[tsan] Cast floating-point types correctly when instrumenting atomic accesses, LLVM part
Although rare, atomic accesses to floating-point types seem to be valid, i.e. `%a = load atomic float ...`. The TSan instrumentation pass however tries to emit inttoptr, which is incorrect, we should use a bitcast here. Anyway, IRBuilder already has a convenient helper function for this. Differential Revision: https://reviews.llvm.org/D26266 llvm-svn: 286135
1 parent f530e8b commit 44e875a

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,6 @@ bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
549549
return false;
550550
}
551551

552-
static Value *createIntOrPtrToIntCast(Value *V, Type* Ty, IRBuilder<> &IRB) {
553-
return isa<PointerType>(V->getType()) ?
554-
IRB.CreatePtrToInt(V, Ty) : IRB.CreateIntCast(V, Ty, false);
555-
}
556-
557552
// Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
558553
// standards. For background see C++11 standard. A slightly older, publicly
559554
// available draft of the standard (not entirely up-to-date, but close enough
@@ -576,15 +571,9 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
576571
Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
577572
createOrdering(&IRB, LI->getOrdering())};
578573
Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
579-
if (Ty == OrigTy) {
580-
Instruction *C = CallInst::Create(TsanAtomicLoad[Idx], Args);
581-
ReplaceInstWithInst(I, C);
582-
} else {
583-
// We are loading a pointer, so we need to cast the return value.
584-
Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
585-
Instruction *Cast = CastInst::Create(Instruction::IntToPtr, C, OrigTy);
586-
ReplaceInstWithInst(I, Cast);
587-
}
574+
Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
575+
Value *Cast = IRB.CreateBitOrPointerCast(C, OrigTy);
576+
I->replaceAllUsesWith(Cast);
588577
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
589578
Value *Addr = SI->getPointerOperand();
590579
int Idx = getMemoryAccessFuncIndex(Addr, DL);
@@ -595,7 +584,7 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
595584
Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
596585
Type *PtrTy = Ty->getPointerTo();
597586
Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
598-
createIntOrPtrToIntCast(SI->getValueOperand(), Ty, IRB),
587+
IRB.CreateBitOrPointerCast(SI->getValueOperand(), Ty),
599588
createOrdering(&IRB, SI->getOrdering())};
600589
CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args);
601590
ReplaceInstWithInst(I, C);
@@ -626,9 +615,9 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
626615
Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
627616
Type *PtrTy = Ty->getPointerTo();
628617
Value *CmpOperand =
629-
createIntOrPtrToIntCast(CASI->getCompareOperand(), Ty, IRB);
618+
IRB.CreateBitOrPointerCast(CASI->getCompareOperand(), Ty);
630619
Value *NewOperand =
631-
createIntOrPtrToIntCast(CASI->getNewValOperand(), Ty, IRB);
620+
IRB.CreateBitOrPointerCast(CASI->getNewValOperand(), Ty);
632621
Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
633622
CmpOperand,
634623
NewOperand,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: opt < %s -tsan -S | FileCheck %s
2+
; Check that atomic memory operations on floating-point types are converted to calls into ThreadSanitizer runtime.
3+
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-S128"
4+
5+
define float @load_float(float* %fptr) {
6+
%v = load atomic float, float* %fptr unordered, align 4
7+
ret float %v
8+
; CHECK-LABEL: load_float
9+
; CHECK: call i32 @__tsan_atomic32_load(i32* %{{.+}}, i32 0)
10+
; CHECK: bitcast i32 {{.+}} to float
11+
}
12+
13+
define double @load_double(double* %fptr) {
14+
%v = load atomic double, double* %fptr unordered, align 8
15+
ret double %v
16+
; CHECK-LABEL: load_double
17+
; CHECK: call i64 @__tsan_atomic64_load(i64* %{{.+}}, i32 0)
18+
; CHECK: bitcast i64 {{.+}} to double
19+
}
20+
21+
define fp128 @load_fp128(fp128* %fptr) {
22+
%v = load atomic fp128, fp128* %fptr unordered, align 16
23+
ret fp128 %v
24+
; CHECK-LABEL: load_fp128
25+
; CHECK: call i128 @__tsan_atomic128_load(i128* %{{.+}}, i32 0)
26+
; CHECK: bitcast i128 {{.+}} to fp128
27+
}
28+
29+
define void @store_float(float* %fptr, float %v) {
30+
store atomic float %v, float* %fptr unordered, align 4
31+
ret void
32+
; CHECK-LABEL: store_float
33+
; CHECK: bitcast float %v to i32
34+
; CHECK: call void @__tsan_atomic32_store(i32* %{{.+}}, i32 %{{.+}}, i32 0)
35+
}
36+
37+
define void @store_double(double* %fptr, double %v) {
38+
store atomic double %v, double* %fptr unordered, align 8
39+
ret void
40+
; CHECK-LABEL: store_double
41+
; CHECK: bitcast double %v to i64
42+
; CHECK: call void @__tsan_atomic64_store(i64* %{{.+}}, i64 %{{.+}}, i32 0)
43+
}
44+
45+
define void @store_fp128(fp128* %fptr, fp128 %v) {
46+
store atomic fp128 %v, fp128* %fptr unordered, align 16
47+
ret void
48+
; CHECK-LABEL: store_fp128
49+
; CHECK: bitcast fp128 %v to i128
50+
; CHECK: call void @__tsan_atomic128_store(i128* %{{.+}}, i128 %{{.+}}, i32 0)
51+
}

0 commit comments

Comments
 (0)