14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H 15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H 23 #include "llvm/ADT/SetVector.h" 24 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 41 class DebugTranslation;
52 friend std::unique_ptr<llvm::Module>
58 auto result = functionMapping.try_emplace(name, func);
60 assert(result.second &&
61 "attempting to map a function that is already mapped");
66 return functionMapping.lookup(name);
76 assert(llvm ==
nullptr &&
77 "attempting to map a value that is already mapped");
83 return valueMapping.lookup(value);
91 auto result = blockMapping.try_emplace(mlir, llvm);
93 assert(result.second &&
"attempting to map a block that is already mapped");
98 return blockMapping.lookup(block);
104 auto result = branchMapping.try_emplace(mlir, llvm);
106 assert(result.second &&
107 "attempting to map a branch that is already mapped");
113 return branchMapping.lookup(op);
118 void forgetMapping(
Region ®ion);
122 llvm::MDNode *getAccessGroup(
Operation &opInst,
123 SymbolRefAttr accessGroupRef)
const;
127 llvm::MDNode *getAliasScope(
Operation &opInst,
128 SymbolRefAttr aliasScopeRef)
const;
133 return loopOptionsMetadataMapping.lookup(options);
137 auto result = loopOptionsMetadataMapping.try_emplace(options, metadata);
139 assert(result.second &&
140 "attempting to map loop options that was already mapped");
144 void setAccessGroupsMetadata(
Operation *op, llvm::Instruction *inst);
147 void setAliasScopeMetadata(
Operation *op, llvm::Instruction *inst);
150 llvm::Type *convertType(
Type type);
161 return globalsMapping.lookup(op);
168 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
169 ompBuilder->initialize();
171 return ompBuilder.get();
175 const llvm::DILocation *translateLoc(
Location loc, llvm::DILocalScope *scope);
185 llvm::IRBuilderBase &builder);
189 llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
202 virtual void anchor();
212 template <
typename Derived>
223 template <
typename T,
typename... Args>
227 "can only push instances of StackFrame on ModuleTranslation stack");
228 stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
236 template <
typename T>
240 "expected T derived from StackFrame");
242 return WalkResult::skip();
243 for (
const std::unique_ptr<StackFrame> &frame : llvm::reverse(stack)) {
244 if (T *ptr = dyn_cast_or_null<T>(frame.get())) {
250 return WalkResult::advance();
254 template <
typename T>
256 template <
typename... Args>
258 : moduleTranslation(m) {
259 moduleTranslation.stackPush<T>(std::forward<Args>(args)...);
269 std::unique_ptr<llvm::Module> llvmModule);
292 std::unique_ptr<llvm::Module> llvmModule;
294 std::unique_ptr<detail::DebugTranslation> debugTranslation;
297 std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
310 llvm::StringMap<llvm::Function *> functionMapping;
358 llvm::Intrinsic::ID intrinsic,
367 template <
typename T>
370 doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {
371 return frame.getTypeID() == ::mlir::TypeID::get<T>();
376 #endif // MLIR_TARGET_LLVMIR_MODULETRANSLATION_H Include the generated interface declarations.
This class contains a list of basic blocks and a link to the parent operation it is attached to...
Explicitly register a set of "builtin" types.
Operation is a basic unit of execution within MLIR.
Block represents an ordered list of Operations.
Utility class to translate MLIR LLVM dialect types to LLVM IR.
bool wasInterrupted() const
Returns true if the walk was interrupted.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
Concrete CRTP base class for ModuleTranslation stack frames.
void mapLoopOptionsMetadata(Attribute options, llvm::MDNode *metadata)
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
static constexpr const bool value
This class provides an efficient unique identifier for a specific C++ type.
void mapBlock(Block *mlir, llvm::BasicBlock *llvm)
Stores the mapping between an MLIR block and LLVM IR basic block.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
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.
Implementation class for module translation.
llvm::OpenMPIRBuilder * getOpenMPBuilder()
Returns the OpenMP IR builder associated with the LLVM IR module being constructed.
This class represents an efficient way to signal success or failure.
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
llvm::Instruction * lookupBranch(Operation *op) const
Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
Attributes are known-constant values of operations.
static bool doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame)
RAII object calling stackPush/stackPop on construction/destruction.
void stackPush(Args &&...args)
Creates a stack frame of type T on ModuleTranslation stack.
A utility result that is used to signal how to proceed with an ongoing walk:
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
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 connectPHINodes(Region ®ion, const ModuleTranslation &state)
For all blocks in the region that were converted to LLVM IR using the given ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks to the results of preceding blocks.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
static llvm::ManagedStatic< PassManagerOptions > options
SaveStack(ModuleTranslation &m, Args &&...args)
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
Common CRTP base class for ModuleTranslation stack frames.
void mapBranch(Operation *mlir, llvm::Instruction *llvm)
Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction...
SetVector< Block * > getTopologicallySortedBlocks(Region ®ion)
Get a topologically sorted list of blocks of the given region.
void stackPop()
Pops the last element from the ModuleTranslation stack.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value...
MLIRContext is the top-level object for a collection of MLIR operations.
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 ...
void mapFunction(StringRef name, llvm::Function *func)
Stores the mapping between a function name and its LLVM IR representation.
Interface collection for translation to LLVM IR, dispatches to a concrete interface implementation ba...
llvm::Value *& mapValue(Value value)
Provides write-once access to store the LLVM IR value corresponding to the given MLIR value...
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.
StackFrame(TypeID typeID)
llvm::MDNode * lookupLoopOptionsMetadata(Attribute options) const
Returns the LLVM metadata corresponding to a llvm loop's codegen options attribute.
This class provides an abstraction over the different types of ranges over Values.