MLIR 24.0.0git
LLVMDialect.cpp File Reference
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "IR/LLVMOps.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Matchers.h"
#include "mlir/Interfaces/FunctionImplementation.h"
#include "mlir/Transforms/InliningUtils.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/Support/Error.h"
#include "LLVMDialectBytecode.h"
#include <numeric>
#include <optional>
#include "mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc"

Go to the source code of this file.

Macros

#define REGISTER_ENUM_TYPE(Ty)

Functions

static NamedAttrList getAttrsForPrinting (Operation *op)
static auto processFMFAttr (ArrayRef< NamedAttribute > attrs)
static LogicalResult verifySymbolAttrUse (FlatSymbolRefAttr symbol, Operation *op, SymbolTableCollection &symbolTable)
 Verifies symbol's use in op to ensure the symbol is a valid and fully defined llvm.func.
static int parseOptionalKeywordAlternative (OpAsmParser &parser, ArrayRef< StringRef > keywords)
template<typename EnumTy, typename RetTy = EnumTy>
static RetTy parseOptionalLLVMKeyword (OpAsmParser &parser, EnumTy defaultValue)
 Parse an enum from the keyword, or default to the provided default value.
static ArrayAttr getLLVMAlignParamForCompressExpand (OpBuilder &builder, bool isExpandLoad, uint64_t alignment=1)
static void printOneOpBundle (OpAsmPrinter &p, OperandRange operands, TypeRange operandTypes, StringRef tag)
static ParseResult parseOneOpBundle (OpAsmParser &p, SmallVector< SmallVector< OpAsmParser::UnresolvedOperand > > &opBundleOperands, SmallVector< SmallVector< Type > > &opBundleOperandTypes, SmallVector< Attribute > &opBundleTags)
template<typename PredicateAttr, typename Predicate>
static ParseResult parseCmpPredicateImpl (OpAsmParser &parser, PredicateAttr &predicate, function_ref< std::optional< Predicate >(StringRef)> symbolize)
static Attribute getBoolAttribute (Type type, MLIRContext *ctx, bool value)
 Returns a scalar or vector boolean attribute of the given type.
static Type extractVectorElementType (Type type)
 Returns the elemental type of any LLVM-compatible vector type or self.
static void destructureIndices (Type currType, ArrayRef< GEPArg > indices, SmallVectorImpl< int32_t > &rawConstantIndices, SmallVectorImpl< Value > &dynamicIndices)
 Destructures the 'indices' parameter into 'rawConstantIndices' and 'dynamicIndices', encoding the former in the process.
static LogicalResult verifyStructIndices (Type baseGEPType, unsigned indexPos, GEPIndicesAdaptor< ValueRange > indices, function_ref< InFlightDiagnostic()> emitOpError)
 For the given indices, check if they comply with baseGEPType, especially check against LLVMStructTypes nested within.
static LogicalResult verifyStructIndices (Type baseGEPType, GEPIndicesAdaptor< ValueRange > indices, function_ref< InFlightDiagnostic()> emitOpError)
 Driver function around verifyStructIndices.
template<typename OpTy>
static LogicalResult verifyAtomicMemOp (OpTy memOp, Type valueType, ArrayRef< AtomicOrdering > unsupportedOrderings)
 Verifies the attributes and the type of atomic memory access operations.
static SmallVector< Type, 1 > getCallOpResultTypes (LLVMFunctionType calleeType)
 Gets the MLIR Op-like result types of a LLVMFunctionType.
static TypeAttr getCallOpVarCalleeType (LLVMFunctionType calleeType)
 Gets the variadic callee type for a LLVMFunctionType.
static LLVMFunctionType getLLVMFuncType (MLIRContext *context, TypeRange results, ValueRange args)
 Constructs a LLVMFunctionType from MLIR results and args.
template<typename OpTy>
static unsigned getNumConsumedCalleeOperands (OpTy callOp)
 Return the number of leading callee operands of callOp that the operation consumes instead of passing them to the callee.
template<typename OpTy>
static Operation::operand_range getOperandsPassedToCallee (OpTy callOp)
 Return the operands of callOp that are passed to the callee, including the variadic arguments in case of a call to a variadic callee.
template<typename OpTy>
static Operation::operand_range getArgOperandsImpl (OpTy callOp)
 Return the operands of callOp that correspond to the declared parameters of the callee, i.e., its CallOpInterface argument operands.
