[llvm] [VPlan] Make canonical IV part of the region (PR #156262)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 15:29:54 PDT 2025


================
@@ -868,18 +881,37 @@ void VPRegionBlock::print(raw_ostream &O, const Twine &Indent,
 
 void VPRegionBlock::dissolveToCFGLoop() {
   auto *Header = cast<VPBasicBlock>(getEntry());
-  if (auto *CanIV = dyn_cast<VPCanonicalIVPHIRecipe>(&Header->front())) {
-    assert(this == getPlan()->getVectorLoopRegion() &&
-           "Canonical IV must be in the entry of the top-level loop region");
-    auto *ScalarR = VPBuilder(CanIV).createScalarPhi(
-        {CanIV->getStartValue(), CanIV->getBackedgeValue()},
-        CanIV->getDebugLoc(), "index");
+  auto *ExitingLatch = cast<VPBasicBlock>(getExiting());
+  VPValue *CanIV = getCanonicalIV();
+  if (CanIV && CanIV->getNumUsers() > 0) {
+    auto *ExitingTerm = ExitingLatch->getTerminator();
+    VPInstruction *CanIVInc = nullptr;
+    // Check if there's a canonical IV increment via an existing terminator.
+    if (match(ExitingTerm,
+              m_BranchOnCount(m_VPInstruction(CanIVInc), m_VPValue()))) {
+      assert(match(CanIVInc,
+                   m_Add(m_CombineOr(m_Specific(CanIV),
+                                     m_Add(m_Specific(CanIV), m_LiveIn())),
+                         m_VPValue())) &&
+             "invalid existing IV increment");
+    }
----------------
ayalz wrote:

Seems a bit odd to have only an assert under an `if`. Perhaps worth a comment, as this alternative seems even worse:
```suggestion
    [[maybe_unused]] bool BranchOnIVInc = match(ExitingTerm,
              m_BranchOnCount(m_VPInstruction(CanIVInc), m_VPValue()));
    assert(!BranchOnIVInc || match(CanIVInc,
                   m_Add(m_CombineOr(m_Specific(CanIV),
                                     m_Add(m_Specific(CanIV), m_LiveIn())),
                         m_VPValue())) &&
             "invalid existing IV increment");
```

https://github.com/llvm/llvm-project/pull/156262


More information about the llvm-commits mailing list