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
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. Similarly for methods using
82// `InsertPoint` directly.
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//===----------------------------------------------------------------------===//
137/// Block and operation creation/insertion/cloning
138//===----------------------------------------------------------------------===//
139
140// These functions do not include the IRMapper, as it is not yet exposed by the
141// C API.
142
143/// Add new block with 'argTypes' arguments and set the insertion point to the
144/// end of it. The block is placed before 'insertBefore'. `locs` contains the
145/// locations of the inserted arguments, and should match the size of
146/// `argTypes`.
148 MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes,
149 MlirType const *argTypes, MlirLocation const *locations);
150
151/// Insert the given operation at the current insertion point and return it.
152MLIR_CAPI_EXPORTED MlirOperation
153mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op);
154
155/// Creates a deep copy of the specified operation.
156MLIR_CAPI_EXPORTED MlirOperation
157mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op);
158
159/// Creates a deep copy of this operation but keep the operation regions
160/// empty.
162 MlirRewriterBase rewriter, MlirOperation op);
163
164/// Clone the blocks that belong to "region" before the given position in
165/// another region "parent".
167mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
168 MlirBlock before);
169
170//===----------------------------------------------------------------------===//
171/// RewriterBase API
172//===----------------------------------------------------------------------===//
173
174/// Move the blocks that belong to "region" before the given position in
175/// another region "parent". The two regions must be different. The caller
176/// is responsible for creating or updating the operation transferring flow
177/// of control to the region and passing it the correct block arguments.
179mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
180 MlirBlock before);
181
182/// Replace the results of the given (original) operation with the specified
183/// list of values (replacements). The result types of the given op and the
184/// replacements must match. The original op is erased.
187 intptr_t nValues, MlirValue const *values);
188
189/// Replace the results of the given (original) operation with the specified
190/// new op (replacement). The result types of the two ops must match. The
191/// original op is erased.
194 MlirOperation op, MlirOperation newOp);
195
196/// Erases an operation that is known to have no uses.
198 MlirOperation op);
199
200/// Erases a block along with all operations inside it.
202 MlirBlock block);
203
204/// Inline the operations of block 'source' before the operation 'op'. The
205/// source block will be deleted and must have no uses. 'argValues' is used to
206/// replace the block arguments of 'source'
207///
208/// The source block must have no successors. Otherwise, the resulting IR
209/// would have unreachable operations.
211mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source,
212 MlirOperation op, intptr_t nArgValues,
213 MlirValue const *argValues);
214
215/// Inline the operations of block 'source' into the end of block 'dest'. The
216/// source block will be deleted and must have no uses. 'argValues' is used to
217/// replace the block arguments of 'source'
218///
219/// The dest block must have no successors. Otherwise, the resulting IR would
220/// have unreachable operation.
222 MlirBlock source,
223 MlirBlock dest,
224 intptr_t nArgValues,
225 MlirValue const *argValues);
226
227/// Unlink this operation from its current block and insert it right before
228/// `existingOp` which may be in the same or another block in the same
229/// function.
231 MlirOperation op,
232 MlirOperation existingOp);
233
234/// Unlink this operation from its current block and insert it right after
235/// `existingOp` which may be in the same or another block in the same
236/// function.
238 MlirOperation op,
239 MlirOperation existingOp);
240
241/// Unlink this block and insert it right before `existingBlock`.
243mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block,
244 MlirBlock existingBlock);
245
246/// This method is used to notify the rewriter that an in-place operation
247/// modification is about to happen. A call to this function *must* be
248/// followed by a call to either `finalizeOpModification` or
249/// `cancelOpModification`. This is a minor efficiency win (it avoids creating
250/// a new operation and removing the old one) but also often allows simpler
251/// code in the client.
254 MlirOperation op);
255
256/// This method is used to signal the end of an in-place modification of the
257/// given operation. This can only be called on operations that were provided
258/// to a call to `startOpModification`.
261 MlirOperation op);
262
263/// This method cancels a pending in-place modification. This can only be
264/// called on operations that were provided to a call to
265/// `startOpModification`.
268 MlirOperation op);
269
270/// Find uses of `from` and replace them with `to`. Also notify the listener
271/// about every in-place op modification (for every use that was replaced).
274 MlirValue to);
275
276/// Find uses of `from` and replace them with `to`. Also notify the listener
277/// about every in-place op modification (for every use that was replaced).
279 MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from,
280 MlirValue const *to);
281
282/// Find uses of `from` and replace them with `to`. Also notify the listener
283/// about every in-place op modification (for every use that was replaced)
284/// and that the `from` operation is about to be replaced.
287 MlirOperation from, intptr_t nTo,
288 MlirValue const *to);
289
290/// Find uses of `from` and replace them with `to`. Also notify the listener
291/// about every in-place op modification (for every use that was replaced)
292/// and that the `from` operation is about to be replaced.
294 MlirRewriterBase rewriter, MlirOperation from, MlirOperation to);
295
296/// Find uses of `from` within `block` and replace them with `to`. Also notify
297/// the listener about every in-place op modification (for every use that was
298/// replaced). The optional `allUsesReplaced` flag is set to "true" if all
299/// uses were replaced.
301 MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues,
302 MlirValue const *newValues, MlirBlock block);
303
304/// Find uses of `from` and replace them with `to` except if the user is
305/// `exceptedUser`. Also notify the listener about every in-place op
306/// modification (for every use that was replaced).
309 MlirValue to, MlirOperation exceptedUser);
310
311//===----------------------------------------------------------------------===//
312/// IRRewriter API
313//===----------------------------------------------------------------------===//
314
315/// Create an IRRewriter and transfer ownership to the caller.
317
318/// Create an IRRewriter and transfer ownership to the caller. Additionally
319/// set the insertion point before the operation.
321mlirIRRewriterCreateFromOp(MlirOperation op);
322
323/// Takes an IRRewriter owned by the caller and destroys it. It is the
324/// responsibility of the user to only pass an IRRewriter class.
326
327//===----------------------------------------------------------------------===//
328/// FrozenRewritePatternSet API
329//===----------------------------------------------------------------------===//
330
331/// Freeze the given MlirRewritePatternSet to a MlirFrozenRewritePatternSet.
332/// Note that the ownership of the input set is transferred into the frozen set
333/// after this call.
334MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet
335mlirFreezeRewritePattern(MlirRewritePatternSet set);
336
337/// Destroy the given MlirFrozenRewritePatternSet.
339mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set);
340
342 MlirOperation op, MlirFrozenRewritePatternSet patterns,
343 MlirGreedyRewriteDriverConfig);
344
346 MlirModule op, MlirFrozenRewritePatternSet patterns,
347 MlirGreedyRewriteDriverConfig config);
348
349//===----------------------------------------------------------------------===//
350/// GreedyRewriteDriverConfig API
351//===----------------------------------------------------------------------===//
352
353/// Creates a greedy rewrite driver configuration with default settings.
354MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig
356
357/// Destroys a greedy rewrite driver configuration.
359mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config);
360
361/// Sets the maximum number of iterations for the greedy rewrite driver.
362/// Use -1 for no limit.
364 MlirGreedyRewriteDriverConfig config, int64_t maxIterations);
365
366/// Sets the maximum number of rewrites within an iteration.
367/// Use -1 for no limit.
369 MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites);
370
371/// Sets whether to use top-down traversal for the initial population of the
372/// worklist.
374 MlirGreedyRewriteDriverConfig config, bool useTopDownTraversal);
375
376/// Enables or disables folding during greedy rewriting.
378mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config,
379 bool enable);
380
381/// Sets the strictness level for the greedy rewrite driver.
383 MlirGreedyRewriteDriverConfig config,
384 MlirGreedyRewriteStrictness strictness);
385
386/// Sets the region simplification level.
389 MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level);
390
391/// Enables or disables constant CSE.
393 MlirGreedyRewriteDriverConfig config, bool enable);
394
395/// Gets the maximum number of iterations for the greedy rewrite driver.
397 MlirGreedyRewriteDriverConfig config);
398
399/// Gets the maximum number of rewrites within an iteration.
401 MlirGreedyRewriteDriverConfig config);
402
403/// Gets whether top-down traversal is used for initial worklist population.
405 MlirGreedyRewriteDriverConfig config);
406
407/// Gets whether folding is enabled during greedy rewriting.
409 MlirGreedyRewriteDriverConfig config);
410
411/// Gets the strictness level for the greedy rewrite driver.
414 MlirGreedyRewriteDriverConfig config);
415
416/// Gets the region simplification level.
419 MlirGreedyRewriteDriverConfig config);
420
421/// Gets whether constant CSE is enabled.
423 MlirGreedyRewriteDriverConfig config);
424
425/// Applies the given patterns to the given op by a fast walk-based pattern
426/// rewrite driver.
428mlirWalkAndApplyPatterns(MlirOperation op,
429 MlirFrozenRewritePatternSet patterns);
430
431/// Apply a partial conversion on the given operation.
433 MlirOperation op, MlirConversionTarget target,
434 MlirFrozenRewritePatternSet patterns, MlirConversionConfig config);
435
436/// Apply a full conversion on the given operation.
438 MlirOperation op, MlirConversionTarget target,
439 MlirFrozenRewritePatternSet patterns, MlirConversionConfig config);
440
441//===----------------------------------------------------------------------===//
442/// ConversionConfig API
443//===----------------------------------------------------------------------===//
444
445/// Create a default ConversionConfig.
446MLIR_CAPI_EXPORTED MlirConversionConfig mlirConversionConfigCreate(void);
447
448/// Destroy the given ConversionConfig.
450mlirConversionConfigDestroy(MlirConversionConfig config);
451
457
458/// Set the folding mode for the given ConversionConfig.
460mlirConversionConfigSetFoldingMode(MlirConversionConfig config,
462
463/// Get the folding mode for the given ConversionConfig.
465mlirConversionConfigGetFoldingMode(MlirConversionConfig config);
466
467/// Enable or disable building materializations during conversion.
469mlirConversionConfigEnableBuildMaterializations(MlirConversionConfig config,
470 bool enable);
471
472/// Check if building materializations during conversion is enabled.
474mlirConversionConfigIsBuildMaterializationsEnabled(MlirConversionConfig config);
475
476//===----------------------------------------------------------------------===//
477/// PatternRewriter API
478//===----------------------------------------------------------------------===//
479
480/// Cast the PatternRewriter to a RewriterBase
482mlirPatternRewriterAsBase(MlirPatternRewriter rewriter);
483
484//===----------------------------------------------------------------------===//
485/// ConversionPatternRewriter API
486//===----------------------------------------------------------------------===//
487
488/// Cast the ConversionPatternRewriter to a PatternRewriter
489MLIR_CAPI_EXPORTED MlirPatternRewriter
491 MlirConversionPatternRewriter rewriter);
492
493//===----------------------------------------------------------------------===//
494/// ConversionTarget API
495//===----------------------------------------------------------------------===//
496
497/// Create an empty ConversionTarget.
498MLIR_CAPI_EXPORTED MlirConversionTarget
499mlirConversionTargetCreate(MlirContext context);
500
501/// Destroy the given ConversionTarget.
503mlirConversionTargetDestroy(MlirConversionTarget target);
504
505/// Register the given operations as legal.
507mlirConversionTargetAddLegalOp(MlirConversionTarget target,
508 MlirStringRef opName);
509
510/// Register the given operations as illegal.
512mlirConversionTargetAddIllegalOp(MlirConversionTarget target,
513 MlirStringRef opName);
514
515/// Register the operations of the given dialect as legal.
518 MlirStringRef dialectName);
519
520/// Register the operations of the given dialect as illegal.
523 MlirStringRef dialectName);
524
525//===----------------------------------------------------------------------===//
526/// TypeConverter API
527//===----------------------------------------------------------------------===//
528
529/// Create a TypeConverter.
530MLIR_CAPI_EXPORTED MlirTypeConverter mlirTypeConverterCreate(void);
531
532/// Destroy the given TypeConverter.
534mlirTypeConverterDestroy(MlirTypeConverter typeConverter);
535
536/// Callback type for type conversion functions.
537/// Returns failure or sets convertedType to MlirType{NULL} to indicate failure.
538/// If failure is returned, the converter is allowed to try another
539/// conversion function to perform the conversion.
541 MlirType type, MlirType *convertedType, void *userData);
542
543/// Add a type conversion function to the given TypeConverter.
545mlirTypeConverterAddConversion(MlirTypeConverter typeConverter,
547 void *userData);
548
549//===----------------------------------------------------------------------===//
550/// ConversionPattern API
551//===----------------------------------------------------------------------===//
552
553typedef struct {
554 /// Optional constructor for the user data.
555 /// Set to nullptr to disable it.
556 void (*construct)(void *userData);
557 /// Optional destructor for the user data.
558 /// Set to nullptr to disable it.
559 void (*destruct)(void *userData);
560 /// The callback function to match against code rooted at the specified
561 /// operation, and perform the conversion rewrite if the match is successful,
562 /// corresponding to ConversionPattern::matchAndRewrite.
563 MlirLogicalResult (*matchAndRewrite)(MlirConversionPattern pattern,
564 MlirOperation op, intptr_t nOperands,
565 MlirValue *operands,
566 MlirConversionPatternRewriter rewriter,
567 void *userData);
569
570/// Create a conversion pattern that matches the operation with the given
571/// rootName, corresponding to mlir::OpConversionPattern.
573 MlirStringRef rootName, unsigned benefit, MlirContext context,
574 MlirTypeConverter typeConverter, MlirConversionPatternCallbacks callbacks,
575 void *userData, size_t nGeneratedNames, MlirStringRef *generatedNames);
576
577/// Get the type converter used by this conversion pattern.
578MLIR_CAPI_EXPORTED MlirTypeConverter
579mlirConversionPatternGetTypeConverter(MlirConversionPattern pattern);
580
581/// Cast the ConversionPattern to a RewritePattern.
582MLIR_CAPI_EXPORTED MlirRewritePattern
583mlirConversionPatternAsRewritePattern(MlirConversionPattern pattern);
584
585//===----------------------------------------------------------------------===//
586/// RewritePattern API
587//===----------------------------------------------------------------------===//
588
589/// Callbacks to construct a rewrite pattern.
590typedef struct {
591 /// Optional constructor for the user data.
592 /// Set to nullptr to disable it.
593 void (*construct)(void *userData);
594 /// Optional destructor for the user data.
595 /// Set to nullptr to disable it.
596 void (*destruct)(void *userData);
597 /// The callback function to match against code rooted at the specified
598 /// operation, and perform the rewrite if the match is successful,
599 /// corresponding to RewritePattern::matchAndRewrite.
600 MlirLogicalResult (*matchAndRewrite)(MlirRewritePattern pattern,
601 MlirOperation op,
602 MlirPatternRewriter rewriter,
603 void *userData);
605
606/// Create a rewrite pattern that matches the operation
607/// with the given rootName, corresponding to mlir::OpRewritePattern.
609 MlirStringRef rootName, unsigned benefit, MlirContext context,
610 MlirRewritePatternCallbacks callbacks, void *userData,
611 size_t nGeneratedNames, MlirStringRef *generatedNames);
612
613//===----------------------------------------------------------------------===//
614/// RewritePatternSet API
615//===----------------------------------------------------------------------===//
616
617/// Create an empty MlirRewritePatternSet.
618MLIR_CAPI_EXPORTED MlirRewritePatternSet
619mlirRewritePatternSetCreate(MlirContext context);
620
621/// Destruct the given MlirRewritePatternSet.
622MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set);
623
624/// Add the given MlirRewritePattern into a MlirRewritePatternSet.
625/// Note that the ownership of the pattern is transferred to the set after this
626/// call.
627MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set,
628 MlirRewritePattern pattern);
629
630//===----------------------------------------------------------------------===//
631/// PDLPatternModule API
632//===----------------------------------------------------------------------===//
633
634#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
635DEFINE_C_API_STRUCT(MlirPDLPatternModule, void);
636DEFINE_C_API_STRUCT(MlirPDLValue, const void);
637DEFINE_C_API_STRUCT(MlirPDLResultList, void);
638
639MLIR_CAPI_EXPORTED MlirPDLPatternModule
640mlirPDLPatternModuleFromModule(MlirModule op);
641
642MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op);
643
644MLIR_CAPI_EXPORTED MlirRewritePatternSet
645mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op);
646
647/// Cast the MlirPDLValue to an MlirValue.
648/// Return a null value if the cast fails, just like llvm::dyn_cast.
649MLIR_CAPI_EXPORTED MlirValue mlirPDLValueAsValue(MlirPDLValue value);
650
651/// Cast the MlirPDLValue to an MlirType.
652/// Return a null value if the cast fails, just like llvm::dyn_cast.
653MLIR_CAPI_EXPORTED MlirType mlirPDLValueAsType(MlirPDLValue value);
654
655/// Cast the MlirPDLValue to an MlirOperation.
656/// Return a null value if the cast fails, just like llvm::dyn_cast.
657MLIR_CAPI_EXPORTED MlirOperation mlirPDLValueAsOperation(MlirPDLValue value);
658
659/// Cast the MlirPDLValue to an MlirAttribute.
660/// Return a null value if the cast fails, just like llvm::dyn_cast.
661MLIR_CAPI_EXPORTED MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value);
662
663/// Push the MlirValue into the given MlirPDLResultList.
665mlirPDLResultListPushBackValue(MlirPDLResultList results, MlirValue value);
666
667/// Push the MlirType into the given MlirPDLResultList.
668MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackType(MlirPDLResultList results,
669 MlirType value);
670
671/// Push the MlirOperation into the given MlirPDLResultList.
673mlirPDLResultListPushBackOperation(MlirPDLResultList results,
674 MlirOperation value);
675
676/// Push the MlirAttribute into the given MlirPDLResultList.
678mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
679 MlirAttribute value);
680
681/// This function type is used as callbacks for PDL native rewrite functions.
682/// Input values can be accessed by `values` with its size `nValues`;
683/// output values can be added into `results` by `mlirPDLResultListPushBack*`
684/// APIs. And the return value indicates whether the rewrite succeeds.
685typedef MlirLogicalResult (*MlirPDLRewriteFunction)(
686 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
687 MlirPDLValue *values, void *userData);
688
689/// Register a rewrite function into the given PDL pattern module.
690/// `userData` will be provided as an argument to the rewrite function.
691MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterRewriteFunction(
692 MlirPDLPatternModule pdlModule, MlirStringRef name,
693 MlirPDLRewriteFunction rewriteFn, void *userData);
694
695/// This function type is used as callbacks for PDL native constraint functions.
696/// Input values can be accessed by `values` with its size `nValues`;
697/// output values can be added into `results` by `mlirPDLResultListPushBack*`
698/// APIs. And the return value indicates whether the constraint holds.
699typedef MlirLogicalResult (*MlirPDLConstraintFunction)(
700 MlirPatternRewriter rewriter, MlirPDLResultList results, size_t nValues,
701 MlirPDLValue *values, void *userData);
702
703/// Register a constraint function into the given PDL pattern module.
704/// `userData` will be provided as an argument to the constraint function.
705MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterConstraintFunction(
706 MlirPDLPatternModule pdlModule, MlirStringRef name,
707 MlirPDLConstraintFunction constraintFn, void *userData);
708
709#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
710
711#undef DEFINE_C_API_STRUCT
712
713#ifdef __cplusplus
714}
715#endif
716
717#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:187
MLIR_CAPI_EXPORTED void mlirConversionTargetDestroy(MlirConversionTarget target)
Destroy the given ConversionTarget.
Definition Rewrite.cpp:538
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:197
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:136
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block)
Erases a block along with all operations inside it.
Definition Rewrite.cpp:154
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:43
MlirDialectConversionFoldingMode
Definition Rewrite.h:452
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_AFTER_PATTERNS
Definition Rewrite.h:455
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_BEFORE_PATTERNS
Definition Rewrite.h:454
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_NEVER
Definition Rewrite.h:453
MLIR_CAPI_EXPORTED MlirRewritePattern mlirConversionPatternAsRewritePattern(MlirConversionPattern pattern)
Cast the ConversionPattern to a RewritePattern.
Definition Rewrite.cpp:656
MLIR_CAPI_EXPORTED void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method cancels a pending in-place modification.
Definition Rewrite.cpp:202
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableConstantCSE(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables constant CSE.
Definition Rewrite.cpp:365
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:48
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPartialConversion(MlirOperation op, MlirConversionTarget target, MlirFrozenRewritePatternSet patterns, MlirConversionConfig config)
Apply a partial conversion on the given operation.
Definition Rewrite.cpp:447
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:238
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter)
Returns the operation right after the current insertion point of the rewriter.
Definition Rewrite.cpp:77
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllOpUsesWithOperation(MlirRewriterBase rewriter, MlirOperation from, MlirOperation to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:232
MLIR_CAPI_EXPORTED MlirGreedySimplifyRegionLevel mlirGreedyRewriteDriverConfigGetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config)
Gets the region simplification level.
Definition Rewrite.cpp:405
MLIR_CAPI_EXPORTED MlirDialectConversionFoldingMode mlirConversionConfigGetFoldingMode(MlirConversionConfig config)
Get the folding mode for the given ConversionConfig.
Definition Rewrite.cpp:492
MLIR_CAPI_EXPORTED bool mlirConversionConfigIsBuildMaterializationsEnabled(MlirConversionConfig config)
Check if building materializations during conversion is enabled.
Definition Rewrite.cpp:508
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyFullConversion(MlirOperation op, MlirConversionTarget target, MlirFrozenRewritePatternSet patterns, MlirConversionConfig config)
Apply a full conversion on the given operation.
Definition Rewrite.cpp:454
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsFoldingEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether folding is enabled during greedy rewriting.
Definition Rewrite.cpp:385
MLIR_CAPI_EXPORTED void mlirConversionConfigEnableBuildMaterializations(MlirConversionConfig config, bool enable)
Enable or disable building materializations during conversion.
Definition Rewrite.cpp:503
MLIR_CAPI_EXPORTED MlirGreedyRewriteDriverConfig mlirGreedyRewriteDriverConfigCreate(void)
GreedyRewriteDriverConfig API.
Definition Rewrite.cpp:301
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigIsConstantCSEEnabled(MlirGreedyRewriteDriverConfig config)
Gets whether constant CSE is enabled.
Definition Rewrite.cpp:420
MLIR_CAPI_EXPORTED void mlirRewritePatternSetAdd(MlirRewritePatternSet set, MlirRewritePattern pattern)
Add the given MlirRewritePattern into a MlirRewritePatternSet.
Definition Rewrite.cpp:723
MLIR_CAPI_EXPORTED void mlirConversionTargetAddIllegalDialect(MlirConversionTarget target, MlirStringRef dialectName)
Register the operations of the given dialect as illegal.
Definition Rewrite.cpp:559
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of the specified operation.
Definition Rewrite.cpp:111
#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:441
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetRegionSimplificationLevel(MlirGreedyRewriteDriverConfig config, MlirGreedySimplifyRegionLevel level)
Sets the region simplification level.
Definition Rewrite.cpp:348
MLIR_CAPI_EXPORTED MlirTypeConverter mlirTypeConverterCreate(void)
TypeConverter API.
Definition Rewrite.cpp:568
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition Rewrite.cpp:104
MLIR_CAPI_EXPORTED void mlirConversionTargetAddLegalOp(MlirConversionTarget target, MlirStringRef opName)
Register the given operations as legal.
Definition Rewrite.cpp:542
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:212
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig config)
Definition Rewrite.cpp:426
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigDestroy(MlirGreedyRewriteDriverConfig config)
Destroys a greedy rewrite driver configuration.
Definition Rewrite.cpp:305
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:320
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition Rewrite.cpp:150
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxIterations(MlirGreedyRewriteDriverConfig config, int64_t maxIterations)
Sets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:310
MLIR_CAPI_EXPORTED MlirTypeConverter mlirConversionPatternGetTypeConverter(MlirConversionPattern pattern)
Get the type converter used by this conversion pattern.
Definition Rewrite.cpp:651
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:158
MLIR_CAPI_EXPORTED MlirPatternRewriter mlirConversionPatternRewriterAsPatternRewriter(MlirConversionPatternRewriter rewriter)
ConversionPatternRewriter API.
Definition Rewrite.cpp:525
MLIR_CAPI_EXPORTED MlirConversionTarget mlirConversionTargetCreate(MlirContext context)
ConversionTarget API.
Definition Rewrite.cpp:534
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:697
MLIR_CAPI_EXPORTED void mlirConversionTargetAddIllegalOp(MlirConversionTarget target, MlirStringRef opName)
Register the given operations as illegal.
Definition Rewrite.cpp:548
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op)
Create an IRRewriter and transfer ownership to the caller.
Definition Rewrite.cpp:264
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:192
MLIR_CAPI_EXPORTED void mlirConversionConfigSetFoldingMode(MlirConversionConfig config, MlirDialectConversionFoldingMode mode)
Set the folding mode for the given ConversionConfig.
Definition Rewrite.cpp:474
MLIR_CAPI_EXPORTED MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition Rewrite.cpp:31
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:144
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxIterations(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of iterations for the greedy rewrite driver.
Definition Rewrite.cpp:370
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context)
IRRewriter API.
Definition Rewrite.cpp:260
MLIR_CAPI_EXPORTED MlirGreedyRewriteStrictness mlirGreedyRewriteDriverConfigGetStrictness(MlirGreedyRewriteDriverConfig config)
Gets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:390
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition Rewrite.cpp:68
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetStrictness(MlirGreedyRewriteDriverConfig config, MlirGreedyRewriteStrictness strictness)
Sets the strictness level for the greedy rewrite driver.
Definition Rewrite.cpp:330
MLIR_CAPI_EXPORTED void mlirTypeConverterAddConversion(MlirTypeConverter typeConverter, MlirTypeConverterConversionCallback convertType, void *userData)
Add a type conversion function to the given TypeConverter.
Definition Rewrite.cpp:576
MLIR_CAPI_EXPORTED void mlirTypeConverterDestroy(MlirTypeConverter typeConverter)
Destroy the given TypeConverter.
Definition Rewrite.cpp:572
MLIR_CAPI_EXPORTED void mlirRewritePatternSetDestroy(MlirRewritePatternSet set)
Destruct the given MlirRewritePatternSet.
Definition Rewrite.cpp:719
MLIR_CAPI_EXPORTED void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter)
Insertion points methods.
Definition Rewrite.cpp:39
MlirLogicalResult(* MlirTypeConverterConversionCallback)(MlirType type, MlirType *convertedType, void *userData)
Callback type for type conversion functions.
Definition Rewrite.h:540
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:182
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:121
MLIR_CAPI_EXPORTED MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter)
PatternRewriter API.
Definition Rewrite.cpp:517
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition Rewrite.cpp:434
MLIR_CAPI_EXPORTED bool mlirGreedyRewriteDriverConfigGetUseTopDownTraversal(MlirGreedyRewriteDriverConfig config)
Gets whether top-down traversal is used for initial worklist population.
Definition Rewrite.cpp:380
MLIR_CAPI_EXPORTED MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context)
RewritePatternSet API.
Definition Rewrite.cpp:715
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigSetMaxNumRewrites(MlirGreedyRewriteDriverConfig config, int64_t maxNumRewrites)
Sets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:315
MLIR_CAPI_EXPORTED void mlirConversionConfigDestroy(MlirConversionConfig config)
Destroy the given ConversionConfig.
Definition Rewrite.cpp:470
MLIR_CAPI_EXPORTED void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
RewriterBase API.
Definition Rewrite.cpp:131
MLIR_CAPI_EXPORTED MlirConversionConfig mlirConversionConfigCreate(void)
ConversionConfig API.
Definition Rewrite.cpp:466
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter)
Returns the current block of the rewriter.
Definition Rewrite.cpp:72
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the end of the specified block.
Definition Rewrite.cpp:63
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:249
MLIR_CAPI_EXPORTED void mlirGreedyRewriteDriverConfigEnableFolding(MlirGreedyRewriteDriverConfig config, bool enable)
Enables or disables folding during greedy rewriting.
Definition Rewrite.cpp:325
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:223
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:177
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:169
MLIR_CAPI_EXPORTED void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set)
Destroy the given MlirFrozenRewritePatternSet.
Definition Rewrite.cpp:283
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter, MlirValue value)
Sets the insertion point to the node after the specified value.
Definition Rewrite.cpp:53
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:116
MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet set)
FrozenRewritePatternSet API.
Definition Rewrite.cpp:277
MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter)
Takes an IRRewriter owned by the caller and destroys it.
Definition Rewrite.cpp:268
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:91
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from, MlirValue to)
Find uses of from and replace them with to.
Definition Rewrite.cpp:207
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:637
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 mlirConversionTargetAddLegalDialect(MlirConversionTarget target, MlirStringRef dialectName)
Register the operations of the given dialect as legal.
Definition Rewrite.cpp:554
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the start of the specified block.
Definition Rewrite.cpp:58
MLIR_CAPI_EXPORTED int64_t mlirGreedyRewriteDriverConfigGetMaxNumRewrites(MlirGreedyRewriteDriverConfig config)
Gets the maximum number of rewrites within an iteration.
Definition Rewrite.cpp:375
struct MlirLogicalResult MlirLogicalResult
Definition Support.h:124
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
ConversionPattern API.
Definition Rewrite.h:553
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:563
void(* construct)(void *userData)
Optional constructor for the user data.
Definition Rewrite.h:556
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition Rewrite.h:559
A logical result value, essentially a boolean with named states.
Definition Support.h:121
RewritePattern API.
Definition Rewrite.h:590
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:600
void(* construct)(void *userData)
Optional constructor for the user data.
Definition Rewrite.h:593
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition Rewrite.h:596
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78