MLIR  20.0.0git
Macros | Functions | Variables
ModuleTranslation.cpp File Reference
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "AttrKindDetail.h"
#include "DebugTranslation.h"
#include "LoopAnnotationTranslation.h"
#include "mlir/Analysis/TopologicalSortUtils.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
#include "mlir/Dialect/LLVMIR/Transforms/DIExpressionLegalization.h"
#include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
#include "mlir/IR/AttrTypeSubElements.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectResourceBlobManager.h"
#include "mlir/IR/RegionGraphTraits.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <numeric>
#include <optional>
#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "llvm-dialect-to-llvm-ir"
 

Functions

static FailureOr< llvm::DataLayout > translateDataLayout (DataLayoutSpecInterface attribute, const DataLayout &dataLayout, std::optional< Location > loc=std::nullopt)
 Translates the given data layout spec attribute to the LLVM IR data layout. More...
 
static llvm::Constant * buildSequentialConstant (ArrayRef< llvm::Constant * > &constants, ArrayRef< int64_t > shape, llvm::Type *type, Location loc)
 Builds a constant of a sequential LLVM type type, potentially containing other sequential types recursively, from the individual constant values provided in constants. More...
 
static llvm::Type * getInnermostElementType (llvm::Type *type)
 Returns the first non-sequential type nested in sequential types. More...
 
static llvm::Constant * convertDenseElementsAttr (Location loc, DenseElementsAttr denseElementsAttr, llvm::Type *llvmType, const ModuleTranslation &moduleTranslation)
 Convert a dense elements attribute to an LLVM IR constant using its raw data storage if possible. More...
 
static llvm::Constant * convertDenseResourceElementsAttr (Location loc, DenseResourceElementsAttr denseResourceAttr, llvm::Type *llvmType, const ModuleTranslation &moduleTranslation)
 Convert a dense resource elements attribute to an LLVM IR constant using its raw data storage if possible. More...
 
static Value getPHISourceValue (Block *current, Block *pred, unsigned numArguments, unsigned index)
 Get the SSA value passed to the current block from the terminator operation of its predecessor. More...
 
static BlockgetModuleBody (Operation *module)
 A helper method to get the single Block in an operation honoring LLVM's module requirements. More...
 
static bool shouldDropGlobalInitializer (llvm::GlobalValue::LinkageTypes linkage, llvm::Constant *cst)
 A helper method to decide if a constant must not be set as a global variable initializer. More...
 
static void addRuntimePreemptionSpecifier (bool dsoLocalRequested, llvm::GlobalValue *gv)
 Sets the runtime preemption specifier of gv to dso_local if dsoLocalRequested is true, otherwise it is left unchanged. More...
 
static LogicalResult checkedAddLLVMFnAttribute (Location loc, llvm::Function *llvmFunc, StringRef key, StringRef value=StringRef())
 Attempts to add an attribute identified by key, optionally with the given value to LLVM function llvmFunc. More...
 
static llvm::Metadata * convertIntegerToMetadata (llvm::LLVMContext &context, const llvm::APInt &value)
 Return a representation of value as metadata. More...
 
static llvm::MDNode * convertIntegerToMDNode (llvm::LLVMContext &context, const llvm::APInt &value)
 Return a representation of value as an MDNode. More...
 
static llvm::MDNode * convertVecTypeHintToMDNode (llvm::LLVMContext &context, llvm::Type *type, bool isSigned)
 Return an MDNode encoding vec_type_hint metadata. More...
 
static llvm::MDNode * convertIntegerArrayToMDNode (llvm::LLVMContext &context, ArrayRef< int32_t > values)
 Return an MDNode with a tuple given by the values in values. More...
 
static LogicalResult forwardPassthroughAttributes (Location loc, std::optional< ArrayAttr > attributes, llvm::Function *llvmFunc)
 Attaches the attributes listed in the given array attribute to llvmFunc. More...
 
static void convertFunctionMemoryAttributes (LLVMFuncOp func, llvm::Function *llvmFunc)
 Converts memory effect attributes from func and attaches them to llvmFunc. More...
 
static void convertFunctionAttributes (LLVMFuncOp func, llvm::Function *llvmFunc)
 Converts function attributes from func and attaches them to llvmFunc. More...
 
static void convertFunctionKernelAttributes (LLVMFuncOp func, llvm::Function *llvmFunc, ModuleTranslation &translation)
 Converts function attributes from func and attaches them to llvmFunc. More...
 
static std::unique_ptr< llvm::Module > prepareLLVMModule (Operation *m, llvm::LLVMContext &llvmContext, StringRef name)
 

Variables

llvm::cl::opt< bool > UseNewDbgInfoFormat
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "llvm-dialect-to-llvm-ir"

Definition at line 61 of file ModuleTranslation.cpp.

Function Documentation

◆ addRuntimePreemptionSpecifier()

