MLIR 23.0.0git
Rewrite.cpp
Go to the documentation of this file.
1//===- Rewrite.cpp - C API for Rewrite Patterns ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir-c/Rewrite.h"
10
11#include "mlir-c/Transforms.h"
12#include "mlir/CAPI/IR.h"
13#include "mlir/CAPI/Rewrite.h"
14#include "mlir/CAPI/Support.h"
15#include "mlir/CAPI/Wrap.h"
16#include "mlir/IR/Attributes.h"
22
23using namespace mlir;
24
25//===----------------------------------------------------------------------===//
26/// RewriterBase API inherited from OpBuilder
27//===----------------------------------------------------------------------===//
28
29MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter) {
30 return wrap(unwrap(rewriter)->getContext());
31}
32
33//===----------------------------------------------------------------------===//
34/// Insertion points methods
35//===----------------------------------------------------------------------===//
36
37void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter) {
38 unwrap(rewriter)->clearInsertionPoint();
39}
40
41void mlirRewriterBaseSetInsertionPointBefore(MlirRewriterBase rewriter,
42 MlirOperation op) {
43 unwrap(rewriter)->setInsertionPoint(unwrap(op));
44}
45
46void mlirRewriterBaseSetInsertionPointAfter(MlirRewriterBase rewriter,
47 MlirOperation op) {
48 unwrap(rewriter)->setInsertionPointAfter(unwrap(op));
49}
50
51void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter,
52 MlirValue value) {
53 unwrap(rewriter)->setInsertionPointAfterValue(unwrap(value));
54}
55
56void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter,
57 MlirBlock block) {
58 unwrap(rewriter)->setInsertionPointToStart(unwrap(block));
59}
60
61void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter,
62 MlirBlock block) {
63 unwrap(rewriter)->setInsertionPointToEnd(unwrap(block));
64}
65
66MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter) {
67 return wrap(unwrap(rewriter)->getInsertionBlock());
68}
69
70MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter) {
71 return wrap(unwrap(rewriter)->getBlock());
72}
73
74MlirOperation
76 mlir::RewriterBase *base = unwrap(rewriter);
77 mlir::Block *block = base->getInsertionBlock();
79 if (it == block->end())
80 return {nullptr};
81
82 return wrap(std::addressof(*it));
83}
84
85//===----------------------------------------------------------------------===//
86/// Block and operation creation/insertion/cloning
87//===----------------------------------------------------------------------===//
88
89MlirBlock mlirRewriterBaseCreateBlockBefore(MlirRewriterBase rewriter,
90 MlirBlock insertBefore,
91 intptr_t nArgTypes,
92 MlirType const *argTypes,
93 MlirLocation const *locations) {
95 ArrayRef<Type> unwrappedArgs = unwrapList(nArgTypes, argTypes, args);
97 ArrayRef<Location> unwrappedLocs = unwrapList(nArgTypes, locations, locs);
98 return wrap(unwrap(rewriter)->createBlock(unwrap(insertBefore), unwrappedArgs,
99 unwrappedLocs));
100}
101
102MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter,
103 MlirOperation op) {
104 return wrap(unwrap(rewriter)->insert(unwrap(op)));
105}
106
107// Other methods of OpBuilder
108
109MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter,
110 MlirOperation op) {
111 return wrap(unwrap(rewriter)->clone(*unwrap(op)));
112}
113
114MlirOperation mlirRewriterBaseCloneWithoutRegions(MlirRewriterBase rewriter,
115 MlirOperation op) {
116 return wrap(unwrap(rewriter)->cloneWithoutRegions(*unwrap(op)));
117}
118
119void mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter,
120 MlirRegion region, MlirBlock before) {
121
122 unwrap(rewriter)->cloneRegionBefore(*unwrap(region), unwrap(before));
123}
124
125//===----------------------------------------------------------------------===//
126/// RewriterBase API
127//===----------------------------------------------------------------------===//
128
129void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter,
130 MlirRegion region, MlirBlock before) {
131 unwrap(rewriter)->inlineRegionBefore(*unwrap(region), unwrap(before));
132}
133
134void mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter,
135 MlirOperation op, intptr_t nValues,
136 MlirValue const *values) {
138 ArrayRef<Value> unwrappedVals = unwrapList(nValues, values, vals);
139 unwrap(rewriter)->replaceOp(unwrap(op), unwrappedVals);
140}
141
142void mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter,
143 MlirOperation op,
144 MlirOperation newOp) {
145 unwrap(rewriter)->replaceOp(unwrap(op), unwrap(newOp));
146}
147
148void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op) {
149 unwrap(rewriter)->eraseOp(unwrap(op));
150}
151
152void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block) {
153 unwrap(rewriter)->eraseBlock(unwrap(block));
154}
155
156void mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter,
157 MlirBlock source, MlirOperation op,
158 intptr_t nArgValues,
159 MlirValue const *argValues) {
161 ArrayRef<Value> unwrappedVals = unwrapList(nArgValues, argValues, vals);
162
163 unwrap(rewriter)->inlineBlockBefore(unwrap(source), unwrap(op),
164 unwrappedVals);
165}
166
167void mlirRewriterBaseMergeBlocks(MlirRewriterBase rewriter, MlirBlock source,
168 MlirBlock dest, intptr_t nArgValues,
169 MlirValue const *argValues) {
171 ArrayRef<Value> unwrappedArgs = unwrapList(nArgValues, argValues, args);
172 unwrap(rewriter)->mergeBlocks(unwrap(source), unwrap(dest), unwrappedArgs);
173}
174
175void mlirRewriterBaseMoveOpBefore(MlirRewriterBase rewriter, MlirOperation op,
176 MlirOperation existingOp) {
177 unwrap(rewriter)->moveOpBefore(unwrap(op), unwrap(existingOp));
178}
179
180void mlirRewriterBaseMoveOpAfter(MlirRewriterBase rewriter, MlirOperation op,
181 MlirOperation existingOp) {
182 unwrap(rewriter)->moveOpAfter(unwrap(op), unwrap(existingOp));
183}
184
185void mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block,
186 MlirBlock existingBlock) {
187 unwrap(rewriter)->moveBlockBefore(unwrap(block), unwrap(existingBlock));
188}
189
190void mlirRewriterBaseStartOpModification(MlirRewriterBase rewriter,
191 MlirOperation op) {
192 unwrap(rewriter)->startOpModification(unwrap(op));
193}
194
195void mlirRewriterBaseFinalizeOpModification(MlirRewriterBase rewriter,
196 MlirOperation op) {
197 unwrap(rewriter)->finalizeOpModification(unwrap(op));
198}
199
200void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter,
201 MlirOperation op) {
202 unwrap(rewriter)->cancelOpModification(unwrap(op));
203}
204
205void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter,
206 MlirValue from, MlirValue to) {
207 unwrap(rewriter)->replaceAllUsesWith(unwrap(from), unwrap(to));
208}
209
210void mlirRewriterBaseReplaceAllValueRangeUsesWith(MlirRewriterBase rewriter,
211 intptr_t nValues,
212 MlirValue const *from,
213 MlirValue const *to) {
214 SmallVector<Value, 4> fromVals;
215 ArrayRef<Value> unwrappedFromVals = unwrapList(nValues, from, fromVals);
217 ArrayRef<Value> unwrappedToVals = unwrapList(nValues, to, toVals);
218 unwrap(rewriter)->replaceAllUsesWith(unwrappedFromVals, unwrappedToVals);
219}
220
222 MlirOperation from,
223 intptr_t nTo,
224 MlirValue const *to) {
226 ArrayRef<Value> unwrappedToVals = unwrapList(nTo, to, toVals);
227 unwrap(rewriter)->replaceAllOpUsesWith(unwrap(from), unwrappedToVals);
228}
229
231 MlirOperation from,
232 MlirOperation to) {
233 unwrap(rewriter)->replaceAllOpUsesWith(unwrap(from), unwrap(to));
234}
235
236void mlirRewriterBaseReplaceOpUsesWithinBlock(MlirRewriterBase rewriter,
237 MlirOperation op,
238 intptr_t nNewValues,
239 MlirValue const *newValues,
240 MlirBlock block) {
242 ArrayRef<Value> unwrappedVals = unwrapList(nNewValues, newValues, vals);
243 unwrap(rewriter)->replaceOpUsesWithinBlock(unwrap(op), unwrappedVals,
244 unwrap(block));
245}
246
247void mlirRewriterBaseReplaceAllUsesExcept(MlirRewriterBase rewriter,
248 MlirValue from, MlirValue to,
249 MlirOperation exceptedUser) {
250 unwrap(rewriter)->replaceAllUsesExcept(unwrap(from), unwrap(to),
251 unwrap(exceptedUser));
252}
253
254//===----------------------------------------------------------------------===//
255/// IRRewriter API
256//===----------------------------------------------------------------------===//
257
258MlirRewriterBase mlirIRRewriterCreate(MlirContext context) {
259 return wrap(new IRRewriter(unwrap(context)));
260}
261
262MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op) {
263 return wrap(new IRRewriter(unwrap(op)));
264}
265
266void mlirIRRewriterDestroy(MlirRewriterBase rewriter) {
267 delete static_cast<IRRewriter *>(unwrap(rewriter));
268}
269
270//===----------------------------------------------------------------------===//
271/// RewritePatternSet and FrozenRewritePatternSet API
272//===----------------------------------------------------------------------===//
273
274MlirFrozenRewritePatternSet
275mlirFreezeRewritePattern(MlirRewritePatternSet set) {
276 auto *m = new mlir::FrozenRewritePatternSet(std::move(*unwrap(set)));
277 set.ptr = nullptr;
278 return wrap(m);
279}
280
281void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set) {
282 delete unwrap(set);
283 set.ptr = nullptr;
284}
285
286//===----------------------------------------------------------------------===//
287/// GreedyRewriteDriverConfig API
288//===----------------------------------------------------------------------===//
289
290inline mlir::GreedyRewriteConfig *unwrap(MlirGreedyRewriteDriverConfig config) {
291 assert(config.ptr && "unexpected null config");
292 return static_cast<mlir::GreedyRewriteConfig *>(config.ptr);
293}
294
295inline MlirGreedyRewriteDriverConfig wrap(mlir::GreedyRewriteConfig *config) {
296 return {config};
297}
298
299MlirGreedyRewriteDriverConfig mlirGreedyRewriteDriverConfigCreate() {
300 return wrap(new mlir::GreedyRewriteConfig());
301}
302
304 MlirGreedyRewriteDriverConfig config) {
305 delete unwrap(config);
306}
307
309 MlirGreedyRewriteDriverConfig config, int64_t maxIterations) {
310 unwrap(config)->setMaxIterations(maxIterations);
311}
312
314 MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites) {
315 unwrap(config)->setMaxNumRewrites(maxNumRewrites);
316}
317
319 MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal) {
320 unwrap(config)->setUseTopDownTraversal(useTopDownTraversal);
321}
322
324 MlirGreedyRewriteDriverConfig config, bool enable) {
325 unwrap(config)->enableFolding(enable);
326}
327
329 MlirGreedyRewriteDriverConfig config,
330 MlirGreedyRewriteStrictness strictness) {
331 mlir::GreedyRewriteStrictness cppStrictness;
332 switch (strictness) {
335 break;
338 break;
341 break;
342 }
343 unwrap(config)->setStrictness(cppStrictness);
344}
345
362
364 MlirGreedyRewriteDriverConfig config, bool enable) {
366}
367
369 MlirGreedyRewriteDriverConfig config) {
370 return unwrap(config)->getMaxIterations();
371}
372
374 MlirGreedyRewriteDriverConfig config) {
376}
377
379 MlirGreedyRewriteDriverConfig config) {
381}
382
384 MlirGreedyRewriteDriverConfig config) {
385 return unwrap(config)->isFoldingEnabled();
386}
387
400
415
417 MlirGreedyRewriteDriverConfig config) {
419}
420
423 MlirFrozenRewritePatternSet patterns,
424 MlirGreedyRewriteDriverConfig config) {
426 *unwrap(config)));
427}
428
431 MlirFrozenRewritePatternSet patterns,
432 MlirGreedyRewriteDriverConfig config) {
434 *unwrap(config)));
435}
436
437void mlirWalkAndApplyPatterns(MlirOperation op,
438 MlirFrozenRewritePatternSet patterns) {
440}
441
442//===----------------------------------------------------------------------===//
443/// PatternRewriter API
444//===----------------------------------------------------------------------===//
445
446MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter) {
447 return wrap(static_cast<mlir::RewriterBase *>(unwrap(rewriter)));
448}
449
450//===----------------------------------------------------------------------===//
451/// RewritePattern API
452//===----------------------------------------------------------------------===//
453
454namespace mlir {
455
457public:
459 StringRef rootName, PatternBenefit benefit,
460 MLIRContext *context,
461 ArrayRef<StringRef> generatedNames)
462 : RewritePattern(rootName, benefit, context, generatedNames),
463 callbacks(callbacks), userData(userData) {
464 if (callbacks.construct)
465 callbacks.construct(userData);
466 }
467
469 if (callbacks.destruct)
470 callbacks.destruct(userData);
471 }
472
473 LogicalResult matchAndRewrite(Operation *op,
474 PatternRewriter &rewriter) const override {
475 return unwrap(callbacks.matchAndRewrite(
476 wrap(static_cast<const mlir::RewritePattern *>(this)), wrap(op),
477 wrap(&rewriter), userData));
478 }
479
480private:
482 void *userData;
483};
484
485} // namespace mlir
486
487MlirRewritePattern mlirOpRewritePatternCreate(
488 MlirStringRef rootName, unsigned benefit, MlirContext context,
489 MlirRewritePatternCallbacks callbacks, void *userData,
490 size_t nGeneratedNames, MlirStringRef *generatedNames) {
491 std::vector<mlir::StringRef> generatedNamesVec;
492 generatedNamesVec.reserve(nGeneratedNames);
493 for (size_t i = 0; i < nGeneratedNames; ++i) {
494 generatedNamesVec.push_back(unwrap(generatedNames[i]));
495 }
497 callbacks, userData, unwrap(rootName), PatternBenefit(benefit),
498 unwrap(context), generatedNamesVec));
499}
500
501//===----------------------------------------------------------------------===//
502/// RewritePatternSet API
503//===----------------------------------------------------------------------===//
504
505MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context) {
506 return wrap(new mlir::RewritePatternSet(unwrap(context)));
507}
508
509void mlirRewritePatternSetDestroy(MlirRewritePatternSet set) {
510 delete unwrap(set);
511}
512
513void mlirRewritePatternSetAdd(MlirRewritePatternSet set,
514 MlirRewritePattern pattern) {
515 std::unique_ptr<mlir::RewritePattern> patternPtr(
516 const_cast<mlir::RewritePattern *>(unwrap(pattern)));
517 pattern.ptr = nullptr;
518 unwrap(set)->add(std::move(patternPtr));
519}
520
521//===----------------------------------------------------------------------===//
522/// PDLPatternModule API
523//===----------------------------------------------------------------------===//
524
525#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
526MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) {
527 return wrap(new mlir::PDLPatternModule(
529}
530
531void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op) {
532 delete unwrap(op);
533 op.ptr = nullptr;
534}
535
536MlirRewritePatternSet
537mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) {
538 auto *m = new mlir::RewritePatternSet(std::move(*unwrap(op)));
539 op.ptr = nullptr;
540 return wrap(m);
541}
542
543MlirValue mlirPDLValueAsValue(MlirPDLValue value) {
544 return wrap(unwrap(value)->dyn_cast<mlir::Value>());
545}
546
547MlirType mlirPDLValueAsType(MlirPDLValue value) {
548 return wrap(unwrap(value)->dyn_cast<mlir::Type>());
549}
550
551MlirOperation mlirPDLValueAsOperation(MlirPDLValue value) {
552 return wrap(unwrap(value)->dyn_cast<mlir::Operation *>());
553}
554
555MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value) {
556 return wrap(unwrap(value)->dyn_cast<mlir::Attribute>());
557}
558
559void mlirPDLResultListPushBackValue(MlirPDLResultList results,
560 MlirValue value) {
561 unwrap(results)->push_back(unwrap(value));
562}
563
564void mlirPDLResultListPushBackType(MlirPDLResultList results, MlirType value) {
565 unwrap(results)->push_back(unwrap(value));
566}
567
568void mlirPDLResultListPushBackOperation(MlirPDLResultList results,
569 MlirOperation value) {
570 unwrap(results)->push_back(unwrap(value));
571}
572
573void mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
574 MlirAttribute value) {
575 unwrap(results)->push_back(unwrap(value));
576}
577
578inline std::vector<MlirPDLValue> wrap(ArrayRef<PDLValue> values) {
579 std::vector<MlirPDLValue> mlirValues;
580 mlirValues.reserve(values.size());
581 for (auto &value : values) {
582 mlirValues.push_back(wrap(&value));
583 }
584 return mlirValues;
585}
586
587void mlirPDLPatternModuleRegisterRewriteFunction(
588 MlirPDLPatternModule pdlModule, MlirStringRef name,
589 MlirPDLRewriteFunction rewriteFn, void *userData) {
590 unwrap(pdlModule)->registerRewriteFunction(
591 unwrap(name),
592 [userData, rewriteFn](PatternRewriter &rewriter, PDLResultList &results,
593 ArrayRef<PDLValue> values) -> LogicalResult {
594 std::vector<MlirPDLValue> mlirValues = wrap(values);
595 return unwrap(rewriteFn(wrap(&rewriter), wrap(&results),
596 mlirValues.size(), mlirValues.data(),
597 userData));
598 });
599}
600
601void mlirPDLPatternModuleRegisterConstraintFunction(
602 MlirPDLPatternModule pdlModule, MlirStringRef name,
603 MlirPDLConstraintFunction constraintFn, void *userData) {
604 unwrap(pdlModule)->registerConstraintFunction(
605 unwrap(name),
606 [userData, constraintFn](PatternRewriter &rewriter,
607 PDLResultList &results,
608 ArrayRef<PDLValue> values) -> LogicalResult {
609 std::vector<MlirPDLValue> mlirValues = wrap(values);
610 return unwrap(constraintFn(wrap(&rewriter), wrap(&results),
611 mlirValues.size(), mlirValues.data(),
612 userData));
613 });
614}
615#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
void mlirGreedyRewriteDriverConfigSetMaxNumRewrites(MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites)
Sets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:313
void mlirRewriterBaseReplaceOpUsesWithinBlock(MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues, MlirValue const *newValues, MlirBlock block)
Find uses of from within block and replace them with to.
Definition Rewrite.cpp:236
MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context)
RewritePatternSet API.
Definition Rewrite.cpp:505
void mlirRewriterBaseMergeBlocks(MlirRewriterBase rewriter, MlirBlock source, MlirBlock dest, intptr_t nArgValues, MlirValue const *argValues)
Inline the operations of block 'source' into the end of block 'dest'.
Definition Rewrite.cpp:167
void mlirIRRewriterDestroy(MlirRewriterBase rewriter)
Takes an IRRewriter owned by the caller and destroys it.
Definition Rewrite.cpp:266
void mlirRewriterBaseStartOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
Definition Rewrite.cpp:190
MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition Rewrite.cpp:102
MlirRewriterBase mlirIRRewriterCreate(MlirContext context)
IRRewriter API.
Definition Rewrite.cpp:258
void mlirRewriterBaseMoveOpAfter(MlirRewriterBase rewriter, MlirOperation op, MlirOperation existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
Definition Rewrite.cpp:180
void mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
Clone the blocks that belong to "region" before the given position in another region "parent".
Definition Rewrite.cpp:119
void mlirRewriterBaseSetInsertionPointAfter(MlirRewriterBase rewriter, MlirOperation op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition Rewrite.cpp:46
int64_t mlirGreedyRewriteDriverConfigGetMaxNumRewrites(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:373
void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set)
Destroy the given MlirFrozenRewritePatternSet.
Definition Rewrite.cpp:281
void mlirRewriterBaseReplaceAllOpUsesWithOperation(MlirRewriterBase rewriter, MlirOperation from, MlirOperation to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:230
void mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block, MlirBlock existingBlock)
Unlink this block and insert it right before existingBlock.
Definition Rewrite.cpp:185
void mlirRewriterBaseReplaceAllValueRangeUsesWith(MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from, MlirValue const *to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:210
void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block)
Erases a block along with all operations inside it.
Definition Rewrite.cpp:152
void mlirRewriterBaseReplaceAllUsesExcept(MlirRewriterBase rewriter, MlirValue from, MlirValue to, MlirOperation exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
Definition Rewrite.cpp:247
MlirLogicalResult mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig config)
Definition Rewrite.cpp:430
void mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config)
Destroys a greedy rewrite driver configuration.
Definition Rewrite.cpp:303
mlir::GreedyRewriteConfig * unwrap(MlirGreedyRewriteDriverConfig config)
GreedyRewriteDriverConfig API.
Definition Rewrite.cpp:290
MlirBlock mlirRewriterBaseCreateBlockBefore(MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes, MlirType const *argTypes, MlirLocation const *locations)
Block and operation creation/insertion/cloning.
Definition Rewrite.cpp:89
MlirGreedyRewriteDriverConfig mlirGreedyRewriteDriverConfigCreate()
GreedyRewriteDriverConfig API.
Definition Rewrite.cpp:299
bool mlirGreedyRewriteDriverConfigGetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config)
Gets whether top-down traversal is used for initial worklist population.
Definition Rewrite.cpp:378
void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the start of the specified block.
Definition Rewrite.cpp:56
MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter)
PatternRewriter API.
Definition Rewrite.cpp:446
MlirRewritePattern mlirOpRewritePatternCreate(MlirStringRef rootName, unsigned benefit, MlirContext context, MlirRewritePatternCallbacks callbacks, void *userData, size_t nGeneratedNames, MlirStringRef *generatedNames)
Create a rewrite pattern that matches the operation with the given rootName, corresponding to mlir::O...
Definition Rewrite.cpp:487
void mlirGreedyRewriteDriverConfigSetStrictness(MlirGreedyRewriteDriverConfig config, MlirGreedyRewriteStrictness strictness)
Sets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:328
MlirGreedyRewriteStrictness mlirGreedyRewriteDriverConfigGetStrictness(MlirGreedyRewriteDriverConfig config)
Gets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:388
MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition Rewrite.cpp:29
void mlirRewriterBaseReplaceAllOpUsesWithValueRange(MlirRewriterBase rewriter, MlirOperation from, intptr_t nTo, MlirValue const *to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:221
MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of the specified operation.
Definition Rewrite.cpp:109
void mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source, MlirOperation op, intptr_t nArgValues, MlirValue const *argValues)
Inline the operations of block 'source' before the operation 'op'.
Definition Rewrite.cpp:156
void mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter, MlirOperation op, intptr_t nValues, MlirValue const *values)
Replace the results of the given (original) operation with the specified list of values (replacements...
Definition Rewrite.cpp:134
void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method cancels a pending in-place modification.
Definition Rewrite.cpp:200
void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter, MlirValue value)
Sets the insertion point to the node after the specified value.
Definition Rewrite.cpp:51
void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the end of the specified block.
Definition Rewrite.cpp:61
MlirOperation mlirRewriterBaseCloneWithoutRegions(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of this operation but keep the operation regions empty.
Definition Rewrite.cpp:114
void mlirRewritePatternSetDestroy(MlirRewritePatternSet set)
Destruct the given MlirRewritePatternSet.
Definition Rewrite.cpp:509
MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter)
Returns the current block of the rewriter.
Definition Rewrite.cpp:70
MlirOperation mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter)
Returns the operation right after the current insertion point of the rewriter.
Definition Rewrite.cpp:75
void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter)
Insertion points methods.
Definition Rewrite.cpp:37
MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig config)
Definition Rewrite.cpp:422
MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet set)
RewritePatternSet and FrozenRewritePatternSet API.
Definition Rewrite.cpp:275
bool mlirGreedyRewriteDriverConfigIsFoldingEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether folding is enabled during greedy rewriting.
Definition Rewrite.cpp:383
bool mlirGreedyRewriteDriverConfigIsConstantCSEEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether constant CSE is enabled.
Definition Rewrite.cpp:416
void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
RewriterBase API.
Definition Rewrite.cpp:129
int64_t mlirGreedyRewriteDriverConfigGetMaxIterations(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:368
void mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter, MlirOperation op, MlirOperation newOp)
Replace the results of the given (original) operation with the specified new op (replacement).
Definition Rewrite.cpp:142
void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from, MlirValue to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:205
void mlirRewriterBaseFinalizeOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method is used to signal the end of an in-place modification of the given operation.
Definition Rewrite.cpp:195
void mlirRewritePatternSetAdd(MlirRewritePatternSet set, MlirRewritePattern pattern)
Add the given MlirRewritePattern into a MlirRewritePatternSet.
Definition Rewrite.cpp:513
MlirGreedyRewriteDriverConfig wrap(mlir::GreedyRewriteConfig *config)
Definition Rewrite.cpp:295
void mlirGreedyRewriteDriverConfigSetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal)
Sets whether to use top-down traversal for the initial population of the worklist.
Definition Rewrite.cpp:318
void mlirWalkAndApplyPatterns(MlirOperation op, MlirFrozenRewritePatternSet patterns)
Applies the given patterns to the given op by a fast walk-based pattern rewrite driver.
Definition Rewrite.cpp:437
MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op)
Create an IRRewriter and transfer ownership to the caller.
Definition Rewrite.cpp:262
void mlirGreedyRewriteDriverConfigEnableConstantCSE(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables constant CSE.
Definition Rewrite.cpp:363
MlirGreedySimplifyRegionLevel mlirGreedyRewriteDriverConfigGetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config)
Gets the region simplification level.
Definition Rewrite.cpp:402
void mlirGreedyRewriteDriverConfigSetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level)
Sets the region simplification level.
Definition Rewrite.cpp:346
void mlirRewriterBaseMoveOpBefore(MlirRewriterBase rewriter, MlirOperation op, MlirOperation existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
Definition Rewrite.cpp:175
void mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables folding during greedy rewriting.
Definition Rewrite.cpp:323
void mlirRewriterBaseSetInsertionPointBefore(MlirRewriterBase rewriter, MlirOperation op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
Definition Rewrite.cpp:41
MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition Rewrite.cpp:66
void mlirGreedyRewriteDriverConfigSetMaxIterations(MlirGreedyRewriteDriverConfig config, int64_t maxIterations)
Sets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:308
void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition Rewrite.cpp:148
b getContext())
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition Wrap.h:40
Block represents an ordered list of Operations.
Definition Block.h:33
OpListType::iterator iterator
Definition Block.h:150
iterator end()
Definition Block.h:154
ExternalRewritePattern(MlirRewritePatternCallbacks callbacks, void *userData, StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames)
Definition Rewrite.cpp:458
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const override
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
Definition Rewrite.cpp:473
This class represents a frozen set of patterns that can be processed by a pattern applicator.
This class allows control over how the GreedyPatternRewriteDriver works.
bool isFoldingEnabled() const
Whether this should fold while greedily rewriting.
GreedyRewriteConfig & setRegionSimplificationLevel(GreedySimplifyRegionLevel level)
bool isConstantCSEEnabled() const
If set to "true", constants are CSE'd (even across multiple regions that are in a parent-ancestor rel...
GreedyRewriteConfig & enableConstantCSE(bool enable=true)
GreedyRewriteStrictness getStrictness() const
Strict mode can restrict the ops that are added to the worklist during the rewrite.
bool getUseTopDownTraversal() const
This specifies the order of initial traversal that populates the rewriters worklist.
GreedyRewriteConfig & enableFolding(bool enable=true)
int64_t getMaxNumRewrites() const
This specifies the maximum number of rewrites within an iteration.
GreedyRewriteConfig & setMaxIterations(int64_t iterations)
GreedyRewriteConfig & setMaxNumRewrites(int64_t limit)
int64_t getMaxIterations() const
This specifies the maximum number of times the rewriter will iterate between applying patterns and si...
GreedyRewriteConfig & setUseTopDownTraversal(bool use=true)
GreedySimplifyRegionLevel getRegionSimplificationLevel() const
Perform control flow optimizations to the region tree after applying all patterns.
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Definition Builders.h:445
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Definition Builders.h:442
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
Definition OwningOpRef.h:29
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePattern is the common base class for all DAG to DAG replacements.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
MlirGreedySimplifyRegionLevel
Greedy simplify region levels.
Definition Rewrite.h:51
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_DISABLED
Disable region control-flow simplification.
Definition Rewrite.h:53
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_NORMAL
Run the normal simplification (e.g. dead args elimination).
Definition Rewrite.h:55
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_AGGRESSIVE
Run extra simplifications (e.g. block merging).
Definition Rewrite.h:57
MlirGreedyRewriteStrictness
Greedy rewrite strictness levels.
Definition Rewrite.h:41
@ MLIR_GREEDY_REWRITE_STRICTNESS_EXISTING_AND_NEW_OPS
Only pre-existing and newly created ops are processed.
Definition Rewrite.h:45
@ MLIR_GREEDY_REWRITE_STRICTNESS_EXISTING_OPS
Only pre-existing ops are processed.
Definition Rewrite.h:47
@ MLIR_GREEDY_REWRITE_STRICTNESS_ANY_OP
No restrictions wrt. which ops are processed.
Definition Rewrite.h:43
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
Include the generated interface declarations.
@ Aggressive
Run extra simplificiations (e.g.
@ Normal
Run the normal simplification (e.g. dead args elimination).
@ Disabled
Disable region control-flow simplification.
const FrozenRewritePatternSet GreedyRewriteConfig config
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
Operation * cloneWithoutRegions(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
const FrozenRewritePatternSet & patterns
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
GreedyRewriteStrictness
This enum controls which ops are put on the worklist during a greedy pattern rewrite.
@ ExistingOps
Only pre-existing ops are processed.
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
@ AnyOp
No restrictions wrt. which ops are processed.
A logical result value, essentially a boolean with named states.
Definition Support.h:118
RewritePattern API.
Definition Rewrite.h:439
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75