[Mlir-commits] [mlir] [mlir][docs] Add more examples for the "canonical form" (PR #173667)

Jakub Kuderski llvmlistbot at llvm.org
Mon Dec 29 06:47:02 PST 2025


================
@@ -63,30 +63,42 @@ Some important things to think about w.r.t. canonicalization patterns:
 *   Canonicalize shouldn't lose the semantic of original operation: the original
     information should always be recoverable from the transformed IR.
 
-For example, a pattern that transform
-
-```
-  %transpose = linalg.transpose
-      ins(%input : tensor<1x2x3xf32>)
-      outs(%init1 : tensor<2x1x3xf32>)
-      dimensions = [1, 0, 2]
-  %out = linalg.transpose
-      ins(%transpose: tensor<2x1x3xf32>)
-      outs(%init2 : tensor<3x1x2xf32>)
-      permutation = [2, 1, 0]
-```
-
-to
-
-```
-  %out= linalg.transpose
-      ins(%input : tensor<1x2x3xf32>)
-      outs(%init2: tensor<3x1x2xf32>)
-      permutation = [2, 0, 1]
-```
-
-is a good canonicalization pattern because it removes a redundant operation,
-making other analysis optimizations and more efficient.
+## What is the Canonical Form?
+
+There is no formally defined canonical form in MLIR. The de-facto canonical
+form keeps evolving, as canonicalization patterns and folders are getting
+added / removed / modified by the community.
+
+The canonicalizer pass is used in many projects but does not offer fine-grained
+control over individual patterns or foldings, making changes to the canonical
+form potentially contentious. Whether a transformation belongs in the canonical
+form must be decided on a case-by-case basis, but common community-agreed
+canonicalizations include:
+
+* Identity / no-op elimination. E.g., folding `arith.addi %x, %c0` to `%x` or
+  erasing `memref.copy %x, %x`.
+* Scalar constant folding. E.g., folding `arith.addi %c1, %c2` to `%c3`. Note:
+  this is not true for "large" tensors where constant folding can lead to IR
+  size explosion.
----------------
kuhar wrote:

This is also true for large vectors in some places. Instead, maybe we can say that for practical reasons, some canonicalizations are disabled over large constants to prevent size explosion?

https://github.com/llvm/llvm-project/pull/173667


More information about the Mlir-commits mailing list