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

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 08:45:16 PDT 2024


================
@@ -1213,6 +1218,17 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
 void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
   auto *Addr = getCounterAddress(CoverInstruction);
   IRBuilder<> Builder(CoverInstruction);
+  if (ConditionalCounterUpdate) {
+    Instruction *SplitBefore = CoverInstruction->getNextNode();
+    auto &Ctx = CoverInstruction->getParent()->getContext();
+    auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
+    Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
+    Value *Cmp = Builder.CreateIsNotNull(Load, "pgocount.ifnonzero");
+    Instruction *ThenBranch =
+        SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
----------------
ellishg wrote:

After reading my old comment again, I remembered that it's important that we compute the CFG hash before changing the CFG, otherwise IRPGO will not correctly attribute profile counts. Luckily we compute the CFG hash in the earlier pass `PGOInstrumentationGen`.

https://github.com/llvm/llvm-project/blob/c66dee4c6bd650ef20105532a311a95abb25ece5/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp#L919-L923

My apologies, my original comment in https://github.com/llvm/llvm-project/pull/95585/files was wrong.

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


More information about the llvm-commits mailing list