[llvm] Fix SCC enter block collection in BPI (PR #210580)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 19 01:22:01 PDT 2026


https://github.com/SomeFlyingThing created https://github.com/llvm/llvm-project/pull/210580

 Fixes incorrect SCC enter-block collection in BPI, getSccEnterBlocks is documented to collect blocks outside an SCC that have edges into the SCC. It was instead adding the SCC header block itself, which
 prevented estimated cold loop weights from propagating to the actual predecessor entering the irreducible SCC.

>From cac5a3984be931effb38e1aaf8a69cce34b72c6f Mon Sep 17 00:00:00 2001
From: SomeFlyingThing <306498559+SomeFlyingThing at users.noreply.github.com>
Date: Sun, 19 Jul 2026 09:16:13 +0100
Subject: [PATCH] Fix SCC enter block collection in BPI

---
 llvm/lib/Analysis/BranchProbabilityInfo.cpp   |  2 +-
 .../Analysis/BranchProbabilityInfo/loop.ll    | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
index 1b0a8b8e74f4a..feb0b34cd4c48 100644
--- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -391,7 +391,7 @@ void BPIConstruction::SccInfo::getSccEnterBlocks(
     if (isSCCHeader(BB, SccNum))
       for (const auto *Pred : predecessors(BB))
         if (getSCCNum(Pred) != SccNum)
-          Enters.push_back(const_cast<BasicBlock *>(BB));
+          Enters.push_back(const_cast<BasicBlock *>(Pred));
   }
 }
 
diff --git a/llvm/test/Analysis/BranchProbabilityInfo/loop.ll b/llvm/test/Analysis/BranchProbabilityInfo/loop.ll
index ffac1cd466641..e6b8429eddaa7 100644
--- a/llvm/test/Analysis/BranchProbabilityInfo/loop.ll
+++ b/llvm/test/Analysis/BranchProbabilityInfo/loop.ll
@@ -722,4 +722,34 @@ exit:
 }
 
 
+; Check that a cold irreducible loop propagates its estimated weight through
+; the blocks entering the SCC.
+define void @test19(i1 %arg, i1 %arg2, i1 %arg3, i1 %arg4) {
+; CHECK: edge %entry -> %dispatch probability is 0x078780e3 / 0x80000000 = 5.88%
+; CHECK: edge %entry -> %exit probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
+
+entry:
+  br i1 %arg, label %dispatch, label %exit
+
+dispatch:
+  br i1 %arg2, label %entry1, label %entry2
+
+entry1:
+  br label %loop1
+
+entry2:
+  br label %loop2
+
+loop1:
+  br i1 %arg3, label %loop2, label %cold
+
+loop2:
+  br i1 %arg4, label %loop1, label %cold
 
+cold:
+  call void @cold()
+  br label %exit
+
+exit:
+  ret void
+}



More information about the llvm-commits mailing list