[llvm] [CodeGen] Fix profiled triangular CFG threshold in MachineBlockPlacement (PR #188752)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 17 07:30:03 PDT 2026
https://github.com/worthlane updated https://github.com/llvm/llvm-project/pull/188752
>From 36a23092c7fb17a419cf87d905a723509222ce3d Mon Sep 17 00:00:00 2001
From: Artem Maklakov <art.maklakov28 at gmail.com>
Date: Fri, 27 Mar 2026 18:08:39 +0300
Subject: [PATCH 1/5] [CodeGen] Fix profiled triangular CFG threshold in
MachineBlockPlacement
---
llvm/lib/CodeGen/MachineBlockPlacement.cpp | 19 +++++--
...placement-triangle-profile-likely-prob.mir | 57 +++++++++++++++++++
2 files changed, 72 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index 08acf4e716c7c..9147ea50b6f46 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -1452,11 +1452,22 @@ 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)
+ *
+ * We then remap the user-controlled ProfileLikelyProb into
+ * a triangle-specific threshold T. We use a piecewise-linear mapping:
+ * - for ProfileLikelyProb in [0, 50], map linearly from 0 to 2/3;
+ * T = (2/3) * (ProfileLikelyProb / 50)
+ * = (2 * ProfileLikelyProb) / 150
+ * - for ProfileLikelyProb in [50, 100], map linearly from 2/3 to 1.
+ * T = 1/3 + (2/3) * (ProfileLikelyProb / 100)
+ * = (100 + 2 * ProfileLikelyProb) / 300
+ *
+ * This keeps T in [0, 1] and preserves T = 2/3 at ProfileLikelyProb = 50.
*/
- return BranchProbability(2 * ProfileLikelyProb, 150);
+ if (ProfileLikelyProb <= 50)
+ return BranchProbability(2 * ProfileLikelyProb, 150);
+ else
+ return BranchProbability(100 + 2 * ProfileLikelyProb, 300);
}
}
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..435f8a4b199a8
--- /dev/null
+++ b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
@@ -0,0 +1,57 @@
+# 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
+
+--- |
+ ; ModuleID = 'triangle.ll'
+ source_filename = "triangle.c"
+ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+ target triple = "x86_64-unknown-linux-gnu"
+
+ define dso_local 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
\ No newline at end of file
>From a8872e01b85f4957334c40f38e51a39c4e3f5243 Mon Sep 17 00:00:00 2001
From: artyo_Om <art.maklakov28 at gmail.com>
Date: Mon, 6 Apr 2026 20:16:23 +0300
Subject: [PATCH 2/5] Simplify MIR test input after review
Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
---
.../X86/block-placement-triangle-profile-likely-prob.mir | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
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
index 435f8a4b199a8..b42a24802de26 100644
--- a/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
+++ b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
@@ -4,12 +4,7 @@
# Verify that a large ProfileLikelyProb value does not fail compilation
--- |
- ; ModuleID = 'triangle.ll'
- source_filename = "triangle.c"
- target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
- target triple = "x86_64-unknown-linux-gnu"
-
- define dso_local i32 @main(i32 %argc, ptr %argv) #0 !prof !0 {
+ 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
>From c9a14ff04cdca94fa0d30436afda58b4627cf4a2 Mon Sep 17 00:00:00 2001
From: Artem Maklakov <art.maklakov28 at gmail.com>
Date: Mon, 6 Apr 2026 21:51:52 +0300
Subject: [PATCH 3/5] [CodeGen][X86] Test case expanded after review
---
.../X86/block-placement-triangle-profile-likely-prob.mir | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
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
index b42a24802de26..c1e0a2ac6423e 100644
--- a/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
+++ b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
@@ -49,4 +49,11 @@ body: |
...
# CHECK: name: main
-# CHECK: bb.0.entry
\ No newline at end of file
+# 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
>From 2a1ab02ef1ce7fdc897189149a89fce86ae4e944 Mon Sep 17 00:00:00 2001
From: Artem Maklakov <art.maklakov28 at gmail.com>
Date: Mon, 6 Apr 2026 23:30:52 +0300
Subject: [PATCH 4/5] [CodeGen][X86] After mapping review
---
llvm/lib/CodeGen/MachineBlockPlacement.cpp | 17 +++++------------
...k-placement-triangle-profile-likely-prob.mir | 1 +
2 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index 9147ea50b6f46..7a6d90a68f8bc 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -1453,21 +1453,14 @@ getLayoutSuccessorProbThreshold(const MachineBasicBlock *BB) {
* (1-T) * Prob(BB->Succ) > T * Prob(BB->Pred)
* So T / (1 - T) = 2, Yielding T = 2/3
*
- * We then remap the user-controlled ProfileLikelyProb into
- * a triangle-specific threshold T. We use a piecewise-linear mapping:
- * - for ProfileLikelyProb in [0, 50], map linearly from 0 to 2/3;
+ * Then remap the user-controlled ProfileLikelyProb into
+ * a triangle-specific threshold T.
* T = (2/3) * (ProfileLikelyProb / 50)
* = (2 * ProfileLikelyProb) / 150
- * - for ProfileLikelyProb in [50, 100], map linearly from 2/3 to 1.
- * T = 1/3 + (2/3) * (ProfileLikelyProb / 100)
- * = (100 + 2 * ProfileLikelyProb) / 300
- *
- * This keeps T in [0, 1] and preserves T = 2/3 at ProfileLikelyProb = 50.
+ * This preserves T = 2/3 at ProfileLikelyProb = 50.
+ * The result is capped at 1.
*/
- if (ProfileLikelyProb <= 50)
- return BranchProbability(2 * ProfileLikelyProb, 150);
- else
- return BranchProbability(100 + 2 * ProfileLikelyProb, 300);
+ 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
index c1e0a2ac6423e..f04fa1ad37c08 100644
--- a/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
+++ b/llvm/test/CodeGen/X86/block-placement-triangle-profile-likely-prob.mir
@@ -57,3 +57,4 @@ body: |
# CHECK: successors: %bb.1
# CHECK: bb.1.succ:
# CHECK: RET64
+
>From 093dfa071934d9b4ecdb345e589124d477cd2e4d Mon Sep 17 00:00:00 2001
From: Artem Maklakov <art.maklakov28 at gmail.com>
Date: Fri, 17 Apr 2026 17:29:02 +0300
Subject: [PATCH 5/5] [CodeGen][X86] clang-format fix
---
llvm/lib/CodeGen/MachineBlockPlacement.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index 7a6d90a68f8bc..73d040bdaa19a 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -1453,8 +1453,8 @@ getLayoutSuccessorProbThreshold(const MachineBasicBlock *BB) {
* (1-T) * Prob(BB->Succ) > T * Prob(BB->Pred)
* So T / (1 - T) = 2, Yielding T = 2/3
*
- * Then remap the user-controlled ProfileLikelyProb into
- * a triangle-specific threshold T.
+ * 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.
More information about the llvm-commits
mailing list