Skip to content

Commit

Permalink
[LV] Move selecting vectorization factor logic to LVP (NFC).
Browse files Browse the repository at this point in the history
Split off from D143938. This moves the planning logic to select the
vectorization factor to LoopVectorizationPlanner as a step towards only
computing costs for individual VFs in LoopVectorizationCostModel and do
planning in LVP.

Reviewed By: Ayal

Differential Revision: https://reviews.llvm.org/D150197
  • Loading branch information
fhahn committed May 13, 2023
1 parent 7472f1d commit f40a790
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 89 deletions.
34 changes: 34 additions & 0 deletions llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
Expand Up @@ -25,6 +25,7 @@
#define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONPLANNER_H

#include "VPlan.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Support/InstructionCost.h"

namespace llvm {
Expand Down Expand Up @@ -217,6 +218,16 @@ struct VectorizationFactor {
}
};

/// ElementCountComparator creates a total ordering for ElementCount
/// for the purposes of using it in a set structure.
struct ElementCountComparator {
bool operator()(const ElementCount &LHS, const ElementCount &RHS) const {
return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) <
std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue());
}
};
using ElementCountSet = SmallSet<ElementCount, 16, ElementCountComparator>;

/// A class that represents two vectorization factors (initialized with 0 by
/// default). One for fixed-width vectorization and one for scalable
/// vectorization. This can be used by the vectorizer to choose from a range of
Expand Down Expand Up @@ -280,6 +291,9 @@ class LoopVectorizationPlanner {

SmallVector<VPlanPtr, 4> VPlans;

/// Profitable vector factors.
SmallVector<VectorizationFactor, 8> ProfitableVFs;

/// A builder used to construct the current plan.
VPBuilder Builder;

Expand Down Expand Up @@ -342,6 +356,12 @@ class LoopVectorizationPlanner {
/// Check if the number of runtime checks exceeds the threshold.
bool requiresTooManyRuntimeChecks() const;

/// \return The most profitable vectorization factor and the cost of that VF
/// for vectorizing the epilogue. Returns VectorizationFactor::Disabled if
/// epilogue vectorization is not supported for the loop.
VectorizationFactor
selectEpilogueVectorizationFactor(const ElementCount MaxVF);

protected:
/// Build VPlans for power-of-2 VF's between \p MinVF and \p MaxVF inclusive,
/// according to the information gathered by Legal when it checked if it is
Expand Down Expand Up @@ -376,6 +396,20 @@ class LoopVectorizationPlanner {
void adjustRecipesForReductions(VPBasicBlock *LatchVPBB, VPlanPtr &Plan,
VPRecipeBuilder &RecipeBuilder,
ElementCount MinVF);

/// \return The most profitable vectorization factor and the cost of that VF.
/// This method checks every VF in \p CandidateVFs.
VectorizationFactor
selectVectorizationFactor(const ElementCountSet &CandidateVFs);

/// Returns true if the per-lane cost of VectorizationFactor A is lower than
/// that of B.
bool isMoreProfitable(const VectorizationFactor &A,
const VectorizationFactor &B) const;

/// Determines if we have the infrastructure to vectorize the loop and its
/// epilogue, assuming the main loop is vectorized by \p VF.
bool isCandidateForEpilogueVectorization(const ElementCount VF) const;
};

} // namespace llvm
Expand Down

0 comments on commit f40a790

Please sign in to comment.