Skip to content

Commit 2a913ae

Browse files
Florian Hahnfhahn
authored andcommitted
Revert "[LV] Vectorize Epilogues for loops with small VF but high IC (llvm#108190)"
This reverts commit a8538b9.
1 parent df890b8 commit 2a913ae

28 files changed

+2575
-1399
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,6 @@ class LoopVectorizationPlanner {
529529
bool isMoreProfitable(const VectorizationFactor &A,
530530
const VectorizationFactor &B) const;
531531

532-
/// Returns true if the per-lane cost of VectorizationFactor A is lower than
533-
/// that of B in the context of vectorizing a loop with known \p MaxTripCount.
534-
bool isMoreProfitable(const VectorizationFactor &A,
535-
const VectorizationFactor &B,
536-
const unsigned MaxTripCount) const;
537-
538532
/// Determines if we have the infrastructure to vectorize the loop and its
539533
/// epilogue, assuming the main loop is vectorized by \p VF.
540534
bool isCandidateForEpilogueVectorization(const ElementCount VF) const;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,7 @@ class LoopVectorizationCostModel {
15351535
/// Returns true if epilogue vectorization is considered profitable, and
15361536
/// false otherwise.
15371537
/// \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;
15421539

15431540
/// Returns the execution time cost of an instruction for a given vector
15441541
/// width. Vector width of one means scalar.
@@ -4264,11 +4261,12 @@ static unsigned getEstimatedRuntimeVF(const Loop *L,
42644261
}
42654262

42664263
bool LoopVectorizationPlanner::isMoreProfitable(
4267-
const VectorizationFactor &A, const VectorizationFactor &B,
4268-
const unsigned MaxTripCount) const {
4264+
const VectorizationFactor &A, const VectorizationFactor &B) const {
42694265
InstructionCost CostA = A.Cost;
42704266
InstructionCost CostB = B.Cost;
42714267

4268+
unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
4269+
42724270
// Improve estimate for the vector width if it is scalable.
42734271
unsigned EstimatedWidthA = A.Width.getKnownMinValue();
42744272
unsigned EstimatedWidthB = B.Width.getKnownMinValue();
@@ -4317,12 +4315,6 @@ bool LoopVectorizationPlanner::isMoreProfitable(
43174315
return CmpFn(RTCostA, RTCostB);
43184316
}
43194317

4320-
bool LoopVectorizationPlanner::isMoreProfitable(
4321-
const VectorizationFactor &A, const VectorizationFactor &B) const {
4322-
const unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
4323-
return LoopVectorizationPlanner::isMoreProfitable(A, B, MaxTripCount);
4324-
}
4325-
43264318
void LoopVectorizationPlanner::emitInvalidCostRemarks(
43274319
OptimizationRemarkEmitter *ORE) {
43284320
using RecipeVFPair = std::pair<VPRecipeBase *, ElementCount>;
@@ -4637,7 +4629,7 @@ bool LoopVectorizationPlanner::isCandidateForEpilogueVectorization(
46374629
}
46384630

46394631
bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
4640-
const ElementCount VF, const unsigned IC) const {
4632+
const ElementCount VF) const {
46414633
// FIXME: We need a much better cost-model to take different parameters such
46424634
// as register pressure, code size increase and cost of extra branches into
46434635
// account. For now we apply a very crude heuristic and only consider loops
@@ -4652,15 +4644,12 @@ bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
46524644
if (TTI.getMaxInterleaveFactor(VF) <= 1)
46534645
return false;
46544646

4655-
// TODO: PR #108190 introduced a discrepancy between fixed-width and scalable
4656-
// VFs when deciding profitability.
4657-
// See related "TODO: extend to support scalable VFs." in
4658-
// selectEpilogueVectorizationFactor.
4659-
unsigned Multiplier = VF.isFixed() ? IC : 1;
4660-
unsigned MinVFThreshold = EpilogueVectorizationMinVF.getNumOccurrences() > 0
4661-
? EpilogueVectorizationMinVF
4662-
: TTI.getEpilogueVectorizationMinVF();
4663-
return getEstimatedRuntimeVF(TheLoop, TTI, VF * Multiplier) >= MinVFThreshold;
4647+
unsigned Multiplier = 1;
4648+
if (VF.isScalable())
4649+
Multiplier = getVScaleForTuning(TheLoop, TTI).value_or(1);
4650+
if ((Multiplier * VF.getKnownMinValue()) >= EpilogueVectorizationMinVF)
4651+
return true;
4652+
return false;
46644653
}
46654654

46664655
VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
@@ -4703,7 +4692,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47034692
return Result;
47044693
}
47054694

4706-
if (!CM.isEpilogueVectorizationProfitable(MainLoopVF, IC)) {
4695+
if (!CM.isEpilogueVectorizationProfitable(MainLoopVF)) {
47074696
LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization is not profitable for "
47084697
"this loop\n");
47094698
return Result;
@@ -4718,20 +4707,16 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47184707
ScalarEvolution &SE = *PSE.getSE();
47194708
Type *TCType = Legal->getWidestInductionType();
47204709
const SCEV *RemainingIterations = nullptr;
4721-
unsigned MaxTripCount = 0;
47224710
for (auto &NextVF : ProfitableVFs) {
47234711
// Skip candidate VFs without a corresponding VPlan.
47244712
if (!hasPlanWithVF(NextVF.Width))
47254713
continue;
47264714

4727-
// Skip candidate VFs with widths >= the (estimated) runtime VF (scalable
4728-
// vectors) or > the VF of the main loop (fixed vectors).
4715+
// Skip candidate VFs with widths >= the estimate runtime VF (scalable
4716+
// vectors) or the VF of the main loop (fixed vectors).
47294717
if ((!NextVF.Width.isScalable() && MainLoopVF.isScalable() &&
47304718
ElementCount::isKnownGE(NextVF.Width, EstimatedRuntimeVF)) ||
4731-
(NextVF.Width.isScalable() &&
4732-
ElementCount::isKnownGE(NextVF.Width, MainLoopVF)) ||
4733-
(!NextVF.Width.isScalable() && !MainLoopVF.isScalable() &&
4734-
ElementCount::isKnownGT(NextVF.Width, MainLoopVF)))
4719+
ElementCount::isKnownGE(NextVF.Width, MainLoopVF))
47354720
continue;
47364721

47374722
// If NextVF is greater than the number of remaining iterations, the
@@ -4745,14 +4730,6 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47454730
"Trip count SCEV must be computable");
47464731
RemainingIterations = SE.getURemExpr(
47474732
TC, SE.getConstant(TCType, MainLoopVF.getKnownMinValue() * IC));
4748-
MaxTripCount = MainLoopVF.getKnownMinValue() * IC - 1;
4749-
if (SE.isKnownPredicate(CmpInst::ICMP_ULT, RemainingIterations,
4750-
SE.getConstant(TCType, MaxTripCount))) {
4751-
MaxTripCount =
4752-
SE.getUnsignedRangeMax(RemainingIterations).getZExtValue();
4753-
}
4754-
LLVM_DEBUG(dbgs() << "LEV: Maximum Trip Count for Epilogue: "
4755-
<< MaxTripCount << "\n");
47564733
}
47574734
if (SE.isKnownPredicate(
47584735
CmpInst::ICMP_UGT,
@@ -4761,8 +4738,7 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47614738
continue;
47624739
}
47634740

4764-
if (Result.Width.isScalar() ||
4765-
isMoreProfitable(NextVF, Result, MaxTripCount))
4741+
if (Result.Width.isScalar() || isMoreProfitable(NextVF, Result))
47664742
Result = NextVF;
47674743
}
47684744

llvm/test/Transforms/LoopVectorize/AArch64/deterministic-type-shrinkage.ll

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ define void @test_pr25490(i32 %n, ptr noalias nocapture %a, ptr noalias nocaptur
1616
; CHECK-NEXT: br i1 [[CMP_28]], label [[FOR_COND_CLEANUP:%.*]], label [[ITER_CHECK:%.*]]
1717
; CHECK: iter.check:
1818
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N]] to i64
19-
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
19+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 8
2020
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[VEC_EPILOG_SCALAR_PH:%.*]], label [[VECTOR_MAIN_LOOP_ITER_CHECK:%.*]]
2121
; CHECK: vector.main.loop.iter.check:
2222
; CHECK-NEXT: [[MIN_ITERS_CHECK1:%.*]] = icmp ult i32 [[N]], 16
@@ -50,33 +50,33 @@ define void @test_pr25490(i32 %n, ptr noalias nocapture %a, ptr noalias nocaptur
5050
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N_VEC]], [[TMP0]]
5151
; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[VEC_EPILOG_ITER_CHECK:%.*]]
5252
; CHECK: vec.epilog.iter.check:
53-
; CHECK-NEXT: [[N_VEC_REMAINING:%.*]] = and i64 [[TMP0]], 12
54-
; CHECK-NEXT: [[MIN_EPILOG_ITERS_CHECK:%.*]] = icmp eq i64 [[N_VEC_REMAINING]], 0
55-
; CHECK-NEXT: br i1 [[MIN_EPILOG_ITERS_CHECK]], label [[VEC_EPILOG_SCALAR_PH]], label [[VEC_EPILOG_PH]]
53+
; CHECK-NEXT: [[N_VEC_REMAINING:%.*]] = and i64 [[TMP0]], 8
54+
; CHECK-NEXT: [[MIN_EPILOG_ITERS_CHECK_NOT_NOT:%.*]] = icmp eq i64 [[N_VEC_REMAINING]], 0
55+
; CHECK-NEXT: br i1 [[MIN_EPILOG_ITERS_CHECK_NOT_NOT]], label [[VEC_EPILOG_SCALAR_PH]], label [[VEC_EPILOG_PH]]
5656
; CHECK: vec.epilog.ph:
5757
; CHECK-NEXT: [[VEC_EPILOG_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[VEC_EPILOG_ITER_CHECK]] ], [ 0, [[VECTOR_MAIN_LOOP_ITER_CHECK]] ]
58-
; CHECK-NEXT: [[N_VEC5:%.*]] = and i64 [[TMP0]], 4294967292
58+
; CHECK-NEXT: [[N_VEC5:%.*]] = and i64 [[TMP0]], 4294967288
5959
; CHECK-NEXT: br label [[VEC_EPILOG_VECTOR_BODY:%.*]]
6060
; CHECK: vec.epilog.vector.body:
6161
; CHECK-NEXT: [[INDEX6:%.*]] = phi i64 [ [[VEC_EPILOG_RESUME_VAL]], [[VEC_EPILOG_PH]] ], [ [[INDEX_NEXT10:%.*]], [[VEC_EPILOG_VECTOR_BODY]] ]
6262
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr inbounds i8, ptr [[C]], i64 [[INDEX6]]
63-
; CHECK-NEXT: [[WIDE_LOAD7:%.*]] = load <4 x i8>, ptr [[TMP14]], align 1
63+
; CHECK-NEXT: [[WIDE_LOAD7:%.*]] = load <8 x i8>, ptr [[TMP14]], align 1
6464
; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i8, ptr [[A]], i64 [[INDEX6]]
65-
; CHECK-NEXT: [[WIDE_LOAD8:%.*]] = load <4 x i8>, ptr [[TMP15]], align 1
66-
; CHECK-NEXT: [[TMP16:%.*]] = zext <4 x i8> [[WIDE_LOAD8]] to <4 x i16>
67-
; CHECK-NEXT: [[TMP17:%.*]] = zext <4 x i8> [[WIDE_LOAD7]] to <4 x i16>
68-
; CHECK-NEXT: [[TMP18:%.*]] = mul nuw <4 x i16> [[TMP16]], [[TMP17]]
69-
; CHECK-NEXT: [[TMP19:%.*]] = lshr <4 x i16> [[TMP18]], splat (i16 8)
70-
; CHECK-NEXT: [[TMP20:%.*]] = trunc nuw <4 x i16> [[TMP19]] to <4 x i8>
71-
; CHECK-NEXT: store <4 x i8> [[TMP20]], ptr [[TMP15]], align 1
65+
; CHECK-NEXT: [[WIDE_LOAD8:%.*]] = load <8 x i8>, ptr [[TMP15]], align 1
66+
; CHECK-NEXT: [[TMP16:%.*]] = zext <8 x i8> [[WIDE_LOAD8]] to <8 x i16>
67+
; CHECK-NEXT: [[TMP17:%.*]] = zext <8 x i8> [[WIDE_LOAD7]] to <8 x i16>
68+
; CHECK-NEXT: [[TMP18:%.*]] = mul nuw <8 x i16> [[TMP16]], [[TMP17]]
69+
; CHECK-NEXT: [[TMP19:%.*]] = lshr <8 x i16> [[TMP18]], splat (i16 8)
70+
; CHECK-NEXT: [[TMP20:%.*]] = trunc nuw <8 x i16> [[TMP19]] to <8 x i8>
71+
; CHECK-NEXT: store <8 x i8> [[TMP20]], ptr [[TMP15]], align 1
7272
; CHECK-NEXT: [[TMP21:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[INDEX6]]
73-
; CHECK-NEXT: [[WIDE_LOAD9:%.*]] = load <4 x i8>, ptr [[TMP21]], align 1
74-
; CHECK-NEXT: [[TMP22:%.*]] = zext <4 x i8> [[WIDE_LOAD9]] to <4 x i16>
75-
; CHECK-NEXT: [[TMP23:%.*]] = mul nuw <4 x i16> [[TMP22]], [[TMP17]]
76-
; CHECK-NEXT: [[TMP24:%.*]] = lshr <4 x i16> [[TMP23]], splat (i16 8)
77-
; CHECK-NEXT: [[TMP25:%.*]] = trunc nuw <4 x i16> [[TMP24]] to <4 x i8>
78-
; CHECK-NEXT: store <4 x i8> [[TMP25]], ptr [[TMP21]], align 1
79-
; CHECK-NEXT: [[INDEX_NEXT10]] = add nuw i64 [[INDEX6]], 4
73+
; CHECK-NEXT: [[WIDE_LOAD9:%.*]] = load <8 x i8>, ptr [[TMP21]], align 1
74+
; CHECK-NEXT: [[TMP22:%.*]] = zext <8 x i8> [[WIDE_LOAD9]] to <8 x i16>
75+
; CHECK-NEXT: [[TMP23:%.*]] = mul nuw <8 x i16> [[TMP22]], [[TMP17]]
76+
; CHECK-NEXT: [[TMP24:%.*]] = lshr <8 x i16> [[TMP23]], splat (i16 8)
77+
; CHECK-NEXT: [[TMP25:%.*]] = trunc nuw <8 x i16> [[TMP24]] to <8 x i8>
78+
; CHECK-NEXT: store <8 x i8> [[TMP25]], ptr [[TMP21]], align 1
79+
; CHECK-NEXT: [[INDEX_NEXT10]] = add nuw i64 [[INDEX6]], 8
8080
; CHECK-NEXT: [[TMP26:%.*]] = icmp eq i64 [[INDEX_NEXT10]], [[N_VEC5]]
8181
; CHECK-NEXT: br i1 [[TMP26]], label [[VEC_EPILOG_MIDDLE_BLOCK:%.*]], label [[VEC_EPILOG_VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
8282
; CHECK: vec.epilog.middle.block:

0 commit comments

Comments
 (0)