Skip to content

Commit fc769cc

Browse files
committed
[InstrProf] Support conditional counter updates
This patch adds support of conditional counter updates in single byte counters mode to reduce the write contention by first checking whether the counter is set before overwriting it.
1 parent 37ec6e5 commit fc769cc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ cl::opt<bool> AtomicFirstCounter(
132132
"the entry counter)"),
133133
cl::init(false));
134134

135+
cl::opt<bool> ConditionalCounterUpdate(
136+
"conditional-counter-update",
137+
cl::desc("Do conditional counter updates in single byte counters mode)"),
138+
cl::init(false));
139+
135140
// If the option is not specified, the default behavior about whether
136141
// counter promotion is done depends on how instrumentaiton lowering
137142
// pipeline is setup, i.e., the default value of true of this option
@@ -1213,6 +1218,18 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
12131218
void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
12141219
auto *Addr = getCounterAddress(CoverInstruction);
12151220
IRBuilder<> Builder(CoverInstruction);
1221+
if (ConditionalCounterUpdate) {
1222+
Instruction *SplitBefore = CoverInstruction->getNextNode();
1223+
auto &Ctx = CoverInstruction->getParent()->getContext();
1224+
auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
1225+
Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
1226+
Value *Cmp = Builder.CreateICmpNE(Load, ConstantInt::get(Int8Ty, 0),
1227+
"pgocount.ifnonzero");
1228+
Instruction *ThenBranch =
1229+
SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
1230+
Builder.SetInsertPoint(ThenBranch);
1231+
}
1232+
12161233
// We store zero to represent that this block is covered.
12171234
Builder.CreateStore(Builder.getInt8(0), Addr);
12181235
CoverInstruction->eraseFromParent();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: opt < %s -S -passes=instrprof -conditional-counter-update | FileCheck %s
2+
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
@__profn_foo = private constant [3 x i8] c"foo"
6+
7+
; CHECK-LABEL: define void @foo
8+
; CHECK-NEXT: %pgocount = load i8, ptr @__profc_foo, align 1
9+
; CHECK-NEXT: %pgocount.ifnonzero = icmp ne i8 %pgocount, 0
10+
; CHECK-NEXT: br i1 %pgocount.ifnonzero, label %1, label %2
11+
; CHECK: 1: ; preds = %0
12+
; CHECK-NEXT: store i8 0, ptr @__profc_foo, align 1
13+
; CHECK-NEXT: br label %2
14+
define void @foo() {
15+
call void @llvm.instrprof.cover(ptr @__profn_foo, i64 0, i32 1, i32 0)
16+
ret void
17+
}

0 commit comments

Comments
 (0)