https://github.com/amehsan created https://github.com/llvm/llvm-project/pull/206275
None
>From fc26037b074a284dbdfb8fd1bb0764f43bc8e5eb Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <e00408328 at ptlabb01.fields-bluezone.huawei.com>
Date: Sat, 27 Jun 2026 18:30:30 +0000
Subject: [PATCH] cheap check first
---
llvm/lib/Transforms/Scalar/LoopFuse.cpp | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
index 012a54de173e7..a69435957256a 100644
--- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
@@ -1192,6 +1192,19 @@ struct LoopFuser {
assert(FC0.L->getLoopDepth() == FC1.L->getLoopDepth());
assert(DT.dominates(FC0.getEntryBlock(), FC1.getEntryBlock()));
+ // Cheap scalar check first: walk through all uses in FC1 and find the
+ // reaching def. If the def is located in FC0 then it is not safe to fuse.
+ // Doing this before the memory dependence analysis below lets us bail out
+ // without running the expensive DependenceInfo::depends() query on every
+ // pair of memory instructions.
+ for (BasicBlock *BB : FC1.L->blocks())
+ for (Instruction &I : *BB)
+ for (auto &Op : I.operands())
+ if (Instruction *Def = dyn_cast<Instruction>(Op))
+ if (FC0.L->contains(Def->getParent())) {
+ return false;
+ }
+
for (Instruction *WriteL0 : FC0.MemWrites) {
for (Instruction *WriteL1 : FC1.MemWrites)
if (!dependencesAllowFusion(FC0, FC1, *WriteL0, *WriteL1)) {
@@ -1211,16 +1224,6 @@ struct LoopFuser {
return false;
}
- // Walk through all uses in FC1. For each use, find the reaching def. If the
- // def is located in FC0 then it is not safe to fuse.
- for (BasicBlock *BB : FC1.L->blocks())
- for (Instruction &I : *BB)
- for (auto &Op : I.operands())
- if (Instruction *Def = dyn_cast<Instruction>(Op))
- if (FC0.L->contains(Def->getParent())) {
- return false;
- }
-
return true;
}