[llvm] [LoopFusion] Fix out-of-date LoopInfo being used during fusion (PR #189452)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 30 11:49:52 PDT 2026
https://github.com/CongzheUalberta created https://github.com/llvm/llvm-project/pull/189452
This is fix for [187902](https://github.com/llvm/llvm-project/issues/187902).
The reason for the bug is that, `mergeLatch()` is called at a place where control flow and dominator trees have been updated but LoopInfo has not completed the update yet. `mergeLatch()` calls into ScalarEvolution that uses LoopInfo, where out-of-date LoopInfo would result in crash or unpredicatable results.
This patch moves `mergeLatch()` to the place where LoopInfo has completed its update and hence is in a valid state.
>From dd4c0baec4b725ea7a0c3b7520e526a2dc1e0613 Mon Sep 17 00:00:00 2001
From: Congzhe Cao <congzhe.cao at huawei.com>
Date: Mon, 30 Mar 2026 14:22:17 -0400
Subject: [PATCH] [LoopFusion] Fix out-of-date LoopInfo being used
This is bug fix for issue 187902.
The reason for the bug is that, `mergeLatch()` is called at a place
where control flow and dominator trees have been updated but LoopInfo
has not completed the update yet. `mergeLatch()` calls into
ScalarEvolution that uses LoopInfo, where out-of-date LoopInfo would
result in crash or unpredicatable results.
This patch moves `mergeLatch()` to the place where LoopInfo has
completed its update.
---
llvm/lib/Transforms/Scalar/LoopFuse.cpp | 44 ++++++++++++-------------
1 file changed, 22 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
index 23bba139212e8..07cc86c76ca1d 100644
--- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp
@@ -1676,19 +1676,6 @@ struct LoopFuser {
SE.forgetLoop(FC1.L);
SE.forgetLoop(FC0.L);
- // Move instructions from FC0.Latch to FC1.Latch.
- // Note: mergeLatch requires an updated DT.
- mergeLatch(FC0, FC1);
-
- // Forget block dispositions as well, so that there are no dangling
- // pointers to erased/free'ed blocks. It should be done after mergeLatch()
- // since merging the latches may affect the dispositions.
- SE.forgetBlockAndLoopDispositions();
-
- // Forget the cached SCEV values including the induction variable that may
- // have changed after the fusion.
- SE.forgetLoop(FC0.L);
-
// Merge the loops.
SmallVector<BasicBlock *, 8> Blocks(FC1.L->blocks());
for (BasicBlock *BB : Blocks) {
@@ -1708,6 +1695,19 @@ struct LoopFuser {
// Delete the now empty loop L1.
LI.erase(FC1.L);
+ // Move instructions from FC0.Latch to FC1.Latch.
+ // Note: mergeLatch requires an updated DT.
+ mergeLatch(FC0, FC1);
+
+ // Forget block dispositions as well, so that there are no dangling
+ // pointers to erased/free'ed blocks. It should be done after mergeLatch()
+ // since merging the latches may affect the dispositions.
+ SE.forgetBlockAndLoopDispositions();
+
+ // Forget the cached SCEV values including the induction variable that may
+ // have changed after the fusion.
+ SE.forgetLoop(FC0.L);
+
#ifndef NDEBUG
assert(!verifyFunction(*FC0.Header->getParent(), &errs()));
assert(DT.verify(DominatorTree::VerificationLevel::Fast));
@@ -1978,15 +1978,6 @@ struct LoopFuser {
SE.forgetLoop(FC1.L);
SE.forgetLoop(FC0.L);
- // Move instructions from FC0.Latch to FC1.Latch.
- // Note: mergeLatch requires an updated DT.
- mergeLatch(FC0, FC1);
-
- // Forget block dispositions as well, so that there are no dangling
- // pointers to erased/free'ed blocks. It should be done after mergeLatch()
- // since merging the latches may affect the dispositions.
- SE.forgetBlockAndLoopDispositions();
-
// Merge the loops.
SmallVector<BasicBlock *, 8> Blocks(FC1.L->blocks());
for (BasicBlock *BB : Blocks) {
@@ -2006,6 +1997,15 @@ struct LoopFuser {
// Delete the now empty loop L1.
LI.erase(FC1.L);
+ // Move instructions from FC0.Latch to FC1.Latch.
+ // Note: mergeLatch requires an updated DT.
+ mergeLatch(FC0, FC1);
+
+ // Forget block dispositions as well, so that there are no dangling
+ // pointers to erased/free'ed blocks. It should be done after mergeLatch()
+ // since merging the latches may affect the dispositions.
+ SE.forgetBlockAndLoopDispositions();
+
#ifndef NDEBUG
assert(!verifyFunction(*FC0.Header->getParent(), &errs()));
assert(DT.verify(DominatorTree::VerificationLevel::Fast));
More information about the llvm-commits
mailing list