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

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 16 10:12:42 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;
----------------
mshockwave wrote:

> Per [ArrayRef docs](https://llvm.org/doxygen/ArrayRef_8h_source.html#l00545) `==` is essentially `std::equal(begin(), end(), RHS.begin()`

You're right about this, my bad.

> I am curious why rhs.begin(),rhs.end() is not used

I guess you're referring to the implementation of `ArrayRef::operator==`, and that is because it already compares the length of LHS and RHS first, so `RHS.end()` is not really necessary needed in `std::equal`.

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


More information about the llvm-commits mailing list