[Mlir-commits] [mlir] 2f23f9e - [Canonicalize] Fully parameterize the pass based on config options. NFC.

Chris Lattner llvmlistbot at llvm.org
Tue May 25 13:22:51 PDT 2021


Author: Chris Lattner
Date: 2021-05-25T13:09:51-07:00
New Revision: 2f23f9e641e3830948cd2357a8fa52798df8ffcb

URL: https://github.com/llvm/llvm-project/commit/2f23f9e641e3830948cd2357a8fa52798df8ffcb
DIFF: https://github.com/llvm/llvm-project/commit/2f23f9e641e3830948cd2357a8fa52798df8ffcb.diff

LOG: [Canonicalize] Fully parameterize the pass based on config options. NFC.

This allows C++ clients of the Canonicalize pass to specify their own
Config option struct to control how Canonicalize works, increasing reusability.

This also allows controlling these settings for the default Canonicalize pass
using command line options.  This is useful for testing and for playing with
things on the command line.

Differential Revision: https://reviews.llvm.org/D103069

Added: 
    

Modified: 
    mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
    mlir/include/mlir/Transforms/Passes.h
    mlir/include/mlir/Transforms/Passes.td
    mlir/lib/Transforms/Canonicalizer.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
index a85bdef8cea01..2ef8501bdc0c5 100644
--- a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
+++ b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
@@ -18,8 +18,9 @@
 
 namespace mlir {
 
-/// This struct allows control over how the GreedyPatternRewriteDriver works.
-struct GreedyRewriteConfig {
+/// This class allows control over how the GreedyPatternRewriteDriver works.
+class GreedyRewriteConfig {
+public:
   /// This specifies the order of initial traversal that populates the rewriters
   /// worklist.  When set to true, it walks the operations top-down, which is
   /// generally more efficient in compile time.  When set to false, its initial

diff  --git a/mlir/include/mlir/Transforms/Passes.h b/mlir/include/mlir/Transforms/Passes.h
index fd6f25a019182..c4b748488740c 100644
--- a/mlir/include/mlir/Transforms/Passes.h
+++ b/mlir/include/mlir/Transforms/Passes.h
@@ -23,6 +23,7 @@
 namespace mlir {
 
 class AffineForOp;
+class GreedyRewriteConfig;
 
 //===----------------------------------------------------------------------===//
 // Passes
@@ -60,9 +61,14 @@ std::unique_ptr<FunctionPass> createFinalizingBufferizePass();
 /// Creates a pass that converts memref function results to out-params.
 std::unique_ptr<Pass> createBufferResultsToOutParamsPass();
 
-/// Creates an instance of the Canonicalizer pass.
+/// Creates an instance of the Canonicalizer pass, configured with default
+/// settings (which can be overridden by pass options on the command line).
 std::unique_ptr<Pass> createCanonicalizerPass();
 
+/// Creates an instance of the Canonicalizer pass with the specified config.
+std::unique_ptr<Pass>
+createCanonicalizerPass(const GreedyRewriteConfig &config);
+
 /// Creates a pass to perform common sub expression elimination.
 std::unique_ptr<Pass> createCSEPass();
 

diff  --git a/mlir/include/mlir/Transforms/Passes.td b/mlir/include/mlir/Transforms/Passes.td
index bfb6f242682ed..cb044a0f7ed93 100644
--- a/mlir/include/mlir/Transforms/Passes.td
+++ b/mlir/include/mlir/Transforms/Passes.td
@@ -365,6 +365,12 @@ def Canonicalizer : Pass<"canonicalize"> {
   let options = [
     Option<"topDownProcessingEnabled", "top-down", "bool",
            /*default=*/"false",
+           "Seed the worklist in general top-down order">,
+    Option<"enableRegionSimplification", "region-simplify", "bool",
+           /*default=*/"true",
+           "Seed the worklist in general top-down order">,
+    Option<"maxIterations", "max-iterations", "unsigned",
+           /*default=*/"10",
            "Seed the worklist in general top-down order">
   ];
 }

diff  --git a/mlir/lib/Transforms/Canonicalizer.cpp b/mlir/lib/Transforms/Canonicalizer.cpp
index 5d91243507eb7..1f27fc7203005 100644
--- a/mlir/lib/Transforms/Canonicalizer.cpp
+++ b/mlir/lib/Transforms/Canonicalizer.cpp
@@ -21,6 +21,16 @@ using namespace mlir;
 namespace {
 /// Canonicalize operations in nested regions.
 struct Canonicalizer : public CanonicalizerBase<Canonicalizer> {
+  Canonicalizer(const GreedyRewriteConfig &config) : config(config) {}
+
+  Canonicalizer() {
+    // Default constructed Canonicalizer takes its settings from command line
+    // options.
+    config.useTopDownTraversal = topDownProcessingEnabled;
+    config.enableRegionSimplification = enableRegionSimplification;
+    config.maxIterations = maxIterations;
+  }
+
   /// Initialize the canonicalizer by building the set of patterns used during
   /// execution.
   LogicalResult initialize(MLIRContext *context) override {
@@ -31,12 +41,11 @@ struct Canonicalizer : public CanonicalizerBase<Canonicalizer> {
     return success();
   }
   void runOnOperation() override {
-    GreedyRewriteConfig config;
-    config.useTopDownTraversal = topDownProcessingEnabled;
     (void)applyPatternsAndFoldGreedily(getOperation()->getRegions(), patterns,
                                        config);
   }
 
+  GreedyRewriteConfig config;
   FrozenRewritePatternSet patterns;
 };
 } // end anonymous namespace
@@ -45,3 +54,9 @@ struct Canonicalizer : public CanonicalizerBase<Canonicalizer> {
 std::unique_ptr<Pass> mlir::createCanonicalizerPass() {
   return std::make_unique<Canonicalizer>();
 }
+
+/// Creates an instance of the Canonicalizer pass with the specified config.
+std::unique_ptr<Pass>
+createCanonicalizerPass(const GreedyRewriteConfig &config) {
+  return std::make_unique<Canonicalizer>(config);
+}


        


More information about the Mlir-commits mailing list