-
Notifications
You must be signed in to change notification settings - Fork 74
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
core: Make PatternRewriter a Builder #3540
base: math-fehr/stack/3
Are you sure you want to change the base?
Conversation
This allows us to now use the ImplicitBuilder on PatternRewriter, or to use `insert` using an InsertPoint. stack-info: PR: #3540, branch: math-fehr/stack/4
56bc13b
to
a8b1c12
Compare
902ccce
to
7ce487c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## math-fehr/stack/3 #3540 +/- ##
==================================================
Coverage 90.36% 90.36%
==================================================
Files 466 466
Lines 58517 58544 +27
Branches 5584 5588 +4
==================================================
+ Hits 52878 52905 +27
Misses 4210 4210
Partials 1429 1429 ☔ View full report in Codecov by Sentry. |
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.
Fantastico
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 left some comments but they may be more due to my ignorance than actual problems with the change.
@@ -91,6 +91,11 @@ class PatternRewriter(PatternRewriterListener): | |||
has_done_action: bool = field(default=False, init=False) | |||
"""Has the rewriter done any action during the current match.""" | |||
|
|||
def __init__(self, current_operation: Operation): |
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.
You could possibly do this with a __post_init__
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.
The self.current_operation = current_operation
seems like strictly __init__
code to me
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 cannot actually do a __post_init__
, because if I would use the generated __init__
, this would require the arguments for PatternRewriterListener
and Builder
. But the Builder
arguments gets constructed in the __init__
I wrote here.
@@ -91,6 +91,11 @@ class PatternRewriter(PatternRewriterListener): | |||
has_done_action: bool = field(default=False, init=False) |
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.
has_done_action: bool = field(default=False, init=False) | |
has_done_action: bool |
I think field
is dead code if you add an init?
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.
Nope, the default
still get added. I removed the init=False
though, as we don't have an __init__
here.
@@ -726,7 +731,11 @@ def _handle_operation_removal(self, op: Operation) -> None: | |||
"""Handle removal of an operation.""" | |||
if self.apply_recursively: | |||
self._add_operands_to_worklist(op.operands) | |||
self._worklist.remove(op) | |||
if op.regions: |
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.
Is this a separate change to making PatternRewriter
a Builder
?
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 will rebase this branch on a new one, this was indeed a bug before that can be refactored.
else: | ||
self._worklist.remove(op) |
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.
else: | |
self._worklist.remove(op) | |
self._worklist.remove(op) |
Surely the operation itself still needs to be removed when it has regions?
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.
op.walk()
will walk the current operation as well.
if (block := op.parent) is not None: | ||
block.erase_op(op, safe_erase=safe_erase) | ||
else: | ||
op.erase(safe_erase=safe_erase) |
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 is it now ok to erase a operation with no parents?
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'll move this to a separate PR as well, the idea is that sometimes we detach an operation and then destroy it.
@@ -43,7 +44,8 @@ def match_and_rewrite(self, loop: scf.ParallelOp, rewriter: PatternRewriter, /): | |||
regions=[Region(Block())], | |||
operands=[[], [], [], [], [], []], | |||
) | |||
with ImplicitBuilder(parallel.region): | |||
rewriter.insertion_point = InsertPoint.at_end(parallel.region.block) | |||
with ImplicitBuilder(rewriter): |
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 I'm a fan of this API. Does the user need to reset the insertion point of the rewriter after doing this?
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 agree the API is not optimal.
Currently, what we need to use ImplicitBuilder
is:
- set the insertion point on the rewriter
- Create the ImplicitBuilder using the rewriter
We don't need to reset it after though.
What would you think of ImplicitBuilder(rewriter, at=InsertPoint.at_end(...))
, which would set the insertion point just for that region of code?
if (block := op.parent) is not None: | ||
block.erase_op(op, safe_erase=safe_erase) | ||
else: | ||
op.erase(safe_erase=safe_erase) |
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.
TBH this feels like it could be its own PR, with an accompanying test
…project#3537) Stacked PRs: * xdslproject#3540 * xdslproject#3539 * xdslproject#3538 * __->__#3537 --- --- --- ### transforms: use Rewriter instead of PatternRewriter in mlir-opt `PatternRewriter` should only be used for rewrite patterns.
…il (xdslproject#3538) Stacked PRs: * xdslproject#3540 * xdslproject#3539 * __->__#3538 * xdslproject#3537 --- --- --- ### transforms: use rewriter and listener in convert-stencil-to-csl-stencil The pass was not propagating the listener from the PatternRewriter, and thus some operations were modified without notifying the rewrite worklist.
Stacked PRs:
core: Make PatternRewriter a Builder
This allows us to now use the ImplicitBuilder on PatternRewriter,
or to use
insert
using an InsertPoint.