40 unwrap(rewriter)->clearInsertionPoint();
55 unwrap(rewriter)->setInsertionPointAfterValue(
unwrap(value));
60 unwrap(rewriter)->setInsertionPointToStart(
unwrap(block));
69 return wrap(
unwrap(rewriter)->getInsertionBlock());
81 if (it == block->
end())
84 return wrap(std::addressof(*it));
92 MlirBlock insertBefore,
94 MlirType
const *argTypes,
95 MlirLocation
const *locations) {
100 return wrap(
unwrap(rewriter)->createBlock(
unwrap(insertBefore), unwrappedArgs,
122 MlirRegion region, MlirBlock before) {
132 MlirRegion region, MlirBlock before) {
138 MlirValue
const *values) {
146 MlirOperation newOp) {
159 MlirBlock source, MlirOperation op,
161 MlirValue
const *argValues) {
170 MlirBlock dest,
intptr_t nArgValues,
171 MlirValue
const *argValues) {
178 MlirOperation existingOp) {
183 MlirOperation existingOp) {
188 MlirBlock existingBlock) {
208 MlirValue from, MlirValue to) {
214 MlirValue
const *from,
215 MlirValue
const *to) {
220 unwrap(rewriter)->replaceAllUsesWith(unwrappedFromVals, unwrappedToVals);
226 MlirValue
const *to) {
229 unwrap(rewriter)->replaceAllOpUsesWith(
unwrap(from), unwrappedToVals);
241 MlirValue
const *newValues,
245 unwrap(rewriter)->replaceOpUsesWithinBlock(
unwrap(op), unwrappedVals,
250 MlirValue from, MlirValue to,
251 MlirOperation exceptedUser) {
276MlirFrozenRewritePatternSet
293 assert(
config.ptr &&
"unexpected null config");
306 MlirGreedyRewriteDriverConfig
config) {
311 MlirGreedyRewriteDriverConfig
config,
int64_t maxIterations) {
316 MlirGreedyRewriteDriverConfig
config,
int64_t maxNumRewrites) {
321 MlirGreedyRewriteDriverConfig
config,
bool useTopDownTraversal) {
326 MlirGreedyRewriteDriverConfig
config,
bool enable) {
331 MlirGreedyRewriteDriverConfig
config,
334 switch (strictness) {
366 MlirGreedyRewriteDriverConfig
config,
bool enable) {
371 MlirGreedyRewriteDriverConfig
config) {
376 MlirGreedyRewriteDriverConfig
config) {
381 MlirGreedyRewriteDriverConfig
config) {
386 MlirGreedyRewriteDriverConfig
config) {
391 MlirGreedyRewriteDriverConfig
config) {
393 switch (cppStrictness) {
401 llvm_unreachable(
"Unknown GreedyRewriteStrictness");
406 MlirGreedyRewriteDriverConfig
config) {
417 llvm_unreachable(
"Unknown GreedySimplifyRegionLevel");
421 MlirGreedyRewriteDriverConfig
config) {
427 MlirFrozenRewritePatternSet
patterns,
428 MlirGreedyRewriteDriverConfig
config) {
435 MlirFrozenRewritePatternSet
patterns,
436 MlirGreedyRewriteDriverConfig
config) {
442 MlirFrozenRewritePatternSet
patterns) {
448 MlirFrozenRewritePatternSet
patterns,
449 MlirConversionConfig
config) {
455 MlirConversionTarget
target,
456 MlirFrozenRewritePatternSet
patterns,
457 MlirConversionConfig
config) {
467 return wrap(
new mlir::ConversionConfig());
476 mlir::DialectConversionFoldingMode cppMode;
479 cppMode = mlir::DialectConversionFoldingMode::Never;
482 cppMode = mlir::DialectConversionFoldingMode::BeforePatterns;
485 cppMode = mlir::DialectConversionFoldingMode::AfterPatterns;
494 case mlir::DialectConversionFoldingMode::Never:
496 case mlir::DialectConversionFoldingMode::BeforePatterns:
498 case mlir::DialectConversionFoldingMode::AfterPatterns:
504 MlirConversionConfig
config,
bool enable) {
509 MlirConversionConfig
config) {
526 MlirConversionPatternRewriter rewriter) {
535 return wrap(
new mlir::ConversionTarget(*
unwrap(context)));
569 return wrap(
new mlir::TypeConverter());
573 delete unwrap(typeConverter);
577 MlirTypeConverter typeConverter,
581 [convertType, userData](
Type type) -> std::optional<Type> {
582 MlirType converted{
nullptr};
584 convertType(
wrap(type), &converted, userData);
602 void *userData, StringRef rootName,
606 : ConversionPattern(*typeConverter, rootName, benefit, context,
608 callbacks(callbacks), userData(userData) {
609 if (callbacks.construct)
610 callbacks.construct(userData);
614 if (callbacks.destruct)
615 callbacks.destruct(userData);
620 ConversionPatternRewriter &rewriter)
const override {
621 std::vector<MlirValue> wrappedOperands;
622 for (
Value val : operands)
623 wrappedOperands.push_back(
wrap(val));
624 return unwrap(callbacks.matchAndRewrite(
625 wrap(
static_cast<const mlir::ConversionPattern *
>(
this)),
wrap(op),
626 wrappedOperands.size(), wrappedOperands.data(),
wrap(&rewriter),
638 MlirStringRef rootName,
unsigned benefit, MlirContext context,
640 void *userData,
size_t nGeneratedNames,
MlirStringRef *generatedNames) {
641 std::vector<mlir::StringRef> generatedNamesVec;
642 generatedNamesVec.reserve(nGeneratedNames);
643 for (
size_t i = 0; i < nGeneratedNames; ++i)
644 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
647 unwrap(context),
unwrap(typeConverter), generatedNamesVec));
673 callbacks(callbacks), userData(userData) {
674 if (callbacks.construct)
675 callbacks.construct(userData);
679 if (callbacks.destruct)
680 callbacks.destruct(userData);
685 return unwrap(callbacks.matchAndRewrite(
687 wrap(&rewriter), userData));
698 MlirStringRef rootName,
unsigned benefit, MlirContext context,
701 std::vector<mlir::StringRef> generatedNamesVec;
702 generatedNamesVec.reserve(nGeneratedNames);
703 for (
size_t i = 0; i < nGeneratedNames; ++i) {
704 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
708 unwrap(context), generatedNamesVec));
724 MlirRewritePattern pattern) {
725 std::unique_ptr<mlir::RewritePattern> patternPtr(
727 pattern.ptr =
nullptr;
728 unwrap(set)->add(std::move(patternPtr));
735#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
736MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) {
737 return wrap(
new mlir::PDLPatternModule(
741void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op) {
747mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) {
753MlirValue mlirPDLValueAsValue(MlirPDLValue value) {
754 return wrap(
unwrap(value)->dyn_cast<mlir::Value>());
757MlirType mlirPDLValueAsType(MlirPDLValue value) {
758 return wrap(
unwrap(value)->dyn_cast<mlir::Type>());
761MlirOperation mlirPDLValueAsOperation(MlirPDLValue value) {
762 return wrap(
unwrap(value)->dyn_cast<mlir::Operation *>());
765MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value) {
766 return wrap(
unwrap(value)->dyn_cast<mlir::Attribute>());
769void mlirPDLResultListPushBackValue(MlirPDLResultList results,
774void mlirPDLResultListPushBackType(MlirPDLResultList results, MlirType value) {
778void mlirPDLResultListPushBackOperation(MlirPDLResultList results,
779 MlirOperation value) {
783void mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
784 MlirAttribute value) {
789 std::vector<MlirPDLValue> mlirValues;
790 mlirValues.reserve(values.size());
791 for (
auto &value : values) {
792 mlirValues.push_back(
wrap(&value));
797void mlirPDLPatternModuleRegisterRewriteFunction(
799 MlirPDLRewriteFunction rewriteFn,
void *userData) {
800 unwrap(pdlModule)->registerRewriteFunction(
802 [userData, rewriteFn](
PatternRewriter &rewriter, PDLResultList &results,
804 std::vector<MlirPDLValue> mlirValues =
wrap(values);
806 mlirValues.size(), mlirValues.data(),
811void mlirPDLPatternModuleRegisterConstraintFunction(
813 MlirPDLConstraintFunction constraintFn,
void *userData) {
814 unwrap(pdlModule)->registerConstraintFunction(
817 PDLResultList &results,
819 std::vector<MlirPDLValue> mlirValues =
wrap(values);
821 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.
const FrozenRewritePatternSet GreedyRewriteConfig config
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)
const FrozenRewritePatternSet & patterns
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
GreedyRewriteStrictness
This enum controls which ops are put on the worklist during a greedy pattern rewrite.
@ ExistingOps
Only pre-existing ops are processed.
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
@ AnyOp
No restrictions wrt. which ops are processed.
A logical result value, essentially a boolean with named states.
A pointer to a sized fragment of a string, not necessarily null-terminated.