MLIR 24.0.0git
ModuleImport.cpp File Reference
#include "mlir/Target/LLVMIR/ModuleImport.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "AttrKindDetail.h"
#include "DebugImporter.h"
#include "LoopAnnotationImporter.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Matchers.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Target/LLVMIR/DataLayoutImporter.h"
#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/Comdat.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugProgramInstruction.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/LogicalResult.h"
#include "llvm/Support/ModRef.h"
#include <optional>
#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc"
#include "mlir/Dialect/LLVMIR/LLVMOpFromLLVMIRConversions.inc"

Go to the source code of this file.

Functions

static std::string truncateDiag (std::string str)
static std::string renderMetadataWithTracker (const llvm::Metadata *node, llvm::ModuleSlotTracker &tracker, const llvm::Module *module)
 Prints a metadata node through a caller-provided tracker so module-flag conversion shares the importer's tracker instead of building one per call.
static constexpr StringRef getGlobalCtorsVarName ()
 Returns the name of the global_ctors global variables.
static constexpr StringRef getNamelessGlobalPrefix ()
 Prefix used for symbols of nameless llvm globals.
static constexpr StringRef getGlobalDtorsVarName ()
 Returns the name of the global_dtors global variables.
static constexpr StringRef getGlobalComdatOpName ()
 Returns the symbol name for the module-level comdat operation.
static StringRef getLLVMSyncScope (llvm::Instruction *inst)
 Converts the sync scope identifier of inst to the string representation necessary to build an atomic LLVM dialect operation.
static SmallVector< int64_t > getPositionFromIndices (ArrayRef< unsigned > indices)
 Converts an array of unsigned indices to a signed integer position array.
static LogicalResult convertInstructionImpl (OpBuilder &odsBuilder, llvm::Instruction *inst, ModuleImport &moduleImport, LLVMImportInterface &iface)
 Converts the LLVM instructions that have a generated MLIR builder.
static SetVector< llvm::BasicBlock * > getTopologicallySortedBlocks (ArrayRef< llvm::BasicBlock * > basicBlocks)
 Get a topologically sorted list of blocks for the given basic blocks.
static Attribute convertCGProfileModuleFlagValue (ModuleOp mlirModule, llvm::MDTuple *mdTuple)
static llvm::MDTuple * getTwoElementMDTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md, llvm::ModuleSlotTracker &tracker)
 Extract a two element MDTuple from a MDOperand.
static llvm::ConstantAsMetadata * getConstantMDFromKeyValueTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md, StringRef matchKey, llvm::ModuleSlotTracker &tracker, bool optional=false)
 Extract a constant metadata value from a two element tuple (<key, value>).
static FailureOr< uint64_t > convertInt64FromKeyValueTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md, StringRef matchKey, llvm::ModuleSlotTracker &tracker)
 Extract an integer value from a two element tuple (<key, value>).
static std::optional< ProfileSummaryFormatKind > convertProfileSummaryFormat (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &formatMD, llvm::ModuleSlotTracker &tracker)
static FailureOr< SmallVector< ModuleFlagProfileSummaryDetailedAttr > > convertProfileSummaryDetailed (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &summaryMD, llvm::ModuleSlotTracker &tracker)
static Attribute convertProfileSummaryModuleFlagValue (ModuleOp mlirModule, const llvm::Module *llvmModule, llvm::MDTuple *mdTuple, llvm::ModuleSlotTracker &tracker)
static Attribute convertModuleFlagValueFromMDTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, StringRef key, llvm::MDTuple *mdTuple, llvm::ModuleSlotTracker &tracker)
 Invoke specific handlers for each known module flag value, returns nullptr if the key is unknown or unimplemented.
static Type getVectorTypeForAttr (Type type, ArrayRef< int64_t > arrayShape={})
 Returns type if it is a builtin integer or floating-point vector type that can be used to create an attribute or nullptr otherwise.
static TypedAttr getScalarConstantAsAttr (OpBuilder &builder, llvm::Constant *constScalar)
 Returns an integer or float attribute for the provided scalar constant constScalar or nullptr if the conversion fails.
