MLIR  22.0.0git
Macros | Functions
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/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/Support/LLVM.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Analysis/TargetFolder.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/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/ErrorHandling.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 FailureOr< llvm::Attribute > convertMLIRAttributeToLLVM (Location loc, llvm::LLVMContext &ctx, StringRef key, StringRef value=StringRef())
 Attempts to translate an MLIR attribute identified by key, optionally with the given value, into an LLVM IR attribute. More...
 
static FailureOr< llvm::AttrBuilder > convertMLIRAttributesToLLVM (Location loc, llvm::LLVMContext &ctx, ArrayAttr arrayAttr, StringRef arrayAttrName)
 Converts the MLIR attributes listed in the given array attribute into LLVM attributes. 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 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 LogicalResult convertParameterAttr (llvm::AttrBuilder &attrBuilder, llvm::Attribute::AttrKind llvmKind, NamedAttribute namedAttr, ModuleTranslation &moduleTranslation, Location loc)
 
static std::unique_ptr< llvm::Module > prepareLLVMModule (Operation *m, llvm::LLVMContext &llvmContext, StringRef name)
 

Macro Definition Documentation

◆ DEBUG_TYPE

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

Definition at line 58 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 1078 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 320 of file ModuleTranslation.cpp.

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

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

◆ 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 375 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 472 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 1650 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 1676 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 1629 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 1495 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 1478 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 1471 of file ModuleTranslation.cpp.

References mlir::get().

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

◆ convertMLIRAttributesToLLVM()

static FailureOr<llvm::AttrBuilder> convertMLIRAttributesToLLVM ( Location  loc,
llvm::LLVMContext &  ctx,
ArrayAttr  arrayAttr,
StringRef  arrayAttrName 
)
static

Converts the MLIR attributes listed in the given array attribute into LLVM attributes.

Returns an AttrBuilder containing the converted attributes. Reports error to loc if any and returns immediately. Expects arrayAttr to contain 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 1124 of file ModuleTranslation.cpp.

References convertMLIRAttributeToLLVM(), mlir::emitError(), and mlir::remark::failed().

◆ convertMLIRAttributeToLLVM()

static FailureOr<llvm::Attribute> convertMLIRAttributeToLLVM ( Location  loc,
llvm::LLVMContext &  ctx,
StringRef  key,
StringRef  value = StringRef() 
)
static

Attempts to translate an MLIR attribute identified by key, optionally with the given value, into an LLVM IR attribute.

Reports errors at loc if any. If the attribute name corresponds to a known LLVM IR attribute kind, creates the LLVM attribute of that kind; otherwise, keeps 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 1091 of file ModuleTranslation.cpp.

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

Referenced by convertMLIRAttributesToLLVM().

◆ convertParameterAttr()

static LogicalResult convertParameterAttr ( llvm::AttrBuilder &  attrBuilder,
llvm::Attribute::AttrKind  llvmKind,
NamedAttribute  namedAttr,
ModuleTranslation moduleTranslation,
Location  loc 
)
static

◆ 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 1484 of file ModuleTranslation.cpp.

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

Referenced by convertFunctionKernelAttributes().

◆ getInnermostElementType()

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

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

Definition at line 355 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 1061 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 808 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

◆ 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 1070 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 176 of file ModuleTranslation.cpp.

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