Skip to content

Commit f3bc8c3

Browse files
AidanGoldfarbAidan
andauthored
Add SD matchers and unit test coverage for ISD::VECTOR_SHUFFLE (#119592)
This PR resolves #118845. I aimed to mirror the implementation `m_Shuffle()` in [PatternMatch.h](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/PatternMatch.h). Updated [SDPatternMatch.h](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/SDPatternMatch.h) - Added `struct m_Mask` to match masks (`ArrayRef<int>`) - Added two `m_Shuffle` functions. One to match independently of mask, and one to match considering mask. - Added `struct SDShuffle_match` to match `ISD::VECTOR_SHUFFLE` considering mask Updated [SDPatternMatchTest.cpp](https://github.com/llvm/llvm-project/blob/main/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp) - Added `matchVecShuffle` test, which tests the behavior of both `m_Shuffle()` functions - - - I am not sure if my test coverage is complete. I am not sure how to test a `false` match, simply test against a different instruction? [Other tests ](https://github.com/llvm/llvm-project/blob/main/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp#L175), such as for `VSelect`, test against `Select`. I am not sure if there is an analogous instruction to compare against for `VECTOR_SHUFFLE`. I would appreciate some pointers in this area. In general, please liberally critique this PR! --------- Co-authored-by: Aidan <[email protected]>
1 parent 9236751 commit f3bc8c3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,39 @@ struct BinaryOpc_match {
547547
}
548548
};
549549

550+
/// Matching while capturing mask
551+
template <typename T0, typename T1, typename T2> struct SDShuffle_match {
552+
T0 Op1;
553+
T1 Op2;
554+
T2 Mask;
555+
556+
SDShuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
557+
: Op1(Op1), Op2(Op2), Mask(Mask) {}
558+
559+
template <typename MatchContext>
560+
bool match(const MatchContext &Ctx, SDValue N) {
561+
if (auto *I = dyn_cast<ShuffleVectorSDNode>(N)) {
562+
return Op1.match(Ctx, I->getOperand(0)) &&
563+
Op2.match(Ctx, I->getOperand(1)) && Mask.match(I->getMask());
564+
}
565+
return false;
566+
}
567+
};
568+
struct m_Mask {
569+
ArrayRef<int> &MaskRef;
570+
m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
571+
bool match(ArrayRef<int> Mask) {
572+
MaskRef = Mask;
573+
return true;
574+
}
575+
};
576+
577+
struct m_SpecificMask {
578+
ArrayRef<int> MaskRef;
579+
m_SpecificMask(ArrayRef<int> MaskRef) : MaskRef(MaskRef) {}
580+
bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
581+
};
582+
550583
template <typename LHS_P, typename RHS_P, typename Pred_t,
551584
bool Commutable = false, bool ExcludeChain = false>
552585
struct MaxMin_match {
@@ -797,6 +830,17 @@ inline BinaryOpc_match<LHS, RHS> m_FRem(const LHS &L, const RHS &R) {
797830
return BinaryOpc_match<LHS, RHS>(ISD::FREM, L, R);
798831
}
799832

833+
template <typename V1_t, typename V2_t>
834+
inline BinaryOpc_match<V1_t, V2_t> m_Shuffle(const V1_t &v1, const V2_t &v2) {
835+
return BinaryOpc_match<V1_t, V2_t>(ISD::VECTOR_SHUFFLE, v1, v2);
836+
}
837+
838+
template <typename V1_t, typename V2_t, typename Mask_t>
839+
inline SDShuffle_match<V1_t, V2_t, Mask_t>
840+
m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
841+
return SDShuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
842+
}
843+
800844
template <typename LHS, typename RHS>
801845
inline BinaryOpc_match<LHS, RHS> m_ExtractElt(const LHS &Vec, const RHS &Idx) {
802846
return BinaryOpc_match<LHS, RHS>(ISD::EXTRACT_VECTOR_ELT, Vec, Idx);

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ TEST_F(SelectionDAGPatternMatchTest, matchValueType) {
119119
EXPECT_FALSE(sd_match(Op2, m_ScalableVectorVT()));
120120
}
121121

122+
TEST_F(SelectionDAGPatternMatchTest, matchVecShuffle) {
123+
SDLoc DL;
124+
auto Int32VT = EVT::getIntegerVT(Context, 32);
125+
auto VInt32VT = EVT::getVectorVT(Context, Int32VT, 4);
126+
const std::array<int, 4> MaskData = {2, 0, 3, 1};
127+
const std::array<int, 4> OtherMaskData = {1, 2, 3, 4};
128+
ArrayRef<int> Mask;
129+
130+
SDValue V0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, VInt32VT);
131+
SDValue V1 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 2, VInt32VT);
132+
SDValue VecShuffleWithMask =
133+
DAG->getVectorShuffle(VInt32VT, DL, V0, V1, MaskData);
134+
135+
using namespace SDPatternMatch;
136+
EXPECT_TRUE(sd_match(VecShuffleWithMask, m_Shuffle(m_Value(), m_Value())));
137+
EXPECT_TRUE(sd_match(VecShuffleWithMask,
138+
m_Shuffle(m_Value(), m_Value(), m_Mask(Mask))));
139+
EXPECT_TRUE(
140+
sd_match(VecShuffleWithMask,
141+
m_Shuffle(m_Value(), m_Value(), m_SpecificMask(MaskData))));
142+
EXPECT_FALSE(
143+
sd_match(VecShuffleWithMask,
144+
m_Shuffle(m_Value(), m_Value(), m_SpecificMask(OtherMaskData))));
145+
EXPECT_TRUE(
146+
std::equal(MaskData.begin(), MaskData.end(), Mask.begin(), Mask.end()));
147+
}
148+
122149
TEST_F(SelectionDAGPatternMatchTest, matchTernaryOp) {
123150
SDLoc DL;
124151
auto Int32VT = EVT::getIntegerVT(Context, 32);

0 commit comments

Comments
 (0)