[llvm] [InstrProf] Support conditional counter updates for integer counters (PR #109222)

Alan Zhao via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 11:23:23 PDT 2024


================
@@ -1263,8 +1263,21 @@ void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
                             MaybeAlign(), AtomicOrdering::Monotonic);
   } else {
     Value *IncStep = Inc->getStep();
-    Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
-    auto *Count = Builder.CreateAdd(Load, Inc->getStep());
+    auto *CtrTy = IncStep->getType();
+    Value *Load = Builder.CreateLoad(CtrTy, Addr, "pgocount");
+    Value *Count;
+
+    if (ConditionalCounterUpdate) {
+      Instruction *SplitBefore = Inc->getNextNode();
+      Value *Cmp = Builder.CreateIsNull(Load, "pgocount.ifzero");
+      Instruction *ThenBranch =
+          SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
+      Builder.SetInsertPoint(ThenBranch);
+      Count = ConstantInt::get(CtrTy, 1);
+    } else {
+      Count = Builder.CreateAdd(Load, Inc->getStep());
+    }
----------------
alanzhao1 wrote:

Done.

As I mentioned in the commit message, I confirmed that -O3 lowers this to a `store i64 1` anyways

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


More information about the llvm-commits mailing list