Skip to content

Commit 4c4e4e4

Browse files
authored
[LV] Strengthen calls to collectInstsToScalarize (NFC) (#130642)
Avoid the pattern of always calling collectInstsToScalarize after collectUniformsAndScalars, and call it in collectUniformsAndScalars instead. Also strengthen checks for early exits in the function.
1 parent 8c75501 commit 4c4e4e4

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,7 @@ class LoopVectorizationCostModel {
979979
/// Setup cost-based decisions for user vectorization factor.
980980
/// \return true if the UserVF is a feasible VF to be chosen.
981981
bool selectUserVectorizationFactor(ElementCount UserVF) {
982-
collectUniformsAndScalars(UserVF);
983-
collectInstsToScalarize(UserVF);
982+
collectNonVectorizedAndSetWideningDecisions(UserVF);
984983
return expectedCost(UserVF).isValid();
985984
}
986985

@@ -1236,20 +1235,22 @@ class LoopVectorizationCostModel {
12361235
/// the loop.
12371236
void collectInstsToScalarize(ElementCount VF);
12381237

1239-
/// Collect Uniform and Scalar values for the given \p VF.
1238+
/// Collect values that will not be widened, including Uniforms, Scalars, and
1239+
/// Instructions to Scalarize for the given \p VF.
12401240
/// The sets depend on CM decision for Load/Store instructions
12411241
/// that may be vectorized as interleave, gather-scatter or scalarized.
12421242
/// Also make a decision on what to do about call instructions in the loop
12431243
/// at that VF -- scalarize, call a known vector routine, or call a
12441244
/// vector intrinsic.
1245-
void collectUniformsAndScalars(ElementCount VF) {
1245+
void collectNonVectorizedAndSetWideningDecisions(ElementCount VF) {
12461246
// Do the analysis once.
12471247
if (VF.isScalar() || Uniforms.contains(VF))
12481248
return;
12491249
setCostBasedWideningDecision(VF);
12501250
collectLoopUniforms(VF);
12511251
setVectorizedCallDecision(VF);
12521252
collectLoopScalars(VF);
1253+
collectInstsToScalarize(VF);
12531254
}
12541255

12551256
/// Returns true if the target machine supports masked store operation
@@ -5285,7 +5286,7 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
52855286
RegUsage[ClassID] += 1;
52865287
}
52875288
} else {
5288-
collectUniformsAndScalars(VFs[J]);
5289+
collectNonVectorizedAndSetWideningDecisions(VFs[J]);
52895290
for (auto *Inst : OpenIntervals) {
52905291
// Skip ignored values for VF > 1.
52915292
if (VecValuesToIgnore.count(Inst))
@@ -5382,20 +5383,21 @@ bool LoopVectorizationCostModel::useEmulatedMaskMemRefHack(Instruction *I,
53825383
}
53835384

53845385
void LoopVectorizationCostModel::collectInstsToScalarize(ElementCount VF) {
5385-
// If we aren't vectorizing the loop, or if we've already collected the
5386-
// instructions to scalarize, there's nothing to do. Collection may already
5387-
// have occurred if we have a user-selected VF and are now computing the
5388-
// expected cost for interleaving.
5389-
if (VF.isScalar() || VF.isZero() || InstsToScalarize.contains(VF))
5386+
assert(VF.isVector() && "Expected VF >= 2");
5387+
5388+
// If we've already collected the instructions to scalarize or the predicated
5389+
// BBs after vectorization, there's nothing to do. Collection may already have
5390+
// occurred if we have a user-selected VF and are now computing the expected
5391+
// cost for interleaving.
5392+
if (InstsToScalarize.contains(VF) ||
5393+
PredicatedBBsAfterVectorization.contains(VF))
53905394
return;
53915395

53925396
// Initialize a mapping for VF in InstsToScalalarize. If we find that it's
53935397
// not profitable to scalarize any instructions, the presence of VF in the
53945398
// map will indicate that we've analyzed it already.
53955399
ScalarCostsTy &ScalarCostsVF = InstsToScalarize[VF];
53965400

5397-
PredicatedBBsAfterVectorization[VF].clear();
5398-
53995401
// Find all the instructions that are scalar with predication in the loop and
54005402
// determine if it would be better to not if-convert the blocks they are in.
54015403
// If so, we also record the instructions to scalarize.
@@ -7187,12 +7189,7 @@ void LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
71877189
CM.collectInLoopReductions();
71887190
for (const auto &VF : VFCandidates) {
71897191
// Collect Uniform and Scalar instructions after vectorization with VF.
7190-
CM.collectUniformsAndScalars(VF);
7191-
7192-
// Collect the instructions (and their associated costs) that will be more
7193-
// profitable to scalarize.
7194-
if (VF.isVector())
7195-
CM.collectInstsToScalarize(VF);
7192+
CM.collectNonVectorizedAndSetWideningDecisions(VF);
71967193
}
71977194

71987195
buildVPlansWithVPRecipes(ElementCount::getFixed(1), MaxFactors.FixedVF);

0 commit comments

Comments
 (0)