MLIR
17.0.0git
|
Conversion from types to the LLVM IR dialect. More...
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
Public Member Functions | |
LLVMTypeConverter (MLIRContext *ctx, const DataLayoutAnalysis *analysis=nullptr) | |
Create an LLVMTypeConverter using the default LowerToLLVMOptions. More... | |
LLVMTypeConverter (MLIRContext *ctx, const LowerToLLVMOptions &options, const DataLayoutAnalysis *analysis=nullptr) | |
Create an LLVMTypeConverter using custom LowerToLLVMOptions. More... | |
Type | convertFunctionSignature (FunctionType funcTy, bool isVariadic, bool useBarePtrCallConv, SignatureConversion &result) |
Convert a function type. More... | |
Type | packFunctionResults (TypeRange types, bool useBarePointerCallConv=false) |
Convert a non-empty list of types to be returned from a function into an LLVM-compatible type. More... | |
Type | packOperationResults (TypeRange types) |
Convert a non-empty list of types of values produced by an operation into an LLVM-compatible type. More... | |
Type | convertCallingConventionType (Type type, bool useBarePointerCallConv=false) |
Convert a type in the context of the default or bare pointer calling convention. More... | |
void | promoteBarePtrsToDescriptors (ConversionPatternRewriter &rewriter, Location loc, ArrayRef< Type > stdTypes, SmallVectorImpl< Value > &values) |
Promote the bare pointers in 'values' that resulted from memrefs to descriptors. More... | |
MLIRContext & | getContext () |
Returns the MLIR context. More... | |
LLVM::LLVMDialect * | getDialect () |
Returns the LLVM dialect. More... | |
const LowerToLLVMOptions & | getOptions () const |
void | dangerousSetOptions (LowerToLLVMOptions newOptions) |
Set the lowering options to newOptions . More... | |
SmallVector< Value, 4 > | promoteOperands (Location loc, ValueRange opOperands, ValueRange operands, OpBuilder &builder, bool useBarePtrCallConv=false) |
Promote the LLVM representation of all operands including promoting MemRef descriptors to stack and use pointers to struct to avoid the complexity of the platform-specific C/C++ ABI lowering related to struct argument passing. More... | |
Value | promoteOneMemRefDescriptor (Location loc, Value operand, OpBuilder &builder) |
Promote the LLVM struct representation of one MemRef descriptor to stack and use pointer to struct to avoid the complexity of the platform-specific C/C++ ABI lowering related to struct argument passing. More... | |
std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > | convertFunctionTypeCWrapper (FunctionType type) |
Converts the function type to a C-compatible format, in particular using pointers to memref descriptors for arguments. More... | |
const llvm::DataLayout & | getDataLayout () |
Returns the data layout to use during and after conversion. More... | |
const DataLayoutAnalysis * | getDataLayoutAnalysis () const |
Returns the data layout analysis to query during conversion. More... | |
Type | getIndexType () |
Gets the LLVM representation of the index type. More... | |
bool | useOpaquePointers () const |
Returns true if using opaque pointers was enabled in the lowering options. More... | |
LLVM::LLVMPointerType | getPointerType (Type elementType, unsigned addressSpace=0) |
Creates an LLVM pointer type with the given element type and address space. More... | |
unsigned | getIndexTypeBitwidth () |
Gets the bitwidth of the index type when converted to LLVM. More... | |
unsigned | getPointerBitwidth (unsigned addressSpace=0) |
Gets the pointer bitwidth. More... | |
unsigned | getMemRefDescriptorSize (MemRefType type, const DataLayout &layout) |
Returns the size of the memref descriptor object in bytes. More... | |
unsigned | getUnrankedMemRefDescriptorSize (UnrankedMemRefType type, const DataLayout &layout) |
Returns the size of the unranked memref descriptor object in bytes. More... | |
FailureOr< unsigned > | getMemRefAddressSpace (BaseMemRefType type) |
Return the LLVM address space corresponding to the memory space of the memref type type or failure if the memory space cannot be converted to an integer. More... | |
LogicalResult | convertType (Type t, SmallVectorImpl< Type > &results) |
Convert the given type. More... | |
Type | convertType (Type t) |
This hook simplifies defining 1-1 type conversions. More... | |
template<typename TargetType > | |
TargetType | convertType (Type t) |
Attempts a 1-1 type conversion, expecting the result type to be TargetType . More... | |
![]() | |
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 | addArgumentMaterialization (FnT &&callback) |
Register a materialization function, which must be convertible to the following form: std::optional<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 | addSourceMaterialization (FnT &&callback) |
This method registers a materialization that will be called when converting a legal type to an illegal source 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 type from an illegal, or source, type to a legal type. 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) |
Convert the given type. More... | |
Type | convertType (Type t) |
This hook simplifies defining 1-1 type conversions. More... | |
template<typename TargetType > | |
TargetType | convertType (Type t) |
Attempts a 1-1 type conversion, expecting the result type to be TargetType . More... | |
LogicalResult | convertTypes (TypeRange types, SmallVectorImpl< Type > &results) |
Convert the given set of types, filling 'results' as necessary. More... | |
bool | isLegal (Type type) |
Return true if the given type is legal for this type converter, i.e. More... | |
template<typename RangeT > | |
std::enable_if_t<!std::is_convertible< RangeT, Type >::value &&!std::is_convertible< RangeT, Operation * >::value, bool > | isLegal (RangeT &&range) |
Return true if all of the given types are legal for this type converter. More... | |
bool | isLegal (Operation *op) |
Return true if the given operation has legal operand and result types. More... | |
bool | isLegal (Region *region) |
Return true if the types of block arguments within the region are legal. More... | |
bool | isSignatureLegal (FunctionType ty) |
Return true if the inputs and outputs of the given function type are legal. More... | |
LogicalResult | convertSignatureArg (unsigned inputNo, Type type, SignatureConversion &result) |
This method allows for converting a specific argument of a signature. More... | |
LogicalResult | convertSignatureArgs (TypeRange types, SignatureConversion &result, unsigned origInputOffset=0) |
std::optional< SignatureConversion > | convertBlockSignature (Block *block) |
This function converts the type signature of the given block, by invoking 'convertSignatureArg' for each argument. More... | |
Value | materializeArgumentConversion (OpBuilder &builder, Location loc, Type resultType, ValueRange inputs) |
Materialize a conversion from a set of types into one result type by generating a cast sequence of some kind. More... | |
Value | materializeSourceConversion (OpBuilder &builder, Location loc, Type resultType, ValueRange inputs) |
Value | materializeTargetConversion (OpBuilder &builder, Location loc, Type resultType, ValueRange inputs) |
std::optional< Attribute > | convertTypeAttribute (Type type, Attribute attr) |
Convert an attribute present attr from within the type type using the registered conversion functions. More... | |
Static Public Member Functions | |
static bool | canConvertToBarePtr (BaseMemRefType type) |
Check if a memref type can be converted to a bare pointer. More... | |
Protected Attributes | |
LLVM::LLVMDialect * | llvmDialect |
Pointer to the LLVM dialect. More... | |
Friends | |
LogicalResult | structFuncArgTypeConverter (LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result) |
Give structFuncArgTypeConverter access to memref-specific functions. More... | |
Conversion from types to the LLVM IR dialect.
Definition at line 33 of file TypeConverter.h.
LLVMTypeConverter::LLVMTypeConverter | ( | MLIRContext * | ctx, |
const DataLayoutAnalysis * | analysis = nullptr |
||
) |
Create an LLVMTypeConverter using the default LowerToLLVMOptions.
Create an LLVMTypeConverter using default LowerToLLVMOptions.
Optionally takes a data layout analysis to use in conversions.
Definition at line 19 of file TypeConverter.cpp.
LLVMTypeConverter::LLVMTypeConverter | ( | MLIRContext * | ctx, |
const LowerToLLVMOptions & | options, | ||
const DataLayoutAnalysis * | analysis = nullptr |
||
) |
Create an LLVMTypeConverter using custom LowerToLLVMOptions.
Optionally takes a data layout analysis to use in conversions.
Definition at line 24 of file TypeConverter.cpp.
References mlir::TypeConverter::addArgumentMaterialization(), mlir::TypeConverter::addConversion(), mlir::TypeConverter::addSourceMaterialization(), mlir::TypeConverter::addTargetMaterialization(), mlir::TypeConverter::addTypeAttributeConversion(), convertType(), mlir::TypeConverter::convertTypes(), mlir::OpBuilder::create(), mlir::failed(), mlir::failure(), mlir::get(), mlir::LLVM::LLVMStructType::getIdentified(), mlir::LLVM::LLVMStructType::getLiteral(), mlir::Operation::getResult(), mlir::LLVM::isCompatibleType(), llvmDialect, mlir::MemRefDescriptor::pack(), mlir::UnrankedMemRefDescriptor::pack(), and mlir::success().
|
static |
Check if a memref type can be converted to a bare pointer.
Definition at line 414 of file TypeConverter.cpp.
References mlir::failed(), and mlir::getStridesAndOffset().
Referenced by canBeCalledWithBarePointers().
Convert a type in the context of the default or bare pointer calling convention.
Calling convention sensitive types, such as MemRefType and UnrankedMemRefType, are converted following the specific rules for the calling convention. Calling convention independent types are converted following the default LLVM type conversions.
Definition at line 476 of file TypeConverter.cpp.
References convertType().
Referenced by mlir::barePtrFuncArgTypeConverter(), and packFunctionResults().
Type LLVMTypeConverter::convertFunctionSignature | ( | FunctionType | funcTy, |
bool | isVariadic, | ||
bool | useBarePtrCallConv, | ||
LLVMTypeConverter::SignatureConversion & | result | ||
) |
Convert a function type.
The arguments and results are converted one by one and results are packed into a wrapped LLVM IR structure type. result
is populated with argument mapping.
Definition at line 223 of file TypeConverter.cpp.
References mlir::TypeConverter::SignatureConversion::addInputs(), mlir::barePtrFuncArgTypeConverter(), mlir::detail::enumerate(), mlir::failed(), mlir::get(), getContext(), mlir::TypeConverter::SignatureConversion::getConvertedTypes(), packFunctionResults(), structFuncArgTypeConverter, and mlir::LowerToLLVMOptions::useBarePtrCallConv.
Referenced by mlir::GPUFuncOpLowering::matchAndRewrite().
std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > LLVMTypeConverter::convertFunctionTypeCWrapper | ( | FunctionType | type | ) |
Converts the function type to a C-compatible format, in particular using pointers to memref descriptors for arguments.
Also converts the return type to a pointer argument if it is a struct. Returns true if this was the case.
Definition at line 254 of file TypeConverter.cpp.
References convertType(), mlir::get(), getContext(), getPointerType(), mlir::LLVM::isCompatibleType(), and packFunctionResults().
Referenced by wrapExternalFunction(), and wrapForExternalCallers().
Type TypeConverter::convertType |
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 224 of file DialectConversion.cpp.
|
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 230 of file DialectConversion.h.
LogicalResult TypeConverter::convertType |
Convert the given type.
This function should return 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.
Definition at line 220 of file DialectConversion.cpp.
Referenced by broadcast(), mlir::MemRefDescriptor::bufferPtr(), convertCallingConventionType(), convertFunctionTypeCWrapper(), convertPointerType(), mlir::ConvertToLLVMPattern::copyUnrankedDescriptors(), mlir::AllocationOpLLVMLowering::createIndexConstant(), mlir::LLVM::detail::extractNDVectorTypeInfo(), extractOne(), mlir::MemRefDescriptor::fromStaticShape(), getIndexedPtrs(), getMemRefAlignment(), getPtrToElementType(), insertOne(), LLVMTypeConverter(), mlir::GPUFuncOpLowering::matchAndRewrite(), modifyFuncOpToUseBarePtrCallingConv(), mlir::MemRefDescriptor::pack(), mlir::UnrankedMemRefDescriptor::pack(), packOperationResults(), mlir::structFuncArgTypeConverter(), wrapExternalFunction(), and wrapForExternalCallers().
|
inline |
Set the lowering options to newOptions
.
Note: using this after some some conversions have been performed can lead to inconsistencies in the IR.
Definition at line 98 of file TypeConverter.h.
MLIRContext & LLVMTypeConverter::getContext | ( | ) |
Returns the MLIR context.
Definition at line 169 of file TypeConverter.cpp.
References getDialect().
Referenced by addOpaquePointerConversion(), convertFunctionSignature(), convertFunctionTypeCWrapper(), getIndexType(), getPointerType(), packFunctionResults(), packOperationResults(), mlir::populateArmSVELegalizeForLLVMExportPatterns(), mlir::populateGpuToLLVMConversionPatterns(), mlir::populateGpuToNVVMConversionPatterns(), and mlir::populateGpuToROCDLConversionPatterns().
|
inline |
Returns the data layout to use during and after conversion.
Definition at line 124 of file TypeConverter.h.
References mlir::LowerToLLVMOptions::dataLayout.
Referenced by getMemRefAlignment().
|
inline |
Returns the data layout analysis to query during conversion.
Definition at line 127 of file TypeConverter.h.
|
inline |
Returns the LLVM dialect.
Definition at line 91 of file TypeConverter.h.
References llvmDialect.
Referenced by getContext(), mlir::ConvertToLLVMPattern::getDialect(), and mlir::populateVectorToLLVMConversionPatterns().
Type LLVMTypeConverter::getIndexType | ( | ) |
Gets the LLVM representation of the index type.
The returned type is an integer type with the size configured for this type converter.
Definition at line 173 of file TypeConverter.cpp.
References mlir::get(), getContext(), and getIndexTypeBitwidth().
Referenced by mlir::MemRefDescriptor::bufferPtr(), mlir::UnrankedMemRefDescriptor::computeSizes(), mlir::ConvertToLLVMPattern::copyUnrankedDescriptors(), mlir::ConvertToLLVMPattern::getIndexType(), getMemRefDescriptorSize(), getUnrankedMemRefDescriptorSize(), mlir::UnrankedMemRefDescriptor::offset(), mlir::UnrankedMemRefDescriptor::offsetBasePtr(), mlir::UnrankedMemRefDescriptor::setSize(), mlir::UnrankedMemRefDescriptor::setStride(), mlir::UnrankedMemRefDescriptor::size(), mlir::UnrankedMemRefDescriptor::sizeBasePtr(), mlir::UnrankedMemRefDescriptor::stride(), and mlir::UnrankedMemRefDescriptor::strideBasePtr().
|
inline |
Gets the bitwidth of the index type when converted to LLVM.
Definition at line 147 of file TypeConverter.h.
References mlir::LowerToLLVMOptions::getIndexBitwidth().
Referenced by mlir::UnrankedMemRefDescriptor::computeSizes(), and getIndexType().
FailureOr< unsigned > LLVMTypeConverter::getMemRefAddressSpace | ( | BaseMemRefType | type | ) |
Return the LLVM address space corresponding to the memory space of the memref type type
or failure if the memory space cannot be converted to an integer.
Definition at line 399 of file TypeConverter.cpp.
References mlir::TypeConverter::convertTypeAttribute(), mlir::failure(), and mlir::BaseMemRefType::getMemorySpace().
Referenced by castAllocFuncResult(), castDataPtr(), mlir::ConvertToLLVMPattern::copyUnrankedDescriptors(), mlir::ConvertToLLVMPattern::getElementPtrType(), getMemRefDescriptorSize(), getUnrankedMemRefDescriptorSize(), and isMemRefTypeSupported().
unsigned LLVMTypeConverter::getMemRefDescriptorSize | ( | MemRefType | type, |
const DataLayout & | layout | ||
) |
Returns the size of the memref descriptor object in bytes.
Definition at line 351 of file TypeConverter.cpp.
References getIndexType(), getMemRefAddressSpace(), getPointerBitwidth(), and mlir::DataLayout::getTypeSize().
|
inline |
Definition at line 93 of file TypeConverter.h.
Referenced by mlir::populateFinalizeMemRefToLLVMConversionPatterns(), and useOpaquePointers().
unsigned LLVMTypeConverter::getPointerBitwidth | ( | unsigned | addressSpace = 0 | ) |
Gets the pointer bitwidth.
Definition at line 184 of file TypeConverter.cpp.
References mlir::LowerToLLVMOptions::dataLayout.
Referenced by mlir::UnrankedMemRefDescriptor::computeSizes(), getMemRefDescriptorSize(), and getUnrankedMemRefDescriptorSize().
LLVM::LLVMPointerType LLVMTypeConverter::getPointerType | ( | Type | elementType, |
unsigned | addressSpace = 0 |
||
) |
Creates an LLVM pointer type with the given element type and address space.
This function is meant to be used in code supporting both typed and opaque pointers, as it will create an opaque pointer with the given address space if opaque pointers are enabled in the lowering options.
Definition at line 178 of file TypeConverter.cpp.
References mlir::get(), getContext(), and useOpaquePointers().
Referenced by addOpaquePointerConversion(), bitAndAddrspaceCast(), castAllocFuncResult(), convertFunctionTypeCWrapper(), convertPointerType(), createPrintMsg(), mlir::ConvertToLLVMPattern::getElementPtrType(), mlir::ConvertToLLVMPattern::getMemRefDescriptorSizes(), mlir::ConvertToLLVMPattern::getSizeInBytes(), mlir::GPUFuncOpLowering::matchAndRewrite(), mlir::GPUPrintfOpToHIPLowering::matchAndRewrite(), mlir::GPUPrintfOpToLLVMCallLowering::matchAndRewrite(), promoteOneMemRefDescriptor(), mlir::UnrankedMemRefDescriptor::setSize(), mlir::UnrankedMemRefDescriptor::setStride(), mlir::UnrankedMemRefDescriptor::size(), mlir::UnrankedMemRefDescriptor::stride(), mlir::UnrankedMemRefDescriptor::strideBasePtr(), and wrapExternalFunction().
unsigned LLVMTypeConverter::getUnrankedMemRefDescriptorSize | ( | UnrankedMemRefType | type, |
const DataLayout & | layout | ||
) |
Returns the size of the unranked memref descriptor object in bytes.
Definition at line 383 of file TypeConverter.cpp.
References getIndexType(), getMemRefAddressSpace(), getPointerBitwidth(), and mlir::DataLayout::getTypeSize().
Convert a non-empty list of types to be returned from a function into an LLVM-compatible type.
In particular, if more than one value is returned, create an LLVM dialect structure type with elements that correspond to each of the types converted with convertCallingConventionType
.
Definition at line 524 of file TypeConverter.cpp.
References convertCallingConventionType(), getContext(), mlir::LLVM::LLVMStructType::getLiteral(), mlir::LLVM::isCompatibleType(), and mlir::LowerToLLVMOptions::useBarePtrCallConv.
Referenced by convertFunctionSignature(), and convertFunctionTypeCWrapper().
Convert a non-empty list of types of values produced by an operation into an LLVM-compatible type.
In particular, if more than one value is produced, create a literal structure with elements that correspond to each of the LLVM-compatible types converted with convertType
.
In particular, if more than one value is produced, create a literal structure with elements that correspond to each of the types converted with convertType
.
Definition at line 503 of file TypeConverter.cpp.
References convertType(), getContext(), mlir::LLVM::LLVMStructType::getLiteral(), and mlir::LLVM::isCompatibleType().
void LLVMTypeConverter::promoteBarePtrsToDescriptors | ( | ConversionPatternRewriter & | rewriter, |
Location | loc, | ||
ArrayRef< Type > | stdTypes, | ||
SmallVectorImpl< Value > & | values | ||
) |
Promote the bare pointers in 'values' that resulted from memrefs to descriptors.
'stdTypes' holds the types of 'values' before the conversion to the LLVM-IR dialect (i.e., MemRefType, or any other builtin type).
'stdTypes' holds they types of 'values' before the conversion to the LLVM-IR dialect (i.e., MemRefType, or any other builtin type).
Definition at line 488 of file TypeConverter.cpp.
References mlir::MemRefDescriptor::fromStaticShape().
Value LLVMTypeConverter::promoteOneMemRefDescriptor | ( | Location | loc, |
Value | operand, | ||
OpBuilder & | builder | ||
) |
Promote the LLVM struct representation of one MemRef descriptor to stack and use pointer to struct to avoid the complexity of the platform-specific C/C++ ABI lowering related to struct argument passing.
Definition at line 544 of file TypeConverter.cpp.
References mlir::OpBuilder::create(), mlir::Builder::getI64Type(), mlir::Builder::getIndexAttr(), getPointerType(), and mlir::Value::getType().
SmallVector< Value, 4 > LLVMTypeConverter::promoteOperands | ( | Location | loc, |
ValueRange | opOperands, | ||
ValueRange | operands, | ||
OpBuilder & | builder, | ||
bool | useBarePtrCallConv = false |
||
) |
Promote the LLVM representation of all operands including promoting MemRef descriptors to stack and use pointers to struct to avoid the complexity of the platform-specific C/C++ ABI lowering related to struct argument passing.
Definition at line 559 of file TypeConverter.cpp.
References mlir::MemRefDescriptor::alignedPtr(), mlir::MemRefDescriptor::unpack(), mlir::UnrankedMemRefDescriptor::unpack(), and mlir::LowerToLLVMOptions::useBarePtrCallConv.
|
inline |
Returns true if using opaque pointers was enabled in the lowering options.
Definition at line 136 of file TypeConverter.h.
References getOptions(), and mlir::LowerToLLVMOptions::useOpaquePointers.
Referenced by bitAndAddrspaceCast(), castAllocFuncResult(), castDataPtr(), createPrintMsg(), and getPointerType().
|
friend |
Give structFuncArgTypeConverter access to memref-specific functions.
It converts a MemRef function argument to a list of non-aggregate types containing descriptor information, and an UnrankedmemRef function argument to a list containing the rank and a pointer to a descriptor struct.
Referenced by convertFunctionSignature().
|
protected |
Pointer to the LLVM dialect.
Definition at line 169 of file TypeConverter.h.
Referenced by getDialect(), and LLVMTypeConverter().