Skip to content

Commit c180216

Browse files
committed
fix: prevent canonicalize looping for extremely long time
A hanging bug can manifest with very large inputs to minimize at canonicalize pass. This is because if the maxNumRewrites is not set, it defaults to kNoLimit, which could be an arbitrary large number. This optional command line argument makes it possible to set the max number of rewrites for large input files to minimize. Signed-off-by: Tianrui Wei <[email protected]>
1 parent 40962a3 commit c180216

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/Reduce/GenericReductions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include "mlir/IR/SymbolTable.h"
1212
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1313
#include "mlir/Transforms/Passes.h"
14+
#include "llvm/Support/CommandLine.h"
1415

1516
using namespace mlir;
1617
using namespace circt;
1718

19+
extern llvm::cl::opt<int64_t> maxGreedyRewrites;
20+
1821
//===----------------------------------------------------------------------===//
1922
// Reduction Patterns
2023
//===----------------------------------------------------------------------===//
@@ -101,6 +104,8 @@ static std::unique_ptr<Pass> createSimpleCanonicalizerPass() {
101104
config.setUseTopDownTraversal(true);
102105
config.setRegionSimplificationLevel(
103106
mlir::GreedySimplifyRegionLevel::Disabled);
107+
if (maxGreedyRewrites >= 0)
108+
config.setMaxNumRewrites(maxGreedyRewrites);
104109
return createCanonicalizerPass(config);
105110
}
106111

tools/circt-reduce/circt-reduce.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ static cl::opt<bool> verbose("v", cl::init(true),
104104
cl::desc("Print reduction progress to stderr"),
105105
cl::cat(mainCategory));
106106

107+
cl::opt<int64_t> maxGreedyRewrites(
108+
"max-greedy-rewrites", cl::init(-1),
109+
cl::desc("Maximum number of rewrites GreedyPatternRewriteDriver may "
110+
"apply (negative value keeps the default)"),
111+
cl::cat(mainCategory));
112+
107113
static cl::opt<unsigned>
108114
maxChunks("max-chunks", cl::init(0),
109115
cl::desc("Stop increasing granularity beyond this number of "

0 commit comments

Comments
 (0)