MLIR 22.0.0git
mlir::LLVMTypeConverter Class Reference

Conversion from types to the LLVM IR dialect. More...

#include "mlir/Conversion/LLVMCommon/TypeConverter.h"

Inheritance diagram for mlir::LLVMTypeConverter:

Public Member Functions

 LLVMTypeConverter (MLIRContext *ctx, const DataLayoutAnalysis *analysis=nullptr)
 Create an LLVMTypeConverter using the default LowerToLLVMOptions.
 LLVMTypeConverter (MLIRContext *ctx, const LowerToLLVMOptions &options, const DataLayoutAnalysis *analysis=nullptr)
 Create an LLVMTypeConverter using custom LowerToLLVMOptions.
Type convertFunctionSignature (FunctionType funcTy, bool isVariadic, bool useBarePtrCallConv, SignatureConversion &result) const
 Convert a function type.
Type convertFunctionSignature (FunctionOpInterface funcOp, bool isVariadic, bool useBarePtrCallConv, LLVMTypeConverter::SignatureConversion &result, SmallVectorImpl< std::optional< NamedAttribute > > &byValRefNonPtrAttrs) const
 Convert a function type.
Type packFunctionResults (TypeRange types, bool useBarePointerCallConv=false, SmallVector< SmallVector< Type > > *groupedTypes=nullptr, int64_t *numConvertedTypes=nullptr) const
 Convert a non-empty list of types to be returned from a function into an LLVM-compatible type.
Type packOperationResults (TypeRange types) const
 Convert a non-empty list of types of values produced by an operation into an LLVM-compatible type.
LogicalResult convertCallingConventionType (Type type, SmallVectorImpl< Type > &result, bool useBarePointerCallConv=false) const
 Convert a type in the context of the default or bare pointer calling convention.
MLIRContextgetContext () const
 Returns the MLIR context.
LLVM::LLVMDialect * getDialect () const
 Returns the LLVM dialect.
const LowerToLLVMOptionsgetOptions () const
SmallVector< Value, 4 > promoteOperands (Location loc, ValueRange opOperands, ArrayRef< ValueRange > adaptorOperands, OpBuilder &builder, bool useBarePtrCallConv=false) const
 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.
SmallVector< Value, 4 > promoteOperands (Location loc, ValueRange opOperands, ValueRange adaptorOperands, OpBuilder &builder, bool useBarePtrCallConv=false) const
Value promoteOneMemRefDescriptor (Location loc, Value operand, OpBuilder &builder) const
 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.
std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > convertFunctionTypeCWrapper (FunctionType type) const
 Converts the function type to a C-compatible format, in particular using pointers to memref descriptors for arguments.
const llvm::DataLayout & getDataLayout () const
 Returns the data layout to use during and after conversion.
const DataLayoutAnalysisgetDataLayoutAnalysis () const
 Returns the data layout analysis to query during conversion.
Type getIndexType () const
 Gets the LLVM representation of the index type.
unsigned getIndexTypeBitwidth () const
 Gets the bitwidth of the index type when converted to LLVM.
unsigned getPointerBitwidth (unsigned addressSpace=0) const
 Gets the pointer bitwidth.
unsigned getMemRefDescriptorSize (MemRefType type, const DataLayout &layout) const
 Returns the size of the memref descriptor object in bytes.
unsigned getUnrankedMemRefDescriptorSize (UnrankedMemRefType type, const DataLayout &layout) const
 Returns the size of the unranked memref descriptor object in bytes.
FailureOr< unsignedgetMemRefAddressSpace (BaseMemRefType type) const
 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.
SmallVector< Type, 5 > getMemRefDescriptorFields (MemRefType type, bool unpackAggregates) const
 Convert a memref type into a list of LLVM IR types that will form the memref descriptor.
SmallVector< Type, 2 > getUnrankedMemRefDescriptorFields () const
 Convert an unranked memref type into a list of non-aggregate LLVM IR types that will form the unranked memref descriptor.

Static Public Member Functions

static bool canConvertToBarePtr (BaseMemRefType type)
 Check if a memref type can be converted to a bare pointer.

Protected Member Functions

SmallVector< Type > & getCurrentThreadRecursiveStack ()

Protected Attributes

LLVM::LLVMDialect * llvmDialect
 Pointer to the LLVM dialect.
DenseMap< uint64_t, std::unique_ptr< SmallVector< Type > > > conversionCallStack
llvm::sys::SmartRWMutex< truecallStackMutex