static SmallVector< Attribute > getSequenceConstantAsAttrs (OpBuilder &builder, llvm::ConstantDataSequential *constSequence)
 Returns an integer or float attribute array for the provided constant sequence constSequence or nullptr if the conversion fails.
static ArrayAttr convertLLVMAttributesToMLIR (Location loc, MLIRContext *context, llvm::AttributeSet attributes, ArrayRef< StringLiteral > attributesToSkip={}, ArrayRef< StringLiteral > attributePrefixesToSkip={})
 Converts LLVM string, integer, and enum attributes into MLIR attributes, skipping those in attributesToSkip and emitting a warning at loc for any other unsupported attributes.
static void processTargetSpecificAttrs (llvm::GlobalVariable *globalVar, GlobalOp globalOp)
 Converts LLVM attributes from globalVar into MLIR attributes and adds them to globalOp as target-specific attributes.
static LogicalResult checkFunctionTypeCompatibility (LLVMFunctionType callType, LLVMFunctionType calleeType)
 Checks if callType and calleeType are compatible and can be represented in MLIR.
static void processMemoryEffects (llvm::Function *func, LLVMFuncOp funcOp)
static void processDenormalFPEnv (llvm::Function *func, LLVMFuncOp funcOp)
template<typename OpTy>
static void convertNoBuiltinAttrs (MLIRContext *ctx, const llvm::AttributeSet &attrs, OpTy target)
template<typename OpTy>
static void convertAllocsizeAttr (MLIRContext *ctx, const llvm::AttributeSet &attrs, OpTy target)
static void processPassthroughAttrs (llvm::Function *func, LLVMFuncOp funcOp)
 Converts LLVM attributes from func into MLIR attributes and adds them to funcOp as passthrough attributes, skipping those listed in kExplicitLLVMFuncAttributes.
template<typename Op>
static LogicalResult convertCallBaseAttributes (llvm::CallBase *inst, Op op)
static bool isMetadataKillLocation (llvm::DbgVariableIntrinsic *dbgIntr)
 Checks if dbgIntr is a kill location that holds metadata instead of an SSA value.
static LogicalResult setDebugIntrinsicBuilderInsertionPoint (mlir::OpBuilder &builder, DominanceInfo &domInfo, Value argOperand)
 Ensure that the debug intrinsic is inserted right after the operand definition.

Variables

static constexpr size_t kMaxDiagLength = 256
 Upper bound on the characters a diagnostic renders for an LLVM entity.
static constexpr std::array kExplicitLLVMFuncOpAttributes
static constexpr std::array kExplicitLLVMFuncOpAttributePrefixes

Function Documentation

◆ checkFunctionTypeCompatibility()

LogicalResult checkFunctionTypeCompatibility ( LLVMFunctionType callType,
LLVMFunctionType calleeType )
static

Checks if callType and calleeType are compatible and can be represented in MLIR.

Definition at line 2414 of file ModuleImport.cpp.

References success().

◆ convertAllocsizeAttr()

template<typename OpTy>
void convertAllocsizeAttr ( MLIRContext * ctx,
const llvm::AttributeSet & attrs,
OpTy target )
static

◆ convertCallBaseAttributes()

template<typename Op>
LogicalResult convertCallBaseAttributes ( llvm::CallBase * inst,
Op op )
static

Definition at line 3333 of file ModuleImport.cpp.

References success().

◆ convertCGProfileModuleFlagValue()

Attribute convertCGProfileModuleFlagValue ( ModuleOp mlirModule,
llvm::MDTuple * mdTuple )
static

Definition at line 691 of file ModuleImport.cpp.

References mlir::FlatSymbolRefAttr::get().

Referenced by convertModuleFlagValueFromMDTuple().

◆ convertInstructionImpl()

LogicalResult convertInstructionImpl ( OpBuilder & odsBuilder,
llvm::Instruction * inst,
ModuleImport & moduleImport,
LLVMImportInterface & iface )
static

Converts the LLVM instructions that have a generated MLIR builder.

Using a static implementation method called from the module import ensures the builders have to use the moduleImport argument and cannot directly call import methods. As a result, both the intrinsic and the instruction MLIR builders have to use the moduleImport argument and none of them has direct access to the private module import methods.

