[Mlir-commits] [mlir] 519663b - [MLIR] Add an option to disable `maxIterations` in greedy pattern rewrites

Frederik Gossen llvmlistbot at llvm.org
Tue Oct 5 02:49:34 PDT 2021


Author: Frederik Gossen
Date: 2021-10-05T11:49:01+02:00
New Revision: 519663bebaf1c1bcb334ef7bae412a7314dd39e7

URL: https://github.com/llvm/llvm-project/commit/519663bebaf1c1bcb334ef7bae412a7314dd39e7
DIFF: https://github.com/llvm/llvm-project/commit/519663bebaf1c1bcb334ef7bae412a7314dd39e7.diff

LOG: [MLIR] Add an option to disable `maxIterations` in greedy pattern rewrites

This option is needed for passes that are known to reach a fix point, but may
need many iterations depending on the size of the input IR.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
index 6be3b3fd384b8..bb0af5bded9b1 100644
--- a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
+++ b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
@@ -33,8 +33,11 @@ class GreedyRewriteConfig {
   bool enableRegionSimplification = true;
 
   /// This specifies the maximum number of times the rewriter will iterate
-  /// between applying patterns and simplifying regions.
-  unsigned maxIterations = 10;
+  /// between applying patterns and simplifying regions. Use `kNoIterationLimit`
+  /// to disable this iteration limit.
+  int64_t maxIterations = 10;
+
+  static constexpr int64_t kNoIterationLimit = -1;
 };
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/include/mlir/Transforms/Passes.td b/mlir/include/mlir/Transforms/Passes.td
index 360b98d87c156..71fb43df83aaf 100644
--- a/mlir/include/mlir/Transforms/Passes.td
+++ b/mlir/include/mlir/Transforms/Passes.td
@@ -378,7 +378,7 @@ def Canonicalizer : Pass<"canonicalize"> {
     Option<"enableRegionSimplification", "region-simplify", "bool",
            /*default=*/"true",
            "Seed the worklist in general top-down order">,
-    Option<"maxIterations", "max-iterations", "unsigned",
+    Option<"maxIterations", "max-iterations", "int64_t",
            /*default=*/"10",
            "Seed the worklist in general top-down order">
   ] # RewritePassUtils.options;

diff  --git a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
index 350b19bae37e9..ba792a79cc16c 100644
--- a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
+++ b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
@@ -222,7 +222,9 @@ bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions) {
     // is kept up to date.
     if (config.enableRegionSimplification)
       changed |= succeeded(simplifyRegions(*this, regions));
-  } while (changed && ++iteration < config.maxIterations);
+  } while (changed &&
+           (++iteration < config.maxIterations ||
+            config.maxIterations == GreedyRewriteConfig::kNoIterationLimit));
 
   // Whether the rewrite converges, i.e. wasn't changed in the last iteration.
   return !changed;
@@ -345,7 +347,9 @@ LogicalResult OpPatternRewriteDriver::simplifyLocally(Operation *op,
     changed |= succeeded(matcher.matchAndRewrite(op, *this));
     if ((erased = opErasedViaPatternRewrites))
       return success();
-  } while (changed && ++iterations < maxIterations);
+  } while (changed &&
+           (++iterations < maxIterations ||
+            maxIterations == GreedyRewriteConfig::kNoIterationLimit));
 
   // Whether the rewrite converges, i.e. wasn't changed in the last iteration.
   return failure(changed);


        


More information about the Mlir-commits mailing list