[llvm] Add SD matchers and unit test coverage for ISD::VECTOR_SHUFFLE (PR #119592)

Aidan Goldfarb via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 15 08:44:08 PST 2024


================
@@ -540,6 +540,45 @@ struct BinaryOpc_match {
   }
 };
 
+/// Matching while capturing mask
+template <typename T0, typename T1> struct SDShuffle_match {
+  T0 Op1;
+  T1 Op2;
+
+  const ArrayRef<int> *MaskRef;
+
+  // capturing mask
+  SDShuffle_match(const T0 &Op1, const T1 &Op2, const ArrayRef<int> &MaskRef)
+      : Op1(Op1), Op2(Op2), MaskRef(&MaskRef) {}
+
+  template <typename MatchContext>
+  bool match(const MatchContext &Ctx, SDValue N) {
+    if (auto *I = dyn_cast<ShuffleVectorSDNode>(N)) {
+      return Op1.match(Ctx, I->getOperand(0)) &&
+             Op2.match(Ctx, I->getOperand(1));
+    }
+    return false;
+  }
+};
+
+/// Matching against a specific match
+template <typename T0, typename T1> struct SDShuffle_maskMatch {
+  T0 Op1;
+  T1 Op2;
+  ArrayRef<int> SpecificMask;
+
+  SDShuffle_maskMatch(const T0 &Op1, const T1 &Op2, const ArrayRef<int> Mask)
+      : Op1(Op1), Op2(Op2), SpecificMask(Mask) {}
+
+  template <typename MatchContext>
+  bool match(const MatchContext &Ctx, SDValue N) {
+    if (auto *I = dyn_cast<ShuffleVectorSDNode>(N)) {
+      return Op1.match(Ctx, I->getOperand(0)) &&
+             Op2.match(Ctx, I->getOperand(1)) && I->getMask() == SpecificMask;
----------------
AidanGoldfarb wrote:

Per [ArrayRef docs](https://llvm.org/doxygen/ArrayRef_8h_source.html#l00545) `==` is essentially `std::equal(begin(), end(), RHS.begin()`, which as you mentioned earlier slips through when `ArrayRef` is empty. I am curious why `rhs.begin(),rhs.end()` is not used. I have changed this to `std::equal(SpecificMask.begin(),SpecificMask.end(),I->getMask().begin(),I->getMask().end())`

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


More information about the llvm-commits mailing list