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  /// Maps a blockaddress operation to its corresponding placeholder LLVM
140  /// value.
141  void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst) {
142  auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);
143  (void)result;
144  assert(result.second &&
145  "attempting to map a blockaddress operation that is already mapped");
146  }
147 
148  /// Maps a BlockAddressAttr to its corresponding LLVM basic block.
149  void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block) {
150  auto result = blockAddressToLLVMMapping.try_emplace(attr, block);
151  (void)result;
152  assert(result.second &&
153  "attempting to map a blockaddress attribute that is already mapped");
154  }
155 
156  /// Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
157  llvm::BasicBlock *lookupBlockAddress(BlockAddressAttr attr) const {
158  return blockAddressToLLVMMapping.lookup(attr);
159  }
160 
161  /// Removes the mapping for blocks contained in the region and values defined
162  /// in these blocks.
163  void forgetMapping(Region &region);
164 
165  /// Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope
166  /// attribute. Creates the metadata node if it has not been converted before.
167  llvm::MDNode *getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr);
168 
169  /// Returns the LLVM metadata corresponding to an array of mlir LLVM dialect
170  /// alias scope attributes. Creates the metadata nodes if they have not been
171  /// converted before.
172  llvm::MDNode *
174 
175  // Sets LLVM metadata for memory operations that are in a parallel loop.
176  void setAccessGroupsMetadata(AccessGroupOpInterface op,
177  llvm::Instruction *inst);
178 
179  // Sets LLVM metadata for memory operations that have alias scope information.
180  void setAliasScopeMetadata(AliasAnalysisOpInterface op,
181  llvm::Instruction *inst);
182 
183  /// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
184  void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
185 
186  /// Sets LLVM dereferenceable metadata for operations that have
187  /// dereferenceable attributes.
188  void setDereferenceableMetadata(DereferenceableOpInterface op,
189  llvm::Instruction *inst);
190 
191  /// Sets LLVM profiling metadata for operations that have branch weights.
192  void setBranchWeightsMetadata(WeightedBranchOpInterface op);
193 
194  /// Sets LLVM loop metadata for branch operations that have a loop annotation
195  /// attribute.
196  void setLoopMetadata(Operation *op, llvm::Instruction *inst);
197 
198  /// Sets the disjoint flag attribute for the exported instruction `value`
199  /// given the original operation `op`. Asserts if the operation does
200  /// not implement the disjoint flag interface, and asserts if the value
201  /// is an instruction that implements the disjoint flag.
202  void setDisjointFlag(Operation *op, llvm::Value *value);
203 
204  /// Converts the type from MLIR LLVM dialect to LLVM.
205  llvm::Type *convertType(Type type);
206 
207  /// Returns the MLIR context of the module being translated.
208  MLIRContext &getContext() { return *mlirModule->getContext(); }
209 
210  /// Returns the LLVM context in which the IR is being constructed.
211  llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }
212 
213  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
214  /// defining a global value.
215  llvm::GlobalValue *lookupGlobal(Operation *op) {
216  return globalsMapping.lookup(op);
217  }
218 
219  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
220  /// defining a global alias value.
221  llvm::GlobalValue *lookupAlias(Operation *op) {
222  return aliasesMapping.lookup(op);
223  }
224 
225  /// Returns the OpenMP IR builder associated with the LLVM IR module being
226  /// constructed.
227  llvm::OpenMPIRBuilder *getOpenMPBuilder();
228 
229  /// Returns the LLVM module in which the IR is being constructed.
230  llvm::Module *getLLVMModule() { return llvmModule.get(); }
231 
232  /// Translates the given location.
233  llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
234 
235  /// Translates the given LLVM DWARF expression metadata.
236  llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
237 
238  /// Translates the given LLVM global variable expression metadata.
239  llvm::DIGlobalVariableExpression *
240  translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
241 
242  /// Translates the given LLVM debug info metadata.
243  llvm::Metadata *translateDebugInfo(LLVM::DINodeAttr attr);
244 
245  /// Translates the given LLVM rounding mode metadata.
246  llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding);
247 
248  /// Translates the given LLVM FP exception behavior metadata.
249  llvm::fp::ExceptionBehavior
250  translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior);
251 
252  /// Translates the contents of the given block to LLVM IR using this
253  /// translator. The LLVM IR basic block corresponding to the given block is
254  /// expected to exist in the mapping of this translator. Uses `builder` to
255  /// translate the IR, leaving it at the end of the block. If `ignoreArguments`
256  /// is set, does not produce PHI nodes for the block arguments. Otherwise, the
257  /// PHI nodes are constructed for block arguments but are _not_ connected to
258  /// the predecessors that may not exist yet.
259  LogicalResult convertBlock(Block &bb, bool ignoreArguments,
260  llvm::IRBuilderBase &builder) {
261  return convertBlockImpl(bb, ignoreArguments, builder,
262  /*recordInsertions=*/false);
263  }
264 
265  /// Translates parameter attributes of a call and adds them to the returned
266  /// AttrBuilder. Returns failure if any of the translations failed.
267  FailureOr<llvm::AttrBuilder> convertParameterAttrs(mlir::Location loc,
268  DictionaryAttr paramAttrs);
269 
270  /// Gets the named metadata in the LLVM IR module being constructed, creating
271  /// it if it does not exist.
272  llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
273 
274  /// Common CRTP base class for ModuleTranslation stack frames.
275  class StackFrame {
276  public:
277  virtual ~StackFrame() = default;
278  TypeID getTypeID() const { return typeID; }
279 
280  protected:
281  explicit StackFrame(TypeID typeID) : typeID(typeID) {}
282 
283  private:
284  const TypeID typeID;
285  virtual void anchor();
286  };
287 
288  /// Concrete CRTP base class for ModuleTranslation stack frames. When
289  /// translating operations with regions, users of ModuleTranslation can store
290  /// state on ModuleTranslation stack before entering the region and inspect
291  /// it when converting operations nested within that region. Users are
292  /// expected to derive this class and put any relevant information into fields
293  /// of the derived class. The usual isa/dyn_cast functionality is available
294  /// for instances of derived classes.
295  template <typename Derived>
296  class StackFrameBase : public StackFrame {
297  public:
298  explicit StackFrameBase() : StackFrame(TypeID::get<Derived>()) {}
299  };
300 
301  /// Creates a stack frame of type `T` on ModuleTranslation stack. `T` must
302  /// be derived from `StackFrameBase<T>` and constructible from the provided
303  /// arguments. Doing this before entering the region of the op being
304  /// translated makes the frame available when translating ops within that
305  /// region.
306  template <typename T, typename... Args>
307  void stackPush(Args &&...args) {
308  static_assert(
309  std::is_base_of<StackFrame, T>::value,
310  "can only push instances of StackFrame on ModuleTranslation stack");
311  stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
312  }
313 
314  /// Pops the last element from the ModuleTranslation stack.
315  void stackPop() { stack.pop_back(); }
316 
317  /// Calls `callback` for every ModuleTranslation stack frame of type `T`
318  /// starting from the top of the stack.
319  template <typename T>
321  static_assert(std::is_base_of<StackFrame, T>::value,
322  "expected T derived from StackFrame");
323  if (!callback)
324  return WalkResult::skip();
325  for (std::unique_ptr<StackFrame> &frame : llvm::reverse(stack)) {
326  if (T *ptr = dyn_cast_or_null<T>(frame.get())) {
327  WalkResult result = callback(*ptr);
328  if (result.wasInterrupted())
329  return result;
330  }
331  }
332  return WalkResult::advance();
333  }
334 
335  /// RAII object calling stackPush/stackPop on construction/destruction.
336  template <typename T>
337  struct SaveStack {
338  template <typename... Args>
339  explicit SaveStack(ModuleTranslation &m, Args &&...args)
340  : moduleTranslation(m) {
341  moduleTranslation.stackPush<T>(std::forward<Args>(args)...);
342  }
343  ~SaveStack() { moduleTranslation.stackPop(); }
344 
345  private:
346  ModuleTranslation &moduleTranslation;
347  };
348 
349  SymbolTableCollection &symbolTable() { return symbolTableCollection; }
350 
351 private:
353  std::unique_ptr<llvm::Module> llvmModule);
355 
356  /// Converts individual components.
357  LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder,
358  bool recordInsertions = false);
359  LogicalResult convertFunctionSignatures();
360  LogicalResult convertFunctions();
361  LogicalResult convertComdats();
362 
363  LogicalResult convertUnresolvedBlockAddress();
364 
365  /// Handle conversion for both globals and global aliases.
366  ///
367  /// - Create named global variables that correspond to llvm.mlir.global
368  /// definitions, similarly Convert llvm.global_ctors and global_dtors ops.
369  /// - Create global alias that correspond to llvm.mlir.alias.
370  LogicalResult convertGlobalsAndAliases();
371  LogicalResult convertOneFunction(LLVMFuncOp func);
372  LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,
373  llvm::IRBuilderBase &builder,
374  bool recordInsertions);
375 
376  /// Returns the LLVM metadata corresponding to the given mlir LLVM dialect
377  /// TBAATagAttr.
378  llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;
379 
380  /// Process tbaa LLVM Metadata operations and create LLVM
381  /// metadata nodes for them.
382  LogicalResult createTBAAMetadata();
383 
384  /// Process the ident LLVM Metadata, if it exists.
385  LogicalResult createIdentMetadata();
386 
387  /// Process the llvm.commandline LLVM Metadata, if it exists.
388  LogicalResult createCommandlineMetadata();
389 
390  /// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
391  LogicalResult createDependentLibrariesMetadata();
392 
393  /// Translates dialect attributes attached to the given operation.
394  LogicalResult
395  convertDialectAttributes(Operation *op,
396  ArrayRef<llvm::Instruction *> instructions);
397 
398  /// Translates parameter attributes of a function and adds them to the
399  /// returned AttrBuilder. Returns failure if any of the translations failed.
400  FailureOr<llvm::AttrBuilder>
401  convertParameterAttrs(LLVMFuncOp func, int argIdx, DictionaryAttr paramAttrs);
402 
403  /// Original and translated module.
404  Operation *mlirModule;
405  std::unique_ptr<llvm::Module> llvmModule;
406  /// A converter for translating debug information.
407  std::unique_ptr<detail::DebugTranslation> debugTranslation;
408 
409  /// A converter for translating loop annotations.
410  std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
411 
412  /// Builder for LLVM IR generation of OpenMP constructs.
413  std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
414 
415  /// Mappings between llvm.mlir.global definitions and corresponding globals.
417 
418  /// Mappings between llvm.mlir.alias definitions and corresponding global
419  /// aliases.
421 
422  /// A stateful object used to translate types.
423  TypeToLLVMIRTranslator typeTranslator;
424 
425  /// A dialect interface collection used for dispatching the translation to
426  /// specific dialects.
428 
429  /// Mappings between original and translated values, used for lookups.
430  llvm::StringMap<llvm::Function *> functionMapping;
431  DenseMap<Value, llvm::Value *> valueMapping;
433 
434  /// A mapping between MLIR LLVM dialect terminators and LLVM IR terminators
435  /// they are converted to. This allows for connecting PHI nodes to the source
436  /// values after all operations are converted.
438 
439  /// A mapping between MLIR LLVM dialect call operations and LLVM IR call
440  /// instructions. This allows for adding branch weights after the operations
441  /// have been converted.
443 
444  /// Mapping from an alias scope attribute to its LLVM metadata.
445  /// This map is populated lazily.
446  DenseMap<AliasScopeAttr, llvm::MDNode *> aliasScopeMetadataMapping;
447 
448  /// Mapping from an alias scope domain attribute to its LLVM metadata.
449  /// This map is populated lazily.
450  DenseMap<AliasScopeDomainAttr, llvm::MDNode *> aliasDomainMetadataMapping;
451 
452  /// Mapping from a tbaa attribute to its LLVM metadata.
453  /// This map is populated on module entry.
454  DenseMap<Attribute, llvm::MDNode *> tbaaMetadataMapping;
455 
456  /// Mapping from a comdat selector operation to its LLVM comdat struct.
457  /// This map is populated on module entry.
459 
460  /// Mapping from llvm.blockaddress operations to their corresponding LLVM
461  /// constant placeholders. After all basic blocks are translated, this
462  /// mapping is used to replace the placeholders with the LLVM block addresses.
463  DenseMap<BlockAddressOp, llvm::Value *> unresolvedBlockAddressMapping;
464 
465  /// Mapping from a BlockAddressAttr attribute to it's matching LLVM basic
466  /// block.
467  DenseMap<BlockAddressAttr, llvm::BasicBlock *> blockAddressToLLVMMapping;
468 
469  /// Stack of user-specified state elements, useful when translating operations
470  /// with regions.
472 
473  /// A cache for the symbol tables constructed during symbols lookup.
474  SymbolTableCollection symbolTableCollection;
475 };
476 
477 namespace detail {
478 /// For all blocks in the region that were converted to LLVM IR using the given
479 /// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
480 /// to the results of preceding blocks.
481 void connectPHINodes(Region &region, const ModuleTranslation &state);
482 
483 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
484 /// This currently supports integer, floating point, splat and dense element
485 /// attributes and combinations thereof. Also, an array attribute with two
486 /// elements is supported to represent a complex constant. In case of error,
487 /// report it to `loc` and return nullptr.
488 llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
489  Location loc,
490  const ModuleTranslation &moduleTranslation);
491 
492 /// Creates a call to an LLVM IR intrinsic function with the given arguments.
493 llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
494  llvm::Intrinsic::ID intrinsic,
495  ArrayRef<llvm::Value *> args = {},
496  ArrayRef<llvm::Type *> tys = {});
497 
498 /// Creates a call to a LLVM IR intrinsic defined by LLVM_IntrOpBase. This
499 /// resolves the overloads, and maps mixed MLIR value and attribute arguments to
500 /// LLVM values.
501 llvm::CallInst *createIntrinsicCall(
502  llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
503  Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
504  ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
505  ArrayRef<unsigned> immArgPositions,
506  ArrayRef<StringLiteral> immArgAttrNames);
507 
508 } // namespace detail
509 
510 } // namespace LLVM
511 } // namespace mlir
512 
513 namespace llvm {
514 template <typename T>
516  static inline bool
517  doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {
518  return frame.getTypeID() == ::mlir::TypeID::get<T>();
519  }
520 };
521 } // namespace llvm
522 
523 #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.
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::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.
void setBranchWeightsMetadata(WeightedBranchOpInterface op)
Sets LLVM profiling metadata for operations that have branch weights.
SymbolTableCollection & symbolTable()
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::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
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 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.
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:76
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:387
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)