Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions mlir/docs/Canonicalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,24 @@ 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.
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.


* 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'

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, otherwise it should be implemented
as a `RewritePattern`.