static void addRuntimePreemptionSpecifier ( bool  dsoLocalRequested,
llvm::GlobalValue *  gv 
)
static

Sets the runtime preemption specifier of gv to dso_local if dsoLocalRequested is true, otherwise it is left unchanged.

Definition at line 1023 of file ModuleTranslation.cpp.

◆ buildSequentialConstant()

static llvm::Constant* buildSequentialConstant ( ArrayRef< llvm::Constant * > &  constants,
ArrayRef< int64_t >  shape,
llvm::Type *  type,
Location  loc 
)
static

Builds a constant of a sequential LLVM type type, potentially containing other sequential types recursively, from the individual constant values provided in constants.

shape contains the number of elements in nested sequential types. Reports errors at loc and returns nullptr on error.

Definition at line 304 of file ModuleTranslation.cpp.

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

Referenced by convertDenseElementsAttr(), convertDenseResourceElementsAttr(), and mlir::LLVM::detail::getLLVMConstant().

◆ checkedAddLLVMFnAttribute()

static LogicalResult checkedAddLLVMFnAttribute ( Location  loc,
llvm::Function *  llvmFunc,
StringRef  key,
StringRef  value = StringRef() 
)
static

Attempts to add an attribute identified by key, optionally with the given value to LLVM function llvmFunc.

Reports errors at loc if any. If the attribute has a kind known to LLVM IR, create the attribute of this kind, otherwise keep it as a string attribute. Performs additional checks for attributes known to have or not have a value in order to avoid assertions inside LLVM upon construction.

Definition at line 1260 of file ModuleTranslation.cpp.

References mlir::emitError(), mlir::get(), and None.

Referenced by forwardPassthroughAttributes().

◆ convertDenseElementsAttr()

static llvm::Constant* convertDenseElementsAttr ( Location  loc,
DenseElementsAttr  denseElementsAttr,
llvm::Type *  llvmType,
const ModuleTranslation moduleTranslation 
)
static

Convert a dense elements attribute to an LLVM IR constant using its raw data storage if possible.

This supports elements attributes of tensor or vector type and avoids constructing separate objects for individual values of the innermost dimension. Constants for other dimensions are still constructed recursively. Returns null if constructing from raw data is not supported for this type, e.g., element type is not a power-of-two-sized primitive. Reports other errors at loc.

Definition at line 359 of file ModuleTranslation.cpp.

References buildSequentialConstant(), getInnermostElementType(), mlir::LLVM::detail::getLLVMConstant(), mlir::DenseElementsAttr::getNumElements(), mlir::DenseElementsAttr::getRawData(), mlir::DenseElementsAttr::getSplatValue(), mlir::DenseElementsAttr::getType(), and mlir::DenseElementsAttr::isSplat().

Referenced by mlir::LLVM::detail::getLLVMConstant().

◆ convertDenseResourceElementsAttr()

static llvm::Constant* convertDenseResourceElementsAttr ( Location  loc,
DenseResourceElementsAttr  denseResourceAttr,
llvm::Type *  llvmType,
const ModuleTranslation moduleTranslation 
)
static

Convert a dense resource elements attribute to an LLVM IR constant using its raw data storage if possible.

This supports elements attributes of tensor or vector type and avoids constructing separate objects for individual values of the innermost dimension. Constants for other dimensions are still constructed recursively. Returns nullptr on failure and emits errors at loc.

Definition at line 456 of file ModuleTranslation.cpp.

References buildSequentialConstant(), mlir::emitError(), mlir::AsmResourceBlob::getData(), and getInnermostElementType().

Referenced by mlir::LLVM::detail::getLLVMConstant().

◆ convertFunctionAttributes()

static void convertFunctionAttributes ( LLVMFuncOp  func,
llvm::Function *  llvmFunc 
)
static

Converts function attributes from func and attaches them to llvmFunc.

Definition at line 1511 of file ModuleTranslation.cpp.

References convertFunctionMemoryAttributes().

◆ convertFunctionKernelAttributes()

static void convertFunctionKernelAttributes ( LLVMFuncOp  func,
llvm::Function *  llvmFunc,
ModuleTranslation translation 
)
static

Converts function attributes from func and attaches them to llvmFunc.

Definition at line 1529 of file ModuleTranslation.cpp.

References convertIntegerArrayToMDNode(), convertIntegerToMDNode(), mlir::LLVM::ModuleTranslation::convertType(), and convertVecTypeHintToMDNode().

◆ convertFunctionMemoryAttributes()

static void convertFunctionMemoryAttributes ( LLVMFuncOp  func,
llvm::Function *  llvmFunc 
)
static

Converts memory effect attributes from func and attaches them to llvmFunc.

Definition at line 1490 of file ModuleTranslation.cpp.

Referenced by convertFunctionAttributes().

◆ convertIntegerArrayToMDNode()

