[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
Mon Dec 30 09:54:48 PST 2024


================
@@ -547,6 +547,39 @@ struct BinaryOpc_match {
   }
 };
 
+/// Matching while capturing mask
+template <typename T0, typename T1, typename T2> struct SDShuffle_match {
+  T0 Op1;
+  T1 Op2;
+  T2 Mask;
+
+  SDShuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
+      : Op1(Op1), Op2(Op2), Mask(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)) && Mask.match(I->getMask());
+    }
+    return false;
+  }
+};
+struct m_Mask {
+  ArrayRef<int> MaskRef;
----------------
AidanGoldfarb wrote:

I am still not understanding passing an ArrayRef by reference. To clarify, I beleive what you and @mshockwave are suggesting is to implement `m_Mask` by reference and `m_SpecificMask` by value. 

The argument passed to `m_Mask` will be reference to an `ArrayRef<int>`. 

The argument passed to `m_SpecificMask` will be an `ArrayRef<int>`, copying the data. The data being `const [T] *Data` and `size_type Length`. 

- - -
Where I am confused,assuming what I stated above is correct: 

- In the IR, patternmatch.h, both `m_Mask` and `m_SpecificMask` are implement as pass-by-reference (as is `m_SplatOrPoisonMask`, `m_ZeroMask` is by value). Is what we want to do different than what happens in the IR?

- I think I am not aware enough of the uses of matching in this context. Because of this, passing an ArrayRef by reference or by value seems almost the same, depending on the usecase. In both cases you have access to the underlying array, so modifcations can be made. Are we trying to avoid a copy of the `Length` field? Is there a case where we want to modify the `ArrayRef` object itself, but not via the underlying array? 

- - - 
Some clarification would be greatly appreciated, thank you!

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


More information about the llvm-commits mailing list