Skip to content

Commit 151ed6a

Browse files
committed
[TSAN] Add option to allow instrumenting reads of reads-before-writes
Add -tsan-instrument-read-before-write which allows instrumenting reads of reads-before-writes. This is required for KCSAN [1], where under certain configurations plain writes behave differently (e.g. aligned writes up to word size may be treated as atomic). In order to avoid missing potential data races due to plain RMW operations ("x++" etc.), we will require instrumenting reads of reads-before-writes. [1] https://github.com/google/ktsan/wiki/KCSAN Author: melver (Marco Elver) Reviewed-in: https://reviews.llvm.org/D79983
1 parent fb1c55b commit 151ed6a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ static cl::opt<bool> ClDistinguishVolatile(
7272
"tsan-distinguish-volatile", cl::init(false),
7373
cl::desc("Emit special instrumentation for accesses to volatiles"),
7474
cl::Hidden);
75+
static cl::opt<bool> ClInstrumentReadBeforeWrite(
76+
"tsan-instrument-read-before-write", cl::init(false),
77+
cl::desc("Do not eliminate read instrumentation for read-before-writes"),
78+
cl::Hidden);
7579

7680
STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
7781
STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
@@ -413,7 +417,7 @@ void ThreadSanitizer::chooseInstructionsToInstrument(
413417
Value *Addr = Load->getPointerOperand();
414418
if (!shouldInstrumentReadWriteFromAddress(I->getModule(), Addr))
415419
continue;
416-
if (WriteTargets.count(Addr)) {
420+
if (!ClInstrumentReadBeforeWrite && WriteTargets.count(Addr)) {
417421
// We will write to this temp, so no reason to analyze the read.
418422
NumOmittedReadsBeforeWrite++;
419423
continue;

llvm/test/Instrumentation/ThreadSanitizer/read_before_write.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt < %s -tsan -S | FileCheck %s
2+
; RUN: opt < %s -tsan -tsan-instrument-read-before-write -S | FileCheck %s --check-prefixes=CHECK,CHECK-UNOPT
23

34
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"
45

@@ -11,6 +12,7 @@ entry:
1112
}
1213
; CHECK: define void @IncrementMe
1314
; CHECK-NOT: __tsan_read
15+
; CHECK-UNOPT: __tsan_read
1416
; CHECK: __tsan_write
1517
; CHECK: ret void
1618

0 commit comments

Comments
 (0)