[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
This commit is contained in:
Frederik Gossen 2021-10-04 16:42:24 +02:00
parent 10b93a5dec
commit 519663beba
3 changed files with 12 additions and 5 deletions

View File

@ -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;
};
//===----------------------------------------------------------------------===//

View File

@ -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;

View File

@ -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);