static llvm::MDNode* convertIntegerArrayToMDNode ( llvm::LLVMContext &  context,
ArrayRef< int32_t >  values 
)
static

Return an MDNode with a tuple given by the values in values.

Definition at line 1317 of file ModuleTranslation.cpp.

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

Referenced by convertFunctionKernelAttributes().

◆ convertIntegerToMDNode()

static llvm::MDNode* convertIntegerToMDNode ( llvm::LLVMContext &  context,
const llvm::APInt &  value 
)
static

Return a representation of value as an MDNode.

Definition at line 1300 of file ModuleTranslation.cpp.

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

Referenced by convertFunctionKernelAttributes().

◆ convertIntegerToMetadata()

static llvm::Metadata* convertIntegerToMetadata ( llvm::LLVMContext &  context,
const llvm::APInt &  value 
)
static

Return a representation of value as metadata.

Definition at line 1293 of file ModuleTranslation.cpp.

References mlir::get().

Referenced by convertIntegerArrayToMDNode(), convertIntegerToMDNode(), and convertVecTypeHintToMDNode().

◆ convertVecTypeHintToMDNode()

static llvm::MDNode* convertVecTypeHintToMDNode ( llvm::LLVMContext &  context,
llvm::Type *  type,
bool  isSigned 
)
static

Return an MDNode encoding vec_type_hint metadata.

Definition at line 1306 of file ModuleTranslation.cpp.

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

Referenced by convertFunctionKernelAttributes().

◆ forwardPassthroughAttributes()

static LogicalResult forwardPassthroughAttributes ( Location  loc,
std::optional< ArrayAttr >  attributes,
llvm::Function *  llvmFunc 
)
static

Attaches the attributes listed in the given array attribute to llvmFunc.

Reports error to loc if any and returns immediately. Expects attributes to be an array attribute containing either string attributes, treated as value-less LLVM attributes, or array attributes containing two string attributes, with the first string being the name of the corresponding LLVM attribute and the second string beings its value. Note that even integer attributes are expected to have their values expressed as strings.

Definition at line 1335 of file ModuleTranslation.cpp.

References checkedAddLLVMFnAttribute(), and mlir::emitError().

◆ getInnermostElementType()

static llvm::Type* getInnermostElementType ( llvm::Type *  type)
static

Returns the first non-sequential type nested in sequential types.

Definition at line 339 of file ModuleTranslation.cpp.

Referenced by convertDenseElementsAttr(), convertDenseResourceElementsAttr(), and mlir::LLVM::detail::getLLVMConstant().

◆ getModuleBody()

static Block& getModuleBody ( Operation module)
static

A helper method to get the single Block in an operation honoring LLVM's module requirements.

Definition at line 1006 of file ModuleTranslation.cpp.

References mlir::Region::front(), and mlir::Operation::getRegion().

◆ getPHISourceValue()

static Value getPHISourceValue ( Block current,
Block pred,
unsigned  numArguments,
unsigned  index 
)
static

Get the SSA value passed to the current block from the terminator operation of its predecessor.

Definition at line 761 of file ModuleTranslation.cpp.

References mlir::SuccessorOperands::empty(), mlir::detail::enumerate(), mlir::Operation::getNumSuccessors(), mlir::Operation::getOperand(), mlir::Operation::getSuccessor(), and mlir::Block::getTerminator().

◆ prepareLLVMModule()

static std::unique_ptr<llvm::Module> prepareLLVMModule ( Operation m,
llvm::LLVMContext &  llvmContext,
StringRef  name 
)
static

Definition at line 1983 of file ModuleTranslation.cpp.

◆ shouldDropGlobalInitializer()

static bool shouldDropGlobalInitializer ( llvm::GlobalValue::LinkageTypes  linkage,
llvm::Constant *  cst 
)
static

A helper method to decide if a constant must not be set as a global variable initializer.

For an external linkage variable, the variable with an initializer is considered externally visible and defined in this module, the variable without an initializer is externally available and is defined elsewhere.

Definition at line 1015 of file ModuleTranslation.cpp.

◆ translateDataLayout()

static FailureOr<llvm::DataLayout> translateDataLayout ( DataLayoutSpecInterface  attribute,
const DataLayout dataLayout,
std::optional< Location loc = std::nullopt 
)
static

Translates the given data layout spec attribute to the LLVM IR data layout.

Only integer, float, pointer and endianness entries are currently supported.

Definition at line 181 of file ModuleTranslation.cpp.

References mlir::emitError(), mlir::get(), mlir::DataLayout::getTypeABIAlignment(), mlir::DataLayout::getTypeIndexBitwidth(), mlir::DataLayout::getTypePreferredAlignment(), and mlir::DataLayout::getTypeSizeInBits().

Variable Documentation

◆ UseNewDbgInfoFormat

llvm::cl::opt<bool> UseNewDbgInfoFormat
extern