MLIR 22.0.0git
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.
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.
static llvm::Type * getInnermostElementType (llvm::Type *type)
 Returns the first non-sequential type nested in sequential types.
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.
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.
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.
static BlockgetModuleBody (Operation *module)
 A helper method to get the single Block in an operation honoring LLVM's module requirements.
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.
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.
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.
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.
static llvm::Metadata * convertIntegerToMetadata (llvm::LLVMContext &context, const llvm::APInt &value)
 Return a representation of value as metadata.
static llvm::MDNode * convertIntegerToMDNode (llvm::LLVMContext &context, const llvm::APInt &value)
 Return a representation of value as an MDNode.
static llvm::MDNode * convertVecTypeHintToMDNode (llvm::LLVMContext &context, llvm::Type *type, bool isSigned)
 Return an MDNode encoding vec_type_hint metadata.
static llvm::MDNode * convertIntegerArrayToMDNode (llvm::LLVMContext &context, ArrayRef< int32_t > values)
 Return an MDNode with a tuple given by the values in values.
static void convertFunctionMemoryAttributes (LLVMFuncOp func, llvm::Function *llvmFunc)
 Converts memory effect attributes from func and attaches them to llvmFunc.
static void convertFunctionAttributes (LLVMFuncOp func, llvm::Function *llvmFunc)
 Converts function attributes from func and attaches them to llvmFunc.
static void convertFunctionKernelAttributes (LLVMFuncOp func, llvm::Function *llvmFunc, ModuleTranslation &translation)
 Converts function attributes from func and attaches them to llvmFunc.
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()

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

◆ buildSequentialConstant()

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 buildSequentialConstant(), mlir::emitError(), and result.

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

◆ convertDenseElementsAttr()

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(), getShape(), mlir::DenseElementsAttr::getSplatValue(), mlir::DenseElementsAttr::getType(), and mlir::DenseElementsAttr::isSplat().

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

◆ convertDenseResourceElementsAttr()

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 mlir::emitError(), mlir::AsmResourceBlob::getData(), and getInnermostElementType().

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

◆ convertFunctionAttributes()

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

Converts function attributes from func and attaches them to llvmFunc.

Definition at line 1644 of file ModuleTranslation.cpp.

References convertFunctionMemoryAttributes().

◆ convertFunctionKernelAttributes()

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

Converts function attributes from func and attaches them to llvmFunc.

Definition at line 1672 of file ModuleTranslation.cpp.

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

◆ convertFunctionMemoryAttributes()

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

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

Definition at line 1623 of file ModuleTranslation.cpp.

Referenced by convertFunctionAttributes().

◆ convertIntegerArrayToMDNode()

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

References convertIntegerToMetadata().

Referenced by convertFunctionKernelAttributes().

◆ convertIntegerToMDNode()

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

Return a representation of value as an MDNode.

Definition at line 1475 of file ModuleTranslation.cpp.

References convertIntegerToMetadata().

Referenced by convertFunctionKernelAttributes().

◆ convertIntegerToMetadata()

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

Return a representation of value as metadata.

Definition at line 1468 of file ModuleTranslation.cpp.

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

◆ convertMLIRAttributesToLLVM()

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

References ArrayAttr(), convertMLIRAttributeToLLVM(), and mlir::emitError().

◆ convertMLIRAttributeToLLVM()

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

References mlir::emitError(), and result.

Referenced by convertMLIRAttributesToLLVM().

◆ convertParameterAttr()

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

◆ convertVecTypeHintToMDNode()

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

Return an MDNode encoding vec_type_hint metadata.

Definition at line 1481 of file ModuleTranslation.cpp.

References convertIntegerToMetadata().

Referenced by convertFunctionKernelAttributes().

◆ getInnermostElementType()

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()

Block & getModuleBody ( Operation * module)
static

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

Definition at line 1058 of file ModuleTranslation.cpp.

◆ getPHISourceValue()

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

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

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

◆ prepareLLVMModule()

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

◆ shouldDropGlobalInitializer()

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

◆ translateDataLayout()

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::DataLayout::getTypeABIAlignment(), mlir::DataLayout::getTypeIndexBitwidth(), mlir::DataLayout::getTypePreferredAlignment(), mlir::DataLayout::getTypeSizeInBits(), result, and success().