Definition at line 171 of file ModuleImport.cpp.

References mlir::LLVMImportInterface::convertInstruction(), and mlir::LLVMImportInterface::isConvertibleInstruction().

◆ convertInt64FromKeyValueTuple()

FailureOr< uint64_t > convertInt64FromKeyValueTuple ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
const llvm::MDOperand & md,
StringRef matchKey,
llvm::ModuleSlotTracker & tracker )
static

Extract an integer value from a two element tuple (<key, value>).

Fail if requirements are not met. A warning is emitted if the found value isn't a LLVM constant integer.

Definition at line 768 of file ModuleImport.cpp.

References mlir::emitWarning(), getConstantMDFromKeyValueTuple(), and renderMetadataWithTracker().

Referenced by convertProfileSummaryModuleFlagValue().

◆ convertLLVMAttributesToMLIR()

ArrayAttr convertLLVMAttributesToMLIR ( Location loc,
MLIRContext * context,
llvm::AttributeSet attributes,
ArrayRef< StringLiteral > attributesToSkip = {},
ArrayRef< StringLiteral > attributePrefixesToSkip = {} )
static

Converts LLVM string, integer, and enum attributes into MLIR attributes, skipping those in attributesToSkip and emitting a warning at loc for any other unsupported attributes.

Definition at line 1645 of file ModuleImport.cpp.

References ArrayAttr().

Referenced by processPassthroughAttrs(), and processTargetSpecificAttrs().

◆ convertModuleFlagValueFromMDTuple()

Attribute convertModuleFlagValueFromMDTuple ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
StringRef key,
llvm::MDTuple * mdTuple,
llvm::ModuleSlotTracker & tracker )
static

Invoke specific handlers for each known module flag value, returns nullptr if the key is unknown or unimplemented.

Definition at line 1026 of file ModuleImport.cpp.

References convertCGProfileModuleFlagValue(), convertProfileSummaryModuleFlagValue(), mlir::Builder::getArrayAttr(), and mlir::Builder::getStringAttr().

Referenced by mlir::LLVM::ModuleImport::convertModuleFlagsMetadata().

◆ convertNoBuiltinAttrs()

template<typename OpTy>
void convertNoBuiltinAttrs ( MLIRContext * ctx,
const llvm::AttributeSet & attrs,
OpTy target )
static

Definition at line 3024 of file ModuleImport.cpp.

References target.

Referenced by mlir::LLVM::ModuleImport::processFunctionAttributes().

◆ convertProfileSummaryDetailed()

FailureOr< SmallVector< ModuleFlagProfileSummaryDetailedAttr > > convertProfileSummaryDetailed ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
const llvm::MDOperand & summaryMD,
llvm::ModuleSlotTracker & tracker )
static

◆ convertProfileSummaryFormat()

std::optional< ProfileSummaryFormatKind > convertProfileSummaryFormat ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
const llvm::MDOperand & formatMD,
llvm::ModuleSlotTracker & tracker )
static

◆ convertProfileSummaryModuleFlagValue()

Attribute convertProfileSummaryModuleFlagValue ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
llvm::MDTuple * mdTuple,
llvm::ModuleSlotTracker & tracker )
static

◆ getConstantMDFromKeyValueTuple()

llvm::ConstantAsMetadata * getConstantMDFromKeyValueTuple ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
const llvm::MDOperand & md,
StringRef matchKey,
llvm::ModuleSlotTracker & tracker,
bool optional = false )
static

Extract a constant metadata value from a two element tuple (<key, value>).

Return nullptr if requirements are not met. A warning is emitted if the matchKey is different from the tuple's key.

Definition at line 743 of file ModuleImport.cpp.

References mlir::emitWarning(), getTwoElementMDTuple(), and renderMetadataWithTracker().

Referenced by convertInt64FromKeyValueTuple(), and convertProfileSummaryModuleFlagValue().

◆ getGlobalComdatOpName()

constexpr StringRef getGlobalComdatOpName ( )
staticconstexpr

Returns the symbol name for the module-level comdat operation.

It must not conflict with the user namespace.

Definition at line 130 of file ModuleImport.cpp.

◆ getGlobalCtorsVarName()

