@@ -33,6 +33,10 @@ together.
33
33
34
34
Some important things to think about w.r.t. canonicalization patterns:
35
35
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
+
36
40
* Pass pipelines should not rely on the canonicalizer pass for correctness.
37
41
They should work correctly with all instances of the canonicalization pass
38
42
removed.
@@ -51,6 +55,39 @@ Some important things to think about w.r.t. canonicalization patterns:
51
55
* It is always good to eliminate operations entirely when possible, e.g. by
52
56
folding known identities (like "x + 0 = x").
53
57
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
+
54
91
## Globally Applied Rules
55
92
56
93
These transformations are applied to all levels of IR:
@@ -189,7 +226,7 @@ each of the operands, returning the corresponding constant attribute. These
189
226
operands are those that implement the ` ConstantLike ` trait. If any of the
190
227
operands are non-constant, a null ` Attribute ` value is provided instead. For
191
228
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() ` ,
193
230
and b-value for ` getB() ` .
194
231
195
232
Also above, is the use of ` OpFoldResult ` . This class represents the possible
0 commit comments