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

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 03:47:40 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;
+              }
+            }
----------------
alexey-bataev wrote:

But you don't need to check for sorted array, just need to check that each next extract mask starting index > than the previous one

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


More information about the llvm-commits mailing list