MLIR  21.0.0git
ModuleTranslation.h
Go to the documentation of this file.
1 //===- ModuleTranslation.h - MLIR to LLVM conversion ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the translation between an MLIR LLVM dialect module and
10 // the corresponding LLVMIR module. It only handles core LLVM IR operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
16 
18 #include "mlir/IR/Operation.h"
19 #include "mlir/IR/SymbolTable.h"
20 #include "mlir/IR/Value.h"
24 
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/IR/FPEnv.h"
27 
28 namespace llvm {
29 class BasicBlock;
30 class Function;
31 class IRBuilderBase;
32 class OpenMPIRBuilder;
33 class Value;
34 } // namespace llvm
35 
36 namespace mlir {
37 class Attribute;
38 class Block;
39 class Location;
40 
41 namespace LLVM {
42 
43 namespace detail {
44 class DebugTranslation;
45 class LoopAnnotationTranslation;
46 } // namespace detail
47 
48 class AliasScopeAttr;
49 class AliasScopeDomainAttr;
50 class DINodeAttr;
51 class LLVMFuncOp;
52 class ComdatSelectorOp;
53 
54 /// Implementation class for module translation. Holds a reference to the module
55 /// being translated, and the mappings between the original and the translated
56 /// functions, basic blocks and values. It is practically easier to hold these
57 /// mappings in one class since the conversion of control flow operations
58 /// needs to look up block and function mappings.
60  friend std::unique_ptr<llvm::Module>
61  mlir::translateModuleToLLVMIR(Operation *, llvm::LLVMContext &, StringRef,
62  bool);
63 
64 public:
65  /// Stores the mapping between a function name and its LLVM IR representation.
66  void mapFunction(StringRef name, llvm::Function *func) {
67  auto result = functionMapping.try_emplace(name, func);
68  (void)result;
69  assert(result.second &&
70  "attempting to map a function that is already mapped");
71  }
72 
73  /// Finds an LLVM IR function by its name.
74  llvm::Function *lookupFunction(StringRef name) const {
75  return functionMapping.lookup(name);
76  }
77 
78  /// Stores the mapping between an MLIR value and its LLVM IR counterpart.
79  void mapValue(Value mlir, llvm::Value *llvm) { mapValue(mlir) = llvm; }
80 
81  /// Provides write-once access to store the LLVM IR value corresponding to the
82  /// given MLIR value.
83  llvm::Value *&mapValue(Value value) {
84  llvm::Value *&llvm = valueMapping[value];
85  assert(llvm == nullptr &&
86  "attempting to map a value that is already mapped");
87  return llvm;
88  }
89 
90  /// Finds an LLVM IR value corresponding to the given MLIR value.
91  llvm::Value *lookupValue(Value value) const {
92  return valueMapping.lookup(value);
93  }
94 
95  /// Looks up remapped a list of remapped values.
97 
98  /// Stores the mapping between an MLIR block and LLVM IR basic block.
99  void mapBlock(Block *mlir, llvm::BasicBlock *llvm) {
100  auto result = blockMapping.try_emplace(mlir, llvm);
101  (void)result;
102  assert(result.second && "attempting to map a block that is already mapped");
103  }
104 
105  /// Finds an LLVM IR basic block that corresponds to the given MLIR block.
106  llvm::BasicBlock *lookupBlock(Block *block) const {
107  return blockMapping.lookup(block);
108  }
109 
110  /// Stores the mapping between an MLIR operation with successors and a
111  /// corresponding LLVM IR instruction.
112  void mapBranch(Operation *mlir, llvm::Instruction *llvm) {
113  auto result = branchMapping.try_emplace(mlir, llvm);
114  (void)result;
115  assert(result.second &&
116  "attempting to map a branch that is already mapped");
117  }
118 
119  /// Finds an LLVM IR instruction that corresponds to the given MLIR operation
120  /// with successors.
121  llvm::Instruction *lookupBranch(Operation *op) const {
122  return branchMapping.lookup(op);
123  }
124 
125  /// Stores a mapping between an MLIR call operation and a corresponding LLVM
126  /// call instruction.
127  void mapCall(Operation *mlir, llvm::CallInst *llvm) {
128  auto result = callMapping.try_emplace(mlir, llvm);
129  (void)result;
130  assert(result.second && "attempting to map a call that is already mapped");
131  }
132 
133  /// Finds an LLVM call instruction that corresponds to the given MLIR call
134  /// operation.
135  llvm::CallInst *lookupCall(Operation *op) const {
136  return callMapping.lookup(op);
137  }
138 
139  /// Removes the mapping for blocks contained in the region and values defined
140  /// in these blocks.
141  void forgetMapping(Region &region);
142 
143  /// Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope
144  /// attribute. Creates the metadata node if it has not been converted before.
145  llvm::MDNode *getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr);
146 
147  /// Returns the LLVM metadata corresponding to an array of mlir LLVM dialect
148  /// alias scope attributes. Creates the metadata nodes if they have not been
149  /// converted before.
150  llvm::MDNode *
152 
153  // Sets LLVM metadata for memory operations that are in a parallel loop.
154  void setAccessGroupsMetadata(AccessGroupOpInterface op,
155  llvm::Instruction *inst);
156 
157  // Sets LLVM metadata for memory operations that have alias scope information.
158  void setAliasScopeMetadata(AliasAnalysisOpInterface op,
159  llvm::Instruction *inst);
160 
161  /// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
162  void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
163 
164  /// Sets LLVM dereferenceable metadata for operations that have
165  /// dereferenceable attributes.
166  void setDereferenceableMetadata(DereferenceableOpInterface op,
167  llvm::Instruction *inst);
168 
169  /// Sets LLVM profiling metadata for operations that have branch weights.
170  void setBranchWeightsMetadata(BranchWeightOpInterface op);
171 
172  /// Sets LLVM loop metadata for branch operations that have a loop annotation
173  /// attribute.
174  void setLoopMetadata(Operation *op, llvm::Instruction *inst);
175 
176  /// Sets the disjoint flag attribute for the exported instruction `value`
177  /// given the original operation `op`. Asserts if the operation does
178  /// not implement the disjoint flag interface, and asserts if the value
179  /// is an instruction that implements the disjoint flag.
180  void setDisjointFlag(Operation *op, llvm::Value *value);
181 
182  /// Converts the type from MLIR LLVM dialect to LLVM.
183  llvm::Type *convertType(Type type);
184 
185  /// Returns the MLIR context of the module being translated.
186  MLIRContext &getContext() { return *mlirModule->getContext(); }
187 
188  /// Returns the LLVM context in which the IR is being constructed.
189  llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }
190 
191  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
192  /// defining a global value.
193  llvm::GlobalValue *lookupGlobal(Operation *op) {
194  return globalsMapping.lookup(op);
195  }
196 
197  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
198  /// defining a global alias value.
199  llvm::GlobalValue *lookupAlias(Operation *op) {
200  return aliasesMapping.lookup(op);
201  }
202 
203  /// Returns the OpenMP IR builder associated with the LLVM IR module being
204  /// constructed.
205  llvm::OpenMPIRBuilder *getOpenMPBuilder();
206 
207  /// Returns the LLVM module in which the IR is being constructed.
208  llvm::Module *getLLVMModule() { return llvmModule.get(); }
209 
210  /// Translates the given location.
211  llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
212 
213  /// Translates the given LLVM DWARF expression metadata.
214  llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
215 
216  /// Translates the given LLVM global variable expression metadata.
217  llvm::DIGlobalVariableExpression *
218  translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
219 
220  /// Translates the given LLVM debug info metadata.
221  llvm::Metadata *translateDebugInfo(LLVM::DINodeAttr attr);
222 
223  /// Translates the given LLVM rounding mode metadata.
224  llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding);
225 
226  /// Translates the given LLVM FP exception behavior metadata.
227  llvm::fp::ExceptionBehavior
228  translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior);
229 
230  /// Translates the contents of the given block to LLVM IR using this
231  /// translator. The LLVM IR basic block corresponding to the given block is
232  /// expected to exist in the mapping of this translator. Uses `builder` to
233  /// translate the IR, leaving it at the end of the block. If `ignoreArguments`
234  /// is set, does not produce PHI nodes for the block arguments. Otherwise, the
235  /// PHI nodes are constructed for block arguments but are _not_ connected to
236  /// the predecessors that may not exist yet.
237  LogicalResult convertBlock(Block &bb, bool ignoreArguments,
238  llvm::IRBuilderBase &builder) {
239  return convertBlockImpl(bb, ignoreArguments, builder,
240  /*recordInsertions=*/false);
241  }
242 
243  /// Translates parameter attributes of a call and adds them to the returned
244  /// AttrBuilder. Returns failure if any of the translations failed.
245  FailureOr<llvm::AttrBuilder> convertParameterAttrs(mlir::Location loc,
246  DictionaryAttr paramAttrs);
247 
248  /// Gets the named metadata in the LLVM IR module being constructed, creating
249  /// it if it does not exist.
250  llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
251 
252  /// Common CRTP base class for ModuleTranslation stack frames.
253  class StackFrame {
254  public:
255  virtual ~StackFrame() = default;
256  TypeID getTypeID() const { return typeID; }
257 
258  protected:
259  explicit StackFrame(TypeID typeID) : typeID(typeID) {}
260 
261  private:
262  const TypeID typeID;
263  virtual void anchor();
264  };
265 
266  /// Concrete CRTP base class for ModuleTranslation stack frames. When
267  /// translating operations with regions, users of ModuleTranslation can store
268  /// state on ModuleTranslation stack before entering the region and inspect
269  /// it when converting operations nested within that region. Users are
270  /// expected to derive this class and put any relevant information into fields
271  /// of the derived class. The usual isa/dyn_cast functionality is available
272  /// for instances of derived classes.
273  template <typename Derived>
274  class StackFrameBase : public StackFrame {
275  public:
276  explicit StackFrameBase() : StackFrame(TypeID::get<Derived>()) {}
277  };
278 
279  /// Creates a stack frame of type `T` on ModuleTranslation stack. `T` must
280  /// be derived from `StackFrameBase<T>` and constructible from the provided
281  /// arguments. Doing this before entering the region of the op being
282  /// translated makes the frame available when translating ops within that
283  /// region.
284  template <typename T, typename... Args>
285  void stackPush(Args &&...args) {
286  static_assert(
287  std::is_base_of<StackFrame, T>::value,
288  "can only push instances of StackFrame on ModuleTranslation stack");
289  stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
290  }
291 
292  /// Pops the last element from the ModuleTranslation stack.
293  void stackPop() { stack.pop_back(); }
294 
295  /// Calls `callback` for every ModuleTranslation stack frame of type `T`
296  /// starting from the top of the stack.
297  template <typename T>
299  static_assert(std::is_base_of<StackFrame, T>::value,
300  "expected T derived from StackFrame");
301  if (!callback)
302  return WalkResult::skip();
303  for (std::unique_ptr<StackFrame> &frame : llvm::reverse(stack)) {
304  if (T *ptr = dyn_cast_or_null<T>(frame.get())) {
305  WalkResult result = callback(*ptr);
306  if (result.wasInterrupted())
307  return result;
308  }
309  }
310  return WalkResult::advance();
311  }
312 
313  /// RAII object calling stackPush/stackPop on construction/destruction.
314  template <typename T>
315  struct SaveStack {
316  template <typename... Args>
317  explicit SaveStack(ModuleTranslation &m, Args &&...args)
318  : moduleTranslation(m) {
319  moduleTranslation.stackPush<T>(std::forward<Args>(args)...);
320  }
321  ~SaveStack() { moduleTranslation.stackPop(); }
322 
323  private:
324  ModuleTranslation &moduleTranslation;
325  };
326 
327  SymbolTableCollection &symbolTable() { return symbolTableCollection; }
328 
329 private:
331  std::unique_ptr<llvm::Module> llvmModule);
333 
334  /// Converts individual components.
335  LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder,
336  bool recordInsertions = false);
337  LogicalResult convertFunctionSignatures();
338  LogicalResult convertFunctions();
339  LogicalResult convertComdats();
340 
341  /// Handle conversion for both globals and global aliases.
342  ///
343  /// - Create named global variables that correspond to llvm.mlir.global
344  /// definitions, similarly Convert llvm.global_ctors and global_dtors ops.
345  /// - Create global alias that correspond to llvm.mlir.alias.
346  LogicalResult convertGlobalsAndAliases();
347  LogicalResult convertOneFunction(LLVMFuncOp func);
348  LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,
349  llvm::IRBuilderBase &builder,
350  bool recordInsertions);
351 
352  /// Returns the LLVM metadata corresponding to the given mlir LLVM dialect
353  /// TBAATagAttr.
354  llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;
355 
356  /// Process tbaa LLVM Metadata operations and create LLVM
357  /// metadata nodes for them.
358  LogicalResult createTBAAMetadata();
359 
360  /// Process the ident LLVM Metadata, if it exists.
361  LogicalResult createIdentMetadata();
362 
363  /// Process the llvm.commandline LLVM Metadata, if it exists.
364  LogicalResult createCommandlineMetadata();
365 
366  /// Translates dialect attributes attached to the given operation.
367  LogicalResult
368  convertDialectAttributes(Operation *op,
369  ArrayRef<llvm::Instruction *> instructions);
370 
371  /// Translates parameter attributes of a function and adds them to the
372  /// returned AttrBuilder. Returns failure if any of the translations failed.
373  FailureOr<llvm::AttrBuilder>
374  convertParameterAttrs(LLVMFuncOp func, int argIdx, DictionaryAttr paramAttrs);
375 
376  /// Original and translated module.
377  Operation *mlirModule;
378  std::unique_ptr<llvm::Module> llvmModule;
379  /// A converter for translating debug information.
380  std::unique_ptr<detail::DebugTranslation> debugTranslation;
381 
382  /// A converter for translating loop annotations.
383  std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
384 
385  /// Builder for LLVM IR generation of OpenMP constructs.
386  std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
387 
388  /// Mappings between llvm.mlir.global definitions and corresponding globals.
390 
391  /// Mappings between llvm.mlir.alias definitions and corresponding global
392  /// aliases.
394 
395  /// A stateful object used to translate types.
396  TypeToLLVMIRTranslator typeTranslator;
397 
398  /// A dialect interface collection used for dispatching the translation to
399  /// specific dialects.
401 
402  /// Mappings between original and translated values, used for lookups.
403  llvm::StringMap<llvm::Function *> functionMapping;
404  DenseMap<Value, llvm::Value *> valueMapping;
406 
407  /// A mapping between MLIR LLVM dialect terminators and LLVM IR terminators
408  /// they are converted to. This allows for connecting PHI nodes to the source
409  /// values after all operations are converted.
411 
412  /// A mapping between MLIR LLVM dialect call operations and LLVM IR call
413  /// instructions. This allows for adding branch weights after the operations
414  /// have been converted.
416 
417  /// Mapping from an alias scope attribute to its LLVM metadata.
418  /// This map is populated lazily.
419  DenseMap<AliasScopeAttr, llvm::MDNode *> aliasScopeMetadataMapping;
420 
421  /// Mapping from an alias scope domain attribute to its LLVM metadata.
422  /// This map is populated lazily.
423  DenseMap<AliasScopeDomainAttr, llvm::MDNode *> aliasDomainMetadataMapping;
424 
425  /// Mapping from a tbaa attribute to its LLVM metadata.
426  /// This map is populated on module entry.
427  DenseMap<Attribute, llvm::MDNode *> tbaaMetadataMapping;
428 
429  /// Mapping from a comdat selector operation to its LLVM comdat struct.
430  /// This map is populated on module entry.
432 
433  /// Stack of user-specified state elements, useful when translating operations
434  /// with regions.
436 
437  /// A cache for the symbol tables constructed during symbols lookup.
438  SymbolTableCollection symbolTableCollection;
439 };
440 
441 namespace detail {
442 /// For all blocks in the region that were converted to LLVM IR using the given
443 /// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
444 /// to the results of preceding blocks.
445 void connectPHINodes(Region &region, const ModuleTranslation &state);
446 
447 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
448 /// This currently supports integer, floating point, splat and dense element
449 /// attributes and combinations thereof. Also, an array attribute with two
450 /// elements is supported to represent a complex constant. In case of error,
451 /// report it to `loc` and return nullptr.
452 llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
453  Location loc,
454  const ModuleTranslation &moduleTranslation);
455 
456 /// Creates a call to an LLVM IR intrinsic function with the given arguments.
457 llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
458  llvm::Intrinsic::ID intrinsic,
459  ArrayRef<llvm::Value *> args = {},
460  ArrayRef<llvm::Type *> tys = {});
461 
462 /// Creates a call to a LLVM IR intrinsic defined by LLVM_IntrOpBase. This
463 /// resolves the overloads, and maps mixed MLIR value and attribute arguments to
464 /// LLVM values.
465 llvm::CallInst *createIntrinsicCall(
466  llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
467  Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
468  ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
469  ArrayRef<unsigned> immArgPositions,
470  ArrayRef<StringLiteral> immArgAttrNames);
471 
472 } // namespace detail
473 
474 } // namespace LLVM
475 } // namespace mlir
476 
477 namespace llvm {
478 template <typename T>
480  static inline bool
481  doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {
482  return frame.getTypeID() == ::mlir::TypeID::get<T>();
483  }
484 };
485 } // namespace llvm
486 
487 #endif // MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Block represents an ordered list of Operations.
Definition: Block.h:33
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.
Definition: LLVMAttrs.h:27
Concrete CRTP base class for ModuleTranslation stack frames.
Common CRTP base class for ModuleTranslation stack frames.
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.
FailureOr< llvm::AttrBuilder > convertParameterAttrs(mlir::Location loc, DictionaryAttr paramAttrs)
Translates parameter attributes of a call and adds them to the returned AttrBuilder.
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.
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.
void setDereferenceableMetadata(DereferenceableOpInterface op, llvm::Instruction *inst)
Sets LLVM dereferenceable metadata for operations that have dereferenceable attributes.
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::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
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.
void setDisjointFlag(Operation *op, llvm::Value *value)
Sets the disjoint flag attribute for the exported instruction value given the original operation op.
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.
void stackPop()
Pops the last element from the ModuleTranslation stack.
void forgetMapping(Region &region)
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.
Definition: TypeToLLVM.h:39
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class represents a collection of SymbolTables.
Definition: SymbolTable.h:283
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:107
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:33
static WalkResult skip()
Definition: Visitors.h:52
static WalkResult advance()
Definition: Visitors.h:51
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: Visitors.h:55
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
void connectPHINodes(Region &region, 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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
static bool doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame)
RAII object calling stackPush/stackPop on construction/destruction.
SaveStack(ModuleTranslation &m, Args &&...args)