llvm.org GIT mirror llvm / 8882bb7
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286135 91177308-0d34-0410-b5e6-96231b3b80d8 Kuba Brecka 4 years ago
2 changed file(s) with 57 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
548548 return false;
549549 }
550550
551 static Value *createIntOrPtrToIntCast(Value *V, Type* Ty, IRBuilder<> &IRB) {
552 return isa(V->getType()) ?
553 IRB.CreatePtrToInt(V, Ty) : IRB.CreateIntCast(V, Ty, false);
554 }
555
556551 // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
557552 // standards. For background see C++11 standard. A slightly older, publicly
558553 // available draft of the standard (not entirely up-to-date, but close enough
575570 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
576571 createOrdering(&IRB, LI->getOrdering())};
577572 Type *OrigTy = cast(Addr->getType())->getElementType();
578 if (Ty == OrigTy) {
579 Instruction *C = CallInst::Create(TsanAtomicLoad[Idx], Args);
580 ReplaceInstWithInst(I, C);
581 } else {
582 // We are loading a pointer, so we need to cast the return value.
583 Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
584 Instruction *Cast = CastInst::Create(Instruction::IntToPtr, C, OrigTy);
585 ReplaceInstWithInst(I, Cast);
586 }
573 Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
574 Value *Cast = IRB.CreateBitOrPointerCast(C, OrigTy);
575 I->replaceAllUsesWith(Cast);
587576 } else if (StoreInst *SI = dyn_cast(I)) {
588577 Value *Addr = SI->getPointerOperand();
589578 int Idx = getMemoryAccessFuncIndex(Addr, DL);
594583 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
595584 Type *PtrTy = Ty->getPointerTo();
596585 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
597 createIntOrPtrToIntCast(SI->getValueOperand(), Ty, IRB),
586 IRB.CreateBitOrPointerCast(SI->getValueOperand(), Ty),
598587 createOrdering(&IRB, SI->getOrdering())};
599588 CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args);
600589 ReplaceInstWithInst(I, C);
625614 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
626615 Type *PtrTy = Ty->getPointerTo();
627616 Value *CmpOperand =
628 createIntOrPtrToIntCast(CASI->getCompareOperand(), Ty, IRB);
617 IRB.CreateBitOrPointerCast(CASI->getCompareOperand(), Ty);
629618 Value *NewOperand =
630 createIntOrPtrToIntCast(CASI->getNewValOperand(), Ty, IRB);
619 IRB.CreateBitOrPointerCast(CASI->getNewValOperand(), Ty);
631620 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
632621 CmpOperand,
633622 NewOperand,
0 ; RUN: opt < %s -tsan -S | FileCheck %s
1 ; Check that atomic memory operations on floating-point types are converted to calls into ThreadSanitizer runtime.
2 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"
3
4 define float @load_float(float* %fptr) {
5 %v = load atomic float, float* %fptr unordered, align 4
6 ret float %v
7 ; CHECK-LABEL: load_float
8 ; CHECK: call i32 @__tsan_atomic32_load(i32* %{{.+}}, i32 0)
9 ; CHECK: bitcast i32 {{.+}} to float
10 }
11
12 define double @load_double(double* %fptr) {
13 %v = load atomic double, double* %fptr unordered, align 8
14 ret double %v
15 ; CHECK-LABEL: load_double
16 ; CHECK: call i64 @__tsan_atomic64_load(i64* %{{.+}}, i32 0)
17 ; CHECK: bitcast i64 {{.+}} to double
18 }
19
20 define fp128 @load_fp128(fp128* %fptr) {
21 %v = load atomic fp128, fp128* %fptr unordered, align 16
22 ret fp128 %v
23 ; CHECK-LABEL: load_fp128
24 ; CHECK: call i128 @__tsan_atomic128_load(i128* %{{.+}}, i32 0)
25 ; CHECK: bitcast i128 {{.+}} to fp128
26 }
27
28 define void @store_float(float* %fptr, float %v) {
29 store atomic float %v, float* %fptr unordered, align 4
30 ret void
31 ; CHECK-LABEL: store_float
32 ; CHECK: bitcast float %v to i32
33 ; CHECK: call void @__tsan_atomic32_store(i32* %{{.+}}, i32 %{{.+}}, i32 0)
34 }
35
36 define void @store_double(double* %fptr, double %v) {
37 store atomic double %v, double* %fptr unordered, align 8
38 ret void
39 ; CHECK-LABEL: store_double
40 ; CHECK: bitcast double %v to i64
41 ; CHECK: call void @__tsan_atomic64_store(i64* %{{.+}}, i64 %{{.+}}, i32 0)
42 }
43
44 define void @store_fp128(fp128* %fptr, fp128 %v) {
45 store atomic fp128 %v, fp128* %fptr unordered, align 16
46 ret void
47 ; CHECK-LABEL: store_fp128
48 ; CHECK: bitcast fp128 %v to i128
49 ; CHECK: call void @__tsan_atomic128_store(i128* %{{.+}}, i128 %{{.+}}, i32 0)
50 }