Skip to content

Commit e8fd07c

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 e8fd07c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,20 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
12131213
void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
12141214
auto *Addr = getCounterAddress(CoverInstruction);
12151215
IRBuilder<> Builder(CoverInstruction);
1216-
// We store zero to represent that this block is covered.
1217-
Builder.CreateStore(Builder.getInt8(0), Addr);
1216+
if (ConditionalCounterUpdate) {
1217+
auto &Ctx = CoverInstruction->getParent()->getContext();
1218+
auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
1219+
Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
1220+
Value *Cmp = Builder.CreateICmpNE(Load, ConstantInt::get(Int8Ty, 0),
1221+
"pgocount.ifnonzero");
1222+
Value *Sel =
1223+
Builder.CreateSelect(Cmp, Builder.getInt8(0), Load, "pgocount.select");
1224+
Builder.CreateStore(Sel, Addr);
1225+
} else {
1226+
// We store zero to represent that this block is covered.
1227+
Builder.CreateStore(Builder.getInt8(0), Addr);
1228+
}
1229+
12181230
CoverInstruction->eraseFromParent();
12191231
}
12201232

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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: %pgocount.select = select i1 %pgocount.ifnonzero, i8 0, i8 %pgocount
11+
; CHECK-NEXT: store i8 %pgocount.select, ptr @__profc_foo, align 1
12+
define void @foo() {
13+
call void @llvm.instrprof.cover(ptr @__profn_foo, i64 0, i32 1, i32 0)
14+
ret void
15+
}

0 commit comments

Comments
 (0)