MLIR  22.0.0git
Classes | Public Member Functions | List of all members
mlir::TypeConverter Class Reference

Type conversion class. More...

#include "mlir/Transforms/DialectConversion.h"

+ Inheritance diagram for mlir::TypeConverter:

Classes

class  AttributeConversionResult
 The general result of a type attribute conversion callback, allowing for early termination. More...
 
class  SignatureConversion
 This class provides all of the information necessary to convert a type signature. More...
 

Public Member Functions

virtual ~TypeConverter ()=default
 
 TypeConverter ()=default
 
 TypeConverter (const TypeConverter &other)
 
TypeConverteroperator= (const TypeConverter &other)
 
template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<0>>
void addConversion (FnT &&callback)
 Register a conversion function. More...
 
template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<1>>
void addSourceMaterialization (FnT &&callback)
 All of the following materializations require function objects that are convertible to the following form: Value(OpBuilder &, T, ValueRange, Location), where T is any subclass of Type. More...
 
template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<1>>
void addTargetMaterialization (FnT &&callback)
 This method registers a materialization that will be called when converting a value to a target type according to a pattern's type converter. More...
 
template<typename FnT , typename T = typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<0>, typename A = typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<1>>
void addTypeAttributeConversion (FnT &&callback)
 Register a conversion function for attributes within types. More...
 
LogicalResult convertType (Type t, SmallVectorImpl< Type > &results) const
 Convert the given type. More...
 
LogicalResult convertType (Value v, SmallVectorImpl< Type > &results) const
 Convert the type of the given value. More...
 
Type convertType (Type t) const
 This hook simplifies defining 1-1 type conversions. More...
 
Type convertType (Value v) const
 
template<typename TargetType >
TargetType convertType (Type t) const
 Attempts a 1-1 type conversion, expecting the result type to be TargetType. More...
 
template<typename TargetType >
TargetType convertType (Value v) const
 
LogicalResult convertTypes (TypeRange types, SmallVectorImpl< Type > &results) const
 Convert the given types, filling 'results' as necessary. More...
 
LogicalResult convertTypes (ValueRange values, SmallVectorImpl< Type > &results) const
 Convert the types of the given values, filling 'results' as necessary. More...
 
bool isLegal (Type type) const
 Return true if the given type is legal for this type converter, i.e. More...
 
bool isLegal (Value value) const
 
bool isLegal (TypeRange range) const
 Return true if all of the given types are legal for this type converter. More...
 
bool isLegal (ValueRange range) const
 
bool isLegal (Operation *op) const
 Return true if the given operation has legal operand and result types. More...
 
bool isLegal (Region *region) const
 Return true if the types of block arguments within the region are legal. More...
 
bool isSignatureLegal (FunctionType ty) const
 Return true if the inputs and outputs of the given function type are legal. More...
 
LogicalResult convertSignatureArg (unsigned inputNo, Type type, SignatureConversion &result) const
 This method allows for converting a specific argument of a signature. More...
 
LogicalResult convertSignatureArgs (TypeRange types, SignatureConversion &result, unsigned origInputOffset=0) const
 
LogicalResult convertSignatureArg (unsigned inputNo, Value value, SignatureConversion &result) const
 
LogicalResult convertSignatureArgs (ValueRange values, SignatureConversion &result, unsigned origInputOffset=0) const
 
std::optional< SignatureConversionconvertBlockSignature (Block *block) const
 This function converts the type signature of the given block, by invoking 'convertSignatureArg' for each argument. More...
 
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 some kind. More...
 
Value materializeTargetConversion (OpBuilder &builder, Location loc, Type resultType, ValueRange inputs, Type originalType={}) const
 
SmallVector< ValuematerializeTargetConversion (OpBuilder &builder, Location loc, TypeRange resultType, ValueRange inputs, Type originalType={}) const
 
std::optional< AttributeconvertTypeAttribute (Type type, Attribute attr) const
 Convert an attribute present attr from within the type type using the registered conversion functions. More...
 

Detailed Description

Type conversion class.

Specific conversions and materializations can be registered using addConversion and addMaterialization, respectively.

Definition at line 41 of file DialectConversion.h.

