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;
488 unwrap(config)->foldingMode = cppMode;
493 switch (
unwrap(config)->foldingMode) {
494 case mlir::DialectConversionFoldingMode::Never:
496 case mlir::DialectConversionFoldingMode::BeforePatterns:
498 case mlir::DialectConversionFoldingMode::AfterPatterns:
504 MlirConversionConfig config,
bool enable) {
505 unwrap(config)->buildMaterializations = enable;
509 MlirConversionConfig config) {
510 return unwrap(config)->buildMaterializations;
526 MlirConversionPatternRewriter rewriter) {
531 MlirConversionPatternRewriter rewriter, MlirRegion region,
532 MlirTypeConverter typeConverter) {
542 return wrap(
new mlir::ConversionTarget(*
unwrap(context)));
576 return wrap(
new mlir::TypeConverter());
580 delete unwrap(typeConverter);
584 MlirTypeConverter typeConverter,
588 [convertType, userData](
Type type) -> std::optional<Type> {
589 MlirType converted{
nullptr};
591 convertType(
wrap(type), &converted, userData);
614 void *userData, StringRef rootName,
618 : ConversionPattern(*typeConverter, rootName, benefit, context,
620 callbacks(callbacks), userData(userData) {
621 if (callbacks.construct)
622 callbacks.construct(userData);
626 if (callbacks.destruct)
627 callbacks.destruct(userData);
632 ConversionPatternRewriter &rewriter)
const override {
633 std::vector<MlirValue> wrappedOperands;
634 for (
Value val : operands)
635 wrappedOperands.push_back(
wrap(val));
636 return unwrap(callbacks.matchAndRewrite(
637 wrap(
static_cast<const mlir::ConversionPattern *
>(
this)),
wrap(op),
638 wrappedOperands.size(), wrappedOperands.data(),
wrap(&rewriter),
650 MlirStringRef rootName,
unsigned benefit, MlirContext context,
652 void *userData,
size_t nGeneratedNames,
MlirStringRef *generatedNames) {
653 std::vector<mlir::StringRef> generatedNamesVec;
654 generatedNamesVec.reserve(nGeneratedNames);
655 for (
size_t i = 0; i < nGeneratedNames; ++i)
656 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
659 unwrap(context),
unwrap(typeConverter), generatedNamesVec));
685 callbacks(callbacks), userData(userData) {
686 if (callbacks.construct)
687 callbacks.construct(userData);
691 if (callbacks.destruct)
692 callbacks.destruct(userData);
697 return unwrap(callbacks.matchAndRewrite(
699 wrap(&rewriter), userData));
710 MlirStringRef rootName,
unsigned benefit, MlirContext context,
713 std::vector<mlir::StringRef> generatedNamesVec;
714 generatedNamesVec.reserve(nGeneratedNames);
715 for (
size_t i = 0; i < nGeneratedNames; ++i) {
716 generatedNamesVec.push_back(
unwrap(generatedNames[i]));
720 unwrap(context), generatedNamesVec));
740 MlirRewritePattern pattern) {
741 std::unique_ptr<mlir::RewritePattern> patternPtr(
743 pattern.ptr =
nullptr;
744 unwrap(set)->add(std::move(patternPtr));
751#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
752MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) {
753 return wrap(
new mlir::PDLPatternModule(
757void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op) {
763mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) {
769MlirValue mlirPDLValueAsValue(MlirPDLValue value) {
770 return wrap(
unwrap(value)->dyn_cast<mlir::Value>());
773MlirType mlirPDLValueAsType(MlirPDLValue value) {
774 return wrap(
unwrap(value)->dyn_cast<mlir::Type>());
777MlirOperation mlirPDLValueAsOperation(MlirPDLValue value) {
778 return wrap(
unwrap(value)->dyn_cast<mlir::Operation *>());
781MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value) {
782 return wrap(
unwrap(value)->dyn_cast<mlir::Attribute>());
785void mlirPDLResultListPushBackValue(MlirPDLResultList results,
790void mlirPDLResultListPushBackType(MlirPDLResultList results, MlirType value) {
794void mlirPDLResultListPushBackOperation(MlirPDLResultList results,
795 MlirOperation value) {
799void mlirPDLResultListPushBackAttribute(MlirPDLResultList results,
800 MlirAttribute value) {
805 std::vector<MlirPDLValue> mlirValues;
806 mlirValues.reserve(values.size());
807 for (
auto &value : values) {
808 mlirValues.push_back(
wrap(&value));
813void mlirPDLPatternModuleRegisterRewriteFunction(
815 MlirPDLRewriteFunction rewriteFn,
void *userData) {
816 unwrap(pdlModule)->registerRewriteFunction(
818 [userData, rewriteFn](
PatternRewriter &rewriter, PDLResultList &results,
820 std::vector<MlirPDLValue> mlirValues =
wrap(values);
822 mlirValues.size(), mlirValues.data(),
827void mlirPDLPatternModuleRegisterConstraintFunction(
829 MlirPDLConstraintFunction constraintFn,
void *userData) {
830 unwrap(pdlModule)->registerConstraintFunction(
833 PDLResultList &results,
835 std::vector<MlirPDLValue> mlirValues =
wrap(values);
837 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.