-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DAG] Add generic m_TernaryOp() / m_c_TernaryOp() matchers #165520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-selectiondag Author: 陈子昂 (Michael-Chen-NJU) ChangesSimilar to the m_BinOp/m_c_BinOp matchers, this patch introduces This includes:
Fixes #165378 Full diff: https://github.com/llvm/llvm-project/pull/165520.diff 3 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 0dcf400962393..9a6bf5ffdd227 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -583,6 +583,18 @@ m_InsertSubvector(const LHS &Base, const RHS &Sub, const IDX &Idx) {
return TernaryOpc_match<LHS, RHS, IDX>(ISD::INSERT_SUBVECTOR, Base, Sub, Idx);
}
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P>
+m_TernaryOp(unsigned Opc, const T0_P &Op0, const T1_P &Op1, const T2_P &Op2) {
+ return TernaryOpc_match<T0_P, T1_P, T2_P>(Opc, Op0, Op1, Op2);
+}
+
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P, true>
+m_c_TernaryOp(unsigned Opc, const T0_P &Op0, const T1_P &Op1, const T2_P &Op2) {
+ return TernaryOpc_match<T0_P, T1_P, T2_P, true>(Opc, Op0, Op1, Op2);
+}
+
template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy>
inline auto m_SelectCC(const LTy &L, const RTy &R, const TTy &T, const FTy &F,
const CCTy &CC) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index cf221bba1e3a3..c9ed7b8e4a7d3 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -18369,11 +18369,14 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
}
}
- // FIXME: Support splat of constant.
- if (N0CFP && N0CFP->isExactlyValue(1.0))
- return matcher.getNode(ISD::FADD, DL, VT, N1, N2);
- if (N1CFP && N1CFP->isExactlyValue(1.0))
- return matcher.getNode(ISD::FADD, DL, VT, N0, N2);
+ using namespace SDPatternMatch;
+ SDValue X, Y, Cst;
+
+ // (fma 1.0, X, Y) or (fma X, 1.0, Y) -> (fadd X, Y)
+ SDValue C1 = DAG.getConstantFP(1.0, DL, VT);
+ if (sd_match(N,
+ m_c_TernaryOp(ISD::FMA, m_Specific(C1), m_Value(X), m_Value(Y))))
+ return matcher.getNode(ISD::FADD, DL, VT, X, Y);
// Canonicalize (fma c, x, y) -> (fma x, c, y)
if (DAG.isConstantFPBuildVectorOrConstantFP(N0) &&
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index aa56aafa2812c..ceaee52a3948b 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -354,6 +354,76 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
sd_match(InsertELT, m_InsertElt(m_Value(), m_Value(), m_SpecificInt(1))));
}
+TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
+ SDLoc DL;
+ auto Float32VT = EVT::getFloatingPointVT(32);
+
+ SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Float32VT);
+ SDValue Op1 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 2, Float32VT);
+ SDValue Op2 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 3, Float32VT);
+
+ SDValue FMA = DAG->getNode(ISD::FMA, DL, Float32VT, Op0, Op1, Op2);
+ SDValue FAdd = DAG->getNode(ISD::FADD, DL, Float32VT, Op0, Op1);
+
+ using namespace SDPatternMatch;
+ SDValue A, B, C;
+
+ EXPECT_TRUE(sd_match(FMA, m_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_FALSE(sd_match(FMA, m_TernaryOp(ISD::FADD, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_FALSE(
+ sd_match(FAdd, m_TernaryOp(ISD::FMA, m_Value(), m_Value(), m_Value())));
+ EXPECT_FALSE(sd_match(FMA, m_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op0), m_Specific(Op2))));
+
+ EXPECT_TRUE(
+ sd_match(FMA, m_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+
+ EXPECT_TRUE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_TRUE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op0), m_Specific(Op2))));
+
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op2),
+ m_Specific(Op1), m_Specific(Op0))));
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op2),
+ m_Specific(Op0), m_Specific(Op1))));
+
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op2), m_Specific(Op1))));
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op2), m_Specific(Op0))));
+
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(B), m_Value(A), m_Value(C))));
+ EXPECT_EQ(A, Op1);
+ EXPECT_EQ(B, Op0);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ EXPECT_FALSE(
+ sd_match(FAdd, m_c_TernaryOp(ISD::FMA, m_Value(), m_Value(), m_Value())));
+}
+
TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
SDLoc DL;
auto Int32VT = EVT::getIntegerVT(Context, 32);
|
XChy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like most code in visitFMA can be replaced with SDPatternMatch helpers.
RKSimon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - cheers
|
@Michael-Chen-NJU Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Similar to the m_BinOp/m_c_BinOp matchers, this patch introduces generic matchers for SelectionDAG nodes with three operands. This includes: - Adding m_TernaryOp() and m_c_TernaryOp() templates in SDPatternMatch.h. - Adding comprehensive test coverage in SelectionDAGPatternMatchTest.cpp. Fixes llvm#165378
Similar to the m_BinOp/m_c_BinOp matchers, this patch introduces generic matchers for SelectionDAG nodes with three operands. This includes: - Adding m_TernaryOp() and m_c_TernaryOp() templates in SDPatternMatch.h. - Adding comprehensive test coverage in SelectionDAGPatternMatchTest.cpp. Fixes llvm#165378
Similar to the m_BinOp/m_c_BinOp matchers, this patch introduces
generic matchers for SelectionDAG nodes with three operands.
This includes:
Fixes #165378