Constructor & Destructor Documentation

◆ ~TypeConverter()

virtual mlir::TypeConverter::~TypeConverter ( )
virtualdefault

◆ TypeConverter() [1/2]

mlir::TypeConverter::TypeConverter ( )
default

◆ TypeConverter() [2/2]

mlir::TypeConverter::TypeConverter ( const TypeConverter other)
inline

Definition at line 46 of file DialectConversion.h.

Member Function Documentation

◆ addConversion()

template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<0>>
void mlir::TypeConverter::addConversion ( FnT &&  callback)
inline

Register a conversion function.

A conversion function must be convertible to any of the following forms (where T is Value or a class derived from Type, including Type itself):

  • std::optional<Type>(T)
    • This form represents a 1-1 type conversion. It should return nullptr or std::nullopt to signify failure. If std::nullopt is returned, the converter is allowed to try another conversion function to perform the conversion.
  • std::optional<LogicalResult>(T, SmallVectorImpl<Type> &)
    • This form represents a 1-N type conversion. It should return failure or std::nullopt to signify a failed conversion. If the new set of types is empty, the type is removed and any usages of the existing value are expected to be removed during conversion. If std::nullopt is returned, the converter is allowed to try another conversion function to perform the conversion.

Conversion functions that accept Value as the first argument are context-aware. I.e., they can take into account IR when converting the type of the given value. Context-unaware conversion functions accept Type or a derived class as the first argument.

Note: Context-unaware conversions are cached, but context-aware conversions are not.

Note: When attempting to convert a type, e.g. via 'convertType', the mostly recently added conversions will be invoked first.

Definition at line 170 of file DialectConversion.h.

Referenced by addOpaquePointerConversion(), mlir::configureGpuToNVVMTypeConverter(), mlir::xegpu::doSCFStructuralTypeConversionWithTensorType(), mlir::LLVMTypeConverter::LLVMTypeConverter(), mlir::spirv::MemorySpaceToStorageClassConverter::MemorySpaceToStorageClassConverter(), mlir::arith::NarrowTypeEmulationConverter::NarrowTypeEmulationConverter(), mlir::populateAMXLegalizeForLLVMExportPatterns(), mlir::populateArmSMEToLLVMConversionPatterns(), mlir::populateAsyncStructuralTypeConversionsAndLegality(), mlir::populateEmitCSizeTTypeConversions(), mlir::arith::populateEmulateUnsupportedFloatsConversions(), mlir::math::populateExtendToSupportedTypesTypeConverter(), mlir::memref::populateMemRefNarrowTypeEmulationConversions(), mlir::populateMemRefToEmitCTypeConversion(), mlir::memref::populateMemRefWideIntEmulationConversions(), mlir::populateMMAToSPIRVCoopMatrixTypeConversion(), mlir::mpi::populateMPIToLLVMConversionPatterns(), mlir::populateOpenMPToLLVMConversionPatterns(), mlir::ptr::populatePtrToLLVMConversionPatterns(), mlir::populateSPIRVToLLVMTypeConversion(), mlir::tosa::populateTosaTypeConversion(), mlir::SPIRVTypeConverter::SPIRVTypeConverter(), and mlir::arith::WideIntEmulationConverter::WideIntEmulationConverter().

◆ addSourceMaterialization()

template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<1>>
void mlir::TypeConverter::addSourceMaterialization ( FnT &&  callback)
inline

All of the following materializations require function objects that are convertible to the following form: Value(OpBuilder &, T, ValueRange, Location), where T is any subclass of Type.

This function is responsible for creating an operation, using the OpBuilder and Location provided, that "casts" a range of values into a single value of the given type T. It must return a Value of the type T on success and nullptr if it failed but other materialization should be attempted. Materialization functions must be provided when a type conversion may persist after the conversion has finished.

Note: Target materializations may optionally accept an additional Type parameter, which is the original type of the SSA value. Furthermore, T can be a TypeRange; in that case, the function must return a SmallVector<Value>. This method registers a materialization that will be called when converting a replacement value back to its original source type. This is used when some uses of the original value persist beyond the main conversion.

