[llvm] [SLP][REVEC] Support more mask pattern usage in shufflevector. (PR #106212)

Han-Kuan Chen via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 22:38:46 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);
+              int NextIndex = 0;
+              if (!all_of(Group, [&](Value *V) {
+                    auto *SV = cast<ShuffleVectorInst>(V);
+                    int Index;
+                    SV->isExtractSubvectorMask(Index);
+                    if (NextIndex != Index)
+                      return false;
----------------
HanKuanChen wrote:

When do we need to calculate the vector extract here? Vectorization will merge multiple shufflevector into 1 shufflevector. Vector extract is not used.

https://github.com/llvm/llvm-project/pull/106212


More information about the llvm-commits mailing list