static LogicalResult verifyCallOpDebugInfo (CallOp callOp, LLVMFuncOp callee)
 Verify that an inlinable callsite of a debug-info-bearing function in a debug-info-bearing function has a debug location attached to it.
template<typename OpTy>
static LogicalResult verifyCallOpVarCalleeType (OpTy callOp)
 Verify that the parameter and return types of the variadic callee type match the callOp argument and result types.
template<typename OpType>
static LogicalResult verifyOperandBundles (OpType &op)
static ParseResult parseCallTypeAndResolveOperands (OpAsmParser &parser, OperationState &result, bool isDirect, ArrayRef< OpAsmParser::UnresolvedOperand > operands, SmallVectorImpl< DictionaryAttr > &argAttrs, SmallVectorImpl< DictionaryAttr > &resultAttrs)
 Parses the type of a call operation and resolves the operands if the parsing succeeds.
static ParseResult parseOptionalCallFuncPtr (OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands)
 Parses an optional function pointer operand before the call argument list for indirect calls, or stops parsing at the function identifier otherwise.
static ParseResult resolveOpBundleOperands (OpAsmParser &parser, SMLoc loc, OperationState &state, ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand > > opBundleOperands, ArrayRef< SmallVector< Type > > opBundleOperandTypes, StringAttr opBundleSizesAttrName)
static Type getInsertExtractValueElementType (function_ref< InFlightDiagnostic(StringRef)> emitError, Type containerType, ArrayRef< int64_t > position)
 Extract the type at position in the LLVM IR aggregate type containerType.
static Type getInsertExtractValueElementType (Type llvmType, ArrayRef< int64_t > position)
 Extract the type at position in the wrapped LLVM IR aggregate type containerType.
static Attribute extractElementAt (Attribute attr, size_t index)
 Extracts the element at the given index from an attribute.
template<typename OpType>
static void printCommonGlobalAndAlias (OpAsmPrinter &p, OpType op)
static LogicalResult verifyComdat (Operation *op, std::optional< SymbolRefAttr > attr)
static LogicalResult verifyBlockTags (LLVMFuncOp funcOp)
template<typename OpType>
static ParseResult parseCommonGlobalAndAlias (OpAsmParser &parser, OperationState &result)
 Parse common attributes that might show up in the same order in both GlobalOp and AliasOp.
static bool isZeroAttribute (Attribute value)
static LogicalResult checkGlobalXtorData (Operation *op, ArrayAttr data)
static Type buildLLVMFunctionType (OpAsmParser &parser, SMLoc loc, ArrayRef< Type > inputs, ArrayRef< Type > outputs, function_interface_impl::VariadicFlag variadicFlag)
static int64_t getNumElements (Type t)
 Compute the total number of elements in the given type, also taking into account nested types.
static bool hasScalableVectorType (Type t)
 Check if the given type is a scalable vector type or a vector/array type that contains a nested scalable vector type.
static LogicalResult verifyStructArrayConstant (LLVM::ConstantOp op, LLVM::LLVMArrayType arrayType, ArrayAttr arrayAttr, int dim)
 Verifies the constant array represented by arrayAttr matches the provided arrayType.
template<class ExtOp>
static LogicalResult verifyExtOp (ExtOp op)
 Verifies that the given extension operation operates on consistent scalars or vectors, and that the target width is larger than the input width.
template<typename T>
static OpFoldResult foldChainableCast (T castOp, typename T::FoldAdaptor adaptor)
 Folds a cast op that can be chained.

Variables

static constexpr const char kElemTypeAttrName [] = "elem_type"

Macro Definition Documentation

◆ REGISTER_ENUM_TYPE

