[llvm] [LoopFusion] Return early when no two loops are at the same depth. (PR #226590)
Ehsan Amiri via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 25 14:04:14 PDT 2026
https://github.com/amehsan created https://github.com/llvm/llvm-project/pull/226590
If there are no pair of loops who have the same parent, loop fusion is obviously impossible. But loop fusion will still try to simplify loops in such a funtion and possibly reconstruct postdom tree. Detecting early and returning will save us compile time.
Assisted by Claude Code.
>From 118cc423e2222969782005a3acd71a39a76bca68 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Fri, 25 Sep 2026 17:00:07 -0400
Subject: [PATCH] [LoopFusion] Return early when no two loops are at the same
depth.
If there are no pair of loops who have the same parent, loop fusion is
obviously impossible. But loop fusion will still try to simplify loops
in such a funtion and possibly reconstruct postdom tree. Detecting early and
returning will save us compile time.
Assisted by Claude Code.
---
llvm/lib/Transforms/Scalar/LoopFuse.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
index 20b05ce6a1302..461df18c746df 100644
--- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
@@ -1908,10 +1908,21 @@ struct LoopFuser {
return finalizeFusedLoop(FC0, FC1);
}
};
+
+bool hasSiblingLoops(const LoopInfo &LI) {
+ ArrayRef<Loop *> Loops = LI.getTopLevelLoops();
+ while (Loops.size() == 1)
+ Loops = Loops.front()->getSubLoops();
+ return Loops.size() > 1;
+}
+
} // namespace
+
+
PreservedAnalyses LoopFusePass::run(Function &F, FunctionAnalysisManager &AM) {
auto &LI = AM.getResult<LoopAnalysis>(F);
+
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
auto &DI = AM.getResult<DependenceAnalysis>(F);
auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
@@ -1920,6 +1931,9 @@ PreservedAnalyses LoopFusePass::run(Function &F, FunctionAnalysisManager &AM) {
auto &AC = AM.getResult<AssumptionAnalysis>(F);
const TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
+ if (!hasSiblingLoops(LI))
+ return PreservedAnalyses::all();
+
// Ensure loops are in simplifed form which is a pre-requisite for loop fusion
// pass. Added only for new PM since the legacy PM has already added
// LoopSimplify pass as a dependency.
More information about the llvm-commits
mailing list