Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LV] Strengthen calls to collectInstsToScalarize (NFC) #130642

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,7 @@ class LoopVectorizationCostModel {
/// Setup cost-based decisions for user vectorization factor.
/// \return true if the UserVF is a feasible VF to be chosen.
bool selectUserVectorizationFactor(ElementCount UserVF) {
collectUniformsAndScalars(UserVF);
collectInstsToScalarize(UserVF);
collectNonVectorizedAndSetWideningDecisions(UserVF);
return expectedCost(UserVF).isValid();
}

Expand Down Expand Up @@ -1236,20 +1235,22 @@ class LoopVectorizationCostModel {
/// the loop.
void collectInstsToScalarize(ElementCount VF);

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

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

void LoopVectorizationCostModel::collectInstsToScalarize(ElementCount VF) {
// If we aren't vectorizing the loop, or if we've already collected the
// instructions to scalarize, there's nothing to do. Collection may already
// have occurred if we have a user-selected VF and are now computing the
// expected cost for interleaving.
if (VF.isScalar() || VF.isZero() || InstsToScalarize.contains(VF))
assert(VF.isVector() && "Expected VF >= 2");

// If we've already collected the instructions to scalarize or the predicated
// BBs after vectorization, there's nothing to do. Collection may already have
// occurred if we have a user-selected VF and are now computing the expected
// cost for interleaving.
if (InstsToScalarize.contains(VF) ||
PredicatedBBsAfterVectorization.contains(VF))
return;

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

PredicatedBBsAfterVectorization[VF].clear();

// Find all the instructions that are scalar with predication in the loop and
// determine if it would be better to not if-convert the blocks they are in.
// If so, we also record the instructions to scalarize.
Expand Down Expand Up @@ -7187,12 +7189,7 @@ void LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
CM.collectInLoopReductions();
for (const auto &VF : VFCandidates) {
// Collect Uniform and Scalar instructions after vectorization with VF.
CM.collectUniformsAndScalars(VF);

// Collect the instructions (and their associated costs) that will be more
// profitable to scalarize.
if (VF.isVector())
CM.collectInstsToScalarize(VF);
CM.collectNonVectorizedAndSetWideningDecisions(VF);
}

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