@@ -91,6 +91,15 @@ class GreedyRewriteConfig {
9191
9292 // / An optional listener that should be notified about IR modifications.
9393 RewriterBase::Listener *listener = nullptr ;
94+
95+ // Whether this should fold while greedily rewriting.
96+ //
97+ // Note: greedy here generally refers to two forms, 1) greedily applying
98+ // patterns based purely on benefit and applying without backtracking using
99+ // default cost model, 2) greedily folding where possible while attempting to
100+ // match and rewrite using the provided patterns. With this option set to
101+ // false it only does the former.
102+ bool fold = true ;
94103};
95104
96105// ===----------------------------------------------------------------------===//
@@ -104,8 +113,8 @@ class GreedyRewriteConfig {
104113// / The greedy rewrite may prematurely stop after a maximum number of
105114// / iterations, which can be configured in the configuration parameter.
106115// /
107- // / Also performs folding and simple dead-code elimination before attempting to
108- // / match any of the provided patterns.
116+ // / Also performs simple dead-code elimination before attempting to match any of
117+ // / the provided patterns.
109118// /
110119// / A region scope can be set in the configuration parameter. By default, the
111120// / scope is set to the specified region. Only in-scope ops are added to the
@@ -117,10 +126,18 @@ class GreedyRewriteConfig {
117126// /
118127// / Note: This method does not apply patterns to the region's parent operation.
119128LogicalResult
129+ applyPatternsGreedily (Region ®ion, const FrozenRewritePatternSet &patterns,
130+ GreedyRewriteConfig config = GreedyRewriteConfig(),
131+ bool *changed = nullptr );
132+ // / Same as `applyPatternsAndGreedily` above with folding.
133+ inline LogicalResult
120134applyPatternsAndFoldGreedily (Region ®ion,
121135 const FrozenRewritePatternSet &patterns,
122136 GreedyRewriteConfig config = GreedyRewriteConfig(),
123- bool *changed = nullptr );
137+ bool *changed = nullptr ) {
138+ config.fold = true ;
139+ return applyPatternsGreedily (region, patterns, config, changed);
140+ }
124141
125142// / Rewrite ops nested under the given operation, which must be isolated from
126143// / above, by repeatedly applying the highest benefit patterns in a greedy
@@ -129,8 +146,8 @@ applyPatternsAndFoldGreedily(Region ®ion,
129146// / The greedy rewrite may prematurely stop after a maximum number of
130147// / iterations, which can be configured in the configuration parameter.
131148// /
132- // / Also performs folding and simple dead-code elimination before attempting to
133- // / match any of the provided patterns.
149+ // / Also performs simple dead-code elimination before attempting to match any of
150+ // / the provided patterns.
134151// /
135152// / This overload runs a separate greedy rewrite for each region of the
136153// / specified op. A region scope can be set in the configuration parameter. By
@@ -147,10 +164,9 @@ applyPatternsAndFoldGreedily(Region ®ion,
147164// /
148165// / Note: This method does not apply patterns to the given operation itself.
149166inline LogicalResult
150- applyPatternsAndFoldGreedily (Operation *op,
151- const FrozenRewritePatternSet &patterns,
152- GreedyRewriteConfig config = GreedyRewriteConfig(),
153- bool *changed = nullptr ) {
167+ applyPatternsGreedily (Operation *op, const FrozenRewritePatternSet &patterns,
168+ GreedyRewriteConfig config = GreedyRewriteConfig(),
169+ bool *changed = nullptr ) {
154170 bool anyRegionChanged = false ;
155171 bool failed = false ;
156172 for (Region ®ion : op->getRegions ()) {
@@ -164,15 +180,24 @@ applyPatternsAndFoldGreedily(Operation *op,
164180 *changed = anyRegionChanged;
165181 return failure (failed);
166182}
183+ // / Same as `applyPatternsGreedily` above with folding.
184+ inline LogicalResult
185+ applyPatternsAndFoldGreedily (Operation *op,
186+ const FrozenRewritePatternSet &patterns,
187+ GreedyRewriteConfig config = GreedyRewriteConfig(),
188+ bool *changed = nullptr ) {
189+ config.fold = true ;
190+ return applyPatternsGreedily (op, patterns, config, changed);
191+ }
167192
168193// / Rewrite the specified ops by repeatedly applying the highest benefit
169194// / patterns in a greedy worklist driven manner until a fixpoint is reached.
170195// /
171196// / The greedy rewrite may prematurely stop after a maximum number of
172197// / iterations, which can be configured in the configuration parameter.
173198// /
174- // / Also performs folding and simple dead-code elimination before attempting to
175- // / match any of the provided patterns.
199+ // / Also performs simple dead-code elimination before attempting to match any of
200+ // / the provided patterns.
176201// /
177202// / Newly created ops and other pre-existing ops that use results of rewritten
178203// / ops or supply operands to such ops are also processed, unless such ops are
@@ -194,10 +219,19 @@ applyPatternsAndFoldGreedily(Operation *op,
194219// / the IR was modified at all. `allOpsErased` is set to "true" if all ops in
195220// / `ops` were erased.
196221LogicalResult
222+ applyOpPatternsGreedily (ArrayRef<Operation *> ops,
223+ const FrozenRewritePatternSet &patterns,
224+ GreedyRewriteConfig config = GreedyRewriteConfig(),
225+ bool *changed = nullptr , bool *allErased = nullptr );
226+ // / Same as `applyOpPatternsGreedily` with folding.
227+ inline LogicalResult
197228applyOpPatternsAndFold (ArrayRef<Operation *> ops,
198229 const FrozenRewritePatternSet &patterns,
199230 GreedyRewriteConfig config = GreedyRewriteConfig(),
200- bool *changed = nullptr , bool *allErased = nullptr );
231+ bool *changed = nullptr , bool *allErased = nullptr ) {
232+ config.fold = true ;
233+ return applyOpPatternsGreedily (ops, patterns, config, changed, allErased);
234+ }
201235
202236} // namespace mlir
203237
0 commit comments