[llvm] [LoopFusion] Doing cheaper checks first (NFC) (PR #206275)

Ehsan Amiri via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 29 15:52:40 PDT 2026


https://github.com/amehsan updated https://github.com/llvm/llvm-project/pull/206275

>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 1/2] 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;
   }
 

>From 8272ff59ed1b2bf0b0fbb979929f4a0fb9bff314 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Mon, 29 Jun 2026 18:51:48 -0400
Subject: [PATCH 2/2] modify the comment

---
 llvm/lib/Transforms/Scalar/LoopFuse.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
index a69435957256a..51874366d147f 100644
--- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
@@ -1192,11 +1192,8 @@ 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.
+    // 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())



More information about the llvm-commits mailing list