13 #ifndef MLIR_TRANSFORMS_DIALECTCONVERSION_H_
14 #define MLIR_TRANSFORMS_DIALECTCONVERSION_H_
16 #include "mlir/Config/mlir-config.h"
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include <type_traits>
27 struct ConversionConfig;
28 class ConversionPatternRewriter;
31 struct OperationConverter;
51 : conversions(other.conversions),
52 sourceMaterializations(other.sourceMaterializations),
53 targetMaterializations(other.targetMaterializations),
54 typeAttributeConversions(other.typeAttributeConversions) {}
56 conversions = other.conversions;
57 sourceMaterializations = other.sourceMaterializations;
58 targetMaterializations = other.targetMaterializations;
59 typeAttributeConversions = other.typeAttributeConversions;
68 : remappedInputs(numOrigInputs) {}
85 return remappedInputs[input];
107 void remapInput(
unsigned origInputNo,
unsigned newInputNo,
108 unsigned newInputCount = 1);
137 llvm::PointerIntPair<Attribute, 2>
impl;
140 static constexpr
unsigned naTag = 0;
141 static constexpr
unsigned resultTag = 1;
142 static constexpr
unsigned abortTag = 2;
172 template <
typename FnT,
typename T =
typename llvm::function_traits<
173 std::decay_t<FnT>>::template arg_t<0>>
175 registerConversion(wrapCallback<T>(std::forward<FnT>(callback)));
198 template <
typename FnT,
typename T =
typename llvm::function_traits<
199 std::decay_t<FnT>>::template arg_t<1>>
201 sourceMaterializations.emplace_back(
202 wrapSourceMaterialization<T>(std::forward<FnT>(callback)));
222 template <
typename FnT,
typename T =
typename llvm::function_traits<
223 std::decay_t<FnT>>::template arg_t<1>>
225 targetMaterializations.emplace_back(
226 wrapTargetMaterialization<T>(std::forward<FnT>(callback)));
250 typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<0>,
252 typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<1>>
254 registerTypeAttributeConversion(
255 wrapTypeAttributeConversion<T, A>(std::forward<FnT>(callback)));
284 template <
typename TargetType>
286 return dyn_cast_or_null<TargetType>(
convertType(t));
288 template <
typename TargetType>
290 return dyn_cast_or_null<TargetType>(
convertType(v));
312 return llvm::all_of(range, [
this](
Type type) {
return isLegal(type); });
315 return llvm::all_of(range, [
this](
Value value) {
return isLegal(value); });
332 SignatureConversion &result)
const;
334 SignatureConversion &result,
335 unsigned origInputOffset = 0)
const;
337 SignatureConversion &result)
const;
339 SignatureConversion &result,
340 unsigned origInputOffset = 0)
const;
355 Type originalType = {})
const;
358 TypeRange resultType,
360 Type originalType = {})
const;
367 Attribute attr)
const;
373 using ConversionCallbackFn = std::function<std::optional<LogicalResult>(
374 PointerUnion<Type, Value>, SmallVectorImpl<Type> &)>;
379 using SourceMaterializationCallbackFn =
380 std::function<Value(OpBuilder &, Type, ValueRange, Location)>;
385 using TargetMaterializationCallbackFn = std::function<SmallVector<Value>(
386 OpBuilder &, TypeRange, ValueRange, Location, Type)>;
389 using TypeAttributeConversionCallbackFn =
390 std::function<AttributeConversionResult(Type, Attribute)>;
396 template <
typename T,
typename FnT>
397 std::enable_if_t<std::is_invocable_v<FnT, T>, ConversionCallbackFn>
398 wrapCallback(FnT &&callback) {
399 return wrapCallback<T>([callback = std::forward<FnT>(callback)](
400 T typeOrValue, SmallVectorImpl<Type> &results) {
401 if (std::optional<Type> resultOpt = callback(typeOrValue)) {
402 bool wasSuccess =
static_cast<bool>(*resultOpt);
404 results.push_back(*resultOpt);
405 return std::optional<LogicalResult>(success(wasSuccess));
407 return std::optional<LogicalResult>();
412 template <
typename T,
typename FnT>
413 std::enable_if_t<std::is_invocable_v<FnT, T, SmallVectorImpl<Type> &> &&
414 std::is_base_of_v<Type, T>,
415 ConversionCallbackFn>
416 wrapCallback(FnT &&callback)
const {
417 return [callback = std::forward<FnT>(callback)](
418 PointerUnion<Type, Value> typeOrValue,
419 SmallVectorImpl<Type> &results) -> std::optional<LogicalResult> {
421 if (Type t = dyn_cast<Type>(typeOrValue)) {
422 derivedType = dyn_cast<T>(t);
423 }
else if (Value v = dyn_cast<Value>(typeOrValue)) {
424 derivedType = dyn_cast<T>(v.getType());
426 llvm_unreachable(
"unexpected variant");
430 return callback(derivedType, results);
435 template <
typename T,
typename FnT>
436 std::enable_if_t<std::is_invocable_v<FnT, T, SmallVectorImpl<Type> &> &&
437 std::is_same_v<T, Value>,
438 ConversionCallbackFn>
439 wrapCallback(FnT &&callback) {
440 contextAwareTypeConversionsIndex = conversions.size();
441 return [callback = std::forward<FnT>(callback)](
442 PointerUnion<Type, Value> typeOrValue,
443 SmallVectorImpl<Type> &results) -> std::optional<LogicalResult> {
444 if (Type t = dyn_cast<Type>(typeOrValue)) {
447 }
else if (Value v = dyn_cast<Value>(typeOrValue)) {
448 return callback(v, results);
450 llvm_unreachable(
"unexpected variant");
456 void registerConversion(ConversionCallbackFn callback) {
457 conversions.emplace_back(std::move(callback));
458 cachedDirectConversions.clear();
459 cachedMultiConversions.clear();
465 template <
typename T,
typename FnT>
466 SourceMaterializationCallbackFn
467 wrapSourceMaterialization(FnT &&callback)
const {
468 return [callback = std::forward<FnT>(callback)](
469 OpBuilder &builder, Type resultType, ValueRange inputs,
470 Location loc) -> Value {
471 if (T derivedType = dyn_cast<T>(resultType))
472 return callback(builder, derivedType, inputs, loc);
485 template <
typename T,
typename FnT>
487 std::is_invocable_v<FnT, OpBuilder &, T, ValueRange, Location, Type>,
488 TargetMaterializationCallbackFn>
489 wrapTargetMaterialization(FnT &&callback)
const {
490 return [callback = std::forward<FnT>(callback)](
491 OpBuilder &builder, TypeRange resultTypes, ValueRange inputs,
492 Location loc, Type originalType) -> SmallVector<Value> {
493 SmallVector<Value> result;
494 if constexpr (std::is_same<T, TypeRange>::value) {
497 result = callback(builder, resultTypes, inputs, loc, originalType);
498 }
else if constexpr (std::is_assignable<Type, T>::value) {
501 if (resultTypes.size() == 1) {
504 if (T derivedType = dyn_cast<T>(resultTypes.front())) {
509 callback(builder, derivedType, inputs, loc, originalType);
511 result.push_back(val);
515 static_assert(
sizeof(T) == 0,
"T must be a Type or a TypeRange");
523 template <
typename T,
typename FnT>
525 std::is_invocable_v<FnT, OpBuilder &, T, ValueRange, Location>,
526 TargetMaterializationCallbackFn>
527 wrapTargetMaterialization(FnT &&callback)
const {
528 return wrapTargetMaterialization<T>(
529 [callback = std::forward<FnT>(callback)](
530 OpBuilder &builder, T resultTypes, ValueRange inputs, Location loc,
532 return callback(builder, resultTypes, inputs, loc);
540 template <
typename T,
typename A,
typename FnT>
541 TypeAttributeConversionCallbackFn
542 wrapTypeAttributeConversion(FnT &&callback)
const {
543 return [callback = std::forward<FnT>(callback)](
544 Type type, Attribute attr) -> AttributeConversionResult {
545 if (T derivedType = dyn_cast<T>(type)) {
546 if (A derivedAttr = dyn_cast_or_null<A>(attr))
547 return callback(derivedType, derivedAttr);
555 registerTypeAttributeConversion(TypeAttributeConversionCallbackFn callback) {
556 typeAttributeConversions.emplace_back(std::move(callback));
558 cachedDirectConversions.clear();
559 cachedMultiConversions.clear();
563 LogicalResult convertTypeImpl(PointerUnion<Type, Value> t,
564 SmallVectorImpl<Type> &results)
const;
567 SmallVector<ConversionCallbackFn, 4> conversions;
570 SmallVector<SourceMaterializationCallbackFn, 2> sourceMaterializations;
571 SmallVector<TargetMaterializationCallbackFn, 2> targetMaterializations;
574 SmallVector<TypeAttributeConversionCallbackFn, 2> typeAttributeConversions;
579 mutable DenseMap<Type, Type> cachedDirectConversions;
581 mutable DenseMap<Type, SmallVector<Type, 2>> cachedMultiConversions;
583 mutable llvm::sys::SmartRWMutex<true> cacheMutex;
592 int contextAwareTypeConversionsIndex = -1;
611 virtual LogicalResult
614 llvm_unreachable(
"matchAndRewrite is not implemented");
619 virtual LogicalResult
633 template <
typename ConverterTy>
634 std::enable_if_t<std::is_base_of<TypeConverter, ConverterTy>::value,
643 using RewritePattern::RewritePattern;
646 template <
typename... Args>
656 FailureOr<SmallVector<Value>>
663 template <
typename SelfPattern,
typename SourceOp>
664 static LogicalResult
dispatchTo1To1(
const SelfPattern &
self, SourceOp op,
669 template <
typename SelfPattern,
typename SourceOp>
671 const SelfPattern &
self, SourceOp op,
683 template <
typename SourceOp>
692 typename SourceOp::template GenericAdaptor<ArrayRef<ValueRange>>;
706 auto sourceOp = cast<SourceOp>(op);
712 auto sourceOp = cast<SourceOp>(op);
719 virtual LogicalResult
722 llvm_unreachable(
"matchAndRewrite is not implemented");
724 virtual LogicalResult
737 template <
typename SourceOp>
746 SourceOp::getInterfaceID(), benefit, context) {}
750 SourceOp::getInterfaceID(), benefit, context) {}
767 virtual LogicalResult
770 llvm_unreachable(
"matchAndRewrite is not implemented");
772 virtual LogicalResult
785 template <
template <
typename>
class TraitType>
794 TypeID::
get<TraitType>(), benefit, context) {}
798 TypeID::
get<TraitType>(), benefit, context) {}
804 FailureOr<Operation *>
806 const TypeConverter &converter,
807 ConversionPatternRewriter &rewriter);
813 StringRef functionLikeOpName, RewritePatternSet &
patterns,
814 const TypeConverter &converter, PatternBenefit benefit = 1);
816 template <
typename FuncOpT>
821 FuncOpT::getOperationName(),
patterns, converter, benefit);
825 RewritePatternSet &
patterns,
const TypeConverter &converter,
826 PatternBenefit benefit = 1);
833 struct ConversionPatternRewriterImpl;
944 template <
typename RangeT = ValueRange>
949 template <
typename RangeT>
952 ArrayRef(llvm::to_vector_of<ValueRange>(newValues)));
982 detail::ConversionPatternRewriterImpl &
getImpl();
997 std::unique_ptr<detail::ConversionPatternRewriterImpl>
impl;
1000 template <
typename SelfPattern,
typename SourceOp>
1005 FailureOr<SmallVector<Value>> oneToOneOperands =
1006 self.getOneToOneAdaptorOperands(operands);
1007 if (
failed(oneToOneOperands))
1010 "' does not support 1:N conversion");
1011 return self.matchAndRewrite(op, *oneToOneOperands, rewriter);
1014 template <
typename SelfPattern,
typename SourceOp>
1016 const SelfPattern &
self, SourceOp op,
1019 FailureOr<SmallVector<Value>> oneToOneOperands =
1020 self.getOneToOneAdaptorOperands(adaptor.getOperands());
1021 if (
failed(oneToOneOperands))
1024 "' does not support 1:N conversion");
1025 return self.matchAndRewrite(
1026 op,
typename SourceOp::Adaptor(*oneToOneOperands, adaptor), rewriter);
1062 std::function<std::optional<bool>(
Operation *)>;
1073 template <
typename OpT>
1082 template <
typename OpT>
1086 template <
typename OpT,
typename OpT2,
typename... OpTs>
1097 setLegalityCallback(op, callback);
1099 template <
typename OpT>
1104 template <
typename OpT,
typename OpT2,
typename... OpTs>
1106 addDynamicallyLegalOp<OpT>(callback);
1109 template <
typename OpT,
class Callable>
1110 std::enable_if_t<!std::is_invocable_v<Callable, Operation *>>
1112 addDynamicallyLegalOp<OpT>(
1113 [=](
Operation *op) {
return callback(cast<OpT>(op)); });
1121 template <
typename OpT>
1125 template <
typename OpT,
typename OpT2,
typename... OpTs>
1127 addIllegalOp<OpT>();
1138 template <
typename OpT>
1143 template <
typename OpT,
typename OpT2,
typename... OpTs>
1145 markOpRecursivelyLegal<OpT>(callback);
1148 template <
typename OpT,
class Callable>
1149 std::enable_if_t<!std::is_invocable_v<Callable, Operation *>>
1151 markOpRecursivelyLegal<OpT>(
1152 [=](
Operation *op) {
return callback(cast<OpT>(op)); });
1160 template <
typename... Names>
1165 template <
typename... Args>
1173 template <
typename... Names>
1175 StringRef name, Names... names) {
1178 setLegalityCallback(dialectNames, callback);
1180 template <
typename... Args>
1183 Args::getDialectNamespace()...);
1190 setLegalityCallback(fn);
1195 template <
typename... Names>
1200 template <
typename... Args>
1242 struct LegalizationInfo {
1247 bool isRecursivelyLegal =
false;
1254 std::optional<LegalizationInfo> getOpInfo(OperationName op)
const;
1258 llvm::MapVector<OperationName, LegalizationInfo> legalOperations;
1262 DenseMap<OperationName, DynamicLegalityCallbackFn> opRecursiveLegalityFns;
1266 llvm::StringMap<LegalizationAction> legalDialects;
1269 llvm::StringMap<DynamicLegalityCallbackFn> dialectLegalityFns;
1278 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
1314 class PDLConversionConfig final {
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
OpListType::iterator iterator
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
Replace the given operation with the new values.
LogicalResult getRemappedValues(ValueRange keys, SmallVectorImpl< Value > &results)
Return the converted values that replace 'keys' with types defined by the type converter of the curre...
FailureOr< Block * > convertRegionTypes(Region *region, const TypeConverter &converter, TypeConverter::SignatureConversion *entryConversion=nullptr)
Apply a signature conversion to each block in the given region.
void replaceAllUsesWith(Value from, Value to) override
Find uses of from and replace them with to.
const ConversionConfig & getConfig() const
Return the configuration of the current dialect conversion.
void replaceAllUsesWith(Value from, ValueRange to)
Replace all the uses of from with to.
void replaceOpWithMultiple(Operation *op, SmallVector< SmallVector< Value >> &&newValues)
Replace the given operation with the new value ranges.
Block * applySignatureConversion(Block *block, TypeConverter::SignatureConversion &conversion, const TypeConverter *converter=nullptr)
Apply a signature conversion to given block.
void replaceOpWithMultiple(Operation *op, ArrayRef< RangeT > newValues)
void startOpModification(Operation *op) override
PatternRewriter hook for updating the given operation in-place.
void eraseOp(Operation *op) override
PatternRewriter hook for erasing a dead operation.
void replaceOpWithMultiple(Operation *op, RangeT &&newValues)
detail::ConversionPatternRewriterImpl & getImpl()
Return a reference to the internal implementation.
void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={}) override
PatternRewriter hook for inlining the ops of a block into another block.
void eraseBlock(Block *block) override
PatternRewriter hook for erase all operations in a block.
void cancelOpModification(Operation *op) override
PatternRewriter hook for updating the given operation in-place.
bool canRecoverFromRewriteFailure() const override
Indicate that the conversion rewriter can recover from rewrite failure.
Value getRemappedValue(Value key)
Return the converted value of 'key' with a type defined by the type converter of the currently execut...
void finalizeOpModification(Operation *op) override
PatternRewriter hook for updating the given operation in-place.
~ConversionPatternRewriter() override
Base class for the conversion patterns.
FailureOr< SmallVector< Value > > getOneToOneAdaptorOperands(ArrayRef< ValueRange > operands) const
Given an array of value ranges, which are the inputs to a 1:N adaptor, try to extract the single valu...
static LogicalResult dispatchTo1To1(const SelfPattern &self, SourceOp op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter)
Overloaded method used to dispatch to the 1:1 'matchAndRewrite' method if possible and emit diagnosti...
const TypeConverter * typeConverter
An optional type converter for use by this pattern.
ConversionPattern(const TypeConverter &typeConverter, Args &&...args)
Construct a conversion pattern with the given converter, and forward the remaining arguments to Rewri...
const TypeConverter * getTypeConverter() const
Return the type converter held by this pattern, or nullptr if the pattern does not require type conve...
std::enable_if_t< std::is_base_of< TypeConverter, ConverterTy >::value, const ConverterTy * > getTypeConverter() const
virtual LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const
Hook for derived classes to implement combined matching and rewriting.
virtual LogicalResult matchAndRewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const
Hook for derived classes to implement combined matching and rewriting.
This class describes a specific conversion target.
void setDialectAction(ArrayRef< StringRef > dialectNames, LegalizationAction action)
Register a legality action for the given dialects.
void setOpAction(LegalizationAction action)
void addLegalOp(OperationName op)
Register the given operations as legal.
void addDynamicallyLegalDialect(DynamicLegalityCallbackFn callback)
void setOpAction(OperationName op, LegalizationAction action)
Register a legality action for the given operation.
void addDynamicallyLegalDialect(const DynamicLegalityCallbackFn &callback, StringRef name, Names... names)
Register the operations of the given dialects as dynamically legal, i.e.
void addDynamicallyLegalOp(const DynamicLegalityCallbackFn &callback)
void addLegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as legal.
std::optional< LegalOpDetails > isLegal(Operation *op) const
If the given operation instance is legal on this target, a structure containing legality information ...
std::enable_if_t<!std::is_invocable_v< Callable, Operation * > > markOpRecursivelyLegal(Callable &&callback)
void markUnknownOpDynamicallyLegal(const DynamicLegalityCallbackFn &fn)
Register unknown operations as dynamically legal.
std::optional< LegalizationAction > getOpAction(OperationName op) const
Get the legality action for the given operation.
void addDynamicallyLegalOp(OperationName op, const DynamicLegalityCallbackFn &callback)
Register the given operation as dynamically legal and set the dynamic legalization callback to the on...
void addIllegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as illegal, i.e.
std::enable_if_t<!std::is_invocable_v< Callable, Operation * > > addDynamicallyLegalOp(Callable &&callback)
void addDynamicallyLegalOp(const DynamicLegalityCallbackFn &callback)
void markOpRecursivelyLegal(const DynamicLegalityCallbackFn &callback={})
void addIllegalOp(OperationName op)
Register the given operation as illegal, i.e.
LegalizationAction
This enumeration corresponds to the specific action to take when considering an operation legal for t...
@ Illegal
The target explicitly does not support this operation.
@ Dynamic
This operation has dynamic legalization constraints that must be checked by the target.
@ Legal
The target supports this operation.
ConversionTarget(MLIRContext &ctx)
void markOpRecursivelyLegal(OperationName name, const DynamicLegalityCallbackFn &callback)
Mark an operation, that must have either been set as Legal or DynamicallyLegal, as being recursively ...
void markOpRecursivelyLegal(const DynamicLegalityCallbackFn &callback={})
std::function< std::optional< bool >(Operation *)> DynamicLegalityCallbackFn
The signature of the callback used to determine if an operation is dynamically legal on the target.
bool isIllegal(Operation *op) const
Returns true is operation instance is illegal on this target.
virtual ~ConversionTarget()=default
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
This class represents a frozen set of patterns that can be processed by a pattern applicator.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
LogicalResult matchAndRewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement combined matching and rewriting.
typename SourceOp::Adaptor OpAdaptor
virtual LogicalResult matchAndRewrite(SourceOp op, OneToNOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const
OpConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
typename SourceOp::template GenericAdaptor< ArrayRef< ValueRange > > OneToNOpAdaptor
OpConversionPattern(const TypeConverter &typeConverter, MLIRContext *context, PatternBenefit benefit=1)
virtual LogicalResult matchAndRewrite(SourceOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const
Methods that operate on the SourceOp type.
LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Wrappers around the ConversionPattern methods that pass the derived op type.
OpInterfaceConversionPattern is a wrapper around ConversionPattern that allows for matching and rewri...
LogicalResult matchAndRewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement combined matching and rewriting.
virtual LogicalResult matchAndRewrite(SourceOp op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const
LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Wrappers around the ConversionPattern methods that pass the derived op type.
OpInterfaceConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
OpInterfaceConversionPattern(const TypeConverter &typeConverter, MLIRContext *context, PatternBenefit benefit=1)
virtual LogicalResult matchAndRewrite(SourceOp op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const
Methods that operate on the SourceOp type.
OpTraitConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting...
OpTraitConversionPattern(const TypeConverter &typeConverter, MLIRContext *context, PatternBenefit benefit=1)
OpTraitConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
Operation is the basic unit of execution within MLIR.
A PDL configuration that is used to supported dialect conversion functionality.
void notifyRewriteEnd(PatternRewriter &rewriter) final
void notifyRewriteBegin(PatternRewriter &rewriter) final
Hooks that are invoked at the beginning and end of a rewrite of a matched pattern.
const TypeConverter * getTypeConverter() const
Return the type converter used by this configuration, which may be nullptr if no type conversions are...
PDLConversionConfig(const TypeConverter *converter)
~PDLConversionConfig() final=default
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...
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
StringRef getDebugName() const
Return a readable name for this pattern.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
RewritePattern is the common base class for all DAG to DAG replacements.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
The general result of a type attribute conversion callback, allowing for early termination.
Attribute getResult() const
constexpr AttributeConversionResult()
static AttributeConversionResult abort()
static AttributeConversionResult na()
AttributeConversionResult(Attribute attr)
static AttributeConversionResult result(Attribute attr)
This class provides all of the information necessary to convert a type signature.
void addInputs(unsigned origInputNo, ArrayRef< Type > types)
Remap an input of the original signature with a new set of types.
std::optional< InputMapping > getInputMapping(unsigned input) const
Get the input mapping for the given argument.
ArrayRef< Type > getConvertedTypes() const
Return the argument types for the new signature.
void remapInput(unsigned origInputNo, ArrayRef< Value > replacements)
Remap an input of the original signature to replacements values.
SignatureConversion(unsigned numOrigInputs)
std::optional< Attribute > convertTypeAttribute(Type type, Attribute attr) const
Convert an attribute present attr from within the type type using the registered conversion functions...
Value materializeSourceConversion(OpBuilder &builder, Location loc, Type resultType, ValueRange inputs) const
Materialize a conversion from a set of types into one result type by generating a cast sequence of so...
void addConversion(FnT &&callback)
Register a conversion function.
bool isLegal(ValueRange range) const
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
LogicalResult convertSignatureArgs(TypeRange types, SignatureConversion &result, unsigned origInputOffset=0) const
TargetType convertType(Type t) const
Attempts a 1-1 type conversion, expecting the result type to be TargetType.
LogicalResult convertSignatureArg(unsigned inputNo, Type type, SignatureConversion &result) const
This method allows for converting a specific argument of a signature.
TypeConverter(const TypeConverter &other)
TypeConverter & operator=(const TypeConverter &other)
bool isLegal(TypeRange range) const
Return true if all of the given types are legal for this type converter.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
std::optional< SignatureConversion > convertBlockSignature(Block *block) const
This function converts the type signature of the given block, by invoking 'convertSignatureArg' for e...
void addSourceMaterialization(FnT &&callback)
All of the following materializations require function objects that are convertible to the following ...
virtual ~TypeConverter()=default
void addTypeAttributeConversion(FnT &&callback)
Register a conversion function for attributes within types.
void addTargetMaterialization(FnT &&callback)
This method registers a materialization that will be called when converting a value to a target type ...
LogicalResult convertTypes(TypeRange types, SmallVectorImpl< Type > &results) const
Convert the given types, filling 'results' as necessary.
bool isSignatureLegal(FunctionType ty) const
Return true if the inputs and outputs of the given function type are legal.
TargetType convertType(Value v) const
Value materializeTargetConversion(OpBuilder &builder, Location loc, Type resultType, ValueRange inputs, Type originalType={}) const
This class provides an efficient unique identifier for a specific C++ type.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Include the generated interface declarations.
DialectConversionFoldingMode
An enum to control folding behavior during dialect conversion.
@ Never
Never attempt to fold.
@ AfterPatterns
Only attempt to fold not legal operations after applying patterns.
@ BeforePatterns
Only attempt to fold not legal operations before applying patterns.
void populateFunctionOpInterfaceTypeConversionPattern(StringRef functionLikeOpName, RewritePatternSet &patterns, const TypeConverter &converter, PatternBenefit benefit=1)
Add a pattern to the given pattern list to convert the signature of a FunctionOpInterface op with the...
const FrozenRewritePatternSet GreedyRewriteConfig config
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply a complete conversion on the given operations, and all nested operations.
void populateAnyFunctionOpInterfaceTypeConversionPattern(RewritePatternSet &patterns, const TypeConverter &converter, PatternBenefit benefit=1)
FailureOr< Operation * > convertOpResultTypes(Operation *op, ValueRange operands, const TypeConverter &converter, ConversionPatternRewriter &rewriter)
Generic utility to convert op result types according to type converter without knowing exact op type.
void reconcileUnrealizedCasts(const DenseSet< UnrealizedConversionCastOp > &castOps, SmallVectorImpl< UnrealizedConversionCastOp > *remainingCastOps=nullptr)
Try to reconcile all given UnrealizedConversionCastOps and store the left-over ops in remainingCastOp...
const FrozenRewritePatternSet & patterns
LogicalResult applyAnalysisConversion(ArrayRef< Operation * > ops, ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply an analysis conversion on the given operations, and all nested operations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void registerConversionPDLFunctions(RewritePatternSet &patterns)
Register the dialect conversion PDL functions with the given pattern set.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
Dialect conversion configuration.
bool allowPatternRollback
If set to "true", pattern rollback is allowed.
RewriterBase::Listener * listener
An optional listener that is notified about all IR modifications in case dialect conversion succeeds.
DialectConversionFoldingMode foldingMode
The folding mode to use during conversion.
function_ref< void(Diagnostic &)> notifyCallback
An optional callback used to notify about match failure diagnostics during the conversion.
DenseSet< Operation * > * legalizableOps
Analysis conversion only.
bool attachDebugMaterializationKind
If set to "true", the materialization kind ("source" or "target") will be attached to "builtin....
DenseSet< Operation * > * unlegalizedOps
Partial conversion only.
bool buildMaterializations
If set to "true", the dialect conversion attempts to build source/target materializations through the...
A structure containing additional information describing a specific legal operation instance.
bool isRecursivelyLegal
A flag that indicates if this operation is 'recursively' legal.
This class acts as a special tag that makes the desire to match any operation that implements a given...
This class acts as a special tag that makes the desire to match any operation that implements a given...