-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Add m_SelectCCLike matcher to match SELECT_CC or SELECT with SETCC #149646
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
|
@llvm/pr-subscribers-llvm-selectiondag Author: 黃國庭 (houngkoungting) ChangesFix #147282 and Follow-up to #148834 Full diff: https://github.com/llvm/llvm-project/pull/149646.diff 2 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 2967532226197..aeafb4f6dbadf 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -93,7 +93,7 @@ struct Value_match {
explicit Value_match(SDValue Match) : MatchVal(Match) {}
- template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+ template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
if (MatchVal)
return MatchVal == N;
return N.getNode();
@@ -130,7 +130,7 @@ struct DeferredValue_match {
explicit DeferredValue_match(SDValue &Match) : MatchVal(Match) {}
- template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+ template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
return N == MatchVal;
}
};
@@ -196,7 +196,7 @@ struct Value_bind {
explicit Value_bind(SDValue &N) : BindVal(N) {}
- template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+ template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
BindVal = N;
return true;
}
@@ -1203,7 +1203,7 @@ struct CondCode_match {
explicit CondCode_match(ISD::CondCode *CC) : BindCC(CC) {}
- template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+ template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
if (auto *CC = dyn_cast<CondCodeSDNode>(N.getNode())) {
if (CCToMatch && *CCToMatch != CC->get())
return false;
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index fed5e7238433e..5acc3a23a28b5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -265,6 +265,47 @@ namespace {
MaximumLegalStoreInBits = VT.getSizeInBits().getKnownMinValue();
}
+ template <typename LTy, typename RTy, typename TTy, typename FTy,
+ typename CCTy>
+ struct SelectCC_match {
+ LTy L;
+ RTy R;
+ TTy T;
+ FTy F;
+ CCTy CC;
+
+ SelectCC_match(LTy L, RTy R, TTy T, FTy F, CCTy CC)
+ : L(std::move(L)), R(std::move(R)), T(std::move(T)), F(std::move(F)),
+ CC(std::move(CC)) {}
+
+ template <typename MatchContext>
+ bool match(const MatchContext &Ctx, SDValue V) const {
+ return V.getOpcode() == ISD::SELECT_CC && L.match(Ctx, V.getOperand(0)) &&
+ R.match(Ctx, V.getOperand(1)) && T.match(Ctx, V.getOperand(2)) &&
+ F.match(Ctx, V.getOperand(3)) && CC.match(Ctx, V.getOperand(4));
+ }
+ };
+
+ template <typename LTy, typename RTy, typename TTy, typename FTy,
+ typename CCTy>
+ inline auto m_SelectCC(LTy &&L, RTy &&R, TTy &&T, FTy &&F, CCTy &&CC) {
+ return SelectCC_match<std::decay_t<LTy>, std::decay_t<RTy>,
+ std::decay_t<TTy>, std::decay_t<FTy>,
+ std::decay_t<CCTy>>(
+ std::forward<LTy>(L), std::forward<RTy>(R), std::forward<TTy>(T),
+ std::forward<FTy>(F), std::forward<CCTy>(CC));
+ }
+
+ template <typename LTy, typename RTy, typename TTy, typename FTy,
+ typename CCTy>
+ inline auto m_SelectCCLike(LTy &&L, RTy &&R, TTy &&T, FTy &&F, CCTy &&CC) {
+ return SDPatternMatch::m_AnyOf(
+ SDPatternMatch::m_Select(SDPatternMatch::m_SetCC(L, R, CC), T, F),
+ m_SelectCC(std::forward<LTy>(L), std::forward<RTy>(R),
+ std::forward<TTy>(T), std::forward<FTy>(F),
+ std::forward<CCTy>(CC)));
+ }
+
void ConsiderForPruning(SDNode *N) {
// Mark this for potential pruning.
PruningList.insert(N);
@@ -4099,12 +4140,11 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
// (sub x, ([v]select (uge x, y), y, 0)) -> (umin x, (sub x, y))
if (N1.hasOneUse() && hasUMin(VT)) {
SDValue Y;
- if (sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
- m_SpecificCondCode(ISD::SETULT)),
- m_Zero(), m_Deferred(Y))) ||
- sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
- m_SpecificCondCode(ISD::SETUGE)),
- m_Deferred(Y), m_Zero())) ||
+ if (sd_match(N1, m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Zero(),
+ m_Deferred(Y),
+ m_SpecificCondCode(ISD::SETULT))) ||
+ sd_match(N1, m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Deferred(Y),
+ m_Zero(), m_SpecificCondCode(ISD::SETUGE)))||
sd_match(N1, m_VSelect(m_SetCC(m_Specific(N0), m_Value(Y),
m_SpecificCondCode(ISD::SETULT)),
m_Zero(), m_Deferred(Y))) ||
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Please add unit test coverage in SelectionDAGPatternMatchTest.cpp
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.
Something seems to have gone wrong in the patch creation - m_SelectCCLike is missing and clang-format has gone amok
|
HI @RKSimon Got it! Let me figure out the best way to fix this — appreciate the detailed comments! 👍 |
|
HI @RKSimon , I’ve applied clang-format as suggested and adjusted the function signature to the inline style: "template bool match(MatchContext &Ctx, SDValue V) {" However, the pre-commit formatting check still seems to fail at this exact location. |
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.
Why did you have to use this instead of just m_Node(ISD::SELECT_CC,L, R, T, F, CC)?
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.
Why the namespace?
|
HI @RKSimon , Thank you for pointing that out! I've removed the unnecessary SDPatternMatch:: namespace and replaced the custom matcher with m_Node(...) . Please help take another look when you have time. |
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.
probably best not to repeat LHS/RHS like this in the test?
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.
@mshockwave any thoughts?
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.
why do you have to use forwarding?
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.
HI @RKSimon , I have removed the unnecessary std::forward calls as suggested and updated it.
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.
A few minors but looks almost ready to go
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.
do we need rvalue && here?
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.
@RKSimon You're right — thanks for pointing that out! Just give me a few minutes to update it .
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.
do we need rvalue && here?
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.
Please can you add m_SelectCC unit test coverage
| } | ||
|
|
||
| template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy> | ||
| inline auto m_SelectCC(LTy L, RTy R, TTy T, FTy F, CCTy CC) { |
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.
still need to by ref m_SelectCC(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC) (just not rvalue)
| } | ||
|
|
||
| template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy> | ||
| inline auto m_SelectCCLike(LTy L, RTy R, TTy T, FTy F, CCTy CC) { |
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.
m_SelectCCLike(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC)
|
@houngkoungting almost there - please can you fix the references and I can get this committed for you. |
|
HI @RKSimon , No problem! Give me some time — I’ll fix the references very soon. |
|
HI @RKSimon ,I’ve already fixed it — does this look okay? I’ve noticed that most matcher utilities in LLVM typically use const & for their parameters. This allows them to accept rvalues directly, which makes the code cleaner and easier to use. I’m a bit unclear on why it has to be restricted to lvalues only — would you mind explaining?(I'm not very familiar with DAG.) thanks |
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.
Can you remove matchUMinSubSelect and revert this back to using m_Select/m_VSelect? I'm hoping that once #150019 lands m_SelectCCLike can support m_VSelect as well.
|
@mshockwave any comments? |
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.
I don't think you need to create a context here. There are variants of sd_match that do not require context or explicit SelectionDAG: https://llvm.org/doxygen/namespacellvm_1_1SDPatternMatch.html#a22e69d98d1e6e8f2308ede1c5809d0ac
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.
ditto unnecessary context
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.
please use sd_match like other tests in this file.
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.
ditto sd_match
|
Please avoid force push unless necessary: https://llvm.org/docs/GitHub.html#rebasing-pull-requests-and-force-pushes |
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.
@RKSimon do you have any other comments?
Fix #147282 and Follow-up to #148834
@RKSimon