#define REGISTER_ENUM_TYPE ( Ty)
Value:
template <> \
struct EnumTraits<Ty> { \
static StringRef stringify(Ty value) { return stringify##Ty(value); } \
static unsigned getMaxEnumVal() { return getMaxEnumValFor##Ty(); } \
}

Definition at line 116 of file LLVMDialect.cpp.

Function Documentation

◆ buildLLVMFunctionType()

◆ checkGlobalXtorData()

LogicalResult checkGlobalXtorData ( Operation * op,
ArrayAttr data )
static

Definition at line 2775 of file LLVMDialect.cpp.

References ArrayAttr(), mlir::Operation::emitError(), and success().

◆ destructureIndices()

void destructureIndices ( Type currType,
ArrayRef< GEPArg > indices,
SmallVectorImpl< int32_t > & rawConstantIndices,
SmallVectorImpl< Value > & dynamicIndices )
static

Destructures the 'indices' parameter into 'rawConstantIndices' and 'dynamicIndices', encoding the former in the process.

In the process, dynamic indices which are used to index into a structure type are converted to constant indices when possible. To do this, the GEPs element type should be passed as first parameter.

Definition at line 656 of file LLVMDialect.cpp.

References indices, mlir::LLVM::kGEPConstantBitWidth, mlir::m_ConstantInt(), and mlir::matchPattern().

◆ extractElementAt()

Attribute extractElementAt ( Attribute attr,
size_t index )
static

Extracts the element at the given index from an attribute.

For ElementsAttr, returns the element at the specified index, or nullptr if the shaped type does not have rank 1. For ArrayAttr, returns the element at the specified index. For ZeroAttr, UndefAttr, and PoisonAttr, returns the attribute itself unchanged. Returns nullptr if the attribute is not one of these types or if the index is out of bounds.

Definition at line 1982 of file LLVMDialect.cpp.

◆ extractVectorElementType()

Type extractVectorElementType ( Type type)
static

Returns the elemental type of any LLVM-compatible vector type or self.

Definition at line 645 of file LLVMDialect.cpp.

◆ foldChainableCast()

template<typename T>
OpFoldResult foldChainableCast ( T castOp,
typename T::FoldAdaptor adaptor )
static

Folds a cast op that can be chained.

Definition at line 3960 of file LLVMDialect.cpp.

◆ getArgOperandsImpl()

template<typename OpTy>
Operation::operand_range getArgOperandsImpl ( OpTy callOp)
static

Return the operands of callOp that correspond to the declared parameters of the callee, i.e., its CallOpInterface argument operands.

The variadic arguments of a call to a variadic callee are not included: they do not correspond to any argument of the callee. The callee does not receive them as block arguments but reads them with llvm.intr.vastart and friends, so in terms of CallOpInterface they are consumed operands rather than forwarded ones.

Definition at line 1152 of file LLVMDialect.cpp.

References getOperandsPassedToCallee().

◆ getAttrsForPrinting()

◆ getBoolAttribute()

Attribute getBoolAttribute ( Type type,
MLIRContext * ctx,
bool value )
static

Returns a scalar or vector boolean attribute of the given type.

Definition at line 330 of file LLVMDialect.cpp.

References mlir::BoolAttr::get(), and mlir::DenseElementsAttr::get().

◆ getCallOpResultTypes()

SmallVector< Type, 1 > getCallOpResultTypes ( LLVMFunctionType calleeType)
static

Gets the MLIR Op-like result types of a LLVMFunctionType.

Definition at line 951 of file LLVMDialect.cpp.

◆ getCallOpVarCalleeType()

TypeAttr getCallOpVarCalleeType ( LLVMFunctionType calleeType)
static

Gets the variadic callee type for a LLVMFunctionType.

Definition at line 960 of file LLVMDialect.cpp.

◆ getInsertExtractValueElementType() [1/2]

Type getInsertExtractValueElementType ( function_ref< InFlightDiagnostic(StringRef)> emitError,
Type containerType,
ArrayRef< int64_t > position )
static

Extract the type at position in the LLVM IR aggregate type containerType.

Each element of position is an index into a nested aggregate type. Return the resulting type or emit an error.

Definition at line 1928 of file LLVMDialect.cpp.

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

Referenced by mlir::LLVM::parseInsertExtractValueElementType().

◆ getInsertExtractValueElementType() [2/2]

Type getInsertExtractValueElementType ( Type llvmType,
ArrayRef< int64_t > position )
static

Extract the type at position in the wrapped LLVM IR aggregate type containerType.

Definition at line 1965 of file LLVMDialect.cpp.

◆ getLLVMAlignParamForCompressExpand()

ArrayAttr getLLVMAlignParamForCompressExpand ( OpBuilder & builder,
bool isExpandLoad,
uint64_t alignment = 1 )
static

◆ getLLVMFuncType()

LLVMFunctionType getLLVMFuncType ( MLIRContext * context,
TypeRange results,
ValueRange args )
static

Constructs a LLVMFunctionType from MLIR results and args.

Definition at line 965 of file LLVMDialect.cpp.

References mlir::ValueRange::getTypes().

◆ getNumConsumedCalleeOperands()

template<typename OpTy>
unsigned getNumConsumedCalleeOperands ( OpTy callOp)
static

Return the number of leading callee operands of callOp that the operation consumes instead of passing them to the callee.

Definition at line 1128 of file LLVMDialect.cpp.

Referenced by getOperandsPassedToCallee().

◆ getNumElements()

int64_t getNumElements ( Type t)
static

Compute the total number of elements in the given type, also taking into account nested types.

Supported types are VectorType and LLVMArrayType. Everything else is treated as a scalar.

Definition at line 3490 of file LLVMDialect.cpp.

References getNumElements().

Referenced by mlir::LLVM::detail::extractNDVectorTypeInfo(), getNumElements(), hasAtMostSingleNonScalar(), mlir::DenseElementsAttr::tryGetComplexFloatValues(), mlir::DenseElementsAttr::tryGetComplexIntValues(), and verifyFloatIntegerBuiltin().

◆ getOperandsPassedToCallee()

template<typename OpTy>
Operation::operand_range getOperandsPassedToCallee ( OpTy callOp)
static

Return the operands of callOp that are passed to the callee, including the variadic arguments in case of a call to a variadic callee.

Definition at line 1138 of file LLVMDialect.cpp.

References getNumConsumedCalleeOperands().

Referenced by getArgOperandsImpl(), and verifyCallOpVarCalleeType().

◆ hasScalableVectorType()

bool hasScalableVectorType ( Type t)
static

Check if the given type is a scalable vector type or a vector/array type that contains a nested scalable vector type.

Definition at line 3516 of file LLVMDialect.cpp.

References hasScalableVectorType().

Referenced by hasScalableVectorType().

◆ isZeroAttribute()

bool isZeroAttribute ( Attribute value)
static

Definition at line 2649 of file LLVMDialect.cpp.

References isZeroAttribute().

Referenced by isZeroAttribute().

◆ parseCallTypeAndResolveOperands()

ParseResult parseCallTypeAndResolveOperands ( OpAsmParser & parser,
OperationState & result,
bool isDirect,
ArrayRef< OpAsmParser::UnresolvedOperand > operands,
SmallVectorImpl< DictionaryAttr > & argAttrs,
SmallVectorImpl< DictionaryAttr > & resultAttrs )
static

◆ parseCmpPredicateImpl()

template<typename PredicateAttr, typename Predicate>
ParseResult parseCmpPredicateImpl ( OpAsmParser & parser,
PredicateAttr & predicate,
function_ref< std::optional< Predicate >(StringRef)> symbolize )
static

◆ parseCommonGlobalAndAlias()

template<typename OpType>
ParseResult parseCommonGlobalAndAlias ( OpAsmParser & parser,
OperationState & result )
static

◆ parseOneOpBundle()

◆ parseOptionalCallFuncPtr()

ParseResult parseOptionalCallFuncPtr ( OpAsmParser & parser,
SmallVectorImpl< OpAsmParser::UnresolvedOperand > & operands )
static

Parses an optional function pointer operand before the call argument list for indirect calls, or stops parsing at the function identifier otherwise.

Definition at line 1450 of file LLVMDialect.cpp.

References mlir::OptionalParseResult::has_value(), mlir::OpAsmParser::parseOptionalOperand(), and success().

◆ parseOptionalKeywordAlternative()

int parseOptionalKeywordAlternative ( OpAsmParser & parser,
ArrayRef< StringRef > keywords )
static

Definition at line 103 of file LLVMDialect.cpp.

References mlir::AsmParser::parseOptionalKeyword().

Referenced by parseOptionalLLVMKeyword().

◆ parseOptionalLLVMKeyword()

template<typename EnumTy, typename RetTy = EnumTy>
RetTy parseOptionalLLVMKeyword ( OpAsmParser & parser,
EnumTy defaultValue )
static

Parse an enum from the keyword, or default to the provided default value.

The return type is the enum type by default, unless overridden with the second template argument.

Definition at line 135 of file LLVMDialect.cpp.

References parseOptionalKeywordAlternative().

Referenced by parseCommonGlobalAndAlias().

◆ printCommonGlobalAndAlias()

template<typename OpType>
void printCommonGlobalAndAlias ( OpAsmPrinter & p,
OpType op )
static

Definition at line 2443 of file LLVMDialect.cpp.

◆ printOneOpBundle()

void printOneOpBundle ( OpAsmPrinter & p,
OperandRange operands,
TypeRange operandTypes,
StringRef tag )
static

◆ processFMFAttr()

auto processFMFAttr ( ArrayRef< NamedAttribute > attrs)
static

◆ resolveOpBundleOperands()

ParseResult resolveOpBundleOperands ( OpAsmParser & parser,
SMLoc loc,
OperationState & state,
ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand > > opBundleOperands,
ArrayRef< SmallVector< Type > > opBundleOperandTypes,
StringAttr opBundleSizesAttrName )
static

◆ verifyAtomicMemOp()

template<typename OpTy>
LogicalResult verifyAtomicMemOp ( OpTy memOp,
Type valueType,
ArrayRef< AtomicOrdering > unsupportedOrderings )
static

Verifies the attributes and the type of atomic memory access operations.

Definition at line 868 of file LLVMDialect.cpp.

References mlir::DataLayout::closest(), mlir::LLVM::isTypeCompatibleWithAtomicOp(), and success().

◆ verifyBlockTags()

LogicalResult verifyBlockTags ( LLVMFuncOp funcOp)
static

Definition at line 2509 of file LLVMDialect.cpp.

References mlir::WalkResult::advance(), and mlir::WalkResult::interrupt().

◆ verifyCallOpDebugInfo()

LogicalResult verifyCallOpDebugInfo ( CallOp callOp,
LLVMFuncOp callee )
static

Verify that an inlinable callsite of a debug-info-bearing function in a debug-info-bearing function has a debug location attached to it.

This mirrors an LLVM IR verifier.

Definition at line 1171 of file LLVMDialect.cpp.

References success().

◆ verifyCallOpVarCalleeType()

template<typename OpTy>
LogicalResult verifyCallOpVarCalleeType ( OpTy callOp)
static

Verify that the parameter and return types of the variadic callee type match the callOp argument and result types.

Definition at line 1196 of file LLVMDialect.cpp.

References getOperandsPassedToCallee(), and success().

◆ verifyComdat()

LogicalResult verifyComdat ( Operation * op,
std::optional< SymbolRefAttr > attr )
static

◆ verifyExtOp()

template<class ExtOp>
LogicalResult verifyExtOp ( ExtOp op)
static

Verifies that the given extension operation operates on consistent scalars or vectors, and that the target width is larger than the input width.

Definition at line 3902 of file LLVMDialect.cpp.

References mlir::LLVM::getVectorNumElements(), mlir::LLVM::isCompatibleVectorType(), and success().

◆ verifyOperandBundles()

template<typename OpType>
LogicalResult verifyOperandBundles ( OpType & op)
static

Definition at line 1243 of file LLVMDialect.cpp.

References success().

◆ verifyStructArrayConstant()

LogicalResult verifyStructArrayConstant ( LLVM::ConstantOp op,
LLVM::LLVMArrayType arrayType,
ArrayAttr arrayAttr,
int dim )
static

Verifies the constant array represented by arrayAttr matches the provided arrayType.

Definition at line 3529 of file LLVMDialect.cpp.

References ArrayAttr(), success(), and verifyStructArrayConstant().

Referenced by verifyStructArrayConstant().

◆ verifyStructIndices() [1/2]

LogicalResult verifyStructIndices ( Type baseGEPType,
GEPIndicesAdaptor< ValueRange > indices,
function_ref< InFlightDiagnostic()> emitOpError )
static

Driver function around verifyStructIndices.

Definition at line 810 of file LLVMDialect.cpp.

References indices, and verifyStructIndices().

◆ verifyStructIndices() [2/2]

LogicalResult verifyStructIndices ( Type baseGEPType,
unsigned indexPos,
GEPIndicesAdaptor< ValueRange > indices,
function_ref< InFlightDiagnostic()> emitOpError )
static

For the given indices, check if they comply with baseGEPType, especially check against LLVMStructTypes nested within.

Definition at line 770 of file LLVMDialect.cpp.

References indices, success(), and verifyStructIndices().

Referenced by verifyStructIndices(), and verifyStructIndices().

◆ verifySymbolAttrUse()

LogicalResult verifySymbolAttrUse ( FlatSymbolRefAttr symbol,
Operation * op,
SymbolTableCollection & symbolTable )
static

Verifies symbol's use in op to ensure the symbol is a valid and fully defined llvm.func.

Definition at line 77 of file LLVMDialect.cpp.

References mlir::Operation::emitOpError(), mlir::FlatSymbolRefAttr::getAttr(), mlir::FlatSymbolRefAttr::getValue(), mlir::SymbolTableCollection::lookupNearestSymbolFrom(), and success().

Variable Documentation

◆ kElemTypeAttrName

const char kElemTypeAttrName[] = "elem_type"
staticconstexpr

Definition at line 53 of file LLVMDialect.cpp.