Definition at line 196 of file DialectConversion.h.

Referenced by mlir::xegpu::doSCFStructuralTypeConversionWithTensorType(), mlir::LLVMTypeConverter::LLVMTypeConverter(), mlir::populateEmitCSizeTTypeConversions(), mlir::populateMemRefToEmitCTypeConversion(), mlir::tosa::populateTosaTypeConversion(), and mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ addTargetMaterialization()

template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<1>>
void mlir::TypeConverter::addTargetMaterialization ( FnT &&  callback)
inline

This method registers a materialization that will be called when converting a value to a target type according to a pattern's type converter.

Note: Target materializations can optionally inspect the "original" type. This type may be different from the type of the input value. For example, let's assume that a conversion pattern "P1" replaced an SSA value "v1" (type "t1") with "v2" (type "t2"). Then a different conversion pattern "P2" matches an op that has "v1" as an operand. Let's furthermore assume that "P2" determines that the converted target type of "t1" is "t3", which may be different from "t2". In this example, the target materialization will be invoked with: outputType = "t3", inputs = "v2", originalType = "t1". Note that the original type "t1" cannot be recovered from just "t3" and "v2"; that's why the originalType parameter exists.

Note: During a 1:N conversion, the result types can be a TypeRange. In that case the materialization produces a SmallVector<Value>.

Definition at line 220 of file DialectConversion.h.

Referenced by mlir::xegpu::doSCFStructuralTypeConversionWithTensorType(), mlir::LLVMTypeConverter::LLVMTypeConverter(), mlir::populateEmitCSizeTTypeConversions(), mlir::arith::populateEmulateUnsupportedFloatsConversions(), mlir::math::populateExtendToSupportedTypesTypeConverter(), mlir::populateMemRefToEmitCTypeConversion(), mlir::tosa::populateTosaTypeConversion(), and mlir::SPIRVTypeConverter::SPIRVTypeConverter().

◆ addTypeAttributeConversion()

template<typename FnT , typename T = typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<0>, typename A = typename llvm::function_traits<std::decay_t<FnT>>::template arg_t<1>>
void mlir::TypeConverter::addTypeAttributeConversion ( FnT &&  callback)
inline

Register a conversion function for attributes within types.

Type converters may call this function in order to allow hoking into the translation of attributes that exist within types. For example, a type converter for the memref type could use these conversions to convert memory spaces or layouts in an extensible way.

The conversion functions take a non-null Type or subclass of Type and a non-null Attribute (or subclass of Attribute), and returns a AttributeConversionResult. This result can either contain an Attribute, which may be nullptr, representing the conversion's success, AttributeConversionResult::na() (the default empty value), indicating that the conversion function did not apply and that further conversion functions should be checked, or AttributeConversionResult::abort() indicating that the conversion process should be aborted.

Registered conversion functions are callled in the reverse of the order in which they were registered.

Definition at line 249 of file DialectConversion.h.

Referenced by mlir::LLVMTypeConverter::LLVMTypeConverter(), mlir::populateAMDGPUMemorySpaceAttributeConversions(), mlir::populateGpuMemorySpaceAttributeConversions(), and mlir::ptr::populatePtrToLLVMConversionPatterns().

◆ convertBlockSignature()

std::optional< TypeConverter::SignatureConversion > TypeConverter::convertBlockSignature ( Block block) const

This function converts the type signature of the given block, by invoking 'convertSignatureArg' for each argument.

This function should return a valid conversion for the signature on success, std::nullopt otherwise.

Definition at line 3588 of file DialectConversion.cpp.

References convertSignatureArgs(), mlir::remark::failed(), mlir::Block::getArguments(), and mlir::Block::getNumArguments().

Referenced by mlir::detail::ConversionPatternRewriterImpl::convertRegionTypes().

◆ convertSignatureArg() [1/2]

LogicalResult TypeConverter::convertSignatureArg ( unsigned  inputNo,
Type  type,
SignatureConversion result 
) const

This method allows for converting a specific argument of a signature.

It takes as inputs the original argument input number, type. On success, it populates 'result' with any new mappings.

Definition at line 3498 of file DialectConversion.cpp.

