diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp index fd0e9f18b61c9..811911644106b 100644 --- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -358,9 +358,11 @@ static bool isVtableAccess(Instruction *I) { // instrumentation. The user has no way of suppressing them. static bool shouldInstrumentReadWriteFromAddress(const Module *M, Value *Addr) { // Peel off GEPs and BitCasts. - Addr = Addr->stripInBoundsOffsets(); + // Note: This also peels AddrspaceCasts, so this should not be used when + // checking the address space below. + Value *PeeledAddr = Addr->stripInBoundsOffsets(); - if (GlobalVariable *GV = dyn_cast(Addr)) { + if (GlobalVariable *GV = dyn_cast(PeeledAddr)) { if (GV->hasSection()) { StringRef SectionName = GV->getSection(); // Check if the global is in the PGO counters section. @@ -373,11 +375,9 @@ static bool shouldInstrumentReadWriteFromAddress(const Module *M, Value *Addr) { // Do not instrument accesses from different address spaces; we cannot deal // with them. - if (Addr) { - Type *PtrTy = cast(Addr->getType()->getScalarType()); - if (PtrTy->getPointerAddressSpace() != 0) - return false; - } + Type *PtrTy = cast(Addr->getType()->getScalarType()); + if (PtrTy->getPointerAddressSpace() != 0) + return false; return true; } diff --git a/llvm/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr_cast.ll b/llvm/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr_cast.ll new file mode 100644 index 0000000000000..c352915bfda0d --- /dev/null +++ b/llvm/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr_cast.ll @@ -0,0 +1,12 @@ +; RUN: opt < %s -passes='tsan' -S | FileCheck %s +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" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @read_4_bytes(ptr %a) sanitize_thread { +entry: + %b = addrspacecast ptr %a to ptr addrspace(1) + %tmp1 = load volatile i32, ptr addrspace(1) %b, align 4 + ret i32 %tmp1 +} + +; CHECK-NOT: call void @__tsan_read \ No newline at end of file