Friends

LogicalResult structFuncArgTypeConverter (const LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
 Give structFuncArgTypeConverter access to memref-specific functions.

Detailed Description

Conversion from types to the LLVM IR dialect.

Definition at line 35 of file TypeConverter.h.

Constructor & Destructor Documentation

◆ LLVMTypeConverter() [1/2]

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 42 of file TypeConverter.cpp.

References LLVMTypeConverter().

Referenced by LLVMTypeConverter(), and structFuncArgTypeConverter.

◆ LLVMTypeConverter() [2/2]

LLVMTypeConverter::LLVMTypeConverter ( MLIRContext * ctx,
const LowerToLLVMOptions & options,
const DataLayoutAnalysis * analysis = nullptr )

Member Function Documentation

◆ canConvertToBarePtr()

bool LLVMTypeConverter::canConvertToBarePtr ( BaseMemRefType type)
static

Check if a memref type can be converted to a bare pointer.

Definition at line 593 of file TypeConverter.cpp.

Referenced by canBeCalledWithBarePointers().

◆ convertCallingConventionType()

LogicalResult LLVMTypeConverter::convertCallingConventionType ( Type type,
SmallVectorImpl< Type > & result,
bool useBarePtrCallConv = false ) const

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 662 of file TypeConverter.cpp.

References result, and success().

Referenced by mlir::barePtrFuncArgTypeConverter(), and packFunctionResults().

◆ convertFunctionSignature() [1/2]

Type LLVMTypeConverter::convertFunctionSignature ( FunctionOpInterface funcOp,
bool isVariadic,
bool useBarePtrCallConv,
LLVMTypeConverter::SignatureConversion & result,
SmallVectorImpl< std::optional< NamedAttribute > > & byValRefNonPtrAttrs ) const

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. Converted types of llvm.byval and llvm.byref function arguments which are not LLVM pointers are overridden with LLVM pointers. Overridden arguments are returned in byValRefNonPtrAttrs.

Definition at line 411 of file TypeConverter.cpp.

References filterByValRefArgAttrs(), and result.

◆ convertFunctionSignature() [2/2]

Type mlir::LLVMTypeConverter::convertFunctionSignature ( FunctionType funcTy,
bool isVariadic,
bool useBarePtrCallConv,
SignatureConversion & result ) const

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.

References result.

Referenced by mlir::convertFuncOpToLLVMFuncOp(), and mlir::GPUFuncOpLowering::matchAndRewrite().

◆ convertFunctionTypeCWrapper()

std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > LLVMTypeConverter::convertFunctionTypeCWrapper ( FunctionType type) const

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 427 of file TypeConverter.cpp.

References getContext(), mlir::LLVM::isCompatibleType(), and packFunctionResults().

Referenced by wrapExternalFunction(), and wrapForExternalCallers().

◆ getContext()

◆ getCurrentThreadRecursiveStack()

SmallVector< Type > & LLVMTypeConverter::getCurrentThreadRecursiveStack ( )
protected

Definition at line 21 of file TypeConverter.cpp.

References callStackMutex, conversionCallStack, and getContext().

Referenced by LLVMTypeConverter().

◆ getDataLayout()

const llvm::DataLayout & mlir::LLVMTypeConverter::getDataLayout ( ) const
inline

Returns the data layout to use during and after conversion.

Definition at line 136 of file TypeConverter.h.

Referenced by getMemRefAlignment(), and getVectorAlignment().

◆ getDataLayoutAnalysis()

const DataLayoutAnalysis * mlir::LLVMTypeConverter::getDataLayoutAnalysis ( ) const
inline

Returns the data layout analysis to query during conversion.

Definition at line 139 of file TypeConverter.h.

◆ getDialect()

LLVM::LLVMDialect * mlir::LLVMTypeConverter::getDialect ( ) const
inline

Returns the LLVM dialect.

Definition at line 105 of file TypeConverter.h.

References llvmDialect.

Referenced by getContext(), mlir::ConvertToLLVMPattern::getDialect(), and mlir::populateVectorToLLVMConversionPatterns().

◆ getIndexType()

◆ getIndexTypeBitwidth()

unsigned mlir::LLVMTypeConverter::getIndexTypeBitwidth ( ) const
inline

Gets the bitwidth of the index type when converted to LLVM.

Definition at line 148 of file TypeConverter.h.

Referenced by mlir::UnrankedMemRefDescriptor::computeSize(), getIndexType(), and truncOrExtToLLVMType().

◆ getMemRefAddressSpace()

FailureOr< unsigned > LLVMTypeConverter::getMemRefAddressSpace ( BaseMemRefType type) const

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 575 of file TypeConverter.cpp.

References mlir::BaseMemRefType::getMemorySpace().

Referenced by mlir::ConvertToLLVMPattern::copyUnrankedDescriptor(), createMemRefMetadataType(), getDynamicSharedMemorySymbol(), mlir::ConvertToLLVMPattern::getElementPtrType(), getMemRefDescriptorFields(), getMemRefDescriptorSize(), getUnrankedMemRefDescriptorSize(), and isMemRefTypeSupported().

◆ getMemRefDescriptorFields()

SmallVector< Type, 5 > LLVMTypeConverter::getMemRefDescriptorFields ( MemRefType type,
bool unpackAggregates ) const

Convert a memref type into a list of LLVM IR types that will form the memref descriptor.

If unpackAggregates is true the sizes and strides arrays in the descriptors are unpacked to individual index-typed elements, else they are kept as rank-sized arrays of index type. In particular, the list will contain:

  • two pointers to the memref element type, followed by
  • an index-typed offset, followed by
  • (if unpackAggregates = true)
    • one index-typed size per dimension of the memref, followed by
    • one index-typed stride per dimension of the memref.
  • (if unpackArrregates = false)
    • one rank-sized array of index-type for the size of each dimension
    • one rank-sized array of index-type for the stride of each dimension

For example, memref<?x?xf32> is converted to the following list:

  • !llvm<"float*"> (allocated pointer),
  • !llvm<"float*"> (aligned pointer),
  • i64 (offset),
  • i64, i64 (sizes),
  • i64, i64 (strides). These types can be recomposed to a memref descriptor struct.

    The result contains the following types:

    1. The pointer to the allocated data buffer, followed by
    2. The pointer to the aligned data buffer, followed by
    3. A lowered index-type integer containing the distance between the beginning of the buffer and the first element to be accessed through the view, followed by
    4. An array containing as many index-type integers as the rank of the MemRef: the array represents the size, in number of elements, of the memref along the given dimension. For constant MemRef dimensions, the corresponding size entry is a constant whose runtime value must match the static value, followed by
    5. A second array containing as many index-type integers as the rank of the MemRef: the second array represents the "stride" (in tensor abstraction sense), i.e. the number of consecutive elements of the underlying buffer. TODO: add assertions for the static cases.

    If unpackAggregates is set to true, the arrays described in (4) and (5) are expanded into individual index-type elements.

    template <typename Elem, typename Index, size_t Rank> struct { Elem *allocatedPtr; Elem *alignedPtr; Index offset; Index sizes[Rank]; // omitted when rank == 0 Index strides[Rank]; // omitted when rank == 0 };

Definition at line 486 of file TypeConverter.cpp.

References mlir::emitError(), getIndexType(), and getMemRefAddressSpace().

Referenced by packRankedMemRefDesc(), and mlir::structFuncArgTypeConverter().

◆ getMemRefDescriptorSize()

unsigned LLVMTypeConverter::getMemRefDescriptorSize ( MemRefType type,
const DataLayout & layout ) const

Returns the size of the memref descriptor object in bytes.

Definition at line 526 of file TypeConverter.cpp.

References getIndexType(), getMemRefAddressSpace(), getPointerBitwidth(), and mlir::DataLayout::getTypeSize().

◆ getOptions()

const LowerToLLVMOptions & mlir::LLVMTypeConverter::getOptions ( ) const
inline

◆ getPointerBitwidth()

unsigned LLVMTypeConverter::getPointerBitwidth ( unsigned addressSpace = 0) const

Gets the pointer bitwidth.

Definition at line 283 of file TypeConverter.cpp.

Referenced by mlir::UnrankedMemRefDescriptor::computeSize(), getMemRefDescriptorSize(), and getUnrankedMemRefDescriptorSize().

◆ getUnrankedMemRefDescriptorFields()

SmallVector< Type, 2 > LLVMTypeConverter::getUnrankedMemRefDescriptorFields ( ) const

Convert an unranked memref type into a list of non-aggregate LLVM IR types that will form the unranked memref descriptor.

In particular, this list contains:

  • an integer rank, followed by
  • a pointer to the memref descriptor struct. For example, memref<xf32> is converted to the following list: i64 (rank) !llvm<"i8</em>"> (type-erased pointer). These types can be recomposed to a unranked memref descriptor struct.

    In particular, the fields for an unranked memref descriptor are:

  1. index-typed rank, the dynamic rank of this MemRef
  2. void* ptr, pointer to the static ranked MemRef descriptor. This will be stack allocated (alloca) copy of a MemRef descriptor that got casted to be unranked.

Definition at line 554 of file TypeConverter.cpp.

References getContext(), and getIndexType().

Referenced by packUnrankedMemRefDesc(), and mlir::structFuncArgTypeConverter().

◆ getUnrankedMemRefDescriptorSize()

unsigned LLVMTypeConverter::getUnrankedMemRefDescriptorSize ( UnrankedMemRefType type,
const DataLayout & layout ) const

Returns the size of the unranked memref descriptor object in bytes.

Definition at line 558 of file TypeConverter.cpp.

References getIndexType(), getMemRefAddressSpace(), getPointerBitwidth(), and mlir::DataLayout::getTypeSize().

◆ packFunctionResults()

Type LLVMTypeConverter::packFunctionResults ( TypeRange types,
bool useBarePtrCallConv = false,
SmallVector< SmallVector< Type > > * groupedTypes = nullptr,
int64_t * numConvertedTypes = nullptr ) const

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.

Populate the converted (unpacked) types into groupedTypes, if provided. groupedType contains one nested vector per input type. In case of a 1:N conversion, a nested vector may contain 0 or more then 1 converted 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 702 of file TypeConverter.cpp.

References convertCallingConventionType(), and getContext().

Referenced by convertFunctionTypeCWrapper(), and mlir::GPUReturnOpLowering::matchAndRewrite().

◆ packOperationResults()

Type LLVMTypeConverter::packOperationResults ( TypeRange types) const

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 681 of file TypeConverter.cpp.

References getContext(), and mlir::LLVM::isCompatibleType().

Referenced by mlir::LLVM::detail::intrinsicRewrite(), and mlir::LLVM::detail::oneToOneRewrite().

◆ promoteOneMemRefDescriptor()

Value LLVMTypeConverter::promoteOneMemRefDescriptor ( Location loc,
Value operand,
OpBuilder & builder ) const

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 734 of file TypeConverter.cpp.

References mlir::Builder::getContext(), mlir::Builder::getI64Type(), mlir::Builder::getIndexAttr(), and mlir::Value::getType().

◆ promoteOperands() [1/2]

SmallVector< Value, 4 > LLVMTypeConverter::promoteOperands ( Location loc,
ValueRange opOperands,
ArrayRef< ValueRange > adaptorOperands,
OpBuilder & builder,
bool useBarePtrCallConv = false ) const

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.

(The ArrayRef variant is for 1:N.)

Definition at line 757 of file TypeConverter.cpp.

References mlir::MemRefDescriptor::alignedPtr(), mlir::MemRefDescriptor::unpack(), and mlir::UnrankedMemRefDescriptor::unpack().

Referenced by promoteOperands().

◆ promoteOperands() [2/2]

SmallVector< Value, 4 > LLVMTypeConverter::promoteOperands ( Location loc,
ValueRange opOperands,
ValueRange adaptorOperands,
OpBuilder & builder,
bool useBarePtrCallConv = false ) const

Definition at line 748 of file TypeConverter.cpp.

References promoteOperands().

◆ structFuncArgTypeConverter

LogicalResult structFuncArgTypeConverter ( const LLVMTypeConverter & converter,
Type type,
SmallVectorImpl< Type > & result )
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.

References LLVMTypeConverter(), and result.

Member Data Documentation

◆ callStackMutex

llvm::sys::SmartRWMutex<true> mlir::LLVMTypeConverter::callStackMutex
protected

Definition at line 211 of file TypeConverter.h.

Referenced by getCurrentThreadRecursiveStack().

◆ conversionCallStack

DenseMap<uint64_t, std::unique_ptr<SmallVector<Type> > > mlir::LLVMTypeConverter::conversionCallStack
protected

Definition at line 210 of file TypeConverter.h.

Referenced by getCurrentThreadRecursiveStack().

◆ llvmDialect

LLVM::LLVMDialect* mlir::LLVMTypeConverter::llvmDialect
protected

Pointer to the LLVM dialect.

Definition at line 206 of file TypeConverter.h.

Referenced by getDialect(), and LLVMTypeConverter().


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