Skip to content

Commit d4b02fb

Browse files
alexey-bataevyuxuanchen1997
authored andcommitted
[LV][NFC]Introduce isScalableVectorizationAllowed() to refactor getMaxLegalScalableVF().
Summary: Adds isScalableVectorizationAllowed() and the corresponding data member to query if the scalable vectorization is supported rather than performing the analysis each time the scalable vector factor is requested. Part of #91403 Test Plan: Reviewers: Reviewed By: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251745
1 parent c66db23 commit d4b02fb

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,10 @@ class LoopVectorizationCostModel {
16281628
ElementCount MaxSafeVF,
16291629
bool FoldTailByMasking);
16301630

1631+
/// Checks if scalable vectorization is supported and enabled. Caches the
1632+
/// result to avoid repeated debug dumps for repeated queries.
1633+
bool isScalableVectorizationAllowed();
1634+
16311635
/// \return the maximum legal scalable VF, based on the safe max number
16321636
/// of elements.
16331637
ElementCount getMaxLegalScalableVF(unsigned MaxSafeElements);
@@ -1692,6 +1696,9 @@ class LoopVectorizationCostModel {
16921696
std::optional<std::pair<TailFoldingStyle, TailFoldingStyle>>
16931697
ChosenTailFoldingStyle;
16941698

1699+
/// true if scalable vectorization is supported and enabled.
1700+
std::optional<bool> IsScalableVectorizationAllowed;
1701+
16951702
/// A map holding scalar costs for different vectorization factors. The
16961703
/// presence of a cost for an instruction in the mapping indicates that the
16971704
/// instruction will be scalarized when vectorizing with the associated
@@ -4144,15 +4151,18 @@ bool LoopVectorizationCostModel::runtimeChecksRequired() {
41444151
return false;
41454152
}
41464153

4147-
ElementCount
4148-
LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
4154+
bool LoopVectorizationCostModel::isScalableVectorizationAllowed() {
4155+
if (IsScalableVectorizationAllowed)
4156+
return *IsScalableVectorizationAllowed;
4157+
4158+
IsScalableVectorizationAllowed = false;
41494159
if (!TTI.supportsScalableVectors() && !ForceTargetSupportsScalableVectors)
4150-
return ElementCount::getScalable(0);
4160+
return false;
41514161

41524162
if (Hints->isScalableVectorizationDisabled()) {
41534163
reportVectorizationInfo("Scalable vectorization is explicitly disabled",
41544164
"ScalableVectorizationDisabled", ORE, TheLoop);
4155-
return ElementCount::getScalable(0);
4165+
return false;
41564166
}
41574167

41584168
LLVM_DEBUG(dbgs() << "LV: Scalable vectorization is available\n");
@@ -4172,7 +4182,7 @@ LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
41724182
"Scalable vectorization not supported for the reduction "
41734183
"operations found in this loop.",
41744184
"ScalableVFUnfeasible", ORE, TheLoop);
4175-
return ElementCount::getScalable(0);
4185+
return false;
41764186
}
41774187

41784188
// Disable scalable vectorization if the loop contains any instructions
@@ -4184,17 +4194,36 @@ LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
41844194
reportVectorizationInfo("Scalable vectorization is not supported "
41854195
"for all element types found in this loop.",
41864196
"ScalableVFUnfeasible", ORE, TheLoop);
4187-
return ElementCount::getScalable(0);
4197+
return false;
4198+
}
4199+
4200+
if (!Legal->isSafeForAnyVectorWidth()) {
4201+
std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI);
4202+
if (!MaxVScale) {
4203+
reportVectorizationInfo(
4204+
"The target does not provide maximum vscale value.",
4205+
"ScalableVFUnfeasible", ORE, TheLoop);
4206+
return false;
4207+
}
41884208
}
41894209

4210+
IsScalableVectorizationAllowed = true;
4211+
return true;
4212+
}
4213+
4214+
ElementCount
4215+
LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
4216+
if (!isScalableVectorizationAllowed())
4217+
return ElementCount::getScalable(0);
4218+
4219+
auto MaxScalableVF = ElementCount::getScalable(
4220+
std::numeric_limits<ElementCount::ScalarTy>::max());
41904221
if (Legal->isSafeForAnyVectorWidth())
41914222
return MaxScalableVF;
41924223

4224+
std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI);
41934225
// Limit MaxScalableVF by the maximum safe dependence distance.
4194-
if (std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI))
4195-
MaxScalableVF = ElementCount::getScalable(MaxSafeElements / *MaxVScale);
4196-
else
4197-
MaxScalableVF = ElementCount::getScalable(0);
4226+
MaxScalableVF = ElementCount::getScalable(MaxSafeElements / *MaxVScale);
41984227

41994228
if (!MaxScalableVF)
42004229
reportVectorizationInfo(

0 commit comments

Comments
 (0)