[llvm] [LV] Fold middle-block branch in post-dissolution removeBranchOnConst. (PR #204243)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 9 03:10:44 PDT 2026


================
@@ -8308,23 +8339,33 @@ bool LoopVectorizePass::processLoop(Loop *L) {
         LoopVectorizationPlanner::EpilogueVectorizationKind::MainLoop);
     ++LoopsVectorized;
 
-    // Derive EPI fields from VPlan-generated IR.
+    // Skip epilogue if the main loop covered everything or the chain folded.
+    if (!BestMainPlan.getScalarPreheader()->hasPredecessors())
+      return true;
+
+    // Check chain: Entry -> [SCEV] -> [Mem] -> MainCheck -> VecPH. A check
+    // block is detached if removeBranchOnConst dropped its BranchOnCond before
+    // execution, leaving its UnreachableInst terminator.
     BasicBlock *EntryBB =
         cast<VPIRBasicBlock>(BestMainPlan.getEntry())->getIRBasicBlock();
     EntryBB->setName("iter.check");
     EPI.EpilogueIterationCountCheck = EntryBB;
-    // The check chain is: Entry -> [SCEV] -> [Mem] -> MainCheck -> VecPH.
-    // MainCheck is the non-bypass successor of the last runtime check block
-    // (or Entry if there are no runtime checks).
+
     BasicBlock *LastCheck = EntryBB;
-    if (BasicBlock *MemBB = Checks.getMemRuntimeChecks().second)
-      LastCheck = MemBB;
-    else if (BasicBlock *SCEVBB = Checks.getSCEVChecks().second)
-      LastCheck = SCEVBB;
+    auto IsLive = [](BasicBlock *BB) {
+      return BB && !isa<UnreachableInst>(BB->getTerminator());
+    };
+    if (BasicBlock *BB = Checks.getMemRuntimeChecks().second; IsLive(BB))
+      LastCheck = BB;
+    else if (BasicBlock *BB = Checks.getSCEVChecks().second; IsLive(BB))
+      LastCheck = BB;
----------------
artagnon wrote:

Hm, this is a bit unfortunate: would it be possible to return nullptr in MemChecks and SCEVChecks now?

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


More information about the llvm-commits mailing list