[Canonicalizer] Process regions top-down instead of bottom up & reuse existing constants.
authorChris Lattner <clattner@nondot.org>
Sat, 20 Mar 2021 04:22:15 +0000 (21:22 -0700)
committerChris Lattner <clattner@nondot.org>
Sat, 20 Mar 2021 23:30:15 +0000 (16:30 -0700)
commit361b7d125b438cda13fa45f13790767a62252be9
treec005989ed8dbdab85b0cc2bb00dff1f5606b8129
parent0776eca7a4e76bfadc311f3607be3a4f0c0e989a
[Canonicalizer] Process regions top-down instead of bottom up & reuse existing constants.

This reapplies b5d9a3c / https://reviews.llvm.org/D98609 with a one line fix in
processExistingConstants to skip() when erasing a constant we've already seen.

Original commit message:

 1) Change the canonicalizer to walk the function in top-down order instead of
    bottom-up order.  This composes well with the "top down" nature of constant
    folding and simplification, reducing iterations and re-evaluation of ops in
    simple cases.
 2) Explicitly enter existing constants into the OperationFolder table before
    canonicalizing.  Previously we would "constant fold" them and rematerialize
    them, wastefully recreating a bunch fo constants, which lead to pointless
    memory traffic.

Both changes together provide a 33% speedup for canonicalize on some mid-size
CIRCT examples.

One artifact of this change is that the constants generated in normal pattern
application get inserted at the top of the function as the patterns are applied.
Because of this, we get "inverted" constants more often, which is an aethetic
change to the IR but does permute some testcases.

Differential Revision: https://reviews.llvm.org/D99006
mlir/include/mlir/Transforms/FoldUtils.h
mlir/lib/Transforms/Utils/FoldUtils.cpp
mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
mlir/test/Conversion/VectorToSCF/vector-to-loops.mlir
mlir/test/Dialect/Affine/canonicalize.mlir
mlir/test/Dialect/Linalg/transform-patterns.mlir
mlir/test/Dialect/Vector/canonicalize.mlir
mlir/test/Transforms/canonicalize.mlir
mlir/test/mlir-tblgen/pattern.mlir