MLIR 23.0.0git
Rewrite.h
Go to the documentation of this file.
1//===-- mlir-c/Rewrite.h - Helpers for C API to Rewrites ----------*- C -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4// Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This header declares the registration and creation method for
11// rewrite patterns.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_C_REWRITE_H
16#define MLIR_C_REWRITE_H
17
18#include "mlir-c/IR.h"
19#include "mlir-c/Support.h"
20#include "mlir/Config/mlir-config.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26//===----------------------------------------------------------------------===//
27/// Opaque type declarations (see mlir-c/IR.h for more details).
28//===----------------------------------------------------------------------===//
29
30#define DEFINE_C_API_STRUCT(name, storage) \
31 struct name { \
32 storage *ptr; \
33 }; \
34 typedef struct name name
35
36DEFINE_C_API_STRUCT(MlirRewriterBase, void);
37DEFINE_C_API_STRUCT(MlirFrozenRewritePatternSet, void);
38DEFINE_C_API_STRUCT(MlirGreedyRewriteDriverConfig, void);
39
40/// Greedy rewrite strictness levels.
41typedef enum {
42 /// No restrictions wrt. which ops are processed.
44 /// Only pre-existing and newly created ops are processed.
46 /// Only pre-existing ops are processed.
49
50/// Greedy simplify region levels.
51typedef enum {
52 /// Disable region control-flow simplification.
54 /// Run the normal simplification (e.g. dead args elimination).
56 /// Run extra simplifications (e.g. block merging).
59DEFINE_C_API_STRUCT(MlirRewritePatternSet, void);
60DEFINE_C_API_STRUCT(MlirPatternRewriter, void);
61DEFINE_C_API_STRUCT(MlirRewritePattern, const void);
62
63//===----------------------------------------------------------------------===//
64/// RewriterBase API inherited from OpBuilder
65//===----------------------------------------------------------------------===//
66
67/// Get the MLIR context referenced by the rewriter.
68MLIR_CAPI_EXPORTED MlirContext
69mlirRewriterBaseGetContext(MlirRewriterBase rewriter);
70
71//===----------------------------------------------------------------------===//
72/// Insertion points methods
73//===----------------------------------------------------------------------===//
74
75// These do not include functions using Block::iterator or Region::iterator, as
76// they are not exposed by the C API yet. Similarly for methods using
77// `InsertPoint` directly.
78
79/// Reset the insertion point to no location. Creating an operation without a
80/// set insertion point is an error, but this can still be useful when the
81/// current insertion point a builder refers to is being removed.
83mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter);
84
85/// Sets the insertion point to the specified operation, which will cause
86/// subsequent insertions to go right before it.
88mlirRewriterBaseSetInsertionPointBefore(MlirRewriterBase rewriter,
89 MlirOperation op);
90
91/// Sets the insertion point to the node after the specified operation, which
92/// will cause subsequent insertions to go right after it.
94mlirRewriterBaseSetInsertionPointAfter(MlirRewriterBase rewriter,
95 MlirOperation op);
96
97/// Sets the insertion point to the node after the specified value. If value
98/// has a defining operation, sets the insertion point to the node after such
99/// defining operation. This will cause subsequent insertions to go right
100/// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
101/// the start of its block.
103mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter,
104 MlirValue value);
105
106/// Sets the insertion point to the start of the specified block.
108mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter,
109 MlirBlock block);
110
111/// Sets the insertion point to the end of the specified block.
113mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter,
114 MlirBlock block);
115
116/// Return the block the current insertion point belongs to. Note that the
117/// insertion point is not necessarily the end of the block.
118MLIR_CAPI_EXPORTED MlirBlock
119mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter);
120
121/// Returns the current block of the rewriter.
122MLIR_CAPI_EXPORTED MlirBlock
123mlirRewriterBaseGetBlock(MlirRewriterBase rewriter);
124
125/// Returns the operation right after the current insertion point
126/// of the rewriter. A null MlirOperation will be returned
127// if the current insertion point is at the end of the block.
128MLIR_CAPI_EXPORTED MlirOperation
129mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter);
130
131//===----------------------------------------------------------------------===//
132/// Block and operation creation/insertion/cloning
133//===----------------------------------------------------------------------===//
134
135// These functions do not include the IRMapper, as it is not yet exposed by the
136// C API.
137
138/// Add new block with 'argTypes' arguments and set the insertion point to the
139/// end of it. The block is placed before 'insertBefore'. `locs` contains the
140/// locations of the inserted arguments, and should match the size of
141/// `argTypes`.
143 MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes,
144 MlirType const *argTypes, MlirLocation const *locations);
145
146/// Insert the given operation at the current insertion point and return it.
147MLIR_CAPI_EXPORTED MlirOperation
148mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op);
149
150/// Creates a deep copy of the specified operation.
151MLIR_CAPI_EXPORTED MlirOperation
152mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op);
153
154/// Creates a deep copy of this operation but keep the operation regions
155/// empty.
157 MlirRewriterBase rewriter, MlirOperation op);
158
159/// Clone the blocks that belong to "region" before the given position in
160/// another region "parent".
162mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
163 MlirBlock before);
164
165//===----------------------------------------------------------------------===//
166/// RewriterBase API
167//===----------------------------------------------------------------------===//
168
169/// Move the blocks that belong to "region" before the given position in
170/// another region "parent". The two regions must be different. The caller
171/// is responsible for creating or updating the operation transferring flow
172/// of control to the region and passing it the correct block arguments.
174mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
175 MlirBlock before);
176
177/// Replace the results of the given (original) operation with the specified
178/// list of values (replacements). The result types of the given op and the
179/// replacements must match. The original op is erased.
181mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter, MlirOperation op,
182 intptr_t nValues, MlirValue const *values);
183
184/// Replace the results of the given (original) operation with the specified
185/// new op (replacement). The result types of the two ops must match. The
186/// original op is erased.
188mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter,
189 MlirOperation op, MlirOperation newOp);
190
191/// Erases an operation that is known to have no uses.
192MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter,
193 MlirOperation op);
194
195/// Erases a block along with all operations inside it.
196MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter,
197 MlirBlock block);
198
199/// Inline the operations of block 'source' before the operation 'op'. The
200/// source block will be deleted and must have no uses. 'argValues' is used to
201/// replace the block arguments of 'source'
202///
203/// The source block must have no successors. Otherwise, the resulting IR
204/// would have unreachable operations.
206mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source,
207 MlirOperation op, intptr_t nArgValues,
208 MlirValue const *argValues);
209
210/// Inline the operations of block 'source' into the end of block 'dest'. The
211/// source block will be deleted and must have no uses. 'argValues' is used to
212/// replace the block arguments of 'source'
213///
214/// The dest block must have no successors. Otherwise, the resulting IR would
215/// have unreachable operation.
216MLIR_CAPI_EXPORTED void mlirRewriterBaseMergeBlocks(MlirRewriterBase rewriter,
217 MlirBlock source,
218 MlirBlock dest,
219 intptr_t nArgValues,
220 MlirValue const *argValues);
221
222/// Unlink this operation from its current block and insert it right before
223/// `existingOp` which may be in the same or another block in the same
224/// function.
225MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpBefore(MlirRewriterBase rewriter,
226 MlirOperation op,
227 MlirOperation existingOp);
228
229/// Unlink this operation from its current block and insert it right after
230/// `existingOp` which may be in the same or another block in the same
231/// function.
232MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpAfter(MlirRewriterBase rewriter,
233 MlirOperation op,
234 MlirOperation existingOp);
235
236/// Unlink this block and insert it right before `existingBlock`.
238mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block,
239 MlirBlock existingBlock);
240
241/// This method is used to notify the rewriter that an in-place operation
242/// modification is about to happen. A call to this function *must* be
243/// followed by a call to either `finalizeOpModification` or
244/// `cancelOpModification`. This is a minor efficiency win (it avoids creating
245/// a new operation and removing the old one) but also often allows simpler
246/// code in the client.
248mlirRewriterBaseStartOpModification(MlirRewriterBase rewriter,
249 MlirOperation op);
250
251/// This method is used to signal the end of an in-place modification of the
252/// given operation. This can only be called on operations that were provided
253/// to a call to `startOpModification`.
255mlirRewriterBaseFinalizeOpModification(MlirRewriterBase rewriter,
256 MlirOperation op);
257
258/// This method cancels a pending in-place modification. This can only be
259/// called on operations that were provided to a call to
260/// `startOpModification`.
262mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter,
263 MlirOperation op);
264
265/// Find uses of `from` and replace them with `to`. Also notify the listener
266/// about every in-place op modification (for every use that was replaced).
268mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from,
269 MlirValue to);
270
271/// Find uses of `from` and replace them with `to`. Also notify the listener
272/// about every in-place op modification (for every use that was replaced).
274 MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from,
275 MlirValue const *to);
276
277/// Find uses of `from` and replace them with `to`. Also notify the listener
278/// about every in-place op modification (for every use that was replaced)
279/// and that the `from` operation is about to be replaced.
281mlirRewriterBaseReplaceAllOpUsesWithValueRange(MlirRewriterBase rewriter,
282 MlirOperation from, intptr_t nTo,
283 MlirValue const *to);
284
285/// Find uses of `from` and replace them with `to`. Also notify the listener
286/// about every in-place op modification (for every use that was replaced)
287/// and that the `from` operation is about to be replaced.
289 MlirRewriterBase rewriter, MlirOperation from, MlirOperation to);
290
291/// Find uses of `from` within `block` and replace them with `to`. Also notify
292/// the listener about every in-place op modification (for every use that was
293/// replaced). The optional `allUsesReplaced` flag is set to "true" if all
294/// uses were replaced.
296 MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues,
297 MlirValue const *newValues, MlirBlock block);
298
299/// Find uses of `from` and replace them with `to` except if the user is
300/// `exceptedUser`. Also notify the listener about every in-place op
301/// modification (for every use that was replaced).
303mlirRewriterBaseReplaceAllUsesExcept(MlirRewriterBase rewriter, MlirValue from,
304 MlirValue to, MlirOperation exceptedUser);
305
306//===----------------------------------------------------------------------===//
307/// IRRewriter API
308//===----------------------------------------------------------------------===//
309
310/// Create an IRRewriter and transfer ownership to the caller.
311MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context);
312
313/// Create an IRRewriter and transfer ownership to the caller. Additionally
314/// set the insertion point before the operation.
315MLIR_CAPI_EXPORTED MlirRewriterBase
316mlirIRRewriterCreateFromOp(MlirOperation op);
317
318/// Takes an IRRewriter owned by the caller and destroys it. It is the
319/// responsibility of the user to only pass an IRRewriter class.
320MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter);
321
322//===----------------------------------------------------------------------===//
323/// FrozenRewritePatternSet API
324//===----------------------------------------------------------------------===//
325
326/// Freeze the given MlirRewritePatternSet to a MlirFrozenRewritePatternSet.
327/// Note that the ownership of the input set is transferred into the frozen set
328/// after this call.
329MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet
330mlirFreezeRewritePattern(MlirRewritePatternSet set);
331
332/// Destroy the given MlirFrozenRewritePatternSet.
334mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set);
335
337 MlirOperation op, MlirFrozenRewritePatternSet patterns,
338 MlirGreedyRewriteDriverConfig);
339
341 MlirModule op, MlirFrozenRewritePatternSet patterns,
342 MlirGreedyRewriteDriverConfig config);
343
344//===----------------------------------------------------------------------===//
345/// GreedyRewriteDriverConfig API
346//===----------------------------------------------------------------------===//
347
348/// Creates a greedy rewrite driver configuration with default settings.
349MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig
351
352/// Destroys a greedy rewrite driver configuration.
354mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config);
355
356/// Sets the maximum number of iterations for the greedy rewrite driver.
357/// Use -1 for no limit.
359 MlirGreedyRewriteDriverConfig config, int64_t maxIterations);
360
361/// Sets the maximum number of rewrites within an iteration.
362/// Use -1 for no limit.
364 MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites);
365
366/// Sets whether to use top-down traversal for the initial population of the
367/// worklist.
369 MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal);
370
371/// Enables or disables folding during greedy rewriting.
373mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config,
374 bool enable);
375
376/// Sets the strictness level for the greedy rewrite driver.
378 MlirGreedyRewriteDriverConfig config,
379 MlirGreedyRewriteStrictness strictness);
380
381/// Sets the region simplification level.
384 MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level);
385
386/// Enables or disables constant CSE.
388 MlirGreedyRewriteDriverConfig config, bool enable);
389
390/// Gets the maximum number of iterations for the greedy rewrite driver.
392 MlirGreedyRewriteDriverConfig config);
393
394/// Gets the maximum number of rewrites within an iteration.
396 MlirGreedyRewriteDriverConfig config);
397
398/// Gets whether top-down traversal is used for initial worklist population.
400 MlirGreedyRewriteDriverConfig config);
401
402/// Gets whether folding is enabled during greedy rewriting.
404 MlirGreedyRewriteDriverConfig config);
405
406/// Gets the strictness level for the greedy rewrite driver.
409 MlirGreedyRewriteDriverConfig config);
410
411/// Gets the region simplification level.
414 MlirGreedyRewriteDriverConfig config);
415
416/// Gets whether constant CSE is enabled.
418 MlirGreedyRewriteDriverConfig config);
419
420/// Applies the given patterns to the given op by a fast walk-based pattern
421/// rewrite driver.
423mlirWalkAndApplyPatterns(MlirOperation op,
424 MlirFrozenRewritePatternSet patterns);
425
426//===----------------------------------------------------------------------===//
427/// PatternRewriter API
428//===----------------------------------------------------------------------===//
429
430/// Cast the PatternRewriter to a RewriterBase
431MLIR_CAPI_EXPORTED MlirRewriterBase
432mlirPatternRewriterAsBase(MlirPatternRewriter rewriter);
433
434//===----------------------------------------------------------------------===//
435/// RewritePattern API
436//===----------------------------------------------------------------------===//
437
438/// Callbacks to construct a rewrite pattern.
439typedef struct {
440 /// Optional constructor for the user data.
441 /// Set to nullptr to disable it.
442 void (*construct)(void *userData);
443 /// Optional destructor for the user data.
444 /// Set to nullptr to disable it.
445 void (*destruct)(void *userData);
446 /// The callback function to match against code rooted at the specified
447 /// operation, and perform the rewrite if the match is successful,
448 /// corresponding to RewritePattern::matchAndRewrite.
449 MlirLogicalResult (*matchAndRewrite)(MlirRewritePattern pattern,
450 MlirOperation op,
451 MlirPatternRewriter rewriter,
452 void *userData);
454
455/// Create a rewrite pattern that matches the operation
456/// with the given rootName, corresponding to mlir::OpRewritePattern.
458 MlirStringRef rootName, unsigned benefit, MlirContext context,
459 MlirRewritePatternCallbacks callbacks, void *userData,
460 size_t nGeneratedNames, MlirStringRef *generatedNames);
461
462//===----------------------------------------------------------------------===//
463/// RewritePatternSet API
464//===----------------------------------------------------------------------===//
465
466/// Create an empty MlirRewritePatternSet.
467MLIR_CAPI_EXPORTED MlirRewritePatternSet
468mlirRewritePatternSetCreate(MlirContext context);
469
470/// Destruct the given MlirRewritePatternSet.
471MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set);
472
473/// Add the given MlirRewritePattern into a MlirRewritePatternSet.
474/// Note that the ownership of the pattern is transferred to the set after this
475/// call.
476MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set,
477 MlirRewritePattern pattern);
478
479//===----------------------------------------------------------------------===//
480/// PDLPatternModule API
481//===----------------------------------------------------------------------===//
482
483#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
484DEFINE_C_API_STRUCT(MlirPDLPatternModule, void);
485DEFINE_C_API_STRUCT(MlirPDLValue, const void);
487
488MLIR_CAPI_EXPORTED MlirPDLPatternModule
489mlirPDLPatternModuleFromModule(MlirModule op);
490
491MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op);
492
493MLIR_CAPI_EXPORTED MlirRewritePatternSet
494mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op);
495
496/// Cast the MlirPDLValue to an MlirValue.
497/// Return a null value if the cast fails, just like llvm::dyn_cast.
498MLIR_CAPI_EXPORTED MlirValue mlirPDLValueAsValue(MlirPDLValue value);
499
500/// Cast the MlirPDLValue to an MlirType.
501/// Return a null value if the cast fails, just like llvm::dyn_cast.
502MLIR_CAPI_EXPORTED MlirType mlirPDLValueAsType(MlirPDLValue value);
503
504/// Cast the MlirPDLValue to an MlirOperation.
505/// Return a null value if the cast fails, just like llvm::dyn_cast.
506MLIR_CAPI_EXPORTED MlirOperation mlirPDLValueAsOperation(MlirPDLValue value);
507
508/// Cast the MlirPDLValue to an MlirAttribute.
509/// Return a null value if the cast fails, just like llvm::dyn_cast.
510MLIR_CAPI_EXPORTED MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value);
511
512/// Push the MlirValue into the given MlirPDLResultList.
514mlirPDLResultListPushBackValue(MlirPDLResultList results, MlirValue value);
515
516/// Push the MlirType into the given MlirPDLResultList.
517MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackType(MlirPDLResultList results,
518 MlirType value);
519
520/// Push the MlirOperation into the given MlirPDLResultList.
522mlirPDLResultListPushBackOperation(MlirPDLResultList results,
523 MlirOperation value);
524
525/// Push the MlirAttribute into the given MlirPDLResultList.
527mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
528 MlirAttribute value);
529
530/// This function type is used as callbacks for PDL native rewrite functions.
531/// Input values can be accessed by `values` with its size `nValues`;
532/// output values can be added into `results` by `mlirPDLResultListPushBack*`
533/// APIs. And the return value indicates whether the rewrite succeeds.
534typedef MlirLogicalResult (*MlirPDLRewriteFunction)(
535 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
536 MlirPDLValue *values, void *userData);
537
538/// Register a rewrite function into the given PDL pattern module.
539/// `userData` will be provided as an argument to the rewrite function.
540MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterRewriteFunction(
541 MlirPDLPatternModule pdlModule, MlirStringRef name,
542 MlirPDLRewriteFunction rewriteFn, void *userData);
543
544/// This function type is used as callbacks for PDL native constraint functions.
545/// Input values can be accessed by `values` with its size `nValues`;
546/// output values can be added into `results` by `mlirPDLResultListPushBack*`
547/// APIs. And the return value indicates whether the constraint holds.
548typedef MlirLogicalResult (*MlirPDLConstraintFunction)(
549 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
550 MlirPDLValue *values, void *userData);
551
552/// Register a constraint function into the given PDL pattern module.
553/// `userData` will be provided as an argument to the constraint function.
554MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterConstraintFunction(
555 MlirPDLPatternModule pdlModule, MlirStringRef name,
556 MlirPDLConstraintFunction constraintFn, void *userData);
557
558#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
559
560#undef DEFINE_C_API_STRUCT
561
562#ifdef __cplusplus
563}
564#endif
565
566#endif // MLIR_C_REWRITE_H
MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block, MlirBlock existingBlock)
Unlink this block and insert it right before existingBlock.
Definition Rewrite.cpp:185
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block)
Erases a block along with all operations inside it.
Definition Rewrite.cpp:152
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method cancels a pending in-place modification.
Definition Rewrite.cpp:200
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableConstantCSE(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables constant CSE.
Definition Rewrite.cpp:363
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter)
Returns the operation right after the current insertion point of the rewriter.
Definition Rewrite.cpp:75
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllOpUsesWithOperation(MlirRewriterBase rewriter, MlirOperation from, MlirOperation to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:230
MLIR_CAPI_EXPORTED MlirGreedySimplifyRegionLevel mlirGreedyRewriteDriverConfigGetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config)
Gets the region simplification level.
Definition Rewrite.cpp:402
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsFoldingEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether folding is enabled during greedy rewriting.
Definition Rewrite.cpp:383
MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig mlirGreedyRewriteDriverConfigCreate(void)
GreedyRewriteDriverConfig API.
Definition Rewrite.cpp:299
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsConstantCSEEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether constant CSE is enabled.
Definition Rewrite.cpp:416
MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set, MlirRewritePattern pattern)
Add the given MlirRewritePattern into a MlirRewritePatternSet.
Definition Rewrite.cpp:513
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of the specified operation.
Definition Rewrite.cpp:109
#define DEFINE_C_API_STRUCT(name, storage)
Opaque type declarations (see mlir-c/IR.h for more details).
Definition Rewrite.h:30
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level)
Sets the region simplification level.
Definition Rewrite.cpp:346
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition Rewrite.cpp:102
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig config)
Definition Rewrite.cpp:422
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config)
Destroys a greedy rewrite driver configuration.
Definition Rewrite.cpp:303
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal)
Sets whether to use top-down traversal for the initial population of the worklist.
Definition Rewrite.cpp:318
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition Rewrite.cpp:148
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxIterations(MlirGreedyRewriteDriverConfig config, int64_t maxIterations)
Sets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:308
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op)
Create an IRRewriter and transfer ownership to the caller.
Definition Rewrite.cpp:262
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition Rewrite.cpp:29
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxIterations(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:368
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context)
IRRewriter API.
Definition Rewrite.cpp:258
MLIR_CAPI_EXPORTED MlirGreedyRewriteStrictness mlirGreedyRewriteDriverConfigGetStrictness(MlirGreedyRewriteDriverConfig config)
Gets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:388
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition Rewrite.cpp:66
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetStrictness(MlirGreedyRewriteDriverConfig config, MlirGreedyRewriteStrictness strictness)
Sets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:328
MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set)
Destruct the given MlirRewritePatternSet.
Definition Rewrite.cpp:509
MLIR_CAPI_EXPORTED void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter)
Insertion points methods.
Definition Rewrite.cpp:37
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter)
PatternRewriter API.
Definition Rewrite.cpp:446
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition Rewrite.cpp:430
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigGetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config)
Gets whether top-down traversal is used for initial worklist population.
Definition Rewrite.cpp:378
MLIR_CAPI_EXPORTED MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context)
RewritePatternSet API.
Definition Rewrite.cpp:505
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxNumRewrites(MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites)
Sets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:313
MLIR_CAPI_EXPORTED void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
RewriterBase API.
Definition Rewrite.cpp:129
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter)
Returns the current block of the rewriter.
Definition Rewrite.cpp:70
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the end of the specified block.
Definition Rewrite.cpp:61
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables folding during greedy rewriting.
Definition Rewrite.cpp:323
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set)
Destroy the given MlirFrozenRewritePatternSet.
Definition Rewrite.cpp:281
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter, MlirValue value)
Sets the insertion point to the node after the specified value.
Definition Rewrite.cpp:51
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseCloneWithoutRegions(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of this operation but keep the operation regions empty.
Definition Rewrite.cpp:114
MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet set)
FrozenRewritePatternSet API.
Definition Rewrite.cpp:275
MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter)
Takes an IRRewriter owned by the caller and destroys it.
Definition Rewrite.cpp:266
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
MLIR_CAPI_EXPORTED 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
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from, MlirValue to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:205
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
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the start of the specified block.
Definition Rewrite.cpp:56
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxNumRewrites(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:373
struct MlirLogicalResult MlirLogicalResult
Definition Support.h:121
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
A logical result value, essentially a boolean with named states.
Definition Support.h:118
RewritePattern API.
Definition Rewrite.h:439
MlirLogicalResult(* matchAndRewrite)(MlirRewritePattern pattern, MlirOperation op, MlirPatternRewriter rewriter, void *userData)
The callback function to match against code rooted at the specified operation, and perform the rewrit...
Definition Rewrite.h:449
void(* construct)(void *userData)
Optional constructor for the user data.
Definition Rewrite.h:442
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition Rewrite.h:445
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75