[llvm] [Transforms][Utils] Default the LoopSplitUtils pass to innermost loops (PR #214452)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 03:54:45 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Ashutosh Nema (nema-ashutosh)
<details>
<summary>Changes</summary>
Add -loop-split-outer to select top-level loops instead.
---
Full diff: https://github.com/llvm/llvm-project/pull/214452.diff
3 Files Affected:
- (modified) llvm/lib/Transforms/Utils/LoopSplitUtilsPass.cpp (+16-3)
- (added) llvm/test/Transforms/LoopSplit/nested-inner-loop.ll (+93)
- (modified) llvm/test/Transforms/LoopSplit/nested-loop.ll (+6-5)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/LoopSplitUtilsPass.cpp b/llvm/lib/Transforms/Utils/LoopSplitUtilsPass.cpp
index ccdc42bc64ed7..cf8aa4972bf33 100644
--- a/llvm/lib/Transforms/Utils/LoopSplitUtilsPass.cpp
+++ b/llvm/lib/Transforms/Utils/LoopSplitUtilsPass.cpp
@@ -37,6 +37,11 @@ static cl::list<unsigned>
"at which to split each loop"),
cl::CommaSeparated);
+static cl::opt<bool> SplitOuterLoops(
+ "loop-split-outer",
+ cl::desc("Split the top-level loops instead of the innermost ones"),
+ cl::init(false));
+
static cl::list<unsigned> UnguardedPartitions(
"loop-split-unguarded",
cl::desc("Partition indices whose entry guard is omitted (the caller "
@@ -134,9 +139,17 @@ PreservedAnalyses LoopSplitUtilsPass::run(Function &F,
auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
- // Collect the original top-level loops up front; the transform creates new
- // sub-loops that we must not revisit.
- SmallVector<Loop *, 4> Worklist(LI.begin(), LI.end());
+ // Collect the original loops up front; the transform creates new sub-loops
+ // that we must not revisit. Innermost loops are the interesting default: they
+ // are what a client splits, and their new blocks land in a containing loop.
+ SmallVector<Loop *, 4> Worklist;
+ if (SplitOuterLoops) {
+ Worklist.assign(LI.begin(), LI.end());
+ } else {
+ for (Loop *L : LI.getLoopsInPreorder())
+ if (L->isInnermost())
+ Worklist.push_back(L);
+ }
bool Changed = false;
for (Loop *L : Worklist) {
diff --git a/llvm/test/Transforms/LoopSplit/nested-inner-loop.ll b/llvm/test/Transforms/LoopSplit/nested-inner-loop.ll
new file mode 100644
index 0000000000000..721e03115fc21
--- /dev/null
+++ b/llvm/test/Transforms/LoopSplit/nested-inner-loop.ll
@@ -0,0 +1,93 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=loop-split-utils -loop-split-points=4 -S < %s | FileCheck %s
+
+; Split the inner loop of a nest. The blocks the transform creates after the
+; loop (the merged final exit, and each later partition's guard and exit) land
+; inside the containing loop, so LoopInfo has to be told about them. The
+; nested-loop.ll case splits the outer loop, where there is no parent to update.
+
+define i64 @split_inner(ptr %a, i64 %n, i64 %m) {
+; CHECK-LABEL: define i64 @split_inner(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: [[SMAX:%.*]] = call i64 @llvm.smax.i64(i64 [[M]], i64 1)
+; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[SMAX]], -1
+; CHECK-NEXT: [[SMIN:%.*]] = call i64 @llvm.smin.i64(i64 [[TMP0]], i64 3)
+; CHECK-NEXT: br label %[[LS_GUARD0:.*]]
+; CHECK: [[LS_GUARD0]]:
+; CHECK-NEXT: [[O:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[O_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT: [[ACC:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[SUM_NEXT4:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT: [[ITR_CHK:%.*]] = icmp sle i64 0, [[SMIN]]
+; CHECK-NEXT: br i1 [[ITR_CHK]], label %[[OUTER:.*]], label %[[LS_GUARD1:.*]]
+; CHECK: [[OUTER]]:
+; CHECK-NEXT: br label %[[INNER:.*]]
+; CHECK: [[INNER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i64 [ 0, %[[OUTER]] ], [ [[I_NEXT:%.*]], %[[INNER]] ]
+; CHECK-NEXT: [[SUM:%.*]] = phi i64 [ [[ACC]], %[[OUTER]] ], [ [[SUM_NEXT:%.*]], %[[INNER]] ]
+; CHECK-NEXT: [[P:%.*]] = getelementptr inbounds i64, ptr [[A]], i64 [[I]]
+; CHECK-NEXT: [[V:%.*]] = load i64, ptr [[P]], align 4
+; CHECK-NEXT: [[SUM_NEXT]] = add i64 [[SUM]], [[V]]
+; CHECK-NEXT: [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT: [[ITR_CHK1:%.*]] = icmp sle i64 [[I_NEXT]], [[SMIN]]
+; CHECK-NEXT: br i1 [[ITR_CHK1]], label %[[INNER]], label %[[INNER_EXIT:.*]]
+; CHECK: [[INNER_EXIT]]:
+; CHECK-NEXT: br label %[[LS_GUARD1]]
+; CHECK: [[LS_GUARD1]]:
+; CHECK-NEXT: [[SUM_NEXT5:%.*]] = phi i64 [ [[SUM_NEXT]], %[[INNER_EXIT]] ], [ [[ACC]], %[[LS_GUARD0]] ]
+; CHECK-NEXT: [[ITR_CHK2:%.*]] = icmp sle i64 4, [[TMP0]]
+; CHECK-NEXT: br i1 [[ITR_CHK2]], label %[[OUTER_LS1:.*]], label %[[LS_FINAL_EXIT:.*]]
+; CHECK: [[OUTER_LS1]]:
+; CHECK-NEXT: br label %[[INNER_LS1:.*]]
+; CHECK: [[INNER_LS1]]:
+; CHECK-NEXT: [[I_LS1:%.*]] = phi i64 [ 4, %[[OUTER_LS1]] ], [ [[I_NEXT_LS1:%.*]], %[[INNER_LS1]] ]
+; CHECK-NEXT: [[SUM_LS1:%.*]] = phi i64 [ [[SUM_NEXT5]], %[[OUTER_LS1]] ], [ [[SUM_NEXT_LS1:%.*]], %[[INNER_LS1]] ]
+; CHECK-NEXT: [[P_LS1:%.*]] = getelementptr inbounds i64, ptr [[A]], i64 [[I_LS1]]
+; CHECK-NEXT: [[V_LS1:%.*]] = load i64, ptr [[P_LS1]], align 4
+; CHECK-NEXT: [[SUM_NEXT_LS1]] = add i64 [[SUM_LS1]], [[V_LS1]]
+; CHECK-NEXT: [[I_NEXT_LS1]] = add nuw nsw i64 [[I_LS1]], 1
+; CHECK-NEXT: [[ITR_CHK3:%.*]] = icmp sle i64 [[I_NEXT_LS1]], [[TMP0]]
+; CHECK-NEXT: br i1 [[ITR_CHK3]], label %[[INNER_LS1]], label %[[LS_EXIT1:.*]]
+; CHECK: [[LS_EXIT1]]:
+; CHECK-NEXT: br label %[[LS_FINAL_EXIT]]
+; CHECK: [[LS_FINAL_EXIT]]:
+; CHECK-NEXT: [[SUM_NEXT4]] = phi i64 [ [[SUM_NEXT_LS1]], %[[LS_EXIT1]] ], [ [[SUM_NEXT5]], %[[LS_GUARD1]] ]
+; CHECK-NEXT: br label %[[OUTER_LATCH]]
+; CHECK: [[OUTER_LATCH]]:
+; CHECK-NEXT: [[O_NEXT]] = add nuw nsw i64 [[O]], 1
+; CHECK-NEXT: [[OC:%.*]] = icmp slt i64 [[O_NEXT]], [[N]]
+; CHECK-NEXT: br i1 [[OC]], label %[[LS_GUARD0]], label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[R:%.*]] = phi i64 [ [[SUM_NEXT4]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT: ret i64 [[R]]
+;
+entry:
+ br label %outer
+
+outer:
+ %o = phi i64 [ 0, %entry ], [ %o.next, %outer.latch ]
+ %acc = phi i64 [ 0, %entry ], [ %acc.next, %outer.latch ]
+ br label %inner
+
+inner:
+ %i = phi i64 [ 0, %outer ], [ %i.next, %inner ]
+ %sum = phi i64 [ %acc, %outer ], [ %sum.next, %inner ]
+ %p = getelementptr inbounds i64, ptr %a, i64 %i
+ %v = load i64, ptr %p
+ %sum.next = add i64 %sum, %v
+ %i.next = add nuw nsw i64 %i, 1
+ %c = icmp slt i64 %i.next, %m
+ br i1 %c, label %inner, label %inner.exit
+
+inner.exit:
+ %acc.next = phi i64 [ %sum.next, %inner ]
+ br label %outer.latch
+
+outer.latch:
+ %o.next = add nuw nsw i64 %o, 1
+ %oc = icmp slt i64 %o.next, %n
+ br i1 %oc, label %outer, label %exit
+
+exit:
+ %r = phi i64 [ %acc.next, %outer.latch ]
+ ret i64 %r
+}
diff --git a/llvm/test/Transforms/LoopSplit/nested-loop.ll b/llvm/test/Transforms/LoopSplit/nested-loop.ll
index a4cb06feb7aa2..c957373bc544f 100644
--- a/llvm/test/Transforms/LoopSplit/nested-loop.ll
+++ b/llvm/test/Transforms/LoopSplit/nested-loop.ll
@@ -1,10 +1,11 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt -passes=loop-split-utils -loop-split-points=50 -S < %s | FileCheck %s
+; RUN: opt -passes=loop-split-utils -loop-split-outer -loop-split-points=50 -S < %s | FileCheck %s
-; Only the outer (top-level) loop is a split candidate. Splitting it clones the
-; whole loop body, including the inner loop, into the later partition. This
-; exercises that cloning a loop containing a subloop does not crash and that the
-; inner loop is duplicated wholesale rather than split.
+; Split the outer loop of a nest (-loop-split-outer; the default picks the
+; innermost loop instead). Splitting it clones the whole loop body, including
+; the inner loop, into the later partition. This exercises that cloning a loop
+; containing a subloop does not crash and that the inner loop is duplicated
+; wholesale rather than split.
define void @nested(ptr %a, i64 %n, i64 %m) {
; CHECK-LABEL: define void @nested(
``````````
</details>
https://github.com/llvm/llvm-project/pull/214452
More information about the llvm-commits
mailing list