[llvm] [LV] Add epilogue minimum iteration check in VPlan as well. (PR #189372)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 04:47:20 PDT 2026


================
@@ -9811,18 +9659,51 @@ bool LoopVectorizePass::processLoop(Loop *L) {
         preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan);
     EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1,
                                       BestEpiPlan);
+
+    // Add minimum iteration check for the epilogue plan, followed by runtime
+    // checks for the main plan.
+    LVP.addMinimumIterationCheck(*BestMainPlan, EPI.EpilogueVF, EPI.EpilogueUF,
+                                 ElementCount::getFixed(0));
+    bool HasBranchWeights =
+        hasBranchWeightMD(*L->getLoopLatch()->getTerminator());
+    LVP.attachRuntimeChecks(*BestMainPlan, Checks, HasBranchWeights);
+    LVP.addIterationCountCheckBlock(*BestMainPlan, EPI.MainLoopVF,
+                                    EPI.MainLoopUF);
+
     EpilogueVectorizerMainLoop MainILV(L, PSE, LI, DT, TTI, AC, EPI, &CM,
                                        Checks, *BestMainPlan);
     auto ExpandedSCEVs = LVP.executePlan(EPI.MainLoopVF, EPI.MainLoopUF,
-                                         *BestMainPlan, MainILV, DT, false);
+                                         *BestMainPlan, MainILV, DT, true);
     ++LoopsVectorized;
 
+    // Derive EPI fields from VPlan-generated IR.
+    EPI.TripCount = MainILV.getTripCount();
+    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;
----------------
fhahn wrote:

I'm not sure, I could move it to a helper, but would prefer to not expose this more widely (e.g. in `GeneratedRTChecks`), as using those block is discouraged and only till needed while more parts of epilogue vectorization is moved to VPlan

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


More information about the llvm-commits mailing list