@@ -91,6 +91,15 @@ class GreedyRewriteConfig {
91
91
92
92
// / An optional listener that should be notified about IR modifications.
93
93
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 ;
94
103
};
95
104
96
105
// ===----------------------------------------------------------------------===//
@@ -104,8 +113,8 @@ class GreedyRewriteConfig {
104
113
// / The greedy rewrite may prematurely stop after a maximum number of
105
114
// / iterations, which can be configured in the configuration parameter.
106
115
// /
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.
109
118
// /
110
119
// / A region scope can be set in the configuration parameter. By default, the
111
120
// / scope is set to the specified region. Only in-scope ops are added to the
@@ -117,10 +126,18 @@ class GreedyRewriteConfig {
117
126
// /
118
127
// / Note: This method does not apply patterns to the region's parent operation.
119
128
LogicalResult
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
120
134
applyPatternsAndFoldGreedily (Region ®ion,
121
135
const FrozenRewritePatternSet &patterns,
122
136
GreedyRewriteConfig config = GreedyRewriteConfig(),
123
- bool *changed = nullptr );
137
+ bool *changed = nullptr ) {
138
+ config.fold = true ;
139
+ return applyPatternsGreedily (region, patterns, config, changed);
140
+ }
124
141
125
142
// / Rewrite ops nested under the given operation, which must be isolated from
126
143
// / above, by repeatedly applying the highest benefit patterns in a greedy
@@ -129,8 +146,8 @@ applyPatternsAndFoldGreedily(Region ®ion,
129
146
// / The greedy rewrite may prematurely stop after a maximum number of
130
147
// / iterations, which can be configured in the configuration parameter.
131
148
// /
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.
134
151
// /
135
152
// / This overload runs a separate greedy rewrite for each region of the
136
153
// / specified op. A region scope can be set in the configuration parameter. By
@@ -147,10 +164,9 @@ applyPatternsAndFoldGreedily(Region ®ion,
147
164
// /
148
165
// / Note: This method does not apply patterns to the given operation itself.
149
166
inline 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 ) {
154
170
bool anyRegionChanged = false ;
155
171
bool failed = false ;
156
172
for (Region ®ion : op->getRegions ()) {
@@ -164,15 +180,24 @@ applyPatternsAndFoldGreedily(Operation *op,
164
180
*changed = anyRegionChanged;
165
181
return failure (failed);
166
182
}
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
+ }
167
192
168
193
// / Rewrite the specified ops by repeatedly applying the highest benefit
169
194
// / patterns in a greedy worklist driven manner until a fixpoint is reached.
170
195
// /
171
196
// / The greedy rewrite may prematurely stop after a maximum number of
172
197
// / iterations, which can be configured in the configuration parameter.
173
198
// /
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.
176
201
// /
177
202
// / Newly created ops and other pre-existing ops that use results of rewritten
178
203
// / ops or supply operands to such ops are also processed, unless such ops are
@@ -194,10 +219,19 @@ applyPatternsAndFoldGreedily(Operation *op,
194
219
// / the IR was modified at all. `allOpsErased` is set to "true" if all ops in
195
220
// / `ops` were erased.
196
221
LogicalResult
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
197
228
applyOpPatternsAndFold (ArrayRef<Operation *> ops,
198
229
const FrozenRewritePatternSet &patterns,
199
230
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
+ }
201
235
202
236
} // namespace mlir
203
237
0 commit comments