14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
43 class DebugTranslation;
44 class LoopAnnotationTranslation;
48 class AliasScopeDomainAttr;
51 class ComdatSelectorOp;
59 friend std::unique_ptr<llvm::Module>
65 auto result = functionMapping.try_emplace(name, func);
67 assert(result.second &&
68 "attempting to map a function that is already mapped");
73 return functionMapping.lookup(name);
82 llvm::Value *&
llvm = valueMapping[value];
83 assert(
llvm ==
nullptr &&
84 "attempting to map a value that is already mapped");
90 return valueMapping.lookup(value);
98 auto result = blockMapping.try_emplace(
mlir,
llvm);
100 assert(result.second &&
"attempting to map a block that is already mapped");
105 return blockMapping.lookup(block);
111 auto result = branchMapping.try_emplace(
mlir,
llvm);
113 assert(result.second &&
114 "attempting to map a branch that is already mapped");
120 return branchMapping.lookup(op);
126 auto result = callMapping.try_emplace(
mlir,
llvm);
128 assert(result.second &&
"attempting to map a call that is already mapped");
134 return callMapping.lookup(op);
153 llvm::Instruction *inst);
157 llvm::Instruction *inst);
160 void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
181 return globalsMapping.lookup(op);
198 llvm::DIGlobalVariableExpression *
208 llvm::fp::ExceptionBehavior
219 llvm::IRBuilderBase &builder) {
220 return convertBlockImpl(bb, ignoreArguments, builder,
239 virtual void anchor();
249 template <
typename Derived>
260 template <
typename T,
typename... Args>
263 std::is_base_of<StackFrame, T>::value,
264 "can only push instances of StackFrame on ModuleTranslation stack");
265 stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
273 template <
typename T>
276 static_assert(std::is_base_of<StackFrame, T>::value,
277 "expected T derived from StackFrame");
280 for (
const std::unique_ptr<StackFrame> &frame : llvm::reverse(stack)) {
281 if (T *ptr = dyn_cast_or_null<T>(frame.get())) {
291 template <
typename T>
293 template <
typename... Args>
295 : moduleTranslation(m) {
296 moduleTranslation.
stackPush<T>(std::forward<Args>(args)...);
308 std::unique_ptr<llvm::Module> llvmModule);
313 bool recordInsertions =
false);
320 llvm::IRBuilderBase &builder,
321 bool recordInsertions);
325 llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr)
const;
339 convertParameterAttrs(LLVMFuncOp func,
int argIdx, DictionaryAttr paramAttrs);
343 std::unique_ptr<llvm::Module> llvmModule;
345 std::unique_ptr<detail::DebugTranslation> debugTranslation;
348 std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
351 std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
364 llvm::StringMap<llvm::Function *> functionMapping;
415 const ModuleTranslation &moduleTranslation);
421 ArrayRef<llvm::Type *> tys = {});
427 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
429 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
430 ArrayRef<unsigned> immArgPositions,
431 ArrayRef<StringLiteral> immArgAttrNames);
439 template <
typename T>
442 doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {
443 return frame.getTypeID() == ::mlir::TypeID::get<T>();
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
This class provides support for representing a failure result, or a valid value of type T.
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.
Concrete CRTP base class for ModuleTranslation stack frames.
Common CRTP base class for ModuleTranslation stack frames.
virtual ~StackFrame()=default
StackFrame(TypeID typeID)
Implementation class for module translation.
llvm::fp::ExceptionBehavior translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior)
Translates the given LLVM FP exception behavior metadata.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
void mapCall(Operation *mlir, llvm::CallInst *llvm)
Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.
llvm::DIGlobalVariableExpression * translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr)
Translates the given LLVM global variable expression metadata.
llvm::Value *& mapValue(Value value)
Provides write-once access to store the LLVM IR value corresponding to the given MLIR value.
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.
llvm::Instruction * lookupBranch(Operation *op) const
Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.
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.
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translates the given location.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
SymbolTableCollection & symbolTable()
void setBranchWeightsMetadata(BranchWeightOpInterface op)
Sets LLVM profiling metadata for operations that have branch weights.
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
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.
llvm::CallInst * lookupCall(Operation *op) const
Finds an LLVM call instruction that corresponds to the given MLIR call operation.
llvm::Metadata * translateDebugInfo(LLVM::DINodeAttr attr)
Translates the given LLVM debug info metadata.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
llvm::Module * getLLVMModule()
Returns the LLVM module in which the IR is being constructed.
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.
WalkResult stackWalk(llvm::function_ref< WalkResult(const T &)> callback) const
Calls callback for every ModuleTranslation stack frame of type T starting from the top of the stack.
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)
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
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.
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.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
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.
This class provides an efficient unique identifier for a specific C++ type.
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:
static WalkResult advance()
bool wasInterrupted() const
Returns true if the walk was interrupted.
Include the generated interface declarations.
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule")
Translate operation that satisfies LLVM dialect module requirements into an LLVM IR module living in ...
static bool doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame)
RAII object calling stackPush/stackPop on construction/destruction.
SaveStack(ModuleTranslation &m, Args &&...args)
This class represents an efficient way to signal success or failure.