[llvm] [LoopInterchange] Reject inner-latch lcssa PHI feeding the exit condition (PR #202863)

Madhur Amilkanthwar via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 11 21:54:59 PDT 2026


https://github.com/madhur13490 updated https://github.com/llvm/llvm-project/pull/202863

>From 3247ac703a2a1a96ab0b11150a3a05502fad6bc1 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Sat, 6 Jun 2026 06:19:39 -0700
Subject: [PATCH 1/2] [LoopInterchange] Reject inner-latch lcssa PHI feeding
 the exit condition

In a multi-level nest, an lcssa PHI in the inner loop latch that feeds the
latch's exit condition can be left with a stale incoming block after a
subsequent interchange rewires the CFG, producing invalid IR. This happened
even when the outer latch had a single predecessor, where the legality check
returned early. Instead, reject the interchange when such a PHI feeds the
exit condition.

Fixes #202027
---
 .../lib/Transforms/Scalar/LoopInterchange.cpp | 61 +++++++++++--------
 .../inner-latch-lcssa-feeds-exit-condition.ll | 59 ++++++++++++++++++
 2 files changed, 95 insertions(+), 25 deletions(-)
 create mode 100644 llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll

diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index b2e3bbe516ed8..b856c1d6e6c9e 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1437,36 +1437,47 @@ static bool areOuterLoopExitPHIsSupported(Loop *OuterLoop, Loop *InnerLoop) {
   return true;
 }
 
-// In case of multi-level nested loops, it may occur that lcssa phis exist in
-// the latch of InnerLoop, i.e., when defs of the incoming values are further
-// inside the loopnest. Sometimes those incoming values are not available
-// after interchange, since the original inner latch will become the new outer
-// latch which may have predecessor paths that do not include those incoming
-// values.
+// In a multi-level nest the inner loop induction PHI lives in the header, so
+// any PHI in the inner latch is an lcssa phi whose incoming value is defined
+// further inside the nest. Interchange clones the latch's exit condition, so if
+// such a phi feeds that condition, a later interchange can leave the cloned phi
+// with a stale incoming block, producing invalid IR. Reject that case.
+//
+// For example, the inner latch is also the sub-loop's exit, %p is the lcssa phi
+// for the sub-loop value %v, and the inner loop's exit test reads %p, so %p
+// feeds the cloned condition:
+//
+//   inner.latch:
+//     %p  = phi i64 [ %v, %subloop.latch ]   ; lcssa: %v comes from sub-loop
+//     %ec = icmp eq i64 %iv, %p              ; inner exit test reads %p
+//     br i1 %ec, label %exit, label %inner.header
+//
 // TODO: Handle transformation of lcssa phis in the InnerLoop latch in case of
 // multi-level loop nests.
-static bool areInnerLoopLatchPHIsSupported(Loop *OuterLoop, Loop *InnerLoop) {
+static bool areInnerLoopLatchPHIsSupported(Loop *InnerLoop) {
   if (InnerLoop->getSubLoops().empty())
     return true;
-  // If the original outer latch has only one predecessor, then values defined
-  // further inside the looploop, e.g., in the innermost loop, will be available
-  // at the new outer latch after interchange.
-  if (OuterLoop->getLoopLatch()->getUniquePredecessor() != nullptr)
-    return true;
 
-  // The outer latch has more than one predecessors, i.e., the inner
-  // exit and the inner header.
-  // PHI nodes in the inner latch are lcssa phis where the incoming values
-  // are defined further inside the loopnest. Check if those phis are used
-  // in the original inner latch. If that is the case then bail out since
-  // those incoming values may not be available at the new outer latch.
   BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
-  for (PHINode &PHI : InnerLoopLatch->phis()) {
-    for (auto *U : PHI.users()) {
-      Instruction *UI = cast<Instruction>(U);
-      if (InnerLoopLatch == UI->getParent())
-        return false;
-    }
+  auto *LatchBI = dyn_cast<CondBrInst>(InnerLoopLatch->getTerminator());
+  if (!LatchBI)
+    return true;
+  auto *CondI = dyn_cast<Instruction>(LatchBI->getCondition());
+  if (!CondI)
+    return true;
+
+  // Bail if a phi in the inner latch feeds the exit condition, walking operands
+  // within the inner loop.
+  SmallSetVector<Instruction *, 8> Worklist;
+  Worklist.insert(CondI);
+  for (unsigned I = 0; I < Worklist.size(); ++I) {
+    Instruction *Cur = Worklist[I];
+    if (isa<PHINode>(Cur) && Cur->getParent() == InnerLoopLatch)
+      return false;
+    for (Value *Op : Cur->operands())
+      if (auto *OpI = dyn_cast<Instruction>(Op))
+        if (InnerLoop->contains(OpI))
+          Worklist.insert(OpI);
   }
   return true;
 }
@@ -1516,7 +1527,7 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
     return false;
   }
 
-  if (!areInnerLoopLatchPHIsSupported(OuterLoop, InnerLoop)) {
+  if (!areInnerLoopLatchPHIsSupported(InnerLoop)) {
     LLVM_DEBUG(dbgs() << "Found unsupported PHI nodes in inner loop latch.\n");
     ORE->emit([&]() {
       return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerLatchPHI",
diff --git a/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll b/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll
new file mode 100644
index 0000000000000..9a3bba8ec7cc0
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll
@@ -0,0 +1,59 @@
+; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore \
+; RUN:     -verify-dom-info -verify-loop-info -verify-loop-lcssa \
+; RUN:     -pass-remarks-output=%t -disable-output
+; RUN: FileCheck -input-file %t %s
+
+; After interchanging the innermost and the middle loop, an lcssa phi in the new
+; inner latch feeds the latch's exit condition. Interchanging the (new) middle
+; and outermost loop would clone that condition and leave the lcssa phi with a
+; stale incoming block, producing invalid IR. Make sure the second interchange
+; is rejected instead of crashing, even though the outermost latch has a single
+; predecessor. 
+
+; CHECK:  --- !Passed
+; CHECK:  Pass:            loop-interchange
+; CHECK:  Name:            Interchanged
+; CHECK:  Function:        t
+; CHECK:  ...
+; CHECK:  --- !Missed
+; CHECK:  Pass:            loop-interchange
+; CHECK:  Name:            UnsupportedInnerLatchPHI
+; CHECK:  Function:        t
+; CHECK:    - String:          Cannot interchange loops because unsupported PHI nodes found in inner loop latch.
+; CHECK:  ...
+
+define void @t(i32 %b) {
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  br label %mid.header
+
+mid.header:
+  %j = phi i64 [ 0, %outer.header ], [ %j.next, %mid.latch ]
+  %mwide = zext i32 %b to i64                 ; non-induction value, middle-loop invariant
+  br label %inner.header
+
+inner.header:
+  %k = phi i64 [ 0, %mid.header ], [ %k.next, %inner.latch ]
+  br label %inner.latch
+
+inner.latch:
+  %k.next = add nsw i64 %k, 1
+  %ec.k = icmp eq i64 %k.next, %mwide         ; inner exit uses the middle-header value
+  br i1 %ec.k, label %mid.latch, label %inner.header
+
+mid.latch:
+  %j.next = add nsw i64 %j, 1
+  %ec.j = icmp eq i64 %j.next, 100
+  br i1 %ec.j, label %outer.latch, label %mid.header
+
+outer.latch:
+  %i.next = add nsw i64 %i, 1
+  %ec.i = icmp eq i64 %i.next, 100
+  br i1 %ec.i, label %exit, label %outer.header
+
+exit:
+  ret void
+}

>From e07e79cbe3c173b331908d4f5b63998634648466 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Thu, 11 Jun 2026 21:53:06 -0700
Subject: [PATCH 2/2] [LoopInterchange] Check transformed IR instead of remarks
 in latch PHI test

---
 .../inner-latch-lcssa-feeds-exit-condition.ll | 57 +++++++++++++------
 1 file changed, 41 insertions(+), 16 deletions(-)

diff --git a/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll b/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll
index 9a3bba8ec7cc0..fa488b73d2da0 100644
--- a/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll
+++ b/llvm/test/Transforms/LoopInterchange/inner-latch-lcssa-feeds-exit-condition.ll
@@ -1,28 +1,53 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore \
-; RUN:     -verify-dom-info -verify-loop-info -verify-loop-lcssa \
-; RUN:     -pass-remarks-output=%t -disable-output
-; RUN: FileCheck -input-file %t %s
+; RUN:     -verify-dom-info -verify-loop-info -verify-loop-lcssa -S | FileCheck %s
 
 ; After interchanging the innermost and the middle loop, an lcssa phi in the new
 ; inner latch feeds the latch's exit condition. Interchanging the (new) middle
 ; and outermost loop would clone that condition and leave the lcssa phi with a
 ; stale incoming block, producing invalid IR. Make sure the second interchange
 ; is rejected instead of crashing, even though the outermost latch has a single
-; predecessor. 
-
-; CHECK:  --- !Passed
-; CHECK:  Pass:            loop-interchange
-; CHECK:  Name:            Interchanged
-; CHECK:  Function:        t
-; CHECK:  ...
-; CHECK:  --- !Missed
-; CHECK:  Pass:            loop-interchange
-; CHECK:  Name:            UnsupportedInnerLatchPHI
-; CHECK:  Function:        t
-; CHECK:    - String:          Cannot interchange loops because unsupported PHI nodes found in inner loop latch.
-; CHECK:  ...
+; predecessor.
 
 define void @t(i32 %b) {
+; CHECK-LABEL: define void @t(
+; CHECK-SAME: i32 [[B:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    br label %[[INNER_HEADER_PREHEADER:.*]]
+; CHECK:       [[MID_HEADER_PREHEADER:.*]]:
+; CHECK-NEXT:    br label %[[MID_HEADER:.*]]
+; CHECK:       [[MID_HEADER]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[J_NEXT:%.*]], %[[MID_LATCH:.*]] ], [ 0, %[[MID_HEADER_PREHEADER]] ]
+; CHECK-NEXT:    [[MWIDE:%.*]] = zext i32 [[B]] to i64
+; CHECK-NEXT:    br label %[[INNER_LATCH:.*]]
+; CHECK:       [[INNER_HEADER_PREHEADER]]:
+; CHECK-NEXT:    br label %[[INNER_HEADER:.*]]
+; CHECK:       [[INNER_HEADER]]:
+; CHECK-NEXT:    [[K:%.*]] = phi i64 [ [[TMP0:%.*]], %[[INNER_LATCH_SPLIT:.*]] ], [ 0, %[[INNER_HEADER_PREHEADER]] ]
+; CHECK-NEXT:    br label %[[MID_HEADER_PREHEADER]]
+; CHECK:       [[INNER_LATCH]]:
+; CHECK-NEXT:    [[K_NEXT:%.*]] = add nsw i64 [[K]], 1
+; CHECK-NEXT:    [[EC_K:%.*]] = icmp eq i64 [[K_NEXT]], [[MWIDE]]
+; CHECK-NEXT:    br label %[[MID_LATCH]]
+; CHECK:       [[INNER_LATCH_SPLIT]]:
+; CHECK-NEXT:    [[MWIDE_LCSSA:%.*]] = phi i64 [ [[MWIDE]], %[[MID_LATCH]] ]
+; CHECK-NEXT:    [[TMP0]] = add nsw i64 [[K]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], [[MWIDE_LCSSA]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[OUTER_LATCH]], label %[[INNER_HEADER]]
+; CHECK:       [[MID_LATCH]]:
+; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
+; CHECK-NEXT:    [[EC_J:%.*]] = icmp eq i64 [[J_NEXT]], 100
+; CHECK-NEXT:    br i1 [[EC_J]], label %[[INNER_LATCH_SPLIT]], label %[[MID_HEADER]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[I_NEXT]] = add nsw i64 [[I]], 1
+; CHECK-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 100
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[EXIT:.*]], label %[[OUTER_HEADER]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
 entry:
   br label %outer.header
 



More information about the llvm-commits mailing list