41 unwrap(rewriter)->clearInsertionPoint();
56 unwrap(rewriter)->setInsertionPointAfterValue(
unwrap(value));
61 unwrap(rewriter)->setInsertionPointToStart(
unwrap(block));
70 return wrap(
unwrap(rewriter)->getInsertionBlock());
82 if (it == block->
end())
85 return wrap(std::addressof(*it));
93 MlirBlock insertBefore,
95 MlirType
const *argTypes,
96 MlirLocation
const *locations) {
101 return wrap(
unwrap(rewriter)->createBlock(
unwrap(insertBefore), unwrappedArgs,
124 MlirIRMapping mapping) {
129 MlirRegion region, MlirBlock before) {
139 MlirRegion region, MlirBlock before) {
145 MlirValue
const *values) {
153 MlirOperation newOp) {
166 MlirBlock source, MlirOperation op,
168 MlirValue
const *argValues) {
177 MlirBlock dest,
intptr_t nArgValues,
178 MlirValue
const *argValues) {
185 MlirOperation existingOp) {
190 MlirOperation existingOp) {
195 MlirBlock existingBlock) {
215 MlirValue from, MlirValue to) {
221 MlirValue
const *from,
222 MlirValue
const *to) {
227 unwrap(rewriter)->replaceAllUsesWith(unwrappedFromVals, unwrappedToVals);
233 MlirValue
const *to) {
236 unwrap(rewriter)->replaceAllOpUsesWith(
unwrap(from), unwrappedToVals);
248 MlirValue
const *newValues,
252 unwrap(rewriter)->replaceOpUsesWithinBlock(
unwrap(op), unwrappedVals,
257 MlirValue from, MlirValue to,
258 MlirOperation exceptedUser) {
283MlirFrozenRewritePatternSet
300 assert(config.ptr &&
"unexpected null config");
313 MlirGreedyRewriteDriverConfig config) {
318 MlirGreedyRewriteDriverConfig config,
int64_t maxIterations) {
323 MlirGreedyRewriteDriverConfig config,
int64_t maxNumRewrites) {
328 MlirGreedyRewriteDriverConfig config,
bool useTopDownTraversal) {
333 MlirGreedyRewriteDriverConfig config,
bool enable) {
338 MlirGreedyRewriteDriverConfig config,
341 switch (strictness) {
373 MlirGreedyRewriteDriverConfig config,
bool enable) {
378 MlirGreedyRewriteDriverConfig config) {
383 MlirGreedyRewriteDriverConfig config) {
388 MlirGreedyRewriteDriverConfig config) {
393 MlirGreedyRewriteDriverConfig config) {
398 MlirGreedyRewriteDriverConfig config) {
400 switch (cppStrictness) {
408 llvm_unreachable(
"Unknown GreedyRewriteStrictness");
413 MlirGreedyRewriteDriverConfig config) {
424 llvm_unreachable(
"Unknown GreedySimplifyRegionLevel");
428 MlirGreedyRewriteDriverConfig config) {
434 MlirFrozenRewritePatternSet patterns,
435 MlirGreedyRewriteDriverConfig config) {
442 MlirFrozenRewritePatternSet patterns,
443 MlirGreedyRewriteDriverConfig config) {
449 MlirFrozenRewritePatternSet patterns) {
455 MlirFrozenRewritePatternSet patterns,
456 MlirConversionConfig config) {
462 MlirConversionTarget
target,
463 MlirFrozenRewritePatternSet patterns,
464 MlirConversionConfig config) {
474 return wrap(
new mlir::ConversionConfig());
483 mlir::DialectConversionFoldingMode cppMode;
486 cppMode = mlir::DialectConversionFoldingMode::Never;
489 cppMode = mlir::DialectConversionFoldingMode::BeforePatterns;
492 cppMode = mlir::DialectConversionFoldingMode::AfterPatterns;
495 unwrap(config)->foldingMode = cppMode;
500 switch (
unwrap(config)->foldingMode) {
501 case mlir::DialectConversionFoldingMode::Never:
503 case mlir::DialectConversionFoldingMode::BeforePatterns:
505 case mlir::DialectConversionFoldingMode::AfterPatterns:
511 MlirConversionConfig config,
bool enable) {
512 unwrap(config)->buildMaterializations = enable;
516 MlirConversionConfig config) {
517 return unwrap(config)->buildMaterializations;
533 MlirConversionPatternRewriter rewriter) {
538 MlirConversionPatternRewriter rewriter, MlirRegion region,
539 MlirTypeConverter typeConverter) {
549 return wrap(
new mlir::ConversionTarget(*
unwrap(context)));
583 return wrap(
new mlir::TypeConverter());
587 delete unwrap(typeConverter);
591 MlirTypeConverter typeConverter,
595 [convertType, userData](
Type type) -> std::optional<Type> {
596 MlirType converted{
nullptr};
598 convertType(
wrap(type), &converted, userData);
621 void *userData, StringRef rootName,
625 : ConversionPattern(*typeConverter, rootName, benefit, context,
627 callbacks(callbacks), userData(userData) {
628 if (callbacks.construct)
629 callbacks.construct(userData);
633 if (callbacks.destruct)
634 callbacks.destruct(userData);
639 ConversionPatternRewriter &rewriter)
const override {
640 std::vector<MlirValue> wrappedOperands;
641 for (
Value val : operands)
642 wrappedOperands.push_back(
wrap(val));
643 return unwrap(callbacks.matchAndRewrite(
644 wrap(
static_cast<const mlir::ConversionPattern *
>(
this)),
wrap(op),
645 wrappedOperands.size(), wrappedOperands.data(),
wrap(&rewriter),
657 MlirStringRef rootName,
unsigned benefit, MlirContext context,
659 void *userData,
size_t nGeneratedNames,
MlirStringRef *generatedNames) {
660 std::vector<mlir::StringRef> generatedNamesVec;
661 generatedNamesVec.reserve(nGeneratedNames);
662 for (
size_t i = 0; i < nGeneratedNames; ++i)
663 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
666 unwrap(context),
unwrap(typeConverter), generatedNamesVec));
692 callbacks(callbacks), userData(userData) {
693 if (callbacks.construct)
694 callbacks.construct(userData);
698 if (callbacks.destruct)
699 callbacks.destruct(userData);
704 return unwrap(callbacks.matchAndRewrite(
706 wrap(&rewriter), userData));
717 MlirStringRef rootName,
unsigned benefit, MlirContext context,
720 std::vector<mlir::StringRef> generatedNamesVec;
721 generatedNamesVec.reserve(nGeneratedNames);
722 for (
size_t i = 0; i < nGeneratedNames; ++i) {
723 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
727 unwrap(context), generatedNamesVec));
747 MlirRewritePattern pattern) {
748 std::unique_ptr<mlir::RewritePattern> patternPtr(
750 pattern.ptr =
nullptr;
751 unwrap(set)->add(std::move(patternPtr));
758#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
759MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) {
760 return wrap(
new mlir::PDLPatternModule(
764void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op) {
770mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) {
776MlirValue mlirPDLValueAsValue(MlirPDLValue value) {
777 return wrap(
unwrap(value)->dyn_cast<mlir::Value>());
780MlirType mlirPDLValueAsType(MlirPDLValue value) {
781 return wrap(
unwrap(value)->dyn_cast<mlir::Type>());
784MlirOperation mlirPDLValueAsOperation(MlirPDLValue value) {
785 return wrap(
unwrap(value)->dyn_cast<mlir::Operation *>());
788MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value) {
789 return wrap(
unwrap(value)->dyn_cast<mlir::Attribute>());
792void mlirPDLResultListPushBackValue(MlirPDLResultList results,
797void mlirPDLResultListPushBackType(MlirPDLResultList results, MlirType value) {
801void mlirPDLResultListPushBackOperation(MlirPDLResultList results,
802 MlirOperation value) {
806void mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
807 MlirAttribute value) {
812 std::vector<MlirPDLValue> mlirValues;
813 mlirValues.reserve(values.size());
814 for (
auto &value : values) {
815 mlirValues.push_back(
wrap(&value));
820void mlirPDLPatternModuleRegisterRewriteFunction(
822 MlirPDLRewriteFunction rewriteFn,
void *userData) {
823 unwrap(pdlModule)->registerRewriteFunction(
825 [userData, rewriteFn](
PatternRewriter &rewriter, PDLResultList &results,
827 std::vector<MlirPDLValue> mlirValues =
wrap(values);
829 mlirValues.size(), mlirValues.data(),
834void mlirPDLPatternModuleRegisterConstraintFunction(
836 MlirPDLConstraintFunction constraintFn,
void *userData) {
837 unwrap(pdlModule)->registerConstraintFunction(
840 PDLResultList &results,
842 std::vector<MlirPDLValue> mlirValues =
wrap(values);
844 mlirValues.size(), mlirValues.data(),
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Block represents an ordered list of Operations.
OpListType::iterator iterator
~ExternalConversionPattern()
LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const override
ExternalConversionPattern(MlirConversionPatternCallbacks callbacks, void *userData, StringRef rootName, PatternBenefit benefit, MLIRContext *context, TypeConverter *typeConverter, ArrayRef< StringRef > generatedNames)
ExternalRewritePattern(MlirRewritePatternCallbacks callbacks, void *userData, StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames)
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const override
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
~ExternalRewritePattern()
This class represents a frozen set of patterns that can be processed by a pattern applicator.
This class allows control over how the GreedyPatternRewriteDriver works.
bool isFoldingEnabled() const
Whether this should fold while greedily rewriting.
GreedyRewriteConfig & setRegionSimplificationLevel(GreedySimplifyRegionLevel level)
bool isConstantCSEEnabled() const
If set to "true", constants are CSE'd (even across multiple regions that are in a parent-ancestor rel...
GreedyRewriteConfig & enableConstantCSE(bool enable=true)
GreedyRewriteStrictness getStrictness() const
Strict mode can restrict the ops that are added to the worklist during the rewrite.
bool getUseTopDownTraversal() const
This specifies the order of initial traversal that populates the rewriters worklist.
GreedyRewriteConfig & enableFolding(bool enable=true)
int64_t getMaxNumRewrites() const
This specifies the maximum number of rewrites within an iteration.
GreedyRewriteConfig & setMaxIterations(int64_t iterations)
GreedyRewriteConfig & setMaxNumRewrites(int64_t limit)
int64_t getMaxIterations() const
This specifies the maximum number of times the rewriter will iterate between applying patterns and si...
GreedyRewriteConfig & setUseTopDownTraversal(bool use=true)
GreedySimplifyRegionLevel getRegionSimplificationLevel() const
Perform control flow optimizations to the region tree after applying all patterns.
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
MLIRContext is the top-level object for a collection of MLIR operations.
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Operation is the basic unit of execution within MLIR.
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePattern is the common base class for all DAG to DAG replacements.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
MlirDialectConversionFoldingMode
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_AFTER_PATTERNS
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_BEFORE_PATTERNS
@ MLIR_DIALECT_CONVERSION_FOLDING_MODE_NEVER
MlirLogicalResult(* MlirTypeConverterConversionCallback)(MlirType type, MlirType *convertedType, void *userData)
Callback type for type conversion functions.
MlirGreedySimplifyRegionLevel
Greedy simplify region levels.
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_DISABLED
Disable region control-flow simplification.
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_NORMAL
Run the normal simplification (e.g. dead args elimination).
@ MLIR_GREEDY_SIMPLIFY_REGION_LEVEL_AGGRESSIVE
Run extra simplifications (e.g. block merging).
MlirGreedyRewriteStrictness
Greedy rewrite strictness levels.
@ MLIR_GREEDY_REWRITE_STRICTNESS_EXISTING_AND_NEW_OPS
Only pre-existing and newly created ops are processed.
@ MLIR_GREEDY_REWRITE_STRICTNESS_EXISTING_OPS
Only pre-existing ops are processed.
@ MLIR_GREEDY_REWRITE_STRICTNESS_ANY_OP
No restrictions wrt. which ops are processed.
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Include the generated interface declarations.
GreedySimplifyRegionLevel
@ Aggressive
Run extra simplificiations (e.g.
@ Normal
Run the normal simplification (e.g. dead args elimination).
@ Disabled
Disable region control-flow simplification.
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
Operation * cloneWithoutRegions(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
GreedyRewriteStrictness
This enum controls which ops are put on the worklist during a greedy pattern rewrite.
@ ExistingOps
Only pre-existing ops are processed.
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
@ AnyOp
No restrictions wrt. which ops are processed.
A logical result value, essentially a boolean with named states.
A pointer to a sized fragment of a string, not necessarily null-terminated.