MLIR 24.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
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);
62DEFINE_C_API_STRUCT(MlirConversionTarget, void);
63DEFINE_C_API_STRUCT(MlirConversionPattern, const void);
64DEFINE_C_API_STRUCT(MlirTypeConverter, void);
65DEFINE_C_API_STRUCT(MlirConversionPatternRewriter, void);
66DEFINE_C_API_STRUCT(MlirConversionConfig, void);
67
68//===----------------------------------------------------------------------===//
69/// RewriterBase API inherited from OpBuilder
70//===----------------------------------------------------------------------===//
71
72/// Get the MLIR context referenced by the rewriter.
73MLIR_CAPI_EXPORTED MlirContext
75
76//===----------------------------------------------------------------------===//
77/// Insertion points methods
78//===----------------------------------------------------------------------===//
79
80// These do not include functions using Block::iterator or Region::iterator, as
81// they are not exposed by the C API yet. `InsertPoint` is exposed as
82// `MlirRewriterBaseInsertPoint` below.
83
84/// Reset the insertion point to no location. Creating an operation without a
85/// set insertion point is an error, but this can still be useful when the
86/// current insertion point a builder refers to is being removed.
89
90/// Sets the insertion point to the specified operation, which will cause
91/// subsequent insertions to go right before it.
94 MlirOperation op);
95
96/// Sets the insertion point to the node after the specified operation, which
97/// will cause subsequent insertions to go right after it.
100 MlirOperation op);
101
102/// Sets the insertion point to the node after the specified value. If value
103/// has a defining operation, sets the insertion point to the node after such
104/// defining operation. This will cause subsequent insertions to go right
105/// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
106/// the start of its block.
109 MlirValue value);
110
111/// Sets the insertion point to the start of the specified block.
114 MlirBlock block);
115
116/// Sets the insertion point to the end of the specified block.
119 MlirBlock block);
120
121/// Return the block the current insertion point belongs to. Note that the
122/// insertion point is not necessarily the end of the block.
123MLIR_CAPI_EXPORTED MlirBlock
125
126/// Returns the current block of the rewriter.
127MLIR_CAPI_EXPORTED MlirBlock
129
130/// Returns the operation right after the current insertion point
131/// of the rewriter. A null MlirOperation will be returned
132// if the current insertion point is at the end of the block.
133MLIR_CAPI_EXPORTED MlirOperation
135
136/// A saved insertion point: a (block, operationAfter) pair. `operationAfter` is
137/// the operation that subsequent insertions go before. If `operationAfter` is
138/// null, the insertion point is at the end of `block`. If `block` is null, the
139/// insertion point is not set (cleared).
144
145/// Returns the current insertion point of the rewriter so that it can be
146/// restored later with mlirRewriterBaseRestoreInsertionPoint.
149
150/// Restores a previously saved insertion point.
153 MlirRewriterBaseInsertPoint insertPoint);
154
155//===----------------------------------------------------------------------===//
156/// Block and operation creation/insertion/cloning
157//===----------------------------------------------------------------------===//
158
159// These functions do not include the IRMapper, as it is not yet exposed by the
160// C API.
161
162/// Add new block with 'argTypes' arguments and set the insertion point to the
163/// end of it. The block is placed before 'insertBefore'. `locs` contains the
164/// locations of the inserted arguments, and should match the size of
165/// `argTypes`.
167 MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes,
168 MlirType const *argTypes, MlirLocation const *locations);
169
170/// Insert the given operation at the current insertion point and return it.
171MLIR_CAPI_EXPORTED MlirOperation
172mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op);
173
174/// Creates a deep copy of the specified operation.
175MLIR_CAPI_EXPORTED MlirOperation
176mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op);
177
178/// Creates a deep copy of this operation but keep the operation regions
179/// empty.
181 MlirRewriterBase rewriter, MlirOperation op);
182
183/// Clones the given operation using the rewriter and the provided IRMapping.
184/// The mapping is updated with the results of the cloned operation.
186 MlirRewriterBase rewriter, MlirOperation op, MlirIRMapping mapping);
187
188/// Clone the blocks that belong to "region" before the given position in
189/// another region "parent".
191mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
192 MlirBlock before);
193
194//===----------------------------------------------------------------------===//
195/// RewriterBase API
196//===----------------------------------------------------------------------===//
197
198/// Move the blocks that belong to "region" before the given position in
199/// another region "parent". The two regions must be different. The caller
200/// is responsible for creating or updating the operation transferring flow
201/// of control to the region and passing it the correct block arguments.
203mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
204 MlirBlock before);
205
206/// Replace the results of the given (original) operation with the specified
207/// list of values (replacements). The result types of the given op and the
208/// replacements must match. The original op is erased.
211 intptr_t nValues, MlirValue const *values);
212
213/// Replace the results of the given (original) operation with the specified
214/// new op (replacement). The result types of the two ops must match. The
215/// original op is erased.
218 MlirOperation op, MlirOperation newOp);
219
220/// Erases an operation that is known to have no uses.
222 MlirOperation op);
223
224/// Erases a block along with all operations inside it.
226 MlirBlock block);
227
228/// Inline the operations of block 'source' before the operation 'op'. The
229/// source block will be deleted and must have no uses. 'argValues' is used to
230/// replace the block arguments of 'source'
231///
232/// The source block must have no successors. Otherwise, the resulting IR
233/// would have unreachable operations.
235mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source,
236 MlirOperation op, intptr_t nArgValues,
237 MlirValue const *argValues);
238
239/// Inline the operations of block 'source' into the end of block 'dest'. The
240/// source block will be deleted and must have no uses. 'argValues' is used to
241/// replace the block arguments of 'source'
242///
243/// The dest block must have no successors. Otherwise, the resulting IR would
244/// have unreachable operation.
246 MlirBlock source,
247 MlirBlock dest,
248 intptr_t nArgValues,
249 MlirValue const *argValues);
250
251/// Unlink this operation from its current block and insert it right before
252/// `existingOp` which may be in the same or another block in the same
253/// function.
255 MlirOperation op,
256 MlirOperation existingOp);
257
258/// Unlink this operation from its current block and insert it right after
259/// `existingOp` which may be in the same or another block in the same
260/// function.
262 MlirOperation op,
263 MlirOperation existingOp);
264
265/// Unlink this block and insert it right before `existingBlock`.
267mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block,
268 MlirBlock existingBlock);
269
270/// This method is used to notify the rewriter that an in-place operation
271/// modification is about to happen. A call to this function *must* be
272/// followed by a call to either `finalizeOpModification` or
273/// `cancelOpModification`. This is a minor efficiency win (it avoids creating
274/// a new operation and removing the old one) but also often allows simpler
275/// code in the client.
278 MlirOperation op);
279
280/// This method is used to signal the end of an in-place modification of the
281/// given operation. This can only be called on operations that were provided
282/// to a call to `startOpModification`.
285 MlirOperation op);
286
287/// This method cancels a pending in-place modification. This can only be
288/// called on operations that were provided to a call to
289/// `startOpModification`.
292 MlirOperation op);
293
294/// Find uses of `from` and replace them with `to`. Also notify the listener
295/// about every in-place op modification (for every use that was replaced).
298 MlirValue to);
299
300/// Find uses of `from` and replace them with `to`. Also notify the listener
301/// about every in-place op modification (for every use that was replaced).
303 MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from,
304 MlirValue const *to);
305
306/// Find uses of `from` and replace them with `to`. Also notify the listener
307/// about every in-place op modification (for every use that was replaced)
308/// and that the `from` operation is about to be replaced.
311 MlirOperation from, intptr_t nTo,
312 MlirValue const *to);
313
314/// Find uses of `from` and replace them with `to`. Also notify the listener
315/// about every in-place op modification (for every use that was replaced)
316/// and that the `from` operation is about to be replaced.
318 MlirRewriterBase rewriter, MlirOperation from, MlirOperation to);
319
320/// Find uses of `from` within `block` and replace them with `to`. Also notify
321/// the listener about every in-place op modification (for every use that was
322/// replaced). The optional `allUsesReplaced` flag is set to "true" if all
323/// uses were replaced.
325 MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues,
326 MlirValue const *newValues, MlirBlock block);
327
328/// Find uses of `from` and replace them with `to` except if the user is
329/// `exceptedUser`. Also notify the listener about every in-place op
330/// modification (for every use that was replaced).
333 MlirValue to, MlirOperation exceptedUser);
334
335//===----------------------------------------------------------------------===//
336/// IRRewriter API
337//===----------------------------------------------------------------------===//
338
339/// Create an IRRewriter and transfer ownership to the caller.
341
342/// Create an IRRewriter and transfer ownership to the caller. Additionally
343/// set the insertion point before the operation.
345mlirIRRewriterCreateFromOp(MlirOperation op);
346
347/// Takes an IRRewriter owned by the caller and destroys it. It is the
348/// responsibility of the user to only pass an IRRewriter class.
350
351//===----------------------------------------------------------------------===//
352/// FrozenRewritePatternSet API
353//===----------------------------------------------------------------------===//
354
355/// Freeze the given MlirRewritePatternSet to a MlirFrozenRewritePatternSet.
356/// Note that the ownership of the input set is transferred into the frozen set
357/// after this call.
358MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet
359mlirFreezeRewritePattern(MlirRewritePatternSet set);
360
361/// Destroy the given MlirFrozenRewritePatternSet.
363mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set);
364
366 MlirOperation op, MlirFrozenRewritePatternSet patterns,
367 MlirGreedyRewriteDriverConfig);
368
370 MlirModule op, MlirFrozenRewritePatternSet patterns,
371 MlirGreedyRewriteDriverConfig config);
372
373//===----------------------------------------------------------------------===//
374/// GreedyRewriteDriverConfig API
375//===----------------------------------------------------------------------===//
376
377/// Creates a greedy rewrite driver configuration with default settings.
378MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig
380
381/// Destroys a greedy rewrite driver configuration.
383mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config);
384
385/// Sets the maximum number of iterations for the greedy rewrite driver.
386/// Use -1 for no limit.
388 MlirGreedyRewriteDriverConfig config, int64_t maxIterations);
389
390/// Sets the maximum number of rewrites within an iteration.
391/// Use -1 for no limit.
393 MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites);
394
395/// Sets whether to use top-down traversal for the initial population of the
396/// worklist.
398 MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal);
399
400/// Enables or disables folding during greedy rewriting.
402mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config,
403 bool enable);
404
405/// Sets the strictness level for the greedy rewrite driver.
407 MlirGreedyRewriteDriverConfig config,
408 MlirGreedyRewriteStrictness strictness);
409
410/// Sets the region simplification level.
413 MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level);
414
415/// Enables or disables constant CSE.
417 MlirGreedyRewriteDriverConfig config, bool enable);
418
419/// Gets the maximum number of iterations for the greedy rewrite driver.
421 MlirGreedyRewriteDriverConfig config);
422
423/// Gets the maximum number of rewrites within an iteration.
425 MlirGreedyRewriteDriverConfig config);
426
427/// Gets whether top-down traversal is used for initial worklist population.
429 MlirGreedyRewriteDriverConfig config);
430
431/// Gets whether folding is enabled during greedy rewriting.
433 MlirGreedyRewriteDriverConfig config);
434
435/// Gets the strictness level for the greedy rewrite driver.
438 MlirGreedyRewriteDriverConfig config);
439
440/// Gets the region simplification level.
443 MlirGreedyRewriteDriverConfig config);
444
445/// Gets whether constant CSE is enabled.
447 MlirGreedyRewriteDriverConfig config);
448
449/// Applies the given patterns to the given op by a fast walk-based pattern
450/// rewrite driver.
452mlirWalkAndApplyPatterns(MlirOperation op,
453 MlirFrozenRewritePatternSet patterns);
454
455/// Apply a partial conversion on the given operation.
457 MlirOperation op, MlirConversionTarget target,
458 MlirFrozenRewritePatternSet patterns, MlirConversionConfig config);
459
460/// Apply a full conversion on the given operation.
462 MlirOperation op, MlirConversionTarget target,
463 MlirFrozenRewritePatternSet patterns, MlirConversionConfig config);
464
465//===----------------------------------------------------------------------===//
466/// ConversionConfig API
467//===----------------------------------------------------------------------===//
468
469/// Create a default ConversionConfig.
470MLIR_CAPI_EXPORTED MlirConversionConfig mlirConversionConfigCreate(void);
471
472/// Destroy the given ConversionConfig.
474mlirConversionConfigDestroy(MlirConversionConfig config);
475
481
482/// Set the folding mode for the given ConversionConfig.
484mlirConversionConfigSetFoldingMode(MlirConversionConfig config,
486
487/// Get the folding mode for the given ConversionConfig.
489mlirConversionConfigGetFoldingMode(MlirConversionConfig config);
490
491/// Enable or disable building materializations during conversion.
493mlirConversionConfigEnableBuildMaterializations(MlirConversionConfig config,
494 bool enable);
495
496/// Check if building materializations during conversion is enabled.
498mlirConversionConfigIsBuildMaterializationsEnabled(MlirConversionConfig config);
499
500//===----------------------------------------------------------------------===//
501/// PatternRewriter API
502//===----------------------------------------------------------------------===//
503
504/// Cast the PatternRewriter to a RewriterBase
506mlirPatternRewriterAsBase(MlirPatternRewriter rewriter);
507
508//===----------------------------------------------------------------------===//
509/// ConversionPatternRewriter API
510//===----------------------------------------------------------------------===//
511
512/// Cast the ConversionPatternRewriter to a PatternRewriter
513MLIR_CAPI_EXPORTED MlirPatternRewriter
515 MlirConversionPatternRewriter rewriter);
516
517/// Apply a signature conversion to each block in the given region.
520 MlirConversionPatternRewriter rewriter, MlirRegion region,
521 MlirTypeConverter typeConverter);
522
523/// Replace the given operation with multiple value ranges -- one range per
524/// result of `op` -- and erase it. `nRanges` must equal the number of results
525/// of `op`. `rangeSizes[i]` is the number of values in the i-th range, and
526/// `values` is the flat concatenation of all ranges (its length is the sum of
527/// `rangeSizes[0..nRanges)`).
529 MlirConversionPatternRewriter rewriter, MlirOperation op, intptr_t nRanges,
530 intptr_t *rangeSizes, MlirValue *values);
531
532//===----------------------------------------------------------------------===//
533/// ConversionTarget API
534//===----------------------------------------------------------------------===//
535
536/// Create an empty ConversionTarget.
537MLIR_CAPI_EXPORTED MlirConversionTarget
538mlirConversionTargetCreate(MlirContext context);
539
540/// Destroy the given ConversionTarget.
542mlirConversionTargetDestroy(MlirConversionTarget target);
543
544/// Register the given operations as legal.
546mlirConversionTargetAddLegalOp(MlirConversionTarget target,
547 MlirStringRef opName);
548
549/// Register the given operations as illegal.
551mlirConversionTargetAddIllegalOp(MlirConversionTarget target,
552 MlirStringRef opName);
553
554/// Register the operations of the given dialect as legal.
557 MlirStringRef dialectName);
558
559/// Register the operations of the given dialect as illegal.
562 MlirStringRef dialectName);
563
564/// Result of a dynamic legality callback.
565typedef enum {
566 /// The operation instance is legal.
568 /// The operation instance is illegal.
570 /// The callback has no opinion on this instance. The decision is deferred to
571 /// other registered callbacks (legality callbacks are composed) or, failing
572 /// that, to the operation's static legality action.
575
576/// Callback for dynamic legality checks. Returns the legality of the given
577/// operation instance (see MlirConversionTargetLegality).
580 void *userData);
581
582/// Register the given operation as dynamically legal, with a callback to
583/// determine per-instance legality. The callback must not be NULL.
585 MlirConversionTarget target, MlirStringRef opName,
586 MlirConversionTargetDynamicLegalityCallback callback, void *userData);
587
588/// Register the given dialect as dynamically legal, with a callback to
589/// determine per-instance legality for all operations in the dialect. The
590/// callback must not be NULL.
592 MlirConversionTarget target, MlirStringRef dialectName,
593 MlirConversionTargetDynamicLegalityCallback callback, void *userData);
594
595/// Mark the given operation as recursively legal. The optional callback (may
596/// be NULL) determines whether a specific instance is recursively legal; a NULL
597/// callback marks the operation as unconditionally recursively legal.
599 MlirConversionTarget target, MlirStringRef opName,
600 MlirConversionTargetDynamicLegalityCallback callback, void *userData);
601
602/// Mark unknown operations as dynamically legal, with a callback. The callback
603/// must not be NULL.
605 MlirConversionTarget target,
606 MlirConversionTargetDynamicLegalityCallback callback, void *userData);
607
608//===----------------------------------------------------------------------===//
609/// TypeConverter API
610//===----------------------------------------------------------------------===//
611
612/// Create a TypeConverter.
613MLIR_CAPI_EXPORTED MlirTypeConverter mlirTypeConverterCreate(void);
614
615/// Destroy the given TypeConverter.
617mlirTypeConverterDestroy(MlirTypeConverter typeConverter);
618
619/// Outcome of a type conversion callback. Mirrors the three states of the
620/// underlying C++ `std::optional<LogicalResult>` conversion result.
622 /// The type was converted successfully.
624 /// The conversion failed; no further conversion function will be tried.
626 /// The conversion was declined; another registered conversion function may be
627 /// tried.
630
631/// Callback type for type conversion functions. On success the callback sets
632/// `*convertedType` to the converted type and returns
633/// MlirTypeConverterConversionStatusSuccess. Returning
634/// MlirTypeConverterConversionStatusDeclined leaves the type unconverted and
635/// allows another registered conversion function to be tried; returning
636/// MlirTypeConverterConversionStatusFailure fails the conversion without trying
637/// any further function.
640 MlirType *convertedType,
641 void *userData);
642
643/// Add a type conversion function to the given TypeConverter.
645mlirTypeConverterAddConversion(MlirTypeConverter typeConverter,
647 void *userData);
648
649/// Opaque accumulator for the result types of a 1:N type conversion. It is
650/// passed to a MlirTypeConverter1ToNConversionCallback, which appends converted
651/// types to it via mlirTypeConverterConversionResultsAppend.
655
656/// Append a converted result type to the given 1:N conversion result
657/// accumulator.
659 MlirTypeConverterConversionResults results, MlirType type);
660
661/// Callback type for 1:N type conversion functions. For the given `type`, the
662/// callback appends zero or more converted result types to `results` (via
663/// mlirTypeConverterConversionResultsAppend) and returns a status. On
664/// MlirTypeConverterConversionStatusSuccess the appended types make up the
665/// conversion: appending a single type is a 1:1 conversion, appending several
666/// is a 1:N conversion, and appending none erases the type. Returning
667/// MlirTypeConverterConversionStatusDeclined lets another conversion function
668/// be tried; MlirTypeConverterConversionStatusFailure fails the conversion
669/// without trying another. Any types appended before a non-success status are
670/// discarded.
673 MlirType type, MlirTypeConverterConversionResults results, void *userData);
674
675/// Add a 1:N type conversion function to the given TypeConverter.
677 MlirTypeConverter typeConverter,
678 MlirTypeConverter1ToNConversionCallback convertType, void *userData);
679
680/// Convert the given type using the given TypeConverter. This is the 1:1
681/// convenience form: it returns the single converted type, or a null MlirType
682/// on failure or if the type converts to anything other than exactly one type
683/// (e.g. a 1:N conversion registered via mlirTypeConverterAdd1ToNConversion, or
684/// an erasure to zero types).
685MLIR_CAPI_EXPORTED MlirType
686mlirTypeConverterConvertType(MlirTypeConverter typeConverter, MlirType type);
687
688/// Callback type for source materializations. Given a builder (passed as a
689/// rewriter), the desired output type, the input values, and a location, the
690/// callback must build a cast-like operation that produces a single value of
691/// `outputType` and return it. Returning a null MlirValue indicates failure, in
692/// which case another registered materialization may be attempted.
694 MlirRewriterBase rewriter, MlirType outputType, intptr_t nInputs,
695 MlirValue *inputs, MlirLocation loc, void *userData);
696
697/// Callback type for 1:1 target materializations. Behaves like
698/// MlirTypeConverterSourceMaterializationCallback, but additionally receives
699/// `originalType`: the original type of the SSA value being materialized.
700///
701/// Note: This callback is single-output. For the 1:N (multiple-output) form,
702/// use MlirTypeConverter1ToNTargetMaterializationCallback.
704 MlirRewriterBase rewriter, MlirType outputType, intptr_t nInputs,
705 MlirValue *inputs, MlirLocation loc, MlirType originalType, void *userData);
706
707/// Register a source materialization with the given TypeConverter. This is
708/// invoked when a replacement value must be converted back to its original
709/// source type because some uses persist beyond the main conversion.
711 MlirTypeConverter typeConverter,
712 MlirTypeConverterSourceMaterializationCallback callback, void *userData);
713
714/// Register a target materialization with the given TypeConverter. This is
715/// invoked when a value must be converted to a target type according to a
716/// pattern's type converter.
718 MlirTypeConverter typeConverter,
719 MlirTypeConverterTargetMaterializationCallback callback, void *userData);
720
721/// Callback type for 1:N target materializations. Like
722/// MlirTypeConverterTargetMaterializationCallback, but produces a value for
723/// each of the `nOutputTypes` requested output types instead of a single value.
724/// On success the callback must fill `outputs` -- a caller-allocated array of
725/// length `nOutputTypes` -- with that many non-null values; succeeding while
726/// leaving any entry null asserts. Returning failure signals that this
727/// materialization declined (so another may be attempted); in that case
728/// `outputs` is ignored. `originalType` carries the original type of the value
729/// being materialized and may be a null MlirType.
731 MlirRewriterBase rewriter, intptr_t nOutputTypes, MlirType *outputTypes,
732 intptr_t nInputs, MlirValue *inputs, MlirLocation loc,
733 MlirType originalType, MlirValue *outputs, void *userData);
734
735/// Register a 1:N target materialization with the given TypeConverter.
737 MlirTypeConverter typeConverter,
739 void *userData);
740
741//===----------------------------------------------------------------------===//
742/// ConversionPattern API
743//===----------------------------------------------------------------------===//
744
745typedef struct {
746 /// Optional constructor for the user data.
747 /// Set to nullptr to disable it.
748 void (*construct)(void *userData);
749 /// Optional destructor for the user data.
750 /// Set to nullptr to disable it.
751 void (*destruct)(void *userData);
752 /// The callback function to match against code rooted at the specified
753 /// operation, and perform the conversion rewrite if the match is successful,
754 /// corresponding to ConversionPattern::matchAndRewrite.
755 MlirLogicalResult (*matchAndRewrite)(MlirConversionPattern pattern,
756 MlirOperation op, intptr_t nOperands,
757 MlirValue *operands,
758 MlirConversionPatternRewriter rewriter,
759 void *userData);
760 /// Optional callback corresponding to the 1:N
761 /// ConversionPattern::matchAndRewrite(Operation *, ArrayRef<ValueRange>, ...)
762 /// overload, used when one or more operands are remapped to several values
763 /// (e.g. under a 1:N type conversion). `operands` is the flat concatenation
764 /// of all operand ranges; there are `nRanges` ranges (one per original
765 /// operand) and `rangeSizes[i]` is the number of values in the i-th range.
766 /// When this is non-null it takes precedence; when null, the driver falls
767 /// back to the 1:1 `matchAndRewrite` above.
769 MlirConversionPattern pattern, MlirOperation op, intptr_t nRanges,
770 intptr_t *rangeSizes, intptr_t nOperands, MlirValue *operands,
771 MlirConversionPatternRewriter rewriter, void *userData);
773
774/// Create a conversion pattern that matches the operation with the given
775/// rootName, corresponding to mlir::OpConversionPattern.
777 MlirStringRef rootName, unsigned benefit, MlirContext context,
778 MlirTypeConverter typeConverter, MlirConversionPatternCallbacks callbacks,
779 void *userData, size_t nGeneratedNames, MlirStringRef *generatedNames);
780
781/// Get the type converter used by this conversion pattern.
782MLIR_CAPI_EXPORTED MlirTypeConverter
783mlirConversionPatternGetTypeConverter(MlirConversionPattern pattern);
784
785/// Cast the ConversionPattern to a RewritePattern.
786MLIR_CAPI_EXPORTED MlirRewritePattern
787mlirConversionPatternAsRewritePattern(MlirConversionPattern pattern);
788
789//===----------------------------------------------------------------------===//
790/// RewritePattern API
791//===----------------------------------------------------------------------===//
792
793/// Callbacks to construct a rewrite pattern.
794typedef struct {
795 /// Optional constructor for the user data.
796 /// Set to nullptr to disable it.
797 void (*construct)(void *userData);
798 /// Optional destructor for the user data.
799 /// Set to nullptr to disable it.
800 void (*destruct)(void *userData);
801 /// The callback function to match against code rooted at the specified
802 /// operation, and perform the rewrite if the match is successful,
803 /// corresponding to RewritePattern::matchAndRewrite.
804 MlirLogicalResult (*matchAndRewrite)(MlirRewritePattern pattern,
805 MlirOperation op,
806 MlirPatternRewriter rewriter,
807 void *userData);
809
810/// Create a rewrite pattern that matches the operation
811/// with the given rootName, corresponding to mlir::OpRewritePattern.
813 MlirStringRef rootName, unsigned benefit, MlirContext context,
814 MlirRewritePatternCallbacks callbacks, void *userData,
815 size_t nGeneratedNames, MlirStringRef *generatedNames);
816
817//===----------------------------------------------------------------------===//
818/// RewritePatternSet API
819//===----------------------------------------------------------------------===//
820
821/// Create an empty MlirRewritePatternSet.
822MLIR_CAPI_EXPORTED MlirRewritePatternSet
823mlirRewritePatternSetCreate(MlirContext context);
824
825/// Get the context associated with a MlirRewritePatternSet.
826MLIR_CAPI_EXPORTED MlirContext
827mlirRewritePatternSetGetContext(MlirRewritePatternSet set);
828
829/// Destruct the given MlirRewritePatternSet.
830MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set);
831
832/// Add the given MlirRewritePattern into a MlirRewritePatternSet.
833/// Note that the ownership of the pattern is transferred to the set after this
834/// call.
835MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set,
836 MlirRewritePattern pattern);
837
838//===----------------------------------------------------------------------===//
839/// PDLPatternModule API
840//===----------------------------------------------------------------------===//
841
842#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
843DEFINE_C_API_STRUCT(MlirPDLPatternModule, void);
844DEFINE_C_API_STRUCT(MlirPDLValue, const void);
845DEFINE_C_API_STRUCT(MlirPDLResultList, void);
846
847MLIR_CAPI_EXPORTED MlirPDLPatternModule
848mlirPDLPatternModuleFromModule(MlirModule op);
849
850MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op);
851
852MLIR_CAPI_EXPORTED MlirRewritePatternSet
853mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op);
854
855/// Cast the MlirPDLValue to an MlirValue.
856/// Return a null value if the cast fails, just like llvm::dyn_cast.
857MLIR_CAPI_EXPORTED MlirValue mlirPDLValueAsValue(MlirPDLValue value);
858
859/// Cast the MlirPDLValue to an MlirType.
860/// Return a null value if the cast fails, just like llvm::dyn_cast.
861MLIR_CAPI_EXPORTED MlirType mlirPDLValueAsType(MlirPDLValue value);
862
863/// Cast the MlirPDLValue to an MlirOperation.
864/// Return a null value if the cast fails, just like llvm::dyn_cast.
865MLIR_CAPI_EXPORTED MlirOperation mlirPDLValueAsOperation(MlirPDLValue value);
866
867/// Cast the MlirPDLValue to an MlirAttribute.
868/// Return a null value if the cast fails, just like llvm::dyn_cast.
869MLIR_CAPI_EXPORTED MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value);
870
871/// Push the MlirValue into the given MlirPDLResultList.
873mlirPDLResultListPushBackValue(MlirPDLResultList results, MlirValue value);
874
875/// Push the MlirType into the given MlirPDLResultList.
876MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackType(MlirPDLResultList results,
877 MlirType value);
878
879/// Push the MlirOperation into the given MlirPDLResultList.
881mlirPDLResultListPushBackOperation(MlirPDLResultList results,
882 MlirOperation value);
883
884/// Push the MlirAttribute into the given MlirPDLResultList.
886mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
887 MlirAttribute value);
888
889/// This function type is used as callbacks for PDL native rewrite functions.
890/// Input values can be accessed by `values` with its size `nValues`;
891/// output values can be added into `results` by `mlirPDLResultListPushBack*`
892/// APIs. And the return value indicates whether the rewrite succeeds.
893typedef MlirLogicalResult (*MlirPDLRewriteFunction)(
894 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
895 MlirPDLValue *values, void *userData);
896
897/// Register a rewrite function into the given PDL pattern module.
898/// `userData` will be provided as an argument to the rewrite function.
899MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterRewriteFunction(
900 MlirPDLPatternModule pdlModule, MlirStringRef name,
901 MlirPDLRewriteFunction rewriteFn, void *userData);
902
903/// This function type is used as callbacks for PDL native constraint functions.
904/// Input values can be accessed by `values` with its size `nValues`;
905/// output values can be added into `results` by `mlirPDLResultListPushBack*`
906/// APIs. And the return value indicates whether the constraint holds.
907typedef MlirLogicalResult (*MlirPDLConstraintFunction)(
908 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
909 MlirPDLValue *values, void *userData);
910
911/// Register a constraint function into the given PDL pattern module.
912/// `userData` will be provided as an argument to the constraint function.
913MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterConstraintFunction(
914 MlirPDLPatternModule pdlModule, MlirStringRef name,
915 MlirPDLConstraintFunction constraintFn, void *userData);
916
917#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
918
919#undef DEFINE_C_API_STRUCT
920
921#ifdef __cplusplus
922}
923#endif
924
925#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:222
MLIR_CAPI_EXPORTED void mlirConversionTargetDestroy(MlirConversionTarget target)
Destroy the given ConversionTarget.
Definition Rewrite.cpp:597
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:232
MLIR_CAPI_EXPORTED MlirContext mlirRewritePatternSetGetContext(MlirRewritePatternSet set)
Get the context associated with a MlirRewritePatternSet.
Definition Rewrite.cpp:1011
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:171
MlirTypeConverterConversionStatus(* MlirTypeConverterConversionCallback)(MlirType type, MlirType *convertedType, void *userData)
Callback type for type conversion functions.
Definition Rewrite.h:639
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block)
Erases a block along with all operations inside it.
Definition Rewrite.cpp:189
MlirLogicalResult(* MlirTypeConverter1ToNTargetMaterializationCallback)(MlirRewriterBase rewriter, intptr_t nOutputTypes, MlirType *outputTypes, intptr_t nInputs, MlirValue *inputs, MlirLocation loc, MlirType originalType, MlirValue *outputs, void *userData)
Callback type for 1:N target materializations.
Definition Rewrite.h:730
MLIR_CAPI_EXPORTED void mlirTypeConverterAdd1ToNConversion(MlirTypeConverter typeConverter, MlirTypeConverter1ToNConversionCallback convertType, void *userData)
Add a 1:N type conversion function to the given TypeConverter.
Definition Rewrite.cpp:724
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:46
MLIR_CAPI_EXPORTED void mlirConversionTargetMarkUnknownOpDynamicallyLegal(MlirConversionTarget target, MlirConversionTargetDynamicLegalityCallback callback, void *userData)
Mark unknown operations as dynamically legal, with a callback.
Definition Rewrite.cpp:673
MLIR_CAPI_EXPORTED void mlirConversionTargetMarkOpRecursivelyLegal(MlirConversionTarget target, MlirStringRef opName, MlirConversionTargetDynamicLegalityCallback callback, void *userData)
Mark the given operation as recursively legal.
Definition Rewrite.cpp:662
MlirDialectConversionFoldingMode
Definition Rewrite.h:476
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_AFTER_PATTERNS
Definition Rewrite.h:479
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_BEFORE_PATTERNS
Definition Rewrite.h:478
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_NEVER
Definition Rewrite.h:477
MLIR_CAPI_EXPORTED MlirRewritePattern mlirConversionPatternAsRewritePattern(MlirConversionPattern pattern)
Cast the ConversionPattern to a RewritePattern.
Definition Rewrite.cpp:948
MLIR_CAPI_EXPORTED void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method cancels a pending in-place modification.
Definition Rewrite.cpp:237
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableConstantCSE(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables constant CSE.
Definition Rewrite.cpp:400
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:51
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPartialConversion(MlirOperation op, MlirConversionTarget target, MlirFrozenRewritePatternSet patterns, MlirConversionConfig config)
Apply a partial conversion on the given operation.
Definition Rewrite.cpp:482
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:273
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter)
Returns the operation right after the current insertion point of the rewriter.
Definition Rewrite.cpp:80
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllOpUsesWithOperation(MlirRewriterBase rewriter, MlirOperation from, MlirOperation to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:267
MLIR_CAPI_EXPORTED MlirGreedySimplifyRegionLevel mlirGreedyRewriteDriverConfigGetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config)
Gets the region simplification level.
Definition Rewrite.cpp:440
MLIR_CAPI_EXPORTED MlirDialectConversionFoldingMode mlirConversionConfigGetFoldingMode(MlirConversionConfig config)
Get the folding mode for the given ConversionConfig.
Definition Rewrite.cpp:527
MLIR_CAPI_EXPORTED bool mlirConversionConfigIsBuildMaterializationsEnabled(MlirConversionConfig config)
Check if building materializations during conversion is enabled.
Definition Rewrite.cpp:543
MLIR_CAPI_EXPORTED MlirRewriterBaseInsertPoint mlirRewriterBaseSaveInsertionPoint(MlirRewriterBase rewriter)
Returns the current insertion point of the rewriter so that it can be restored later with mlirRewrite...
Definition Rewrite.cpp:91
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyFullConversion(MlirOperation op, MlirConversionTarget target, MlirFrozenRewritePatternSet patterns, MlirConversionConfig config)
Apply a full conversion on the given operation.
Definition Rewrite.cpp:489
MlirValue(* MlirTypeConverterSourceMaterializationCallback)(MlirRewriterBase rewriter, MlirType outputType, intptr_t nInputs, MlirValue *inputs, MlirLocation loc, void *userData)
Callback type for source materializations.
Definition Rewrite.h:693
MLIR_CAPI_EXPORTED void mlirTypeConverterAddTargetMaterialization(MlirTypeConverter typeConverter, MlirTypeConverterTargetMaterializationCallback callback, void *userData)
Register a target materialization with the given TypeConverter.
Definition Rewrite.cpp:844
MLIR_CAPI_EXPORTED void mlirTypeConverterAddSourceMaterialization(MlirTypeConverter typeConverter, MlirTypeConverterSourceMaterializationCallback callback, void *userData)
Register a source materialization with the given TypeConverter.
Definition Rewrite.cpp:835
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsFoldingEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether folding is enabled during greedy rewriting.
Definition Rewrite.cpp:420
MLIR_CAPI_EXPORTED void mlirTypeConverterConversionResultsAppend(MlirTypeConverterConversionResults results, MlirType type)
Append a converted result type to the given 1:N conversion result accumulator.
Definition Rewrite.cpp:719
MLIR_CAPI_EXPORTED void mlirConversionConfigEnableBuildMaterializations(MlirConversionConfig config, bool enable)
Enable or disable building materializations during conversion.
Definition Rewrite.cpp:538
MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig mlirGreedyRewriteDriverConfigCreate(void)
GreedyRewriteDriverConfig API.
Definition Rewrite.cpp:336
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsConstantCSEEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether constant CSE is enabled.
Definition Rewrite.cpp:455
MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set, MlirRewritePattern pattern)
Add the given MlirRewritePattern into a MlirRewritePatternSet.
Definition Rewrite.cpp:1019
MLIR_CAPI_EXPORTED void mlirConversionTargetAddIllegalDialect(MlirConversionTarget target, MlirStringRef dialectName)
Register the operations of the given dialect as illegal.
Definition Rewrite.cpp:618
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of the specified operation.
Definition Rewrite.cpp:140
#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:476
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level)
Sets the region simplification level.
Definition Rewrite.cpp:383
MLIR_CAPI_EXPORTED MlirTypeConverter mlirTypeConverterCreate(void)
TypeConverter API.
Definition Rewrite.cpp:685
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition Rewrite.cpp:133
MlirTypeConverterConversionStatus
Outcome of a type conversion callback.
Definition Rewrite.h:621
@ MlirTypeConverterConversionStatusFailure
The conversion failed; no further conversion function will be tried.
Definition Rewrite.h:625
@ MlirTypeConverterConversionStatusDeclined
The conversion was declined; another registered conversion function may be tried.
Definition Rewrite.h:628
@ MlirTypeConverterConversionStatusSuccess
The type was converted successfully.
Definition Rewrite.h:623
MLIR_CAPI_EXPORTED void mlirConversionTargetAddLegalOp(MlirConversionTarget target, MlirStringRef opName)
Register the given operations as legal.
Definition Rewrite.cpp:601
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:247
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig config)
Definition Rewrite.cpp:461
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config)
Destroys a greedy rewrite driver configuration.
Definition Rewrite.cpp:340
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:355
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition Rewrite.cpp:185
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxIterations(MlirGreedyRewriteDriverConfig config, int64_t maxIterations)
Sets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:345
MLIR_CAPI_EXPORTED MlirTypeConverter mlirConversionPatternGetTypeConverter(MlirConversionPattern pattern)
Get the type converter used by this conversion pattern.
Definition Rewrite.cpp:943
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:193
MLIR_CAPI_EXPORTED MlirPatternRewriter mlirConversionPatternRewriterAsPatternRewriter(MlirConversionPatternRewriter rewriter)
ConversionPatternRewriter API.
Definition Rewrite.cpp:560
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseCloneWithMapping(MlirRewriterBase rewriter, MlirOperation op, MlirIRMapping mapping)
Clones the given operation using the rewriter and the provided IRMapping.
Definition Rewrite.cpp:150
MLIR_CAPI_EXPORTED MlirConversionTarget mlirConversionTargetCreate(MlirContext context)
ConversionTarget API.
Definition Rewrite.cpp:593
MlirConversionTargetLegality
Result of a dynamic legality callback.
Definition Rewrite.h:565
@ MLIR_CONVERSION_TARGET_LEGALITY_LEGAL
The operation instance is legal.
Definition Rewrite.h:567
@ MLIR_CONVERSION_TARGET_LEGALITY_NO_OPINION
The callback has no opinion on this instance.
Definition Rewrite.h:573
@ MLIR_CONVERSION_TARGET_LEGALITY_ILLEGAL
The operation instance is illegal.
Definition Rewrite.h:569
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:989
MLIR_CAPI_EXPORTED void mlirConversionTargetAddIllegalOp(MlirConversionTarget target, MlirStringRef opName)
Register the given operations as illegal.
Definition Rewrite.cpp:607
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op)
Create an IRRewriter and transfer ownership to the caller.
Definition Rewrite.cpp:299
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:227
MLIR_CAPI_EXPORTED void mlirConversionConfigSetFoldingMode(MlirConversionConfig config, MlirDialectConversionFoldingMode mode)
Set the folding mode for the given ConversionConfig.
Definition Rewrite.cpp:509
MLIR_CAPI_EXPORTED MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition Rewrite.cpp:34
MlirTypeConverterConversionStatus(* MlirTypeConverter1ToNConversionCallback)(MlirType type, MlirTypeConverterConversionResults results, void *userData)
Callback type for 1:N type conversion functions.
Definition Rewrite.h:672
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:179
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxIterations(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:405
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context)
IRRewriter API.
Definition Rewrite.cpp:295
MLIR_CAPI_EXPORTED MlirGreedyRewriteStrictness mlirGreedyRewriteDriverConfigGetStrictness(MlirGreedyRewriteDriverConfig config)
Gets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:425
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition Rewrite.cpp:71
MLIR_CAPI_EXPORTED MlirType mlirTypeConverterConvertType(MlirTypeConverter typeConverter, MlirType type)
Convert the given type using the given TypeConverter.
Definition Rewrite.cpp:755
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetStrictness(MlirGreedyRewriteDriverConfig config, MlirGreedyRewriteStrictness strictness)
Sets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:365
MLIR_CAPI_EXPORTED void mlirTypeConverterAddConversion(MlirTypeConverter typeConverter, MlirTypeConverterConversionCallback convertType, void *userData)
Add a type conversion function to the given TypeConverter.
Definition Rewrite.cpp:693
MLIR_CAPI_EXPORTED void mlirTypeConverterDestroy(MlirTypeConverter typeConverter)
Destroy the given TypeConverter.
Definition Rewrite.cpp:689
MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set)
Destruct the given MlirRewritePatternSet.
Definition Rewrite.cpp:1015
MLIR_CAPI_EXPORTED void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter)
Insertion points methods.
Definition Rewrite.cpp:42
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:217
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:156
MLIR_CAPI_EXPORTED MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter)
PatternRewriter API.
Definition Rewrite.cpp:552
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition Rewrite.cpp:469
MlirValue(* MlirTypeConverterTargetMaterializationCallback)(MlirRewriterBase rewriter, MlirType outputType, intptr_t nInputs, MlirValue *inputs, MlirLocation loc, MlirType originalType, void *userData)
Callback type for 1:1 target materializations.
Definition Rewrite.h:703
MLIR_CAPI_EXPORTED void mlirTypeConverterAdd1ToNTargetMaterialization(MlirTypeConverter typeConverter, MlirTypeConverter1ToNTargetMaterializationCallback callback, void *userData)
Register a 1:N target materialization with the given TypeConverter.
Definition Rewrite.cpp:853
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigGetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config)
Gets whether top-down traversal is used for initial worklist population.
Definition Rewrite.cpp:415
MLIR_CAPI_EXPORTED MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context)
RewritePatternSet API.
Definition Rewrite.cpp:1007
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxNumRewrites(MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites)
Sets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:350
MLIR_CAPI_EXPORTED void mlirConversionConfigDestroy(MlirConversionConfig config)
Destroy the given ConversionConfig.
Definition Rewrite.cpp:505
MLIR_CAPI_EXPORTED void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
RewriterBase API.
Definition Rewrite.cpp:166
MLIR_CAPI_EXPORTED MlirConversionConfig mlirConversionConfigCreate(void)
ConversionConfig API.
Definition Rewrite.cpp:501
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter)
Returns the current block of the rewriter.
Definition Rewrite.cpp:75
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the end of the specified block.
Definition Rewrite.cpp:66
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:284
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables folding during greedy rewriting.
Definition Rewrite.cpp:360
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:258
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:212
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:204
MLIR_CAPI_EXPORTED void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set)
Destroy the given MlirFrozenRewritePatternSet.
Definition Rewrite.cpp:318
MlirConversionTargetLegality(* MlirConversionTargetDynamicLegalityCallback)(MlirOperation op, void *userData)
Callback for dynamic legality checks.
Definition Rewrite.h:579
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter, MlirValue value)
Sets the insertion point to the node after the specified value.
Definition Rewrite.cpp:56
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:145
MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet set)
FrozenRewritePatternSet API.
Definition Rewrite.cpp:312
MLIR_CAPI_EXPORTED void mlirConversionTargetAddDynamicallyLegalDialect(MlirConversionTarget target, MlirStringRef dialectName, MlirConversionTargetDynamicLegalityCallback callback, void *userData)
Register the given dialect as dynamically legal, with a callback to determine per-instance legality f...
Definition Rewrite.cpp:654
MLIR_CAPI_EXPORTED void mlirConversionPatternRewriterReplaceOpWithMultiple(MlirConversionPatternRewriter rewriter, MlirOperation op, intptr_t nRanges, intptr_t *rangeSizes, MlirValue *values)
Replace the given operation with multiple value ranges – one range per result of op – and erase it.
Definition Rewrite.cpp:572
MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter)
Takes an IRRewriter owned by the caller and destroys it.
Definition Rewrite.cpp:303
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:120
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from, MlirValue to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:242
MLIR_CAPI_EXPORTED MlirLogicalResult mlirConversionPatternRewriterConvertRegionTypes(MlirConversionPatternRewriter rewriter, MlirRegion region, MlirTypeConverter typeConverter)
Apply a signature conversion to each block in the given region.
Definition Rewrite.cpp:565
MLIR_CAPI_EXPORTED MlirConversionPattern mlirOpConversionPatternCreate(MlirStringRef rootName, unsigned benefit, MlirContext context, MlirTypeConverter typeConverter, MlirConversionPatternCallbacks callbacks, void *userData, size_t nGeneratedNames, MlirStringRef *generatedNames)
Create a conversion pattern that matches the operation with the given rootName, corresponding to mlir...
Definition Rewrite.cpp:929
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 mlirConversionTargetAddDynamicallyLegalOp(MlirConversionTarget target, MlirStringRef opName, MlirConversionTargetDynamicLegalityCallback callback, void *userData)
Register the given operation as dynamically legal, with a callback to determine per-instance legality...
Definition Rewrite.cpp:644
MLIR_CAPI_EXPORTED void mlirConversionTargetAddLegalDialect(MlirConversionTarget target, MlirStringRef dialectName)
Register the operations of the given dialect as legal.
Definition Rewrite.cpp:613
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the start of the specified block.
Definition Rewrite.cpp:61
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxNumRewrites(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:410
MLIR_CAPI_EXPORTED void mlirRewriterBaseRestoreInsertionPoint(MlirRewriterBase rewriter, MlirRewriterBaseInsertPoint insertPoint)
Restores a previously saved insertion point.
Definition Rewrite.cpp:102
struct MlirLogicalResult MlirLogicalResult
Definition Support.h:124
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
ConversionPattern API.
Definition Rewrite.h:745
MlirLogicalResult(* matchAndRewrite)(MlirConversionPattern pattern, MlirOperation op, intptr_t nOperands, MlirValue *operands, MlirConversionPatternRewriter rewriter, void *userData)
The callback function to match against code rooted at the specified operation, and perform the conver...
Definition Rewrite.h:755
MlirLogicalResult(* matchAndRewrite1ToN)(MlirConversionPattern pattern, MlirOperation op, intptr_t nRanges, intptr_t *rangeSizes, intptr_t nOperands, MlirValue *operands, MlirConversionPatternRewriter rewriter, void *userData)
Optional callback corresponding to the 1:N ConversionPattern::matchAndRewrite(Operation *,...
Definition Rewrite.h:768
void(* construct)(void *userData)
Optional constructor for the user data.
Definition Rewrite.h:748
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition Rewrite.h:751
A logical result value, essentially a boolean with named states.
Definition Support.h:121
RewritePattern API.
Definition Rewrite.h:794
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:804
void(* construct)(void *userData)
Optional constructor for the user data.
Definition Rewrite.h:797
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition Rewrite.h:800
A saved insertion point: a (block, operationAfter) pair.
Definition Rewrite.h:140
MlirOperation operationAfter
Definition Rewrite.h:142
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
Opaque accumulator for the result types of a 1:N type conversion.
Definition Rewrite.h:652