Skip to content

Commit a8538b9

Browse files
juliannagelefhahn
andauthored
[LV] Vectorize Epilogues for loops with small VF but high IC (llvm#108190)
- Consider MainLoopVF * IC when determining whether Epilogue Vectorization is profitable - Allow the same VF for the Epilogue as for the main loop - Use an upper bound for the trip count of the Epilogue when choosing the Epilogue VF PR: llvm#108190 --------- Co-authored-by: Florian Hahn <[email protected]>
1 parent feb9b37 commit a8538b9

28 files changed

+2575
-1051
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ class LoopVectorizationPlanner {
525525
bool isMoreProfitable(const VectorizationFactor &A,
526526
const VectorizationFactor &B) const;
527527

528+
/// Returns true if the per-lane cost of VectorizationFactor A is lower than
529+
/// that of B in the context of vectorizing a loop with known \p MaxTripCount.
530+
bool isMoreProfitable(const VectorizationFactor &A,
531+
const VectorizationFactor &B,
532+
const unsigned MaxTripCount) const;
533+
528534
/// Determines if we have the infrastructure to vectorize the loop and its
529535
/// epilogue, assuming the main loop is vectorized by \p VF.
530536
bool isCandidateForEpilogueVectorization(const ElementCount VF) const;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,10 @@ class LoopVectorizationCostModel {
15161516
/// Returns true if epilogue vectorization is considered profitable, and
15171517
/// false otherwise.
15181518
/// \p VF is the vectorization factor chosen for the original loop.
1519-
bool isEpilogueVectorizationProfitable(const ElementCount VF) const;
1519+
/// \p Multiplier is an aditional scaling factor applied to VF before
1520+
/// comparing to EpilogueVectorizationMinVF.
1521+
bool isEpilogueVectorizationProfitable(const ElementCount VF,
1522+
const unsigned Multiplier) const;
15201523

15211524
/// Returns the execution time cost of an instruction for a given vector
15221525
/// width. Vector width of one means scalar.
@@ -4289,12 +4292,11 @@ getVScaleForTuning(const Loop *L, const TargetTransformInfo &TTI) {
42894292
}
42904293

42914294
bool LoopVectorizationPlanner::isMoreProfitable(
4292-
const VectorizationFactor &A, const VectorizationFactor &B) const {
4295+
const VectorizationFactor &A, const VectorizationFactor &B,
4296+
const unsigned MaxTripCount) const {
42934297
InstructionCost CostA = A.Cost;
42944298
InstructionCost CostB = B.Cost;
42954299

4296-
unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
4297-
42984300
// Improve estimate for the vector width if it is scalable.
42994301
unsigned EstimatedWidthA = A.Width.getKnownMinValue();
43004302
unsigned EstimatedWidthB = B.Width.getKnownMinValue();
@@ -4343,6 +4345,12 @@ bool LoopVectorizationPlanner::isMoreProfitable(
43434345
return CmpFn(RTCostA, RTCostB);
43444346
}
43454347

4348+
bool LoopVectorizationPlanner::isMoreProfitable(
4349+
const VectorizationFactor &A, const VectorizationFactor &B) const {
4350+
const unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
4351+
return LoopVectorizationPlanner::isMoreProfitable(A, B, MaxTripCount);
4352+
}
4353+
43464354
void LoopVectorizationPlanner::emitInvalidCostRemarks(
43474355
OptimizationRemarkEmitter *ORE) {
43484356
using RecipeVFPair = std::pair<VPRecipeBase *, ElementCount>;
@@ -4661,7 +4669,7 @@ bool LoopVectorizationPlanner::isCandidateForEpilogueVectorization(
46614669
}
46624670

46634671
bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
4664-
const ElementCount VF) const {
4672+
const ElementCount VF, const unsigned Multiplier) const {
46654673
// FIXME: We need a much better cost-model to take different parameters such
46664674
// as register pressure, code size increase and cost of extra branches into
46674675
// account. For now we apply a very crude heuristic and only consider loops
@@ -4676,9 +4684,6 @@ bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
46764684
if (TTI.getMaxInterleaveFactor(VF) <= 1)
46774685
return false;
46784686

4679-
unsigned Multiplier = 1;
4680-
if (VF.isScalable())
4681-
Multiplier = getVScaleForTuning(TheLoop, TTI).value_or(1);
46824687
if ((Multiplier * VF.getKnownMinValue()) >= EpilogueVectorizationMinVF)
46834688
return true;
46844689
return false;
@@ -4724,7 +4729,11 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47244729
return Result;
47254730
}
47264731

4727-
if (!CM.isEpilogueVectorizationProfitable(MainLoopVF)) {
4732+
unsigned Multiplier = IC;
4733+
if (MainLoopVF.isScalable())
4734+
Multiplier = getVScaleForTuning(OrigLoop, TTI).value_or(1);
4735+
4736+
if (!CM.isEpilogueVectorizationProfitable(MainLoopVF, Multiplier)) {
47284737
LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization is not profitable for "
47294738
"this loop\n");
47304739
return Result;
@@ -4743,16 +4752,20 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47434752
ScalarEvolution &SE = *PSE.getSE();
47444753
Type *TCType = Legal->getWidestInductionType();
47454754
const SCEV *RemainingIterations = nullptr;
4755+
unsigned MaxTripCount = 0;
47464756
for (auto &NextVF : ProfitableVFs) {
47474757
// Skip candidate VFs without a corresponding VPlan.
47484758
if (!hasPlanWithVF(NextVF.Width))
47494759
continue;
47504760

4751-
// Skip candidate VFs with widths >= the estimate runtime VF (scalable
4752-
// vectors) or the VF of the main loop (fixed vectors).
4761+
// Skip candidate VFs with widths >= the (estimated) runtime VF (scalable
4762+
// vectors) or > the VF of the main loop (fixed vectors).
47534763
if ((!NextVF.Width.isScalable() && MainLoopVF.isScalable() &&
47544764
ElementCount::isKnownGE(NextVF.Width, EstimatedRuntimeVF)) ||
4755-
ElementCount::isKnownGE(NextVF.Width, MainLoopVF))
4765+
(NextVF.Width.isScalable() &&
4766+
ElementCount::isKnownGE(NextVF.Width, MainLoopVF)) ||
4767+
(!NextVF.Width.isScalable() && !MainLoopVF.isScalable() &&
4768+
ElementCount::isKnownGT(NextVF.Width, MainLoopVF)))
47564769
continue;
47574770

47584771
// If NextVF is greater than the number of remaining iterations, the
@@ -4766,6 +4779,14 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47664779
"Trip count SCEV must be computable");
47674780
RemainingIterations = SE.getURemExpr(
47684781
TC, SE.getConstant(TCType, MainLoopVF.getKnownMinValue() * IC));
4782+
MaxTripCount = MainLoopVF.getKnownMinValue() * IC - 1;
4783+
if (SE.isKnownPredicate(CmpInst::ICMP_ULT, RemainingIterations,
4784+
SE.getConstant(TCType, MaxTripCount))) {
4785+
MaxTripCount =
4786+
SE.getUnsignedRangeMax(RemainingIterations).getZExtValue();
4787+
}
4788+
LLVM_DEBUG(dbgs() << "LEV: Maximum Trip Count for Epilogue: "
4789+
<< MaxTripCount << "\n");
47694790
}
47704791
if (SE.isKnownPredicate(
47714792
CmpInst::ICMP_UGT,
@@ -4774,7 +4795,8 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
47744795
continue;
47754796
}
47764797

4777-
if (Result.Width.isScalar() || isMoreProfitable(NextVF, Result))
4798+
if (Result.Width.isScalar() ||
4799+
isMoreProfitable(NextVF, Result, MaxTripCount))
47784800
Result = NextVF;
47794801
}
47804802

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]], 8
19+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
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]], 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]]
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]]
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]], 4294967288
58+
; CHECK-NEXT: [[N_VEC5:%.*]] = and i64 [[TMP0]], 4294967292
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 <8 x i8>, ptr [[TMP14]], align 1
63+
; CHECK-NEXT: [[WIDE_LOAD7:%.*]] = load <4 x i8>, ptr [[TMP14]], align 1
6464
; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i8, ptr [[A]], i64 [[INDEX6]]
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
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
7272
; CHECK-NEXT: [[TMP21:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[INDEX6]]
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
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
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)