Skip to content

[SLP] Move transformMaskAfterShuffle into BaseShuffleAnalysis and use it as much as possible. #123896

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

Merged
merged 1 commit into from
Jan 23, 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
47 changes: 14 additions & 33 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9480,6 +9480,15 @@ class BaseShuffleAnalysis {
return Builder.createShuffleVector(V1, NewMask);
return Builder.createIdentity(V1);
}

/// Transforms mask \p CommonMask per given \p Mask to make proper set after
/// shuffle emission.
static void transformMaskAfterShuffle(MutableArrayRef<int> CommonMask,
ArrayRef<int> Mask) {
for (unsigned I : seq<unsigned>(CommonMask.size()))
if (Mask[I] != PoisonMaskElem)
CommonMask[I] = I;
}
};
} // namespace

Expand Down Expand Up @@ -10309,14 +10318,6 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
}
return Cost;
}
/// Transforms mask \p CommonMask per given \p Mask to make proper set after
/// shuffle emission.
static void transformMaskAfterShuffle(MutableArrayRef<int> CommonMask,
ArrayRef<int> Mask) {
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (Mask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
}
/// Adds the cost of reshuffling \p E1 and \p E2 (if present), using given
/// mask \p Mask, register number \p Part, that includes \p SliceSize
/// elements.
Expand Down Expand Up @@ -10939,9 +10940,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
Cost += createShuffle(Vec, InVectors.back(), CommonMask);
else
Cost += createShuffle(Vec, nullptr, CommonMask);
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (CommonMask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
transformMaskAfterShuffle(CommonMask, CommonMask);
assert(VF > 0 &&
"Expected vector length for the final value before action.");
Value *V = cast<Value *>(Vec);
Expand All @@ -10954,9 +10953,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
Cost += createShuffle(Vec, InVectors.back(), CommonMask);
else
Cost += createShuffle(Vec, nullptr, CommonMask);
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (CommonMask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
transformMaskAfterShuffle(CommonMask, CommonMask);
// Add subvectors permutation cost.
if (!SubVectorsMask.empty()) {
assert(SubVectorsMask.size() <= CommonMask.size() &&
Expand Down Expand Up @@ -14202,15 +14199,6 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
ShuffleBuilder);
}

/// Transforms mask \p CommonMask per given \p Mask to make proper set after
/// shuffle emission.
static void transformMaskAfterShuffle(MutableArrayRef<int> CommonMask,
ArrayRef<int> Mask) {
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (Mask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
}

/// Cast value \p V to the vector type with the same number of elements, but
/// the base type \p ScalarTy.
Value *castToScalarTyElem(Value *V,
Expand Down Expand Up @@ -14541,9 +14529,7 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
} else {
Vec = createShuffle(Vec, nullptr, CommonMask);
}
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (CommonMask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
transformMaskAfterShuffle(CommonMask, CommonMask);
assert(VF > 0 &&
"Expected vector length for the final value before action.");
unsigned VecVF = cast<FixedVectorType>(Vec->getType())->getNumElements();
Expand All @@ -14563,9 +14549,7 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
} else {
Vec = createShuffle(Vec, nullptr, CommonMask);
}
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx)
if (CommonMask[Idx] != PoisonMaskElem)
CommonMask[Idx] = Idx;
transformMaskAfterShuffle(CommonMask, CommonMask);
auto CreateSubVectors = [&](Value *Vec,
SmallVectorImpl<int> &CommonMask) {
for (auto [E, Idx] : SubVectors) {
Expand Down Expand Up @@ -14606,10 +14590,7 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
Value *InsertVec =
CreateSubVectors(PoisonValue::get(Vec->getType()), CommonMask);
Vec = createShuffle(InsertVec, Vec, SVMask);
for (unsigned I : seq<unsigned>(CommonMask.size())) {
if (SVMask[I] != PoisonMaskElem)
CommonMask[I] = I;
}
transformMaskAfterShuffle(CommonMask, SVMask);
}
InVectors.front() = Vec;
}
Expand Down
Loading