@@ -1535,10 +1535,7 @@ class LoopVectorizationCostModel {
1535
1535
// / Returns true if epilogue vectorization is considered profitable, and
1536
1536
// / false otherwise.
1537
1537
// / \p VF is the vectorization factor chosen for the original loop.
1538
- // / \p Multiplier is an aditional scaling factor applied to VF before
1539
- // / comparing to EpilogueVectorizationMinVF.
1540
- bool isEpilogueVectorizationProfitable (const ElementCount VF,
1541
- const unsigned IC) const ;
1538
+ bool isEpilogueVectorizationProfitable (const ElementCount VF) const ;
1542
1539
1543
1540
// / Returns the execution time cost of an instruction for a given vector
1544
1541
// / width. Vector width of one means scalar.
@@ -4262,11 +4259,12 @@ static unsigned getEstimatedRuntimeVF(const Loop *L,
4262
4259
}
4263
4260
4264
4261
bool LoopVectorizationPlanner::isMoreProfitable (
4265
- const VectorizationFactor &A, const VectorizationFactor &B,
4266
- const unsigned MaxTripCount) const {
4262
+ const VectorizationFactor &A, const VectorizationFactor &B) const {
4267
4263
InstructionCost CostA = A.Cost ;
4268
4264
InstructionCost CostB = B.Cost ;
4269
4265
4266
+ unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount ();
4267
+
4270
4268
// Improve estimate for the vector width if it is scalable.
4271
4269
unsigned EstimatedWidthA = A.Width .getKnownMinValue ();
4272
4270
unsigned EstimatedWidthB = B.Width .getKnownMinValue ();
@@ -4315,12 +4313,6 @@ bool LoopVectorizationPlanner::isMoreProfitable(
4315
4313
return CmpFn (RTCostA, RTCostB);
4316
4314
}
4317
4315
4318
- bool LoopVectorizationPlanner::isMoreProfitable (
4319
- const VectorizationFactor &A, const VectorizationFactor &B) const {
4320
- const unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount ();
4321
- return LoopVectorizationPlanner::isMoreProfitable (A, B, MaxTripCount);
4322
- }
4323
-
4324
4316
void LoopVectorizationPlanner::emitInvalidCostRemarks (
4325
4317
OptimizationRemarkEmitter *ORE) {
4326
4318
using RecipeVFPair = std::pair<VPRecipeBase *, ElementCount>;
@@ -4635,7 +4627,7 @@ bool LoopVectorizationPlanner::isCandidateForEpilogueVectorization(
4635
4627
}
4636
4628
4637
4629
bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable (
4638
- const ElementCount VF, const unsigned IC ) const {
4630
+ const ElementCount VF) const {
4639
4631
// FIXME: We need a much better cost-model to take different parameters such
4640
4632
// as register pressure, code size increase and cost of extra branches into
4641
4633
// account. For now we apply a very crude heuristic and only consider loops
@@ -4650,15 +4642,12 @@ bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
4650
4642
if (TTI.getMaxInterleaveFactor (VF) <= 1 )
4651
4643
return false ;
4652
4644
4653
- // TODO: PR #108190 introduced a discrepancy between fixed-width and scalable
4654
- // VFs when deciding profitability.
4655
- // See related "TODO: extend to support scalable VFs." in
4656
- // selectEpilogueVectorizationFactor.
4657
- unsigned Multiplier = VF.isFixed () ? IC : 1 ;
4658
- unsigned MinVFThreshold = EpilogueVectorizationMinVF.getNumOccurrences () > 0
4659
- ? EpilogueVectorizationMinVF
4660
- : TTI.getEpilogueVectorizationMinVF ();
4661
- return getEstimatedRuntimeVF (TheLoop, TTI, VF * Multiplier) >= MinVFThreshold;
4645
+ unsigned Multiplier = 1 ;
4646
+ if (VF.isScalable ())
4647
+ Multiplier = getVScaleForTuning (TheLoop, TTI).value_or (1 );
4648
+ if ((Multiplier * VF.getKnownMinValue ()) >= EpilogueVectorizationMinVF)
4649
+ return true ;
4650
+ return false ;
4662
4651
}
4663
4652
4664
4653
VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor (
@@ -4701,7 +4690,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
4701
4690
return Result;
4702
4691
}
4703
4692
4704
- if (!CM.isEpilogueVectorizationProfitable (MainLoopVF, IC )) {
4693
+ if (!CM.isEpilogueVectorizationProfitable (MainLoopVF)) {
4705
4694
LLVM_DEBUG (dbgs () << " LEV: Epilogue vectorization is not profitable for "
4706
4695
" this loop\n " );
4707
4696
return Result;
@@ -4716,20 +4705,16 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
4716
4705
ScalarEvolution &SE = *PSE.getSE ();
4717
4706
Type *TCType = Legal->getWidestInductionType ();
4718
4707
const SCEV *RemainingIterations = nullptr ;
4719
- unsigned MaxTripCount = 0 ;
4720
4708
for (auto &NextVF : ProfitableVFs) {
4721
4709
// Skip candidate VFs without a corresponding VPlan.
4722
4710
if (!hasPlanWithVF (NextVF.Width ))
4723
4711
continue ;
4724
4712
4725
- // Skip candidate VFs with widths >= the (estimated) runtime VF (scalable
4726
- // vectors) or > the VF of the main loop (fixed vectors).
4713
+ // Skip candidate VFs with widths >= the estimate runtime VF (scalable
4714
+ // vectors) or the VF of the main loop (fixed vectors).
4727
4715
if ((!NextVF.Width .isScalable () && MainLoopVF.isScalable () &&
4728
4716
ElementCount::isKnownGE (NextVF.Width , EstimatedRuntimeVF)) ||
4729
- (NextVF.Width .isScalable () &&
4730
- ElementCount::isKnownGE (NextVF.Width , MainLoopVF)) ||
4731
- (!NextVF.Width .isScalable () && !MainLoopVF.isScalable () &&
4732
- ElementCount::isKnownGT (NextVF.Width , MainLoopVF)))
4717
+ ElementCount::isKnownGE (NextVF.Width , MainLoopVF))
4733
4718
continue ;
4734
4719
4735
4720
// If NextVF is greater than the number of remaining iterations, the
@@ -4743,14 +4728,6 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
4743
4728
" Trip count SCEV must be computable" );
4744
4729
RemainingIterations = SE.getURemExpr (
4745
4730
TC, SE.getConstant (TCType, MainLoopVF.getKnownMinValue () * IC));
4746
- MaxTripCount = MainLoopVF.getKnownMinValue () * IC - 1 ;
4747
- if (SE.isKnownPredicate (CmpInst::ICMP_ULT, RemainingIterations,
4748
- SE.getConstant (TCType, MaxTripCount))) {
4749
- MaxTripCount =
4750
- SE.getUnsignedRangeMax (RemainingIterations).getZExtValue ();
4751
- }
4752
- LLVM_DEBUG (dbgs () << " LEV: Maximum Trip Count for Epilogue: "
4753
- << MaxTripCount << " \n " );
4754
4731
}
4755
4732
if (SE.isKnownPredicate (
4756
4733
CmpInst::ICMP_UGT,
@@ -4759,8 +4736,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
4759
4736
continue ;
4760
4737
}
4761
4738
4762
- if (Result.Width .isScalar () ||
4763
- isMoreProfitable (NextVF, Result, MaxTripCount))
4739
+ if (Result.Width .isScalar () || isMoreProfitable (NextVF, Result))
4764
4740
Result = NextVF;
4765
4741
}
4766
4742
0 commit comments