[llvm] [SLP][REVEC] Support more mask pattern usage in shufflevector. (PR #106212)
Han-Kuan Chen via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 27 20:41:17 PDT 2024
================
@@ -10195,12 +10193,38 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
return VecCost;
};
if (SLPReVec && !E->isAltShuffle())
- return GetCostDiff(GetScalarCost, [](InstructionCost) {
- // shufflevector will be eliminated by instcombine because the
- // shufflevector masks are used in order (guaranteed by
- // getShufflevectorNumGroups). The vector cost is 0.
- return TTI::TCC_Free;
- });
+ return GetCostDiff(
+ GetScalarCost, [&](InstructionCost) -> InstructionCost {
+ // If a group uses mask in order, the shufflevector can be
+ // eliminated by instcombine. Then the cost is 0.
+ bool IsIdentity = true;
+ auto *SV = cast<ShuffleVectorInst>(VL.front());
+ unsigned SVNumElements =
+ cast<FixedVectorType>(SV->getOperand(0)->getType())
+ ->getNumElements();
+ unsigned GroupSize = SVNumElements / SV->getShuffleMask().size();
+ for (size_t I = 0, E = VL.size(); I != E; I += GroupSize) {
+ ArrayRef<Value *> Group = VL.slice(I, GroupSize);
+ SmallVector<int> ExtractionIndex(SVNumElements);
+ for_each(Group, [&](Value *V) {
+ auto *SV = cast<ShuffleVectorInst>(V);
+ int Index;
+ SV->isExtractSubvectorMask(Index);
+ for (int I :
+ seq<int>(Index, Index + SV->getShuffleMask().size()))
+ ExtractionIndex.push_back(I);
+ });
+ if (!is_sorted(ExtractionIndex)) {
+ IsIdentity = false;
+ break;
+ }
+ }
----------------
HanKuanChen wrote:
We can vectorize the following instructions and eliminate shufflevector.
```
%5 = shufflevector <4 x i64> %3, <4 x i64> poison, <2 x i32> <i32 0, i32 1>
%6 = shufflevector <4 x i64> %3, <4 x i64> poison, <2 x i32> <i32 2, i32 3>
%7 = shufflevector <4 x i64> %4, <4 x i64> poison, <2 x i32> <i32 0, i32 1>
%8 = shufflevector <4 x i64> %4, <4 x i64> poison, <2 x i32> <i32 2, i32 3>
```
But this cannot.
```
%5 = shufflevector <4 x i64> %3, <4 x i64> poison, <2 x i32> <i32 2, i32 3>
%6 = shufflevector <4 x i64> %3, <4 x i64> poison, <2 x i32> <i32 0, i32 1>
%7 = shufflevector <4 x i64> %4, <4 x i64> poison, <2 x i32> <i32 0, i32 1>
%8 = shufflevector <4 x i64> %4, <4 x i64> poison, <2 x i32> <i32 2, i32 3>
```
We need to check all shufflevector and know whether there is an identity mask.
https://github.com/llvm/llvm-project/pull/106212
More information about the llvm-commits
mailing list