Skip to content
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

[Relax][Bugfix] Provide the full Expr to pattern-match rewriter #16828

Conversation

Lunderberg
Copy link
Contributor

This resolves a bug that was introduced in #16732. If a rewriter function returned a no-op, and the pattern-match continued, then the matches provided to the rewriter function in subsequent calls would contain a variable to which the matched expression was bound, not the matched expression itself. (e.g. For a match of C = R.add(A,B), passing C to the rewriter instead of R.add(A,B).)

This bug was caused by incorrect re-wrapping of OrPattern in ExprPatternRewriter. Prior to #16732, all pattern-match results were populated by ExtractMatchExpr, and contained the result after applying TryGetValOfVar. When re-wrapping the result of an OrPattern, #16732 populated the additional matches with the result before applying TryGetValOfVar. This commit fixes the bug by applying TryGetValOfVar.

This resolves a bug that was introduced in
apache#16732.  If a rewriter function
returned a no-op, and the pattern-match continued, then the `matches`
provided to the rewriter function in subsequent calls would contain
a variable to which the matched expression was bound, not the matched
expression itself.  (e.g. For a match of `C = R.add(A,B)`, passing `C`
to the rewriter instead of `R.add(A,B)`.)

This bug was caused by incorrect re-wrapping of `OrPattern` in
`ExprPatternRewriter`.  Prior to
apache#16732, all pattern-match results
were populated by `ExtractMatchExpr`, and contained the result after
applying `TryGetValOfVar`.  When re-wrapping the result of an
`OrPattern`, apache#16732 populated the
additional matches with the result before applying `TryGetValOfVar`.
This commit fixes the bug by applying `TryGetValOfVar`.

This is a regression test. In versions from
https://github.com/apache/tvm/pull/16732 to
https://github.com/apache/tvm/pull/#####, the `rewrite_call`
Copy link
Contributor

Choose a reason for hiding this comment

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

It's 16828 now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, and fixed!

Copy link
Contributor

@slyubomirsky slyubomirsky left a comment

Choose a reason for hiding this comment

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

Ah, a sneaky bug. Thank you for fixing it and providing a test case.

@tqchen tqchen merged commit 00395ae into apache:main Apr 1, 2024
20 checks passed
@Lunderberg
Copy link
Contributor Author

Thank you for fixing it and providing a test case.

Thank you, though @MasterJH5574 deserves credit for making the test case. That helped quite a bit in narrowing down where the bug was occurring, and was very much appreciated.

@Lunderberg Lunderberg deleted the relax_bugfix_provide_full_expr_to_pattern_rewriter branch April 2, 2024 03:08
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
…he#16828)

* [Relax][Bugfix] Provide the full Expr to pattern-match rewriter

This resolves a bug that was introduced in
apache#16732.  If a rewriter function
returned a no-op, and the pattern-match continued, then the `matches`
provided to the rewriter function in subsequent calls would contain
a variable to which the matched expression was bound, not the matched
expression itself.  (e.g. For a match of `C = R.add(A,B)`, passing `C`
to the rewriter instead of `R.add(A,B)`.)

This bug was caused by incorrect re-wrapping of `OrPattern` in
`ExprPatternRewriter`.  Prior to
apache#16732, all pattern-match results
were populated by `ExtractMatchExpr`, and contained the result after
applying `TryGetValOfVar`.  When re-wrapping the result of an
`OrPattern`, apache#16732 populated the
additional matches with the result before applying `TryGetValOfVar`.
This commit fixes the bug by applying `TryGetValOfVar`.

* Update with PR link of bugfix
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.

4 participants