[llvm] [MachineScheduler] Add an option to split regions into chunks of a given maximum size (PR #180519)
Nathan Corbyn via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 10 04:16:24 PST 2026
https://github.com/cofibrant updated https://github.com/llvm/llvm-project/pull/180519
>From f5e2494cdea254935b87dc45389bb8f6dbc9d6b6 Mon Sep 17 00:00:00 2001
From: Nathan Corbyn <n_corbyn at apple.com>
Date: Fri, 6 Feb 2026 16:26:08 +0000
Subject: [PATCH] [MachineScheduler] Split regions into chunks of a given
maximum size
---
llvm/lib/CodeGen/MachineScheduler.cpp | 11 ++--
.../AArch64/misched-max-region-instrs.mir | 50 +++++++++++++++++++
2 files changed, 58 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/misched-max-region-instrs.mir
diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index 05df5ea59d7a3..f1cca9a719f4c 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -249,6 +249,11 @@ static cl::opt<unsigned> SchedOnlyBlock("misched-only-block", cl::Hidden,
static cl::opt<unsigned> ReadyListLimit("misched-limit", cl::Hidden,
cl::desc("Limit ready list to N instructions"), cl::init(256));
+static cl::opt<unsigned> MaxRegionInstrs(
+ "misched-max-region-instrs", cl::Hidden,
+ cl::desc("Split regions larger than N instructions. 0 disables."),
+ cl::init(0));
+
static cl::opt<bool> EnableRegPressure("misched-regpressure", cl::Hidden,
cl::desc("Enable register pressure scheduling."), cl::init(true));
@@ -785,9 +790,8 @@ getSchedRegions(MachineBasicBlock *MBB,
// Avoid decrementing RegionEnd for blocks with no terminator.
if (RegionEnd != MBB->end() ||
- isSchedBoundary(&*std::prev(RegionEnd), &*MBB, MF, TII)) {
+ isSchedBoundary(&*std::prev(RegionEnd), &*MBB, MF, TII))
--RegionEnd;
- }
// The next region starts above the previous region. Look backward in the
// instruction stream until we find the nearest boundary.
@@ -795,7 +799,8 @@ getSchedRegions(MachineBasicBlock *MBB,
I = RegionEnd;
for (;I != MBB->begin(); --I) {
MachineInstr &MI = *std::prev(I);
- if (isSchedBoundary(&MI, &*MBB, MF, TII))
+ if (isSchedBoundary(&MI, &*MBB, MF, TII) ||
+ (MaxRegionInstrs && NumRegionInstrs >= MaxRegionInstrs))
break;
if (!MI.isDebugOrPseudoInstr()) {
// MBB::size() uses instr_iterator to count. Here we need a bundle to
diff --git a/llvm/test/CodeGen/AArch64/misched-max-region-instrs.mir b/llvm/test/CodeGen/AArch64/misched-max-region-instrs.mir
new file mode 100644
index 0000000000000..edc3ccef42bdb
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/misched-max-region-instrs.mir
@@ -0,0 +1,50 @@
+# RUN: llc -mtriple=arm64-apple-ios -run-pass=machine-scheduler -debug-only=machine-scheduler -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=CHECK-DEFAULT
+# RUN: llc -mtriple=arm64-apple-ios -run-pass=machine-scheduler -misched-max-region-instrs=3 -debug-only=machine-scheduler -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=CHECK-SPLIT
+
+# REQUIRES: asserts
+
+# Test that -misched-max-region-size splits large scheduling regions.
+
+# test_region_split_uneven
+# CHECK-DEFAULT: RegionInstrs: 6
+# CHECK-SPLIT: RegionInstrs: 3
+# CHECK-SPLIT: RegionInstrs: 2
+
+# test_region_split_even
+# CHECK-DEFAULT: RegionInstrs: 8
+# CHECK-SPLIT: RegionInstrs: 3
+# CHECK-SPLIT: RegionInstrs: 3
+
+# CHECK-DEFAULT-NOT: RegionInstrs:
+# CHECK-SPLIT-NOT: RegionInstrs:
+
+---
+name: test_region_split_uneven
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2, $x3, $x4, $x5
+ $x10 = ADDXrr $x0, $x1 ; 1
+ $x11 = ADDXrr $x2, $x3 ; 2
+ $x12 = ADDXrr $x4, $x5 ; 3
+ $x13 = SUBXrr $x0, $x1 ; Boundary
+ $x14 = SUBXrr $x2, $x3 ; 1
+ $x15 = SUBXrr $x4, $x5 ; 2
+ RET_ReallyLR
+...
+---
+name: test_region_split_even
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2, $x3, $x4, $x5, $x6, $x7
+ $x10 = ADDXrr $x0, $x1 ; 1
+ $x11 = ADDXrr $x2, $x3 ; 2
+ $x12 = ADDXrr $x4, $x5 ; 3
+ $x13 = SUBXrr $x0, $x1 ; Boundary
+ $x14 = SUBXrr $x2, $x3 ; 1
+ $x15 = SUBXrr $x4, $x5 ; 2
+ $x16 = ANDXrr $x0, $x6 ; 3
+ $x17 = ORRXrr $x0, $x7 ; Boundary
+ RET_ReallyLR
+...
More information about the llvm-commits
mailing list