constexpr StringRef getGlobalCtorsVarName ( )
staticconstexpr

Returns the name of the global_ctors global variables.

Definition at line 114 of file ModuleImport.cpp.

Referenced by mlir::LLVM::ModuleImport::convertGlobals().

◆ getGlobalDtorsVarName()

constexpr StringRef getGlobalDtorsVarName ( )
staticconstexpr

Returns the name of the global_dtors global variables.

Definition at line 124 of file ModuleImport.cpp.

Referenced by mlir::LLVM::ModuleImport::convertGlobals().

◆ getLLVMSyncScope()

StringRef getLLVMSyncScope ( llvm::Instruction * inst)
static

Converts the sync scope identifier of inst to the string representation necessary to build an atomic LLVM dialect operation.

Returns the empty string if the operation has either no sync scope or the default system-level sync scope attached. The atomic operations only set their sync scope attribute if they have a non-default sync scope attached.

Definition at line 139 of file ModuleImport.cpp.

◆ getNamelessGlobalPrefix()

constexpr StringRef getNamelessGlobalPrefix ( )
staticconstexpr

Prefix used for symbols of nameless llvm globals.

Definition at line 119 of file ModuleImport.cpp.

◆ getPositionFromIndices()

SmallVector< int64_t > getPositionFromIndices ( ArrayRef< unsigned > indices)
static

Converts an array of unsigned indices to a signed integer position array.

Definition at line 159 of file ModuleImport.cpp.

References indices.

◆ getScalarConstantAsAttr()

TypedAttr getScalarConstantAsAttr ( OpBuilder & builder,
llvm::Constant * constScalar )
static

Returns an integer or float attribute for the provided scalar constant constScalar or nullptr if the conversion fails.

Definition at line 1428 of file ModuleImport.cpp.

References mlir::emitError(), mlir::Builder::getContext(), mlir::Builder::getFloatAttr(), mlir::LLVM::detail::getFloatType(), and mlir::Builder::getIntegerAttr().

Referenced by mlir::LLVM::ModuleImport::convertIntrinsicArguments(), and getSequenceConstantAsAttrs().

◆ getSequenceConstantAsAttrs()

SmallVector< Attribute > getSequenceConstantAsAttrs ( OpBuilder & builder,
llvm::ConstantDataSequential * constSequence )
static

Returns an integer or float attribute array for the provided constant sequence constSequence or nullptr if the conversion fails.

Definition at line 1462 of file ModuleImport.cpp.

References getScalarConstantAsAttr().

◆ getTopologicallySortedBlocks()

SetVector< llvm::BasicBlock * > getTopologicallySortedBlocks ( ArrayRef< llvm::BasicBlock * > basicBlocks)
static

Get a topologically sorted list of blocks for the given basic blocks.

Definition at line 312 of file ModuleImport.cpp.

Referenced by mlir::LLVM::ModuleImport::processFunction().

◆ getTwoElementMDTuple()

llvm::MDTuple * getTwoElementMDTuple ( ModuleOp mlirModule,
const llvm::Module * llvmModule,
const llvm::MDOperand & md,
llvm::ModuleSlotTracker & tracker )
static

Extract a two element MDTuple from a MDOperand.

Emit a warning in case something else is found.

Definition at line 728 of file ModuleImport.cpp.

References mlir::emitWarning(), and renderMetadataWithTracker().

Referenced by convertProfileSummaryDetailed(), convertProfileSummaryFormat(), and getConstantMDFromKeyValueTuple().

◆ getVectorTypeForAttr()

Type getVectorTypeForAttr ( Type type,
ArrayRef< int64_t > arrayShape = {} )
static

Returns type if it is a builtin integer or floating-point vector type that can be used to create an attribute or nullptr otherwise.

If provided, arrayShape is added to the shape of the vector to create an attribute that matches an array of vectors.

Definition at line 1381 of file ModuleImport.cpp.

◆ isMetadataKillLocation()

bool isMetadataKillLocation ( llvm::DbgVariableIntrinsic * dbgIntr)
static

Checks if dbgIntr is a kill location that holds metadata instead of an SSA value.

Definition at line 3571 of file ModuleImport.cpp.

◆ processDenormalFPEnv()

void processDenormalFPEnv ( llvm::Function * func,
LLVMFuncOp funcOp )
static

◆ processMemoryEffects()

void processMemoryEffects ( llvm::Function * func,
LLVMFuncOp funcOp )
static

◆ processPassthroughAttrs()

void processPassthroughAttrs ( llvm::Function * func,
LLVMFuncOp funcOp )
static

Converts LLVM attributes from func into MLIR attributes and adds them to funcOp as passthrough attributes, skipping those listed in kExplicitLLVMFuncAttributes.

Definition at line 3072 of file ModuleImport.cpp.

References ArrayAttr(), convertLLVMAttributesToMLIR(), kExplicitLLVMFuncOpAttributePrefixes, and kExplicitLLVMFuncOpAttributes.

Referenced by mlir::LLVM::ModuleImport::processFunctionAttributes().

◆ processTargetSpecificAttrs()

void processTargetSpecificAttrs ( llvm::GlobalVariable * globalVar,
GlobalOp globalOp )
static

Converts LLVM attributes from globalVar into MLIR attributes and adds them to globalOp as target-specific attributes.

Definition at line 1702 of file ModuleImport.cpp.

References ArrayAttr(), and convertLLVMAttributesToMLIR().

◆ renderMetadataWithTracker()

std::string renderMetadataWithTracker ( const llvm::Metadata * node,
llvm::ModuleSlotTracker & tracker,
const llvm::Module * module )
static

Prints a metadata node through a caller-provided tracker so module-flag conversion shares the importer's tracker instead of building one per call.

Definition at line 75 of file ModuleImport.cpp.

References truncateDiag().

Referenced by convertInt64FromKeyValueTuple(), convertProfileSummaryDetailed(), convertProfileSummaryFormat(), convertProfileSummaryModuleFlagValue(), getConstantMDFromKeyValueTuple(), and getTwoElementMDTuple().

◆ setDebugIntrinsicBuilderInsertionPoint()

LogicalResult setDebugIntrinsicBuilderInsertionPoint ( mlir::OpBuilder & builder,
DominanceInfo & domInfo,
Value argOperand )
static

Ensure that the debug intrinsic is inserted right after the operand definition.

Otherwise, the operand might not necessarily dominate the intrinsic. If the defining operation is a terminator, insert the intrinsic into a dominated block.

Definition at line 3585 of file ModuleImport.cpp.

References mlir::Block::empty(), mlir::Block::front(), mlir::Value::getDefiningOp(), mlir::detail::DominanceInfoBase< IsPostDom >::getNode(), mlir::Value::getParentBlock(), mlir::Block::getTerminator(), mlir::Operation::hasTrait(), mlir::OpBuilder::setInsertionPoint(), mlir::OpBuilder::setInsertionPointAfterValue(), and success().

◆ truncateDiag()

std::string truncateDiag ( std::string str)
static

Definition at line 65 of file ModuleImport.cpp.

References kMaxDiagLength.

Referenced by renderMetadataWithTracker().

Variable Documentation

◆ kExplicitLLVMFuncOpAttributePrefixes

std::array kExplicitLLVMFuncOpAttributePrefixes
staticconstexpr
Initial value:
{
StringLiteral("no-builtin-"),
}

Definition at line 3019 of file ModuleImport.cpp.

Referenced by processPassthroughAttrs().

◆ kExplicitLLVMFuncOpAttributes

std::array kExplicitLLVMFuncOpAttributes
staticconstexpr

Definition at line 2970 of file ModuleImport.cpp.

Referenced by processPassthroughAttrs().

◆ kMaxDiagLength

size_t kMaxDiagLength = 256
staticconstexpr

Upper bound on the characters a diagnostic renders for an LLVM entity.

One rendering is unbounded by nature: a GlobalVariable prints its entire initializer, and a node with long MDStrings or large inline constants prints them whole. On a merged library a single unbounded diagnostic reaches tens of megabytes, so every rendering path below truncates its output. Diagnostics only need to identify the entity; the full form is in the input being imported.

Definition at line 63 of file ModuleImport.cpp.

Referenced by truncateDiag().