Skip to content

Commit 324fea9

Browse files
authored
[mlir][docs] Update documentation for canonicalize. (#99753)
Update canonicalize docs.
1 parent 197b142 commit 324fea9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

mlir/docs/Canonicalization.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ together.
3333

3434
Some important things to think about w.r.t. canonicalization patterns:
3535

36+
* The goal of canonicalization is to make subsequent analyses and
37+
optimizations more effective. Therefore, performance improvements are not
38+
necessary for canonicalization.
39+
3640
* Pass pipelines should not rely on the canonicalizer pass for correctness.
3741
They should work correctly with all instances of the canonicalization pass
3842
removed.
@@ -51,6 +55,39 @@ Some important things to think about w.r.t. canonicalization patterns:
5155
* It is always good to eliminate operations entirely when possible, e.g. by
5256
folding known identities (like "x + 0 = x").
5357

58+
* Pattens with expensive running time (i.e. have O(n) complexity) or
59+
complicated cost models don't belong to canonicalization: since the
60+
algorithm is executed iteratively until fixed-point we want patterns that
61+
execute quickly (in particular their matching phase).
62+
63+
* Canonicalize shouldn't lose the semantic of original operation: the original
64+
information should always be recoverable from the transformed IR.
65+
66+
For example, a pattern that transform
67+
68+
```
69+
%transpose = linalg.transpose
70+
ins(%input : tensor<1x2x3xf32>)
71+
outs(%init1 : tensor<2x1x3xf32>)
72+
dimensions = [1, 0, 2]
73+
%out = linalg.transpose
74+
ins(%tranpose: tensor<2x1x3xf32>)
75+
outs(%init2 : tensor<3x1x2xf32>)
76+
permutation = [2, 1, 0]
77+
```
78+
79+
to
80+
81+
```
82+
%out= linalg.transpose
83+
ins(%input : tensor<1x2x3xf32>)
84+
outs(%init2: tensor<3x1x2xf32>)
85+
permutation = [2, 0, 1]
86+
```
87+
88+
is a good canonicalization pattern because it removes a redundant operation,
89+
making other analysis optimizations and more efficient.
90+
5491
## Globally Applied Rules
5592

5693
These transformations are applied to all levels of IR:
@@ -189,7 +226,7 @@ each of the operands, returning the corresponding constant attribute. These
189226
operands are those that implement the `ConstantLike` trait. If any of the
190227
operands are non-constant, a null `Attribute` value is provided instead. For
191228
example, if MyOp provides three operands [`a`, `b`, `c`], but only `b` is
192-
constant then `adaptor` will return Attribute() for `getA()` and `getC()`,
229+
constant then `adaptor` will return Attribute() for `getA()` and `getC()`,
193230
and b-value for `getB()`.
194231

195232
Also above, is the use of `OpFoldResult`. This class represents the possible

0 commit comments

Comments
 (0)