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;
47 : conversions(other.conversions),
48 argumentMaterializations(other.argumentMaterializations),
49 sourceMaterializations(other.sourceMaterializations),
50 targetMaterializations(other.targetMaterializations),
51 typeAttributeConversions(other.typeAttributeConversions) {}
53 conversions = other.conversions;
54 argumentMaterializations = other.argumentMaterializations;
55 sourceMaterializations = other.sourceMaterializations;
56 targetMaterializations = other.targetMaterializations;
57 typeAttributeConversions = other.typeAttributeConversions;
66 : remappedInputs(numOrigInputs) {}
80 return remappedInputs[input];
102 void remapInput(
unsigned origInputNo,
unsigned newInputNo,
103 unsigned newInputCount = 1);
132 llvm::PointerIntPair<Attribute, 2>
impl;
135 static constexpr
unsigned naTag = 0;
136 static constexpr
unsigned resultTag = 1;
137 static constexpr
unsigned abortTag = 2;
158 template <
typename FnT,
typename T =
typename llvm::function_traits<
159 std::decay_t<FnT>>::template arg_t<0>>
161 registerConversion(wrapCallback<T>(std::forward<FnT>(callback)));
184 template <
typename FnT,
typename T =
typename llvm::function_traits<
185 std::decay_t<FnT>>::template arg_t<1>>
187 argumentMaterializations.emplace_back(
188 wrapMaterialization<T>(std::forward<FnT>(callback)));
195 template <
typename FnT,
typename T =
typename llvm::function_traits<
196 std::decay_t<FnT>>::template arg_t<1>>
198 sourceMaterializations.emplace_back(
199 wrapMaterialization<T>(std::forward<FnT>(callback)));
219 template <
typename FnT,
typename T =
typename llvm::function_traits<
220 std::decay_t<FnT>>::template arg_t<1>>
222 targetMaterializations.emplace_back(
223 wrapTargetMaterialization<T>(std::forward<FnT>(callback)));
246 typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<0>,
248 typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<1>>
250 registerTypeAttributeConversion(
251 wrapTypeAttributeConversion<T, A>(std::forward<FnT>(callback)));
267 template <
typename TargetType>
269 return dyn_cast_or_null<TargetType>(
convertType(t));
283 template <
typename RangeT>
284 std::enable_if_t<!std::is_convertible<RangeT, Type>::value &&
285 !std::is_convertible<RangeT, Operation *>::value,
288 return llvm::all_of(range, [
this](
Type type) {
return isLegal(type); });
304 SignatureConversion &result)
const;
306 SignatureConversion &result,
307 unsigned origInputOffset = 0)
const;
324 Type originalType = {})
const;
327 TypeRange resultType,
329 Type originalType = {})
const;
336 Attribute attr)
const;
342 using ConversionCallbackFn = std::function<std::optional<LogicalResult>(
343 Type, SmallVectorImpl<Type> &)>;
349 using MaterializationCallbackFn =
350 std::function<
Value(OpBuilder &, Type, ValueRange, Location)>;
355 using TargetMaterializationCallbackFn = std::function<SmallVector<Value>(
356 OpBuilder &, TypeRange, ValueRange, Location, Type)>;
359 using TypeAttributeConversionCallbackFn =
360 std::function<AttributeConversionResult(Type, Attribute)>;
365 template <
typename T,
typename FnT>
366 std::enable_if_t<std::is_invocable_v<FnT, T>, ConversionCallbackFn>
367 wrapCallback(FnT &&callback)
const {
368 return wrapCallback<T>([callback = std::forward<FnT>(callback)](
369 T type, SmallVectorImpl<Type> &results) {
370 if (std::optional<Type> resultOpt = callback(type)) {
371 bool wasSuccess =
static_cast<bool>(*resultOpt);
373 results.push_back(*resultOpt);
374 return std::optional<LogicalResult>(success(wasSuccess));
376 return std::optional<LogicalResult>();
381 template <
typename T,
typename FnT>
382 std::enable_if_t<std::is_invocable_v<FnT, T, SmallVectorImpl<Type> &>,
383 ConversionCallbackFn>
384 wrapCallback(FnT &&callback)
const {
385 return [callback = std::forward<FnT>(callback)](
387 SmallVectorImpl<Type> &results) -> std::optional<LogicalResult> {
388 T derivedType = dyn_cast<T>(type);
391 return callback(derivedType, results);
396 void registerConversion(ConversionCallbackFn callback) {
397 conversions.emplace_back(std::move(callback));
398 cachedDirectConversions.clear();
399 cachedMultiConversions.clear();
406 template <
typename T,
typename FnT>
407 MaterializationCallbackFn wrapMaterialization(FnT &&callback)
const {
408 return [callback = std::forward<FnT>(callback)](
409 OpBuilder &builder,
Type resultType, ValueRange inputs,
410 Location loc) -> Value {
411 if (T derivedType = dyn_cast<T>(resultType))
412 return callback(builder, derivedType, inputs, loc);
425 template <
typename T,
typename FnT>
427 std::is_invocable_v<FnT, OpBuilder &, T, ValueRange, Location, Type>,
428 TargetMaterializationCallbackFn>
429 wrapTargetMaterialization(FnT &&callback)
const {
430 return [callback = std::forward<FnT>(callback)](
431 OpBuilder &builder, TypeRange resultTypes, ValueRange inputs,
432 Location loc,
Type originalType) -> SmallVector<Value> {
433 SmallVector<Value> result;
434 if constexpr (std::is_same<T, TypeRange>::value) {
437 result = callback(builder, resultTypes, inputs, loc, originalType);
438 }
else if constexpr (std::is_assignable<Type, T>::value) {
441 if (resultTypes.size() == 1) {
444 if (T derivedType = dyn_cast<T>(resultTypes.front())) {
449 callback(builder, derivedType, inputs, loc, originalType);
451 result.push_back(val);
455 static_assert(
sizeof(T) == 0,
"T must be a Type or a TypeRange");
463 template <
typename T,
typename FnT>
465 std::is_invocable_v<FnT, OpBuilder &, T, ValueRange, Location>,
466 TargetMaterializationCallbackFn>
467 wrapTargetMaterialization(FnT &&callback)
const {
468 return wrapTargetMaterialization<T>(
469 [callback = std::forward<FnT>(callback)](
470 OpBuilder &builder, T resultTypes, ValueRange inputs, Location loc,
472 return callback(builder, resultTypes, inputs, loc);
480 template <
typename T,
typename A,
typename FnT>
481 TypeAttributeConversionCallbackFn
482 wrapTypeAttributeConversion(FnT &&callback)
const {
483 return [callback = std::forward<FnT>(callback)](
484 Type type, Attribute attr) -> AttributeConversionResult {
485 if (T derivedType = dyn_cast<T>(type)) {
486 if (A derivedAttr = dyn_cast_or_null<A>(attr))
487 return callback(derivedType, derivedAttr);
495 registerTypeAttributeConversion(TypeAttributeConversionCallbackFn callback) {
496 typeAttributeConversions.emplace_back(std::move(callback));
498 cachedDirectConversions.clear();
499 cachedMultiConversions.clear();
503 SmallVector<ConversionCallbackFn, 4> conversions;
506 SmallVector<MaterializationCallbackFn, 2> argumentMaterializations;
507 SmallVector<MaterializationCallbackFn, 2> sourceMaterializations;
508 SmallVector<TargetMaterializationCallbackFn, 2> targetMaterializations;
511 SmallVector<TypeAttributeConversionCallbackFn, 2> typeAttributeConversions;
516 mutable DenseMap<Type, Type> cachedDirectConversions;
518 mutable DenseMap<Type, SmallVector<Type, 2>> cachedMultiConversions;
520 mutable llvm::sys::SmartRWMutex<true> cacheMutex;
539 llvm_unreachable(
"unimplemented rewrite");
550 virtual LogicalResult
553 if (failed(
match(op)))
555 rewrite(op, operands, rewriter);
561 virtual LogicalResult
575 template <
typename ConverterTy>
576 std::enable_if_t<std::is_base_of<TypeConverter, ConverterTy>::value,
585 using RewritePattern::RewritePattern;
588 template <
typename... Args>
613 template <
typename SourceOp>
618 typename SourceOp::template GenericAdaptor<ArrayRef<ValueRange>>;
630 return match(cast<SourceOp>(op));
634 auto sourceOp = cast<SourceOp>(op);
639 auto sourceOp = cast<SourceOp>(op);
645 auto sourceOp = cast<SourceOp>(op);
651 auto sourceOp = cast<SourceOp>(op);
658 virtual LogicalResult
match(SourceOp op)
const {
659 llvm_unreachable(
"must override match or matchAndRewrite");
663 llvm_unreachable(
"must override matchAndRewrite or a rewrite method");
671 virtual LogicalResult
674 if (failed(
match(op)))
676 rewrite(op, adaptor, rewriter);
679 virtual LogicalResult
694 template <
typename SourceOp>
699 SourceOp::getInterfaceID(), benefit, context) {}
703 SourceOp::getInterfaceID(), benefit, context) {}
709 rewrite(cast<SourceOp>(op), operands, rewriter);
713 rewrite(cast<SourceOp>(op), operands, rewriter);
730 llvm_unreachable(
"must override matchAndRewrite or a rewrite method");
736 virtual LogicalResult
739 if (failed(
match(op)))
741 rewrite(op, operands, rewriter);
744 virtual LogicalResult
757 template <
template <
typename>
class TraitType>
762 TypeID::
get<TraitType>(), benefit, context) {}
766 TypeID::
get<TraitType>(), benefit, context) {}
772 FailureOr<Operation *>
774 const TypeConverter &converter,
775 ConversionPatternRewriter &rewriter);
781 StringRef functionLikeOpName, RewritePatternSet &
patterns,
782 const TypeConverter &converter);
784 template <
typename FuncOpT>
792 RewritePatternSet &
patterns,
const TypeConverter &converter);
799 struct ConversionPatternRewriterImpl;
905 ValueRange argValues = std::nullopt)
override;
936 std::unique_ptr<detail::ConversionPatternRewriterImpl>
impl;
972 std::function<std::optional<bool>(
Operation *)>;
983 template <
typename OpT>
992 template <
typename OpT>
996 template <
typename OpT,
typename OpT2,
typename... OpTs>
1007 setLegalityCallback(op, callback);
1009 template <
typename OpT>
1014 template <
typename OpT,
typename OpT2,
typename... OpTs>
1016 addDynamicallyLegalOp<OpT>(callback);
1019 template <
typename OpT,
class Callable>
1020 std::enable_if_t<!std::is_invocable_v<Callable, Operation *>>
1022 addDynamicallyLegalOp<OpT>(
1023 [=](
Operation *op) {
return callback(cast<OpT>(op)); });
1031 template <
typename OpT>
1035 template <
typename OpT,
typename OpT2,
typename... OpTs>
1037 addIllegalOp<OpT>();
1048 template <
typename OpT>
1053 template <
typename OpT,
typename OpT2,
typename... OpTs>
1055 markOpRecursivelyLegal<OpT>(callback);
1058 template <
typename OpT,
class Callable>
1059 std::enable_if_t<!std::is_invocable_v<Callable, Operation *>>
1061 markOpRecursivelyLegal<OpT>(
1062 [=](
Operation *op) {
return callback(cast<OpT>(op)); });
1070 template <
typename... Names>
1075 template <
typename... Args>
1083 template <
typename... Names>
1085 StringRef name, Names... names) {
1088 setLegalityCallback(dialectNames, callback);
1090 template <
typename... Args>
1093 Args::getDialectNamespace()...);
1100 setLegalityCallback(fn);
1105 template <
typename... Names>
1110 template <
typename... Args>
1152 struct LegalizationInfo {
1157 bool isRecursivelyLegal =
false;
1164 std::optional<LegalizationInfo> getOpInfo(OperationName op)
const;
1168 llvm::MapVector<OperationName, LegalizationInfo> legalOperations;
1172 DenseMap<OperationName, DynamicLegalityCallbackFn> opRecursiveLegalityFns;
1176 llvm::StringMap<LegalizationAction> legalDialects;
1179 llvm::StringMap<DynamicLegalityCallbackFn> dialectLegalityFns;
1188 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
1224 class PDLConversionConfig final {
Attributes are known-constant values of operations.
This class represents an argument of a Block.
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 inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues=std::nullopt) override
PatternRewriter hook for inlining the ops of a block into another block.
Block * applySignatureConversion(Block *block, TypeConverter::SignatureConversion &conversion, const TypeConverter *converter=nullptr)
Apply a signature conversion to given block.
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, ArrayRef< ValueRange > newValues)
Replace the given operation with the new value ranges.
detail::ConversionPatternRewriterImpl & getImpl()
Return a reference to the internal implementation.
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.
void replaceUsesOfBlockArgument(BlockArgument from, Value to)
Replace all the uses of the block argument from with value to.
~ConversionPatternRewriter() override
Base class for the conversion patterns.
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...
virtual void rewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const
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...
virtual void rewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const
Hook for derived classes to implement rewriting.
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...
void rewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement rewriting.
LogicalResult match(Operation *op) const final
Wrappers around the ConversionPattern methods that pass the derived op type.
void rewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const final
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
virtual LogicalResult match(SourceOp op) const
Rewrite and Match methods that operate on the SourceOp type.
OpConversionPattern(const TypeConverter &typeConverter, MLIRContext *context, PatternBenefit benefit=1)
virtual LogicalResult matchAndRewrite(SourceOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const
virtual void rewrite(SourceOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const
LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement combined matching and rewriting.
virtual void rewrite(SourceOp op, OneToNOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const
OpInterfaceConversionPattern is a wrapper around ConversionPattern that allows for matching and rewri...
void rewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const final
LogicalResult matchAndRewrite(Operation *op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement combined matching and rewriting.
void rewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Wrappers around the ConversionPattern methods that pass the derived op type.
virtual LogicalResult matchAndRewrite(SourceOp op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const
LogicalResult matchAndRewrite(Operation *op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const final
Hook for derived classes to implement combined matching and rewriting.
OpInterfaceConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
OpInterfaceConversionPattern(const TypeConverter &typeConverter, MLIRContext *context, PatternBenefit benefit=1)
virtual void rewrite(SourceOp op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const
Rewrite and Match methods that operate on the SourceOp type.
virtual void rewrite(SourceOp op, ArrayRef< ValueRange > operands, ConversionPatternRewriter &rewriter) const
virtual LogicalResult matchAndRewrite(SourceOp op, ArrayRef< Value > operands, ConversionPatternRewriter &rewriter) const
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...
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.
virtual LogicalResult match(Operation *op) const
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
virtual void rewrite(Operation *op, PatternRewriter &rewriter) const
Rewrite the IR rooted at the specified operation with the result of this pattern, generating any new ...
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues=std::nullopt)
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, Value replacement)
Remap an input of the original signature to another replacement value.
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
void addConversion(FnT &&callback)
Register a conversion function.
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
void addArgumentMaterialization(FnT &&callback)
All of the following materializations require function objects that are convertible to the following ...
LogicalResult convertSignatureArgs(TypeRange types, SignatureConversion &result, unsigned origInputOffset=0) const
std::enable_if_t<!std::is_convertible< RangeT, Type >::value &&!std::is_convertible< RangeT, Operation * >::value, bool > isLegal(RangeT &&range) const
Return true if all of the given types are legal for this type converter.
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)
Value materializeArgumentConversion(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...
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)
This method registers a materialization that will be called when converting a replacement value back ...
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 set of types, filling 'results' as necessary.
bool isSignatureLegal(FunctionType ty) const
Return true if the inputs and outputs of the given function type are legal.
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...
@ Type
An inlay hint that for a type annotation.
Include the generated interface declarations.
void populateFunctionOpInterfaceTypeConversionPattern(StringRef functionLikeOpName, RewritePatternSet &patterns, const TypeConverter &converter)
Add a pattern to the given pattern list to convert the signature of a FunctionOpInterface op with the...
void populateAnyFunctionOpInterfaceTypeConversionPattern(RewritePatternSet &patterns, const TypeConverter &converter)
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.
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.
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.
void reconcileUnrealizedCasts(ArrayRef< UnrealizedConversionCastOp > castOps, SmallVectorImpl< UnrealizedConversionCastOp > *remainingCastOps=nullptr)
Try to reconcile all given UnrealizedConversionCastOps and store the left-over ops in remainingCastOp...
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.
RewriterBase::Listener * listener
An optional listener that is notified about all IR modifications in case dialect conversion succeeds.
function_ref< void(Diagnostic &)> notifyCallback
An optional callback used to notify about match failure diagnostics during the conversion.
DenseSet< Operation * > * legalizableOps
Analysis conversion only.
DenseSet< Operation * > * unlegalizedOps
Partial conversion only.
bool buildMaterializations
If set to "true", the dialect conversion attempts to build source/target/ argument materializations t...
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...