References mlir::TypeConverter::SignatureConversion::addInputs(), convertType(), and mlir::remark::failed().

Referenced by convertSignatureArgs().

◆ convertSignatureArg() [2/2]

LogicalResult TypeConverter::convertSignatureArg ( unsigned  inputNo,
Value  value,
SignatureConversion result 
) const

◆ convertSignatureArgs() [1/2]

LogicalResult TypeConverter::convertSignatureArgs ( TypeRange  types,
SignatureConversion result,
unsigned  origInputOffset = 0 
) const

◆ convertSignatureArgs() [2/2]

LogicalResult TypeConverter::convertSignatureArgs ( ValueRange  values,
SignatureConversion result,
unsigned  origInputOffset = 0 
) const

Definition at line 3539 of file DialectConversion.cpp.

References convertSignatureArg(), and mlir::remark::failed().

◆ convertType() [1/6]

Type TypeConverter::convertType ( Type  t) const

This hook simplifies defining 1-1 type conversions.

This function returns the type to convert to on success, and a null type on failure.

Definition at line 3434 of file DialectConversion.cpp.

References convertType(), and mlir::remark::failed().

◆ convertType() [2/6]

template<typename TargetType >
TargetType mlir::TypeConverter::convertType ( Type  t) const
inline

Attempts a 1-1 type conversion, expecting the result type to be TargetType.

Returns the converted type cast to TargetType on success, and a null type on conversion or cast failure.

Definition at line 281 of file DialectConversion.h.

References convertType().

◆ convertType() [3/6]

LogicalResult TypeConverter::convertType ( Type  t,
SmallVectorImpl< Type > &  results 
) const

Convert the given type.

This function returns failure if no valid conversion exists, success otherwise. If the new set of types is empty, the type is removed and any usages of the existing value are expected to be removed during conversion.

Note: This overload invokes only context-unaware type conversion functions. Users should call the other overload if possible.

Definition at line 3361 of file DialectConversion.cpp.

References mlir::Type::getContext(), and mlir::MLIRContext::isMultithreadingEnabled().

Referenced by adjustAccessChainForBitwidth(), broadcast(), convertArrayType(), mlir::spirv::convertMemRefTypesAndAttrs(), convertRuntimeArrayType(), convertSignatureArg(), convertType(), convertTypes(), mlir::ConvertToLLVMPattern::createMemRefDescriptor(), emitElementwiseComputation(), mlir::ConvertToLLVMPattern::getMemRefDescriptorSizes(), mlir::ConvertToLLVMPattern::getSizeInBytes(), mlir::ConvertToLLVMPattern::isConvertibleAndHasIdentityMaps(), isLegal(), legalizeBlockArguments(), lowerAsEntryFunction(), mlir::GPUDynamicSharedMemoryOpLowering::matchAndRewrite(), mlir::GPUFuncOpLowering::matchAndRewrite(), mlir::GPUPrintfOpToHIPLowering::matchAndRewrite(), mlir::GPUPrintfOpToLLVMCallLowering::matchAndRewrite(), mlir::GPUPrintfOpToVPrintfLowering::matchAndRewrite(), mlir::spirv::ElementwiseOpPattern< Op, SPIRVOp >::matchAndRewrite(), mlir::populateAsyncStructuralTypeConversionsAndLegality(), mlir::populateMemRefToEmitCTypeConversion(), mlir::memref::populateMemRefWideIntEmulationConversions(), mlir::tosa::populateTosaTypeConversion(), mlir::detail::ConversionPatternRewriterImpl::remapValues(), replaceWithLoadOrStore(), and wmmaPushInputOperand().

◆ convertType() [4/6]

Type TypeConverter::convertType ( Value  v) const

Definition at line 3444 of file DialectConversion.cpp.

References convertType(), and mlir::remark::failed().

◆ convertType() [5/6]

template<typename TargetType >
TargetType mlir::TypeConverter::convertType ( Value  v) const
inline

Definition at line 285 of file DialectConversion.h.

References convertType().

◆ convertType() [6/6]

LogicalResult TypeConverter::convertType ( Value  v,
SmallVectorImpl< Type > &  results 
) const

