From 519663bebaf1c1bcb334ef7bae412a7314dd39e7 Mon Sep 17 00:00:00 2001 From: Frederik Gossen Date: Mon, 4 Oct 2021 16:42:24 +0200 Subject: [PATCH] [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 --- mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h | 7 +++++-- mlir/include/mlir/Transforms/Passes.td | 2 +- mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h index 6be3b3fd384b..bb0af5bded9b 100644 --- a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h +++ b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h @@ -33,8 +33,11 @@ public: 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 360b98d87c15..71fb43df83aa 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 350b19bae37e..ba792a79cc16 100644 --- a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp +++ b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp @@ -222,7 +222,9 @@ bool GreedyPatternRewriteDriver::simplify(MutableArrayRef 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);