Skip to content

Conversation

@bermondd
Copy link
Contributor

@bermondd bermondd commented Dec 1, 2025

Fixes #169645

The issue was caused by a for-loop improperly overwriting SDValue binds when m_Reassociatable is given two or more patterns that (1) call m_Value with an SDValue parameter and (2) differ by that parameter. This fix comes with added unit tests relevant to SDValue bindings inside m_Reassociatable patterns.

Essentially, the original implementation first tried to match every combination of leaf node and pattern possible and stored that in a matrix-like structure, and then did a recursive search on that matrix to check if it's possible to pair every leaf with a pattern. The problem is that m_Value has a side effect where it changes an SDValue, and the creation of this matrix was corrupting these values. Below is an example of this, following the order of execution in the original implementation and using the case brought by issue #169645, where this behavior was found. The example tries to match ((a >> 1) + (b >> 1) + (a & b & 1)), using uppercase letters for the SDValue variables themselves and lowercase for their values. The result is that the pattern matches the same value for A and B, which was the behavior observed in the issue:

Line Leaf Pattern Match? Effect
1 a >> 1 m_Srl(m_Value(A), m_One()) Yes A <- a
2 a >> 1 m_Srl(m_Value(B), m_One()) Yes B <- a
3 a >> 1 m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One()) No --
4 b >> 1 m_Srl(m_Value(A), m_One()) Yes A <- b
5 b >> 1 m_Srl(m_Value(B), m_One()) Yes B <- b
6 b >> 1 m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One()) No --
7 a & b & 1 m_Srl(m_Value(A), m_One()) No --
8 a & b & 1 m_Srl(m_Value(B), m_One()) No --
9 a & b & 1 m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One()) a == b --

To fix this, the function now matches the patterns during the recursive search itself, instead of preparing the matrix beforehand. Although this does fix the issue, it does mean that we're performing a best case of n and worst case of n! matching attempts, instead of the fixed nˆ2 in the original, where n is the number of patterns provided. Going back to the table above, using this fix the lines 2, 3, 4, 6, 7, and 8 do not happen, and so the only effects happening are A <- a and B <- b, which then will result in line 9 matching correctly.

Maybe the way this was fixed could be improved, tho. This is my first contribution to LLVM and I'm still fairly inexperienced with C++, so I'm not sure if template recursion is discouraged, but it was the way I found to abide by the compile time constraints of the template. This was also my first ever PR to any open source project, so I don't know if this is too length, sorry if so. Open to any feedback :)

@github-actions
Copy link

github-actions bot commented Dec 1, 2025

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 @ followed by their GitHub username.

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.

@github-actions
Copy link

github-actions bot commented Dec 2, 2025

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(B))));
EXPECT_FALSE(sd_match(
ADD010, m_ReassociatableAdd(m_Value(A), m_Deferred(A), m_Deferred(A))));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add some negative test cases? That is, showing that this pattern does not match when it should not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a few more negative tests in response to another review, but they were all related to the binds done on the 2 positive test cases, e.g. to check m_Deferred(A) does not match with Op1 in the first case. I don't know if that meets what you asked for, though. Do you want me to add completely new negative test cases, alongside the current trivial one?

EXPECT_TRUE(sd_match(
ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(A))));
EXPECT_TRUE(sd_match(
ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(B))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I was often seeing wasn't that m_ReassociatableAdd etc. didn't return true - it was that the values of A + B weren't correct - please can you add additional checks that A and B are correctly initialized to the correct ADD01/Op0 pairs?

Copy link
Contributor Author

@bermondd bermondd Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, alright. Just added tests for this in the newest commit.
Edit: second to newest now. Had to do a fixup to use clang-format.

SDValue A, B;
EXPECT_TRUE(sd_match(
ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(A))));
EXPECT_TRUE(sd_match(Op0, m_Deferred(A)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_EQ(Op0, A)? ditto for other similar lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@RKSimon RKSimon requested review from RKSimon and mshockwave December 8, 2025 22:08
@RKSimon
Copy link
Collaborator

RKSimon commented Dec 8, 2025

@mshockwave - any more comments?

Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bermondd
Copy link
Contributor Author

bermondd commented Dec 9, 2025

Alright, thanks for the review! Just one small thing: I don't yet have commit access, so when all looks good to everyone I'll need someone to please merge this PR on my behalf

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - cheers

@RKSimon RKSimon enabled auto-merge (squash) December 9, 2025 08:57
@RKSimon RKSimon merged commit 3a81e03 into llvm:main Dec 9, 2025
9 of 10 checks passed
@github-actions
Copy link

github-actions bot commented Dec 9, 2025

@bermondd 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DAG] SDPatternMatch - m_Reassociatable mismatches

3 participants