-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SLP] NFC. Refactor getEntryCost and isReverseOrder usage. #119680
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
Conversation
Users should check whether an input is empty before using isReverseOrder.
@llvm/pr-subscribers-llvm-transforms Author: Han-Kuan Chen (HanKuanChen) ChangesUsers should check whether an input is empty before using isReverseOrder. Full diff: https://github.com/llvm/llvm-project/pull/119680.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a1d7515f031cfc..10c6b9fae92740 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -4780,8 +4780,10 @@ static Align computeCommonAlignment(ArrayRef<Value *> VL) {
/// Check if \p Order represents reverse order.
static bool isReverseOrder(ArrayRef<unsigned> Order) {
+ assert(!Order.empty() &&
+ "Order is empty. Please check it before using isReverseOrder.");
unsigned Sz = Order.size();
- return !Order.empty() && all_of(enumerate(Order), [&](const auto &Pair) {
+ return all_of(enumerate(Order), [&](const auto &Pair) {
return Pair.value() == Sz || Sz - Pair.index() - 1 == Pair.value();
});
}
@@ -9837,7 +9839,7 @@ void BoUpSLP::transformNodes() {
Align CommonAlignment = computeCommonAlignment<LoadInst>(E.Scalars);
// Check if profitable to represent consecutive load + reverse as strided
// load with stride -1.
- if (isReverseOrder(E.ReorderIndices) &&
+ if (!E.ReorderIndices.empty() && isReverseOrder(E.ReorderIndices) &&
TTI->isLegalStridedLoadStore(VecTy, CommonAlignment)) {
SmallVector<int> Mask;
inversePermutation(E.ReorderIndices, Mask);
@@ -9864,7 +9866,7 @@ void BoUpSLP::transformNodes() {
Align CommonAlignment = computeCommonAlignment<StoreInst>(E.Scalars);
// Check if profitable to represent consecutive load + reverse as strided
// load with stride -1.
- if (isReverseOrder(E.ReorderIndices) &&
+ if (!E.ReorderIndices.empty() && isReverseOrder(E.ReorderIndices) &&
TTI->isLegalStridedLoadStore(VecTy, CommonAlignment)) {
SmallVector<int> Mask;
inversePermutation(E.ReorderIndices, Mask);
@@ -10990,7 +10992,6 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
unsigned EntryVF = E->getVectorFactor();
auto *FinalVecTy = getWidenedType(ScalarTy, EntryVF);
- bool NeedToShuffleReuses = !E->ReuseShuffleIndices.empty();
if (E->isGather()) {
if (allConstant(VL))
return 0;
@@ -11003,9 +11004,8 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
}
InstructionCost CommonCost = 0;
SmallVector<int> Mask;
- bool IsReverseOrder = isReverseOrder(E->ReorderIndices);
- if (!E->ReorderIndices.empty() &&
- (E->State != TreeEntry::StridedVectorize || !IsReverseOrder)) {
+ if (!E->ReorderIndices.empty() && (E->State != TreeEntry::StridedVectorize ||
+ !isReverseOrder(E->ReorderIndices))) {
SmallVector<int> NewMask;
if (E->getOpcode() == Instruction::Store) {
// For stores the order is actually a mask.
@@ -11016,7 +11016,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
}
::addMask(Mask, NewMask);
}
- if (NeedToShuffleReuses)
+ if (!E->ReuseShuffleIndices.empty())
::addMask(Mask, E->ReuseShuffleIndices);
if (!Mask.empty() && !ShuffleVectorInst::isIdentityMask(Mask, Mask.size()))
CommonCost =
@@ -15064,7 +15064,8 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
return Vec;
}
- bool IsReverseOrder = isReverseOrder(E->ReorderIndices);
+ bool IsReverseOrder =
+ !E->ReorderIndices.empty() && isReverseOrder(E->ReorderIndices);
auto FinalShuffle = [&](Value *V, const TreeEntry *E) {
ShuffleInstructionBuilder ShuffleBuilder(ScalarTy, Builder, *this);
if (E->getOpcode() == Instruction::Store &&
|
@llvm/pr-subscribers-vectorizers Author: Han-Kuan Chen (HanKuanChen) ChangesUsers should check whether an input is empty before using isReverseOrder. Full diff: https://github.com/llvm/llvm-project/pull/119680.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a1d7515f031cfc..10c6b9fae92740 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -4780,8 +4780,10 @@ static Align computeCommonAlignment(ArrayRef<Value *> VL) {
/// Check if \p Order represents reverse order.
static bool isReverseOrder(ArrayRef<unsigned> Order) {
+ assert(!Order.empty() &&
+ "Order is empty. Please check it before using isReverseOrder.");
unsigned Sz = Order.size();
- return !Order.empty() && all_of(enumerate(Order), [&](const auto &Pair) {
+ return all_of(enumerate(Order), [&](const auto &Pair) {
return Pair.value() == Sz || Sz - Pair.index() - 1 == Pair.value();
});
}
@@ -9837,7 +9839,7 @@ void BoUpSLP::transformNodes() {
Align CommonAlignment = computeCommonAlignment<LoadInst>(E.Scalars);
// Check if profitable to represent consecutive load + reverse as strided
// load with stride -1.
- if (isReverseOrder(E.ReorderIndices) &&
+ if (!E.ReorderIndices.empty() && isReverseOrder(E.ReorderIndices) &&
TTI->isLegalStridedLoadStore(VecTy, CommonAlignment)) {
SmallVector<int> Mask;
inversePermutation(E.ReorderIndices, Mask);
@@ -9864,7 +9866,7 @@ void BoUpSLP::transformNodes() {
Align CommonAlignment = computeCommonAlignment<StoreInst>(E.Scalars);
// Check if profitable to represent consecutive load + reverse as strided
// load with stride -1.
- if (isReverseOrder(E.ReorderIndices) &&
+ if (!E.ReorderIndices.empty() && isReverseOrder(E.ReorderIndices) &&
TTI->isLegalStridedLoadStore(VecTy, CommonAlignment)) {
SmallVector<int> Mask;
inversePermutation(E.ReorderIndices, Mask);
@@ -10990,7 +10992,6 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
unsigned EntryVF = E->getVectorFactor();
auto *FinalVecTy = getWidenedType(ScalarTy, EntryVF);
- bool NeedToShuffleReuses = !E->ReuseShuffleIndices.empty();
if (E->isGather()) {
if (allConstant(VL))
return 0;
@@ -11003,9 +11004,8 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
}
InstructionCost CommonCost = 0;
SmallVector<int> Mask;
- bool IsReverseOrder = isReverseOrder(E->ReorderIndices);
- if (!E->ReorderIndices.empty() &&
- (E->State != TreeEntry::StridedVectorize || !IsReverseOrder)) {
+ if (!E->ReorderIndices.empty() && (E->State != TreeEntry::StridedVectorize ||
+ !isReverseOrder(E->ReorderIndices))) {
SmallVector<int> NewMask;
if (E->getOpcode() == Instruction::Store) {
// For stores the order is actually a mask.
@@ -11016,7 +11016,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
}
::addMask(Mask, NewMask);
}
- if (NeedToShuffleReuses)
+ if (!E->ReuseShuffleIndices.empty())
::addMask(Mask, E->ReuseShuffleIndices);
if (!Mask.empty() && !ShuffleVectorInst::isIdentityMask(Mask, Mask.size()))
CommonCost =
@@ -15064,7 +15064,8 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
return Vec;
}
- bool IsReverseOrder = isReverseOrder(E->ReorderIndices);
+ bool IsReverseOrder =
+ !E->ReorderIndices.empty() && isReverseOrder(E->ReorderIndices);
auto FinalShuffle = [&](Value *V, const TreeEntry *E) {
ShuffleInstructionBuilder ShuffleBuilder(ScalarTy, Builder, *this);
if (E->getOpcode() == Instruction::Store &&
|
bool IsReverseOrder = isReverseOrder(E->ReorderIndices); | ||
if (!E->ReorderIndices.empty() && | ||
(E->State != TreeEntry::StridedVectorize || !IsReverseOrder)) { | ||
if (!E->ReorderIndices.empty() && (E->State != TreeEntry::StridedVectorize || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this formatted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
IsReverseOrder
is replaced with isReverseOrder
.
Users should check whether an input is empty before using isReverseOrder.