[PATCH] D92177: [NFC][InstructionCost] Refactor LoopVectorizationCostModel::selectVectorizationFactor
David Sherwood via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 27 03:55:41 PST 2020
david-arm updated this revision to Diff 308013.
david-arm added a comment.
- Changed float->unsigned in the tuple used for selecting vectorisation factors.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D92177/new/
https://reviews.llvm.org/D92177
Files:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -5408,54 +5408,57 @@
LoopVectorizationCostModel::selectVectorizationFactor(ElementCount MaxVF) {
assert(!MaxVF.isScalable() && "scalable vectors not yet supported");
- float Cost = expectedCost(ElementCount::getFixed(1)).first;
- const float ScalarCost = Cost;
- unsigned Width = 1;
- LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << (int)ScalarCost << ".\n");
+ unsigned ExpectedCost = expectedCost(ElementCount::getFixed(1)).first;
+ LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << ExpectedCost << ".\n");
+
+ std::pair<unsigned, unsigned> MinCost = {ExpectedCost, 1};
+ const unsigned ScalarCost = ExpectedCost;
bool ForceVectorization = Hints->getForce() == LoopVectorizeHints::FK_Enabled;
if (ForceVectorization && MaxVF.isVector()) {
// Ignore scalar width, because the user explicitly wants vectorization.
// Initialize cost to max so that VF = 2 is, at least, chosen during cost
// evaluation.
- Cost = std::numeric_limits<float>::max();
+ MinCost.first = std::numeric_limits<unsigned>::max();
}
+ auto isLowerVectorCost = [](std::pair<unsigned, unsigned> LHS,
+ std::pair<unsigned, unsigned> RHS) {
+ return (float(LHS.first) / LHS.second) < (float(RHS.first) / RHS.second);
+ };
+
for (unsigned i = 2; i <= MaxVF.getFixedValue(); i *= 2) {
// Notice that the vector loop needs to be executed less times, so
// we need to divide the cost of the vector loops by the width of
// the vector elements.
VectorizationCostTy C = expectedCost(ElementCount::getFixed(i));
- float VectorCost = C.first / (float)i;
+ std::pair<unsigned, unsigned> VectorCost = {C.first, i};
LLVM_DEBUG(dbgs() << "LV: Vector loop of width " << i
- << " costs: " << (int)VectorCost << ".\n");
+ << " costs: " << (int)(C.first / i) << ".\n");
if (!C.second && !ForceVectorization) {
LLVM_DEBUG(
dbgs() << "LV: Not considering vector loop of width " << i
<< " because it will not generate any vector instructions.\n");
continue;
}
- if (VectorCost < Cost) {
- Cost = VectorCost;
- Width = i;
- }
+ if (isLowerVectorCost(VectorCost, MinCost))
+ MinCost = VectorCost;
}
if (!EnableCondStoresVectorization && NumPredStores) {
reportVectorizationFailure("There are conditional stores.",
"store that is conditionally executed prevents vectorization",
"ConditionalStore", ORE, TheLoop);
- Width = 1;
- Cost = ScalarCost;
+ MinCost = {ScalarCost, 1};
}
- LLVM_DEBUG(if (ForceVectorization && Width > 1 && Cost >= ScalarCost) dbgs()
+ LLVM_DEBUG(if (ForceVectorization && MinCost.second > 1 &&
+ isLowerVectorCost({ScalarCost, 1}, MinCost)) dbgs()
<< "LV: Vectorization seems to be not beneficial, "
<< "but was forced by a user.\n");
- LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << Width << ".\n");
- VectorizationFactor Factor = {ElementCount::getFixed(Width),
- (unsigned)(Width * Cost)};
- return Factor;
+ LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << MinCost.second << ".\n");
+ ElementCount EC = ElementCount::getFixed(MinCost.second);
+ return {EC, MinCost.first};
}
std::pair<unsigned, unsigned>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92177.308013.patch
Type: text/x-patch
Size: 3572 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201127/31bd277e/attachment.bin>
More information about the llvm-commits
mailing list