14#ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15#define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
27#include "llvm/ADT/SetVector.h"
28#include "llvm/IR/FPEnv.h"
29#include "llvm/IR/Module.h"
34class CanonicalLoopInfo;
54class AliasScopeDomainAttr;
57class ComdatSelectorOp;
64class ModuleTranslation {
65 friend std::unique_ptr<llvm::Module>
72 auto result = functionMapping.try_emplace(name,
func);
75 "attempting to map a function that is already mapped");
80 return functionMapping.lookup(name);
89 llvm::Value *&
llvm = valueMapping[value];
90 assert(
llvm ==
nullptr &&
91 "attempting to map a value that is already mapped");
97 return valueMapping.lookup(value);
107 assert(
result.second &&
"attempting to map a block that is already mapped");
112 return blockMapping.lookup(block);
117 llvm::CanonicalLoopInfo *
result = loopMapping.lookup(
mlir);
118 assert(
result &&
"attempt to get non-existing loop");
138 assert(
llvm &&
"argument must be non-null");
139 llvm::CanonicalLoopInfo *&cur = loopMapping[
mlir];
140 assert(cur ==
nullptr &&
"attempting to map a loop that is already mapped");
156 "attempting to map a branch that is already mapped");
162 return branchMapping.lookup(op);
170 assert(
result.second &&
"attempting to map a call that is already mapped");
176 return callMapping.lookup(op);
182 auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);
185 "attempting to map a blockaddress operation that is already mapped");
190 auto result = blockAddressToLLVMMapping.try_emplace(attr, block);
193 "attempting to map a blockaddress attribute that is already mapped");
198 return blockAddressToLLVMMapping.lookup(attr);
217 llvm::Instruction *inst);
221 llvm::Instruction *inst);
224 void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
229 llvm::Instruction *inst);
256 return globalsMapping.lookup(op);
262 return aliasesMapping.lookup(op);
268 return ifuncMapping.lookup(op);
285 llvm::DIGlobalVariableExpression *
295 llvm::fp::ExceptionBehavior
306 llvm::IRBuilderBase &builder) {
307 return convertBlockImpl(bb, ignoreArguments, builder,
319 llvm::CallBase *call,
331 template <
typename T,
typename... Args>
333 stack.stackPush<T>(std::forward<Args>(args)...);
341 template <
typename T>
343 return stack.stackWalk(callback);
347 template <
typename T>
354 static std::optional<llvm::Attribute>
convertNoBuiltin(llvm::LLVMContext &ctx,
357 static std::optional<llvm::Attribute>
364 template <
typename AttrsTy,
typename Operation,
typename Converter>
366 const Converter &conv) {
369 for (
auto elt : attrs) {
380 std::unique_ptr<llvm::Module> llvmModule);
384 LogicalResult convertOperation(
Operation &op, llvm::IRBuilderBase &builder,
385 bool recordInsertions =
false);
386 LogicalResult convertFunctionSignatures();
387 LogicalResult convertFunctions();
388 LogicalResult convertIFuncs();
389 LogicalResult convertComdats();
391 LogicalResult convertUnresolvedBlockAddress();
398 LogicalResult convertGlobalsAndAliases();
399 LogicalResult convertOneFunction(LLVMFuncOp
func);
400 LogicalResult convertBlockImpl(
Block &bb,
bool ignoreArguments,
401 llvm::IRBuilderBase &builder,
402 bool recordInsertions);
406 llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr)
const;
410 LogicalResult createTBAAMetadata();
413 LogicalResult createIdentMetadata();
416 LogicalResult createCommandlineMetadata();
419 LogicalResult createDependentLibrariesMetadata();
428 FailureOr<llvm::AttrBuilder> convertParameterAttrs(
mlir::Location loc,
429 DictionaryAttr paramAttrs);
433 FailureOr<llvm::AttrBuilder>
434 convertParameterAttrs(LLVMFuncOp
func,
int argIdx, DictionaryAttr paramAttrs);
438 std::unique_ptr<llvm::Module> llvmModule;
440 std::unique_ptr<detail::DebugTranslation> debugTranslation;
443 std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
446 std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
467 llvm::StringMap<llvm::Function *> functionMapping;
533 const ModuleTranslation &moduleTranslation);
537 llvm::Intrinsic::ID intrinsic,
539 ArrayRef<llvm::Type *> tys = {});
546 llvm::Intrinsic::ID intrinsic,
548 ArrayRef<llvm::Value *> args);
554 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
555 Operation *intrOp, llvm::Intrinsic::ID intrinsic,
unsigned numResults,
556 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
557 ArrayRef<unsigned> immArgPositions,
558 ArrayRef<StringLiteral> immArgAttrNames);
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
Interface collection for translation to LLVM IR, dispatches to a concrete interface implementation ba...
This class represents the base attribute for all debug info attributes.
Implementation class for module translation.
void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst)
Maps a blockaddress operation to its corresponding placeholder LLVM value.
llvm::fp::ExceptionBehavior translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior)
Translates the given LLVM FP exception behavior metadata.
llvm::CallInst * lookupCall(Operation *op) const
Finds an LLVM call instruction that corresponds to the given MLIR call operation.
void mapCall(Operation *mlir, llvm::CallInst *llvm)
Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
llvm::DIGlobalVariableExpression * translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr)
Translates the given LLVM global variable expression metadata.
llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr)
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
WalkResult stackWalk(llvm::function_ref< WalkResult(T &)> callback)
Calls callback for every ModuleTranslation stack frame of type T starting from the top of the stack.
void stackPush(Args &&...args)
Creates a stack frame of type T on ModuleTranslation stack.
llvm::NamedMDNode * getOrInsertNamedModuleMetadata(StringRef name)
Gets the named metadata in the LLVM IR module being constructed, creating it if it does not exist.
LogicalResult convertBlock(Block &bb, bool ignoreArguments, llvm::IRBuilderBase &builder)
Translates the contents of the given block to LLVM IR using this translator.
void mapBranch(Operation *mlir, llvm::Instruction *llvm)
Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction.
void mapOmpLoop(Value mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
SmallVector< llvm::Value * > lookupValues(ValueRange values)
Looks up remapped a list of remapped values.
void mapFunction(StringRef name, llvm::Function *func)
Stores the mapping between a function name and its LLVM IR representation.
void convertFunctionAttrCollection(AttrsTy attrs, Operation *op, const Converter &conv)
A template that takes a collection-like attribute, and converts it via a user provided callback,...
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translates the given location.
void setDereferenceableMetadata(DereferenceableOpInterface op, llvm::Instruction *inst)
Sets LLVM dereferenceable metadata for operations that have dereferenceable attributes.
void setBranchWeightsMetadata(WeightedBranchOpInterface op)
Sets LLVM profiling metadata for operations that have branch weights.
llvm::Instruction * lookupBranch(Operation *op) const
Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
void invalidateOmpLoop(omp::NewCliOp mlir)
Mark an OpenMP loop as having been consumed.
LogicalResult convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp, llvm::CallBase *call, ArrayRef< unsigned > immArgPositions={})
Converts argument and result attributes from attrsOp to LLVM IR attributes on the call instruction.
static std::optional< llvm::Attribute > convertNoBuiltin(llvm::LLVMContext &ctx, mlir::Attribute a)
SymbolTableCollection & symbolTable()
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
llvm::Value *& mapValue(Value value)
Provides write-once access to store the LLVM IR value corresponding to the given MLIR value.
void invalidateOmpLoop(Value mlir)
Mark an OpenMP loop as having been consumed.
llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding)
Translates the given LLVM rounding mode metadata.
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::DIExpression * translateExpression(LLVM::DIExpressionAttr attr)
Translates the given LLVM DWARF expression metadata.
llvm::OpenMPIRBuilder * getOpenMPBuilder()
Returns the OpenMP IR builder associated with the LLVM IR module being constructed.
void mapOmpLoop(omp::NewCliOp mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
SaveStateStack< T, ModuleTranslation > SaveStack
RAII object calling stackPush/stackPop on construction/destruction.
llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
llvm::GlobalValue * lookupIFunc(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining an IFunc.
llvm::Metadata * translateDebugInfo(LLVM::DINodeAttr attr)
Translates the given LLVM debug info metadata.
void setDisjointFlag(Operation *op, llvm::Value *value)
Sets the disjoint flag attribute for the exported instruction value given the original operation op.
llvm::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
llvm::MDNode * getOrCreateAliasScopes(ArrayRef< AliasScopeAttr > aliasScopeAttrs)
Returns the LLVM metadata corresponding to an array of mlir LLVM dialect alias scope attributes.
void mapBlock(Block *mlir, llvm::BasicBlock *llvm)
Stores the mapping between an MLIR block and LLVM IR basic block.
llvm::MDNode * getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr)
Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope attribute.
llvm::Module * getLLVMModule()
Returns the LLVM module in which the IR is being constructed.
static std::optional< llvm::Attribute > convertDefaultFuncAttr(llvm::LLVMContext &ctx, mlir::NamedAttribute namedAttr)
llvm::CanonicalLoopInfo * lookupOMPLoop(Value mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
void stackPop()
Pops the last element from the ModuleTranslation stack.
void forgetMapping(Region ®ion)
Removes the mapping for blocks contained in the region and values defined in these blocks.
void setAliasScopeMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
void setAccessGroupsMetadata(AccessGroupOpInterface op, llvm::Instruction *inst)
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
llvm::CanonicalLoopInfo * lookupOMPLoop(omp::NewCliOp mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block)
Maps a BlockAddressAttr to its corresponding LLVM basic block.
void setLoopMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM loop metadata for branch operations that have a loop annotation attribute.
Utility class to translate MLIR LLVM dialect types to LLVM IR.
A helper class that converts LoopAnnotationAttrs and AccessGroupAttrs into corresponding llvm::MDNode...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttribute represents a combination of a name and an Attribute value.
Operation is the basic unit of execution within MLIR.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
This class represents a collection of SymbolTables.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
A utility result that is used to signal how to proceed with an ongoing walk:
The OpAsmOpInterface, see OpAsmInterface.td for more details.
void connectPHINodes(Region ®ion, const ModuleTranslation &state)
For all blocks in the region that were converted to LLVM IR using the given ModuleTranslation,...
llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)
Create an LLVM IR constant of llvmType from the MLIR attribute attr.
Include the generated interface declarations.
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
RAII object calling stackPush/stackPop on construction/destruction.