[PATCH] D50867: [InstrProf] Use atomic profile counter updates for TSan
Vedant Kumar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 16 14:40:39 PDT 2018
vsk created this revision.
vsk added reviewers: kubamracek, davidxl.
Herald added subscribers: jfb, hiraditya.
Thread sanitizer instrumentation fails to skip all loads and stores to
profile counters. This can happen if profile counter updates are merged:
%.sink = phi i64* ...
%pgocount5 = load i64, i64* %.sink
%27 = add i64 %pgocount5, 1
%28 = bitcast i64* %.sink to i8*
call void @__tsan_write8(i8* %28)
store i64 %27, i64* %.sink
To suppress TSan diagnostics about racy counter updates, make the
counter updates atomic when TSan is enabled. If there's general interest
in this mode it can be surfaced as a clang/swift driver option.
rdar://40477803
https://reviews.llvm.org/D50867
Files:
clang/lib/CodeGen/BackendUtil.cpp
clang/test/CodeGen/tsan-instrprof-atomic.c
llvm/include/llvm/Transforms/Instrumentation.h
llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
llvm/test/Instrumentation/InstrProfiling/atomic-updates.ll
Index: llvm/test/Instrumentation/InstrProfiling/atomic-updates.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/InstrProfiling/atomic-updates.ll
@@ -0,0 +1,14 @@
+; RUN: opt < %s -S -instrprof -instrprof-atomic-counter-update-all | FileCheck %s
+
+target triple = "x86_64-apple-macosx10.10.0"
+
+ at __profn_foo = hidden constant [3 x i8] c"foo"
+
+; CHECK-LABEL: define void @foo
+; CHECK-NEXT: atomicrmw add i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc_foo, i64 0, i64 0), i64 1 seq_cst
+define void @foo() {
+ call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_foo, i32 0, i32 0), i64 0, i32 1, i32 0)
+ ret void
+}
+
+declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -96,6 +96,11 @@
// is usually smaller than 2.
cl::init(1.0));
+cl::opt<bool> AtomicCounterUpdateAll(
+ "instrprof-atomic-counter-update-all", cl::ZeroOrMore,
+ cl::desc("Make all profile counter updates atomic (for testing only)"),
+ cl::init(false));
+
cl::opt<bool> AtomicCounterUpdatePromoted(
"atomic-counter-update-promoted", cl::ZeroOrMore,
cl::desc("Do counter update using atomic fetch add "
@@ -597,12 +602,17 @@
IRBuilder<> Builder(Inc);
uint64_t Index = Inc->getIndex()->getZExtValue();
Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
- Value *Load = Builder.CreateLoad(Addr, "pgocount");
- auto *Count = Builder.CreateAdd(Load, Inc->getStep());
- auto *Store = Builder.CreateStore(Count, Addr);
- Inc->replaceAllUsesWith(Store);
- if (isCounterPromotionEnabled())
- PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
+
+ if (Options.Atomic || AtomicCounterUpdateAll) {
+ Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
+ AtomicOrdering::SequentiallyConsistent);
+ } else {
+ Value *Load = Builder.CreateLoad(Addr, "pgocount");
+ auto *Count = Builder.CreateAdd(Load, Inc->getStep());
+ auto *Store = Builder.CreateStore(Count, Addr);
+ if (isCounterPromotionEnabled())
+ PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
+ }
Inc->eraseFromParent();
}
Index: llvm/include/llvm/Transforms/Instrumentation.h
===================================================================
--- llvm/include/llvm/Transforms/Instrumentation.h
+++ llvm/include/llvm/Transforms/Instrumentation.h
@@ -111,6 +111,9 @@
// Do counter register promotion
bool DoCounterPromotion = false;
+ // Use atomic profile counter increments.
+ bool Atomic = false;
+
// Name of the profile file to use as output
std::string InstrProfileOutput;
Index: clang/test/CodeGen/tsan-instrprof-atomic.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/tsan-instrprof-atomic.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 %s -emit-llvm -fprofile-instrument=clang -fsanitize=thread -o - | FileCheck %s
+
+// CHECK: define void @foo
+// CHECK-NOT: load {{.*}}foo
+// CHECK: ret void
+void foo() {}
Index: clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -653,6 +653,11 @@
InstrProfOptions Options;
Options.NoRedZone = CodeGenOpts.DisableRedZone;
Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
+
+ // TODO: Surface the option to emit atomic profile counter increments at
+ // the driver level.
+ Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread);
+
MPM.add(createInstrProfilingLegacyPass(Options));
}
if (CodeGenOpts.hasProfileIRInstr()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50867.161113.patch
Type: text/x-patch
Size: 3990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180816/5ae5e269/attachment.bin>
More information about the llvm-commits
mailing list