[llvm] b2ca731 - [CodeGen] Fix profiled triangular CFG threshold in MachineBlockPlacement (#188752)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 17 09:24:26 PDT 2026


Author: artyo_Om
Date: 2026-04-17T17:24:21+01:00
New Revision: b2ca7319ca38553d111b9519dfc7a4a2e2e3ac3b

URL: https://github.com/llvm/llvm-project/commit/b2ca7319ca38553d111b9519dfc7a4a2e2e3ac3b
DIFF: https://github.com/llvm/llvm-project/commit/b2ca7319ca38553d111b9519dfc7a4a2e2e3ac3b.diff

LOG: [CodeGen] Fix profiled triangular CFG threshold in MachineBlockPlacement (#188752)

Fix an assertion failure in MachineBlockPlacement for profiled
triangular CFGs with large -profile-likely-prob values.

The existing triangular-CFG threshold scaling can produce a
BranchProbability greater than 1. Capping to `BranchProbability(100,
100)` added.

Added: 
    llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir

Modified: 
    llvm/lib/CodeGen/MachineBlockPlacement.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index 08acf4e716c7c..73d040bdaa19a 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -1452,11 +1452,15 @@ getLayoutSuccessorProbThreshold(const MachineBasicBlock *BB) {
        *   So the threshold T in the calculation below
        *   (1-T) * Prob(BB->Succ) > T * Prob(BB->Pred)
        *   So T / (1 - T) = 2, Yielding T = 2/3
-       * Also adding user specified branch bias, we have
-       *   T = (2/3)*(ProfileLikelyProb/50)
-       *     = (2*ProfileLikelyProb)/150)
+       *
+       * Then remap the user-controlled ProfileLikelyProb into
+       * a triangle-specific threshold T.
+       *   T = (2/3) * (ProfileLikelyProb / 50)
+       *     = (2 * ProfileLikelyProb) / 150
+       * This preserves T = 2/3 at ProfileLikelyProb = 50.
+       * The result is capped at 1.
        */
-      return BranchProbability(2 * ProfileLikelyProb, 150);
+      return BranchProbability(ProfileLikelyProb, 150) * 2;
     }
   }
   return BranchProbability(ProfileLikelyProb, 100);

diff  --git a/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
new file mode 100644
index 0000000000000..f04fa1ad37c08
--- /dev/null
+++ b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
@@ -0,0 +1,60 @@
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=block-placement -profile-likely-prob=80 %s -o - | FileCheck %s
+#
+# Test for profiled triangular CFG handling in MachineBlockPlacement
+# Verify that a large ProfileLikelyProb value does not fail compilation
+
+--- |
+  define i32 @main(i32 %argc, ptr %argv) #0 !prof !0 {
+  entry:
+    %cmp = icmp sgt i32 %argc, 1
+    br i1 %cmp, label %pred, label %succ, !prof !1
+
+  succ:
+    ret i32 0
+
+  pred:
+    br label %succ
+  }
+
+  attributes #0 = { nounwind "use-sample-profile" }
+
+  !0 = !{!"function_entry_count", i64 1}
+  !1 = !{!"branch_weights", i32 0, i32 1}
+...
+---
+name:            main
+alignment:       1
+tracksRegLiveness: true
+noPhis:          true
+isSSA:           false
+noVRegs:         true
+liveins:
+  - { reg: '$edi', virtual-reg: '' }
+body:             |
+  bb.0.entry:
+    successors: %bb.1(0x00000000), %bb.2(0x80000000)
+    liveins: $edi
+
+    CMP32ri killed renamable $edi, 1, implicit-def $eflags
+    JCC_1 %bb.2, 15, implicit killed $eflags
+
+  bb.1.succ:
+    $eax = MOV32ri 0
+    RET64 $eax
+
+  bb.2.pred:
+    successors: %bb.1(0x80000000)
+
+    JMP_1 %bb.1
+...
+
+# CHECK: name: main
+# CHECK: bb.0.entry:
+# CHECK: successors: %bb.1
+# CHECK-SAME: %bb.2
+# CHECK: JCC_1 %bb.1
+# CHECK: bb.2.pred:
+# CHECK: successors: %bb.1
+# CHECK: bb.1.succ:
+# CHECK: RET64
+


        


More information about the llvm-commits mailing list