[llvm] [VPlan] Add VPlan-based addMinIterCheck, replace ILV for non-epilogue. (PR #153643)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 24 02:04:45 PDT 2025


================
@@ -670,6 +670,80 @@ void VPlanTransforms::attachCheckBlock(VPlan &Plan, Value *Cond,
   }
 }
 
+void VPlanTransforms::addMinimumIterationCheck(
+    VPlan &Plan, ElementCount VF, unsigned UF,
+    ElementCount MinProfitableTripCount, bool RequiresScalarEpilogue,
+    bool TailFolded, bool CheckNeededWithTailFolding, Loop *OrigLoop,
+    const uint32_t *MinItersBypassWeights, DebugLoc DL, ScalarEvolution &SE) {
+  // Generate code to check if the loop's trip count is less than VF * UF, or
+  // equal to it in case a scalar epilogue is required; this implies that the
+  // vector trip count is zero. This check also covers the case where adding one
+  // to the backedge-taken count overflowed leading to an incorrect trip count
+  // of zero. In this case we will also jump to the scalar loop.
+  auto P = RequiresScalarEpilogue ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT;
+  // If tail is to be folded, vector loop takes care of all iterations.
+  const SCEV *Count = vputils::getSCEVExprForVPValue(Plan.getTripCount(), SE);
+  Type *CountTy = Count->getType();
+  auto CreateStep = [&]() -> const SCEV * {
+    const SCEV *VFxUF = SE.getElementCount(CountTy, (VF * UF), SCEV::FlagNUW);
+    // Create step with max(MinProTripCount, UF * VF).
+    if (UF * VF.getKnownMinValue() >= MinProfitableTripCount.getKnownMinValue())
+      return VFxUF;
+
+    const SCEV *MinProfTC =
+        SE.getElementCount(CountTy, MinProfitableTripCount, SCEV::FlagNUW);
+    if (!VF.isScalable())
+      return MinProfTC;
+    return SE.getUMaxExpr(MinProfTC, VFxUF);
+  };
+
+  VPBasicBlock *EntryVPBB = Plan.getEntry();
+  VPBuilder Builder(EntryVPBB);
+  VPValue *CheckMinIters = Plan.getFalse();
+  const SCEV *Step = CreateStep();
+  if (!TailFolded) {
----------------
ayalz wrote:

Then perhaps start with `CheckNeededWithTailFolding` case first, as follows:
```
  if (CheckNeededWithTailFolding) {
    // TripCountCheck = compare with upper bound to guard against overflow
  } else if (TailFolded) {
    // TripCountCheck = False, folding tail implies positive vector trip count
  } else if (SE.isKnownPredicate(CmpPred, TripCount, Step)) {
    // TripCountCheck = True, this VPlan should best be discarded, vector trip count is always zero
  } else if (SE.isKnownPredicate(CmpInst::getInversePredicate(CmpPred),
                                    TripCount, Step)) {
    // TripCountCheck = False, vector trip count known to be positive
  } else {
    // TripCountCheck = compare with lower bound to guard against zero vector trip count
  }
```

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


More information about the llvm-commits mailing list