Convert the type of the given value.

This function returns failure if no valid conversion exists, success otherwise. If the new set of types is empty, the type is removed and any usages of the existing value are expected to be removed during conversion.

Note: This overload invokes both context-aware and context-unaware type conversion functions.

Definition at line 3413 of file DialectConversion.cpp.

References convertType(), and mlir::Value::getType().

◆ convertTypeAttribute()

std::optional< Attribute > TypeConverter::convertTypeAttribute ( Type  type,
Attribute  attr 
) const

Convert an attribute present attr from within the type type using the registered conversion functions.

If no applicable conversion has been registered, return std::nullopt. Note that the empty attribute/nullptr is a valid return value for this function.

Definition at line 3631 of file DialectConversion.cpp.

References mlir::TypeConverter::AttributeConversionResult::getResult(), mlir::TypeConverter::AttributeConversionResult::hasResult(), and mlir::TypeConverter::AttributeConversionResult::isAbort().

Referenced by mlir::LLVMTypeConverter::getMemRefAddressSpace(), and mlir::ptr::populatePtrToLLVMConversionPatterns().

◆ convertTypes() [1/2]

LogicalResult TypeConverter::convertTypes ( TypeRange  types,
SmallVectorImpl< Type > &  results 
) const

◆ convertTypes() [2/2]

LogicalResult TypeConverter::convertTypes ( ValueRange  values,
SmallVectorImpl< Type > &  results 
) const

Convert the types of the given values, filling 'results' as necessary.

This returns "failure" if the conversion of any of the types fails, "success" otherwise.

Definition at line 3464 of file DialectConversion.cpp.

References convertType(), and mlir::remark::failed().

◆ isLegal() [1/6]

bool TypeConverter::isLegal ( Operation op) const

Return true if the given operation has legal operand and result types.

Definition at line 3480 of file DialectConversion.cpp.

References mlir::Operation::getOperands(), mlir::Operation::getResults(), and isLegal().

◆ isLegal() [2/6]

bool TypeConverter::isLegal ( Region region) const

Return true if the types of block arguments within the region are legal.

Definition at line 3484 of file DialectConversion.cpp.

References mlir::Block::getArguments(), and isLegal().

◆ isLegal() [3/6]

bool TypeConverter::isLegal ( Type  type) const

◆ isLegal() [4/6]

bool mlir::TypeConverter::isLegal ( TypeRange  range) const
inline

Return true if all of the given types are legal for this type converter.

Definition at line 307 of file DialectConversion.h.

References isLegal().

◆ isLegal() [5/6]

bool TypeConverter::isLegal ( Value  value) const

Definition at line 3476 of file DialectConversion.cpp.

References convertType(), and mlir::Value::getType().

◆ isLegal() [6/6]

bool mlir::TypeConverter::isLegal ( ValueRange  range) const
inline

Definition at line 310 of file DialectConversion.h.

References isLegal().

◆ isSignatureLegal()

bool TypeConverter::isSignatureLegal ( FunctionType  ty) const

Return true if the inputs and outputs of the given function type are legal.

Definition at line 3489 of file DialectConversion.cpp.

References isLegal().

◆ materializeSourceConversion()

Value TypeConverter::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 some kind.

See the respective add*Materialization for more information on the context for these methods.

Definition at line 3548 of file DialectConversion.cpp.

Referenced by legalizeBlockArguments().

◆ materializeTargetConversion() [1/2]

Value TypeConverter::materializeTargetConversion ( OpBuilder builder,
Location  loc,
Type  resultType,
ValueRange  inputs,
Type  originalType = {} 
) const

Definition at line 3558 of file DialectConversion.cpp.

◆ materializeTargetConversion() [2/2]

SmallVector< Value > TypeConverter::materializeTargetConversion ( OpBuilder builder,
Location  loc,
TypeRange  resultType,
ValueRange  inputs,
Type  originalType = {} 
) const

Definition at line 3570 of file DialectConversion.cpp.

◆ operator=()

TypeConverter& mlir::TypeConverter::operator= ( const TypeConverter other)
inline

Definition at line 51 of file DialectConversion.h.


The documentation for this class was generated from the following files: