14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
42 class DebugTranslation;
54 friend std::unique_ptr<llvm::Module>
60 auto result = functionMapping.try_emplace(name, func);
62 assert(result.second &&
63 "attempting to map a function that is already mapped");
68 return functionMapping.lookup(name);
77 llvm::Value *&
llvm = valueMapping[value];
78 assert(
llvm ==
nullptr &&
79 "attempting to map a value that is already mapped");
85 return valueMapping.lookup(value);
93 auto result = blockMapping.try_emplace(
mlir,
llvm);
95 assert(result.second &&
"attempting to map a block that is already mapped");
100 return blockMapping.lookup(block);
106 auto result = branchMapping.try_emplace(
mlir,
llvm);
108 assert(result.second &&
109 "attempting to map a branch that is already mapped");
115 return branchMapping.lookup(op);
125 SymbolRefAttr accessGroupRef)
const;
130 SymbolRefAttr aliasScopeRef)
const;
135 return loopOptionsMetadataMapping.lookup(
options);
139 auto result = loopOptionsMetadataMapping.try_emplace(
options, metadata);
141 assert(result.second &&
142 "attempting to map loop options that was already mapped");
167 return globalsMapping.lookup(op);
174 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
175 ompBuilder->initialize();
177 return ompBuilder.get();
194 llvm::IRBuilderBase &builder);
211 virtual void anchor();
221 template <
typename Derived>
232 template <
typename T,
typename... Args>
235 std::is_base_of<StackFrame, T>::value,
236 "can only push instances of StackFrame on ModuleTranslation stack");
237 stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
245 template <
typename T>
248 static_assert(std::is_base_of<StackFrame, T>::value,
249 "expected T derived from StackFrame");
252 for (
const std::unique_ptr<StackFrame> &frame : llvm::reverse(stack)) {
253 if (T *ptr = dyn_cast_or_null<T>(frame.get())) {
263 template <
typename T>
265 template <
typename... Args>
267 : moduleTranslation(m) {
268 moduleTranslation.
stackPush<T>(std::forward<Args>(args)...);
280 std::unique_ptr<llvm::Module> llvmModule);
300 llvm::MDNode *getTBAANode(
Operation &memOp, SymbolRefAttr tagRef)
const;
310 llvm::AttrBuilder convertParameterAttrs(DictionaryAttr paramAttrs);
314 std::unique_ptr<llvm::Module> llvmModule;
316 std::unique_ptr<detail::DebugTranslation> debugTranslation;
319 std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
332 llvm::StringMap<llvm::Function *> functionMapping;
383 const ModuleTranslation &moduleTranslation);
389 ArrayRef<llvm::Type *> tys = {});
396 template <
typename T>
399 doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {
400 return frame.getTypeID() == ::mlir::TypeID::get<T>();
static llvm::ManagedStatic< PassManagerOptions > options
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.
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.
LogicalResult convertBlock(Block &bb, bool ignoreArguments, llvm::IRBuilderBase &builder)
Translates the contents of the given block to LLVM IR using this translator.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
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.
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.
void setAccessGroupsMetadata(Operation *op, llvm::Instruction *inst)
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 setTBAAMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
SymbolTableCollection & symbolTable()
const llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translates the given location.
llvm::MDNode * getAccessGroup(Operation &opInst, SymbolRefAttr accessGroupRef) const
Returns the LLVM metadata corresponding to a reference to an mlir LLVM dialect access group operation...
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
void setAliasScopeMetadata(Operation *op, llvm::Instruction *inst)
void mapLoopOptionsMetadata(Attribute options, llvm::MDNode *metadata)
llvm::OpenMPIRBuilder * getOpenMPBuilder()
Returns the OpenMP IR builder associated with the LLVM IR module being constructed.
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::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
llvm::MDNode * lookupLoopOptionsMetadata(Attribute options) const
Returns the LLVM metadata corresponding to a llvm loop's codegen options attribute.
void mapBlock(Block *mlir, llvm::BasicBlock *llvm)
Stores the mapping between an MLIR block and LLVM IR basic block.
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.
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.
llvm::MDNode * getAliasScope(Operation &opInst, SymbolRefAttr aliasScopeRef) const
Returns the LLVM metadata corresponding to a reference to an mlir LLVM dialect alias scope operation.
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::Value * 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.
SetVector< Block * > getTopologicallySortedBlocks(Region ®ion)
Get a topologically sorted list of blocks of the given region.
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")
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.