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

core: Make PatternRewriter a Builder #3540

Open
wants to merge 1 commit into
base: math-fehr/stack/3
Choose a base branch
from

Conversation

math-fehr
Copy link
Collaborator

@math-fehr math-fehr commented Nov 29, 2024

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
Copy link

codecov bot commented Nov 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.36%. Comparing base (7ce487c) to head (a8b1c12).

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.
📢 Have feedback on the report? Share it here.

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

Fantastico

Copy link
Collaborator

@alexarice alexarice left a 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):
Copy link
Collaborator

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__

Copy link
Member

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

Copy link
Collaborator Author

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
has_done_action: bool = field(default=False, init=False)
has_done_action: bool

I think field is dead code if you add an init?

Copy link
Collaborator Author

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:
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Comment on lines +737 to +738
else:
self._worklist.remove(op)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
else:
self._worklist.remove(op)
self._worklist.remove(op)

Surely the operation itself still needs to be removed when it has regions?

Copy link
Collaborator Author

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)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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):
Copy link
Collaborator

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?

Copy link
Collaborator Author

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?

Comment on lines +72 to +75
if (block := op.parent) is not None:
block.erase_op(op, safe_erase=safe_erase)
else:
op.erase(safe_erase=safe_erase)
Copy link
Member

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

math-fehr added a commit that referenced this pull request Dec 5, 2024
Stacked PRs:
 * #3540
 * #3539
 * #3538
 * __->__#3537


--- --- ---

### transforms: use Rewriter instead of PatternRewriter in mlir-opt


`PatternRewriter` should only be used for rewrite patterns.
math-fehr added a commit that referenced this pull request Dec 5, 2024
…il (#3538)

Stacked PRs:
 * #3540
 * #3539
 * __->__#3538
 * #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.
EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
…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.
EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants