Skip to content

[mlir][docs] Add docs on canonicalizers being folders or patterns #129517

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

Merged
merged 3 commits into from
Mar 6, 2025

Conversation

Groverkss
Copy link
Member

@Groverkss Groverkss commented Mar 3, 2025

If a transformation should be a canonicalization is an orthogonal question to if a transformation should be implemented as a RewritePattern or a fold method. The later is an implementation detail.

This patch adds a suggestion to always implement a canonicalization as a fold pattern if possible, as they are a restricted subset of a RewritePattern.

This has been a common source of confusion, as to when to implement a canonicalization as a fold method or a RewritePattern.

@llvmbot
Copy link
Member

llvmbot commented Mar 3, 2025

@llvm/pr-subscribers-mlir

Author: Kunwar Grover (Groverkss)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/129517.diff

1 Files Affected:

  • (modified) mlir/docs/Canonicalization.md (+23)
diff --git a/mlir/docs/Canonicalization.md b/mlir/docs/Canonicalization.md
index 6e59a4128093a..c0553d6698c6a 100644
--- a/mlir/docs/Canonicalization.md
+++ b/mlir/docs/Canonicalization.md
@@ -275,3 +275,26 @@ Operation *MyDialect::materializeConstant(OpBuilder &builder, Attribute value,
   ...
 }
 ```
+
+### Choosing between implementing a Folder or a `RewritePattern`
+
+When implementing a new canonicalization, an important thing to think about is
+if the canonicalization should be a folder or a `RewritePattern`. The generally
+accepted policy is:
+
+*   If a transformation should be a canonicalizer is a different question from
+    if a transformation should be a folder or a `RewritePattern`. A
+    transformation is promoted to a canonicalization as defined by the General
+    Design of canonicalizations.
+
+*   If a transformation is a canonicalization, there are two ways to implement
+    it: As a Folder or as a `RewritePattern`, both of which are implementation
+    details of how a canonicalization exists, with the difference being folders
+    have restrictions on what they can do. A `RewritePattern` can implement any
+    transformation a folder can.
+
+*   A canonicalization should always be implemented as a Folder if it fits
+    the "local" definition of a folder.
+
+*   If a canonicalization does not fit as a folder, it should be implemented
+    as a `RewritePattern`.

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

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

That's fine with me, but I'd like to get approval from @jpienaar and/or @River707

@Groverkss Groverkss requested review from Mogball and River707 March 3, 2025 11:46
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

Thanks for making this area less ambiguous

Comment on lines 285 to 288
* If a transformation should be a canonicalizer is a different question from
if a transformation should be a folder or a `RewritePattern`. A
transformation is promoted to a canonicalization as defined by the General
Design of canonicalizations.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe write this in a more imperative way? For example, you could add a 'NOTE' marker that says: 'To decide whether a transformation should be a canonicalization in the first place, see the General Design of Canonicalization.' (link)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if I want it to be a NOTE. I'm trying to stress the point here that you cannot say that a transformation should be a RewritePattern canonicalization, but it shouldn't be a folder canonicalization based on what the transformation does. It's an implementation detail. The only thing you have to reason about is if a transformation should be a canonicalization or not. If it's a folder or a RewritePattern is dependent on it's implementation.

Design of canonicalizations.

* If a transformation is a canonicalization, there are two ways to implement
it: As a Folder or as a `RewritePattern`, both of which are implementation
Copy link
Member

Choose a reason for hiding this comment

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

I'd also demote this bullet to a new paragraph after the 'NOTE'

Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

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

Hey @Groverkss , thank you so much for sending this!

I've left a couple of comments inline, but I also have one high-level comment.

There are already two fairly dense paragraphs on the two approaches to implementing "canonicalizations":

I think that you can safely trim your addition to the following paragraph (I tweaked the title so that the naming aligns with what's currently used in this document):

### When to use the `fold` method vs `RewriterPattern`s for canonicalizations

A canonicalization should always be implemented as a Folder if it fits the "local" definition of a folder, otherwise it should be implemented as a `RewritePattern`.

From what I can tell, this is the main new bit of info added in this PR . The other items seem to repeat what's already here. For example:

If a transformation is a canonicalization, there are two ways to implement
it: As a Folder or as a RewritePattern, both of which are implementation

That's already covered by the paragraphs that I linked :) (i.e. that's why we have them)

Also, this part doesn't really get us closer to understanding what to prefer - the "fold" method vs RewriterPattern:

If a transformation should be a canonicalizer is a different question from
if a transformation should be a folder or a RewritePattern. A
transformation is promoted to a canonicalization as defined by the General
Design of canonicalizations.

One other point, could there be some other heuristics to consider here? For example, the cost and the frequency with which different "implementations" would be run.

@Groverkss
Copy link
Member Author

Hey @Groverkss , thank you so much for sending this!

I've left a couple of comments inline, but I also have one high-level comment.

There are already two fairly dense paragraphs on the two approaches to implementing "canonicalizations":

I think that you can safely trim your addition to the following paragraph (I tweaked the title so that the naming aligns with what's currently used in this document):

### When to use the `fold` method vs `RewriterPattern`s for canonicalizations

A canonicalization should always be implemented as a Folder if it fits the "local" definition of a folder, otherwise it should be implemented as a `RewritePattern`.

From what I can tell, this is the main new bit of info added in this PR . The other items seem to repeat what's already here. For example:

If a transformation is a canonicalization, there are two ways to implement
it: As a Folder or as a RewritePattern, both of which are implementation

That's already covered by the paragraphs that I linked :) (i.e. that's why we have them)

Also, this part doesn't really get us closer to understanding what to prefer - the "fold" method vs RewriterPattern:

If a transformation should be a canonicalizer is a different question from
if a transformation should be a folder or a RewritePattern. A
transformation is promoted to a canonicalization as defined by the General
Design of canonicalizations.

One other point, could there be some other heuristics to consider here? For example, the cost and the frequency with which different "implementations" would be run.

Removed repeated lines and changed to a one liner.

Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

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

The updated format makes more sense to me, thanks!

LGTM, but lets wait for @jpienaar or @River707 to comment on the content itself :) They have much more experience with this than me.

@Groverkss Groverkss merged commit 8e0a63d into llvm:main Mar 6, 2025
11 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…vm#129517)

If a transformation should be a canonicalization is an orthogonal
question to if a transformation should be implemented as a
`RewritePattern` or a `fold` method. The later is an implementation
detail.

This patch adds a suggestion to always implement a canonicalization as a
`fold` pattern if possible, as they are a restricted subset of a
`RewritePattern`.

This has been a common source of confusion, as to when to implement a
canonicalization as a fold method or a RewritePattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants