MLIR  21.0.0git
Functions | Variables
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 "DataLayoutImporter.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/Tools/mlir-translate/Translation.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/Comdat.h"
#include "llvm/IR/Constants.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/Operator.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 diag (const llvm::Value &value)
 
static std::string diagMD (const llvm::Metadata *node, const llvm::Module *module)
 
static constexpr StringRef getGlobalCtorsVarName ()
 Returns the name of the global_ctors global variables. More...
 
static constexpr StringRef getNamelessGlobalPrefix ()
 Prefix used for symbols of nameless llvm globals. More...
 
static constexpr StringRef getGlobalDtorsVarName ()
 Returns the name of the global_dtors global variables. More...
 
static constexpr StringRef getGlobalComdatOpName ()
 Returns the symbol name for the module-level comdat operation. More...
 
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. More...
 
static SmallVector< int64_t > getPositionFromIndices (ArrayRef< unsigned > indices)
 Converts an array of unsigned indices to a signed integer position array. More...
 
static LogicalResult convertInstructionImpl (OpBuilder &odsBuilder, llvm::Instruction *inst, ModuleImport &moduleImport, LLVMImportInterface &iface)
 Converts the LLVM instructions that have a generated MLIR builder. More...
 
static SetVector< llvm::BasicBlock * > getTopologicallySortedBlocks (ArrayRef< llvm::BasicBlock * > basicBlocks)
 Get a topologically sorted list of blocks for the given basic blocks. More...
 
static Attribute convertCGProfileModuleFlagValue (ModuleOp mlirModule, llvm::MDTuple *mdTuple)
 
static llvm::MDTuple * getTwoElementMDTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md)
 Extract a two element MDTuple from a MDOperand. More...
 
static llvm::ConstantAsMetadata * getConstantMDFromKeyValueTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md, StringRef matchKey, bool optional=false)
 Extract a constant metadata value from a two element tuple (<key, value>). More...
 
static FailureOr< uint64_t > convertInt64FromKeyValueTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &md, StringRef matchKey)
 Extract an integer value from a two element tuple (<key, value>). More...
 
static std::optional< ProfileSummaryFormatKind > convertProfileSummaryFormat (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &formatMD)
 
static FailureOr< SmallVector< ModuleFlagProfileSummaryDetailedAttr > > convertProfileSummaryDetailed (ModuleOp mlirModule, const llvm::Module *llvmModule, const llvm::MDOperand &summaryMD)
 
static Attribute convertProfileSummaryModuleFlagValue (ModuleOp mlirModule, const llvm::Module *llvmModule, llvm::MDTuple *mdTuple)
 
static Attribute convertModuleFlagValueFromMDTuple (ModuleOp mlirModule, const llvm::Module *llvmModule, StringRef key, llvm::MDTuple *mdTuple)
 Invoke specific handlers for each known module flag value, returns nullptr if the key is unknown or unimplemented. More...
 
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. More...
 
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. More...
 
static SmallVector< AttributegetSequenceConstantAsAttrs (OpBuilder &builder, llvm::ConstantDataSequential *constSequence)
 Returns an integer or float attribute array for the provided constant sequence constSequence or nullptr if the conversion fails. More...
 
static LogicalResult checkFunctionTypeCompatibility (LLVMFunctionType callType, LLVMFunctionType calleeType)
 Checks if callType and calleeType are compatible and can be represented in MLIR. More...
 
static void processMemoryEffects (llvm::Function *func, LLVMFuncOp funcOp)
 
static void processPassthroughAttrs (llvm::Function *func, LLVMFuncOp funcOp)
 
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. More...
 

Variables

static constexpr std::array kExplicitAttributes
 

Function Documentation

◆ checkFunctionTypeCompatibility()

static LogicalResult checkFunctionTypeCompatibility ( LLVMFunctionType  callType,
LLVMFunctionType  calleeType 
)
static

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

Definition at line 1997 of file ModuleImport.cpp.

◆ convertCallBaseAttributes()

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

Definition at line 2789 of file ModuleImport.cpp.

◆ convertCGProfileModuleFlagValue()

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

Definition at line 524 of file ModuleImport.cpp.

References mlir::get(), and mlir::FlatSymbolRefAttr::get().

Referenced by convertModuleFlagValueFromMDTuple().

◆ convertInstructionImpl()

static 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 131 of file ModuleImport.cpp.

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

◆ convertInt64FromKeyValueTuple()

static FailureOr<uint64_t> convertInt64FromKeyValueTuple ( ModuleOp  mlirModule,
const llvm::Module *  llvmModule,
const llvm::MDOperand &  md,
StringRef  matchKey 
)
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 596 of file ModuleImport.cpp.

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

Referenced by convertProfileSummaryModuleFlagValue().

◆ convertModuleFlagValueFromMDTuple()

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

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

Definition at line 829 of file ModuleImport.cpp.

References convertCGProfileModuleFlagValue(), and convertProfileSummaryModuleFlagValue().

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

◆ convertProfileSummaryDetailed()

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

◆ convertProfileSummaryFormat()

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

◆ convertProfileSummaryModuleFlagValue()

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

◆ diag()

static std::string diag ( const llvm::Value &  value)
static

Definition at line 55 of file ModuleImport.cpp.

Referenced by mlir::transform::gpu::alterGpuLaunch(), mlir::transform::TransformState::applyTransform(), mlir::transform::detail::checkApplyToOne(), checkDoubleConsume(), checkMappingSpec(), mlir::transform::detail::checkNestedConsumption(), checkRepeatedConsumptionInOperand(), cloneAndFuseFirstUse(), mlir::LLVM::ModuleImport::convertAliases(), mlir::lsp::MLIRServer::convertFromBytecode(), mlir::LLVM::ModuleImport::convertGlobals(), mlir::LLVM::ModuleImport::convertValue(), mlir::transform::gpu::createGpuLaunch(), diagnoseInvalidOperandDominance(), mlir::DiagnosticEngine::emit(), mlir::detail::DiagnosticEngineImpl::emit(), emitDiag(), mlir::SourceMgrDiagnosticHandler::emitDiagnostic(), mlir::detail::ParallelDiagnosticHandlerImpl::emitDiagnostics(), mlir::Operation::emitError(), mlir::detail::Parser::emitError(), mlir::Operation::emitRemark(), mlir::Operation::emitWarning(), ensurePayloadIsSeparateFromTransform(), mlir::detail::PassCrashReproducerGenerator::finalize(), findPayloadRoot(), mlir::transform::TrackingListener::findReplacementOp(), mlir::transform::detail::findTransformEntryPoint(), getLspDiagnoticFromDiag(), getThreadIdBuilder(), legalizeUnresolvedMaterialization(), mlir::pdll::Lexer::Lexer(), mlir::transform::gpu::mapForallToBlocksImpl(), mlir::transform::gpu::mapNestedForallToThreadsImpl(), mlir::transform::gpu::mapOneForallToThreadsImpl(), mlir::GPUFuncOpLowering::matchAndRewrite(), mlir::transform::detail::mergeSymbolsInto(), mlir::RewriterBase::notifyMatchFailure(), mlir::transform::TrackingListener::notifyMatchFailure(), mlir::transform::ErrorCheckingTrackingListener::notifyMatchFailure(), mlir::detail::ConversionPatternRewriterImpl::notifyMatchFailure(), mlir::transform::ErrorCheckingTrackingListener::notifyPayloadReplacementNotFound(), mlir::operator<<(), mlir::detail::ParallelDiagnosticHandlerImpl::ParallelDiagnosticHandlerImpl(), mlir::AsmParser::parseColonType(), mlir::AsmParser::parseCustomTypeWithFallback(), mlir::tracing::FileLineColLocBreakpoint::parseFromString(), mlir::AsmParser::parseType(), mlir::detail::ParallelDiagnosticHandlerImpl::print(), mlir::query::matcher::MatchFinder::printMatch(), printRegionEdgeName(), mlir::LLVM::ModuleImport::processFunction(), mlir::dlti::query(), mlir::BytecodeReader::Impl::read(), mlir::DiagnosticEngine::registerHandler(), mlir::detail::ConversionPatternRewriterImpl::remapValues(), mlir::transform::TransformResults::setMappedValues(), mlir::DiagnosedSilenceableFailure::silenceableFailure(), mlir::SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(), mlir::SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(), tileAndFuseFirstExtractUse(), tileAndFuseFirstExtractUseThroughContainingOpBlockArgument(), mlir::lsp::toJSON(), mlir::irdl::RegionConstraint::verify(), verifyAndGetTerminator(), mlir::impl::verifyCastInterfaceOp(), mlir::detail::verifyDataLayoutOp(), verifyNamedSequenceOp(), verifyOpAndAdjustFlags(), mlir::transform::detail::verifyPossibleTopLevelTransformOpTrait(), verifyQuantifierRegions(), mlir::transform::detail::verifyTransformOpInterface(), verifyTypeRangesMatch(), verifyTypesAlongAllEdges(), mlir::detail::verifyTypesAlongControlFlowEdges(), verifyYieldingSingleBlockOp(), and mlir::detail::ParallelDiagnosticHandlerImpl::~ParallelDiagnosticHandlerImpl().

◆ diagMD()

static std::string diagMD ( const llvm::Metadata *  node,
const llvm::Module *  module 
)
static

◆ getConstantMDFromKeyValueTuple()

static llvm::ConstantAsMetadata* getConstantMDFromKeyValueTuple ( ModuleOp  mlirModule,
const llvm::Module *  llvmModule,
const llvm::MDOperand &  md,
StringRef  matchKey,
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 574 of file ModuleImport.cpp.

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

Referenced by convertInt64FromKeyValueTuple(), and convertProfileSummaryModuleFlagValue().

◆ getGlobalComdatOpName()

static 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 90 of file ModuleImport.cpp.

◆ getGlobalCtorsVarName()

static constexpr StringRef getGlobalCtorsVarName ( )
staticconstexpr

Returns the name of the global_ctors global variables.

Definition at line 74 of file ModuleImport.cpp.

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

◆ getGlobalDtorsVarName()

static constexpr StringRef getGlobalDtorsVarName ( )
staticconstexpr

Returns the name of the global_dtors global variables.

Definition at line 84 of file ModuleImport.cpp.

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

◆ getLLVMSyncScope()

static 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 99 of file ModuleImport.cpp.

◆ getNamelessGlobalPrefix()

static constexpr StringRef getNamelessGlobalPrefix ( )
staticconstexpr

Prefix used for symbols of nameless llvm globals.

Definition at line 79 of file ModuleImport.cpp.

◆ getPositionFromIndices()

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

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

Definition at line 119 of file ModuleImport.cpp.

◆ getScalarConstantAsAttr()

static 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 1190 of file ModuleImport.cpp.

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

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

◆ getSequenceConstantAsAttrs()

static 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 1221 of file ModuleImport.cpp.

References getScalarConstantAsAttr().

◆ getTopologicallySortedBlocks()

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

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

Definition at line 151 of file ModuleImport.cpp.

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

◆ getTwoElementMDTuple()

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

Extract a two element MDTuple from a MDOperand.

Emit a warning in case something else is found.

Definition at line 561 of file ModuleImport.cpp.

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

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

◆ getVectorTypeForAttr()

static 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 1143 of file ModuleImport.cpp.

◆ isMetadataKillLocation()

static bool isMetadataKillLocation ( llvm::DbgVariableIntrinsic *  dbgIntr)
static

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

Definition at line 2950 of file ModuleImport.cpp.

◆ processMemoryEffects()

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

Definition at line 2463 of file ModuleImport.cpp.

References mlir::get().

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

◆ processPassthroughAttrs()

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

Variable Documentation

◆ kExplicitAttributes

constexpr std::array kExplicitAttributes
staticconstexpr

Definition at line 2482 of file ModuleImport.cpp.

Referenced by processPassthroughAttrs().