@@ -1555,10 +1555,7 @@ class LoopVectorizationCostModel {
15551555 // / Returns true if epilogue vectorization is considered profitable, and
15561556 // / false otherwise.
15571557 // / \p VF is the vectorization factor chosen for the original loop.
1558- // / \p Multiplier is an aditional scaling factor applied to VF before
1559- // / comparing to EpilogueVectorizationMinVF.
1560- bool isEpilogueVectorizationProfitable (const ElementCount VF,
1561- const unsigned IC) const ;
1558+ bool isEpilogueVectorizationProfitable (const ElementCount VF) const ;
15621559
15631560 // / Returns the execution time cost of an instruction for a given vector
15641561 // / width. Vector width of one means scalar.
@@ -4387,11 +4384,12 @@ static unsigned getEstimatedRuntimeVF(const Loop *L,
43874384}
43884385
43894386bool LoopVectorizationPlanner::isMoreProfitable (
4390- const VectorizationFactor &A, const VectorizationFactor &B,
4391- const unsigned MaxTripCount) const {
4387+ const VectorizationFactor &A, const VectorizationFactor &B) const {
43924388 InstructionCost CostA = A.Cost ;
43934389 InstructionCost CostB = B.Cost ;
43944390
4391+ unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount ();
4392+
43954393 // Improve estimate for the vector width if it is scalable.
43964394 unsigned EstimatedWidthA = A.Width .getKnownMinValue ();
43974395 unsigned EstimatedWidthB = B.Width .getKnownMinValue ();
@@ -4440,12 +4438,6 @@ bool LoopVectorizationPlanner::isMoreProfitable(
44404438 return CmpFn (RTCostA, RTCostB);
44414439}
44424440
4443- bool LoopVectorizationPlanner::isMoreProfitable (
4444- const VectorizationFactor &A, const VectorizationFactor &B) const {
4445- const unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount ();
4446- return LoopVectorizationPlanner::isMoreProfitable (A, B, MaxTripCount);
4447- }
4448-
44494441void LoopVectorizationPlanner::emitInvalidCostRemarks (
44504442 OptimizationRemarkEmitter *ORE) {
44514443 using RecipeVFPair = std::pair<VPRecipeBase *, ElementCount>;
@@ -4760,7 +4752,7 @@ bool LoopVectorizationPlanner::isCandidateForEpilogueVectorization(
47604752}
47614753
47624754bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable (
4763- const ElementCount VF, const unsigned IC ) const {
4755+ const ElementCount VF) const {
47644756 // FIXME: We need a much better cost-model to take different parameters such
47654757 // as register pressure, code size increase and cost of extra branches into
47664758 // account. For now we apply a very crude heuristic and only consider loops
@@ -4775,15 +4767,12 @@ bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
47754767 if (TTI.getMaxInterleaveFactor (VF) <= 1 )
47764768 return false ;
47774769
4778- // TODO: PR #108190 introduced a discrepancy between fixed-width and scalable
4779- // VFs when deciding profitability.
4780- // See related "TODO: extend to support scalable VFs." in
4781- // selectEpilogueVectorizationFactor.
4782- unsigned Multiplier = VF.isFixed () ? IC : 1 ;
4783- unsigned MinVFThreshold = EpilogueVectorizationMinVF.getNumOccurrences () > 0
4784- ? EpilogueVectorizationMinVF
4785- : TTI.getEpilogueVectorizationMinVF ();
4786- return getEstimatedRuntimeVF (TheLoop, TTI, VF * Multiplier) >= MinVFThreshold;
4770+ unsigned Multiplier = 1 ;
4771+ if (VF.isScalable ())
4772+ Multiplier = getVScaleForTuning (TheLoop, TTI).value_or (1 );
4773+ if ((Multiplier * VF.getKnownMinValue ()) >= EpilogueVectorizationMinVF)
4774+ return true ;
4775+ return false ;
47874776}
47884777
47894778VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor (
@@ -4826,7 +4815,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
48264815 return Result;
48274816 }
48284817
4829- if (!CM.isEpilogueVectorizationProfitable (MainLoopVF, IC )) {
4818+ if (!CM.isEpilogueVectorizationProfitable (MainLoopVF)) {
48304819 LLVM_DEBUG (dbgs () << " LEV: Epilogue vectorization is not profitable for "
48314820 " this loop\n " );
48324821 return Result;
@@ -4841,20 +4830,16 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
48414830 ScalarEvolution &SE = *PSE.getSE ();
48424831 Type *TCType = Legal->getWidestInductionType ();
48434832 const SCEV *RemainingIterations = nullptr ;
4844- unsigned MaxTripCount = 0 ;
48454833 for (auto &NextVF : ProfitableVFs) {
48464834 // Skip candidate VFs without a corresponding VPlan.
48474835 if (!hasPlanWithVF (NextVF.Width ))
48484836 continue ;
48494837
4850- // Skip candidate VFs with widths >= the (estimated) runtime VF (scalable
4851- // vectors) or > the VF of the main loop (fixed vectors).
4838+ // Skip candidate VFs with widths >= the estimate runtime VF (scalable
4839+ // vectors) or the VF of the main loop (fixed vectors).
48524840 if ((!NextVF.Width .isScalable () && MainLoopVF.isScalable () &&
48534841 ElementCount::isKnownGE (NextVF.Width , EstimatedRuntimeVF)) ||
4854- (NextVF.Width .isScalable () &&
4855- ElementCount::isKnownGE (NextVF.Width , MainLoopVF)) ||
4856- (!NextVF.Width .isScalable () && !MainLoopVF.isScalable () &&
4857- ElementCount::isKnownGT (NextVF.Width , MainLoopVF)))
4842+ ElementCount::isKnownGE (NextVF.Width , MainLoopVF))
48584843 continue ;
48594844
48604845 // If NextVF is greater than the number of remaining iterations, the
@@ -4868,14 +4853,6 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
48684853 " Trip count SCEV must be computable" );
48694854 RemainingIterations = SE.getURemExpr (
48704855 TC, SE.getConstant (TCType, MainLoopVF.getKnownMinValue () * IC));
4871- MaxTripCount = MainLoopVF.getKnownMinValue () * IC - 1 ;
4872- if (SE.isKnownPredicate (CmpInst::ICMP_ULT, RemainingIterations,
4873- SE.getConstant (TCType, MaxTripCount))) {
4874- MaxTripCount =
4875- SE.getUnsignedRangeMax (RemainingIterations).getZExtValue ();
4876- }
4877- LLVM_DEBUG (dbgs () << " LEV: Maximum Trip Count for Epilogue: "
4878- << MaxTripCount << " \n " );
48794856 }
48804857 if (SE.isKnownPredicate (
48814858 CmpInst::ICMP_UGT,
@@ -4884,8 +4861,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
48844861 continue ;
48854862 }
48864863
4887- if (Result.Width .isScalar () ||
4888- isMoreProfitable (NextVF, Result, MaxTripCount))
4864+ if (Result.Width .isScalar () || isMoreProfitable (NextVF, Result))
48894865 Result = NextVF;
48904866 }
48914867
0 commit comments