MLIR  19.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/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/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
#include "mlir/Transforms/RegionUtils.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 <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 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 convertFunctionAttributes (LLVMFuncOp func, llvm::Function *llvmFunc)
 Converts the function attributes from LLVMFuncOp and attaches them to the llvm::Function. More...
 
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 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 957 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 308 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 1166 of file ModuleTranslation.cpp.

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 363 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 460 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 the function attributes from LLVMFuncOp and attaches them to the llvm::Function.

Definition at line 1349 of file ModuleTranslation.cpp.

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

References checkedAddLLVMFnAttribute(), mlir::emitError(), mlir::failed(), mlir::failure(), and mlir::success().

◆ getInnermostElementType()

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

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

Definition at line 343 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 940 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 730 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 949 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 179 of file ModuleTranslation.cpp.

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