[llvm] [InstrProf] Support conditional counter updates (PR #102542)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 14:48:21 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-pgo

Author: None (gulfemsavrun)

<details>
<summary>Changes</summary>

This patch adds support for conditional counter updates in single byte counters mode to reduce the write contention by first checking whether the counter is set before overwriting it.

---
Full diff: https://github.com/llvm/llvm-project/pull/102542.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp (+14-2) 
- (added) llvm/test/Instrumentation/InstrProfiling/conditional-counter-updates.ll (+15) 


``````````diff
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index 1805ea89272ec7..5cf2096657547d 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -1213,8 +1213,20 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
 void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
   auto *Addr = getCounterAddress(CoverInstruction);
   IRBuilder<> Builder(CoverInstruction);
-  // We store zero to represent that this block is covered.
-  Builder.CreateStore(Builder.getInt8(0), Addr);
+  if (ConditionalCounterUpdate) {
+    auto &Ctx = CoverInstruction->getParent()->getContext();
+    auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
+    Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
+    Value *Cmp = Builder.CreateICmpNE(Load, ConstantInt::get(Int8Ty, 0),
+                                      "pgocount.ifnonzero");
+    Value *Sel =
+        Builder.CreateSelect(Cmp, Builder.getInt8(0), Load, "pgocount.select");
+    Builder.CreateStore(Sel, Addr);
+  } else {
+    // We store zero to represent that this block is covered.
+    Builder.CreateStore(Builder.getInt8(0), Addr);
+  }
+
   CoverInstruction->eraseFromParent();
 }
 
diff --git a/llvm/test/Instrumentation/InstrProfiling/conditional-counter-updates.ll b/llvm/test/Instrumentation/InstrProfiling/conditional-counter-updates.ll
new file mode 100644
index 00000000000000..704a0f3e64b101
--- /dev/null
+++ b/llvm/test/Instrumentation/InstrProfiling/conditional-counter-updates.ll
@@ -0,0 +1,15 @@
+; RUN: opt < %s -S -passes=instrprof -conditional-counter-update | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+ at __profn_foo = private constant [3 x i8] c"foo"
+
+; CHECK-LABEL: define void @foo
+; CHECK-NEXT: %pgocount = load i8, ptr @__profc_foo, align 1
+; CHECK-NEXT: %pgocount.ifnonzero = icmp ne i8 %pgocount, 0
+; CHECK-NEXT: %pgocount.select = select i1 %pgocount.ifnonzero, i8 0, i8 %pgocount
+; CHECK-NEXT: store i8 %pgocount.select, ptr @__profc_foo, align 1
+define void @foo() {
+  call void @llvm.instrprof.cover(ptr @__profn_foo, i64 0, i32 1, i32 0)
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/102542


More information about the llvm-commits mailing list