MLIR  22.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 
19 #include "mlir/IR/Operation.h"
20 #include "mlir/IR/SymbolTable.h"
21 #include "mlir/IR/Value.h"
26 
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/IR/FPEnv.h"
29 #include "llvm/IR/Module.h"
30 
31 namespace llvm {
32 class BasicBlock;
33 class CallBase;
34 class CanonicalLoopInfo;
35 class Function;
36 class IRBuilderBase;
37 class OpenMPIRBuilder;
38 class Value;
39 } // namespace llvm
40 
41 namespace mlir {
42 class Attribute;
43 class Block;
44 class Location;
45 
46 namespace LLVM {
47 
48 namespace detail {
49 class DebugTranslation;
50 class LoopAnnotationTranslation;
51 } // namespace detail
52 
53 class AliasScopeAttr;
54 class AliasScopeDomainAttr;
55 class DINodeAttr;
56 class LLVMFuncOp;
57 class ComdatSelectorOp;
58 
59 /// Implementation class for module translation. Holds a reference to the module
60 /// being translated, and the mappings between the original and the translated
61 /// functions, basic blocks and values. It is practically easier to hold these
62 /// mappings in one class since the conversion of control flow operations
63 /// needs to look up block and function mappings.
65  friend std::unique_ptr<llvm::Module>
66  mlir::translateModuleToLLVMIR(Operation *, llvm::LLVMContext &, StringRef,
67  bool);
68 
69 public:
70  /// Stores the mapping between a function name and its LLVM IR representation.
71  void mapFunction(StringRef name, llvm::Function *func) {
72  auto result = functionMapping.try_emplace(name, func);
73  (void)result;
74  assert(result.second &&
75  "attempting to map a function that is already mapped");
76  }
77 
78  /// Finds an LLVM IR function by its name.
79  llvm::Function *lookupFunction(StringRef name) const {
80  return functionMapping.lookup(name);
81  }
82 
83  /// Stores the mapping between an MLIR value and its LLVM IR counterpart.
84  void mapValue(Value mlir, llvm::Value *llvm) { mapValue(mlir) = llvm; }
85 
86  /// Provides write-once access to store the LLVM IR value corresponding to the
87  /// given MLIR value.
88  llvm::Value *&mapValue(Value value) {
89  llvm::Value *&llvm = valueMapping[value];
90  assert(llvm == nullptr &&
91  "attempting to map a value that is already mapped");
92  return llvm;
93  }
94 
95  /// Finds an LLVM IR value corresponding to the given MLIR value.
96  llvm::Value *lookupValue(Value value) const {
97  return valueMapping.lookup(value);
98  }
99 
100  /// Looks up remapped a list of remapped values.
102 
103  /// Stores the mapping between an MLIR block and LLVM IR basic block.
104  void mapBlock(Block *mlir, llvm::BasicBlock *llvm) {
105  auto result = blockMapping.try_emplace(mlir, llvm);
106  (void)result;
107  assert(result.second && "attempting to map a block that is already mapped");
108  }
109 
110  /// Finds an LLVM IR basic block that corresponds to the given MLIR block.
111  llvm::BasicBlock *lookupBlock(Block *block) const {
112  return blockMapping.lookup(block);
113  }
114 
115  /// Find the LLVM-IR loop that represents an MLIR loop.
116  llvm::CanonicalLoopInfo *lookupOMPLoop(omp::NewCliOp mlir) const {
117  llvm::CanonicalLoopInfo *result = loopMapping.lookup(mlir);
118  assert(result && "attempt to get non-existing loop");
119  return result;
120  }
121 
122  /// Find the LLVM-IR loop that represents an MLIR loop.
123  llvm::CanonicalLoopInfo *lookupOMPLoop(Value mlir) const {
124  return lookupOMPLoop(mlir.getDefiningOp<omp::NewCliOp>());
125  }
126 
127  /// Mark an OpenMP loop as having been consumed.
128  void invalidateOmpLoop(omp::NewCliOp mlir) { loopMapping.erase(mlir); }
129 
130  /// Mark an OpenMP loop as having been consumed.
132  invalidateOmpLoop(mlir.getDefiningOp<omp::NewCliOp>());
133  }
134 
135  /// Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR
136  /// OpenMPIRBuilder CanonicalLoopInfo
137  void mapOmpLoop(omp::NewCliOp mlir, llvm::CanonicalLoopInfo *llvm) {
138  assert(llvm && "argument must be non-null");
139  llvm::CanonicalLoopInfo *&cur = loopMapping[mlir];
140  assert(cur == nullptr && "attempting to map a loop that is already mapped");
141  cur = llvm;
142  }
143 
144  /// Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR
145  /// OpenMPIRBuilder CanonicalLoopInfo
146  void mapOmpLoop(Value mlir, llvm::CanonicalLoopInfo *llvm) {
147  mapOmpLoop(mlir.getDefiningOp<omp::NewCliOp>(), llvm);
148  }
149 
150  /// Stores the mapping between an MLIR operation with successors and a
151  /// corresponding LLVM IR instruction.
152  void mapBranch(Operation *mlir, llvm::Instruction *llvm) {
153  auto result = branchMapping.try_emplace(mlir, llvm);
154  (void)result;
155  assert(result.second &&
156  "attempting to map a branch that is already mapped");
157  }
158 
159  /// Finds an LLVM IR instruction that corresponds to the given MLIR operation
160  /// with successors.
161  llvm::Instruction *lookupBranch(Operation *op) const {
162  return branchMapping.lookup(op);
163  }
164 
165  /// Stores a mapping between an MLIR call operation and a corresponding LLVM
166  /// call instruction.
167  void mapCall(Operation *mlir, llvm::CallInst *llvm) {
168  auto result = callMapping.try_emplace(mlir, llvm);
169  (void)result;
170  assert(result.second && "attempting to map a call that is already mapped");
171  }
172 
173  /// Finds an LLVM call instruction that corresponds to the given MLIR call
174  /// operation.
175  llvm::CallInst *lookupCall(Operation *op) const {
176  return callMapping.lookup(op);
177  }
178 
179  /// Maps a blockaddress operation to its corresponding placeholder LLVM
180  /// value.
181  void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst) {
182  auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);
183  (void)result;
184  assert(result.second &&
185  "attempting to map a blockaddress operation that is already mapped");
186  }
187 
188  /// Maps a BlockAddressAttr to its corresponding LLVM basic block.
189  void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block) {
190  auto result = blockAddressToLLVMMapping.try_emplace(attr, block);
191  (void)result;
192  assert(result.second &&
193  "attempting to map a blockaddress attribute that is already mapped");
194  }
195 
196  /// Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
197  llvm::BasicBlock *lookupBlockAddress(BlockAddressAttr attr) const {
198  return blockAddressToLLVMMapping.lookup(attr);
199  }
200 
201  /// Removes the mapping for blocks contained in the region and values defined
202  /// in these blocks.
203  void forgetMapping(Region &region);
204 
205  /// Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope
206  /// attribute. Creates the metadata node if it has not been converted before.
207  llvm::MDNode *getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr);
208 
209  /// Returns the LLVM metadata corresponding to an array of mlir LLVM dialect
210  /// alias scope attributes. Creates the metadata nodes if they have not been
211  /// converted before.
212  llvm::MDNode *
214 
215  // Sets LLVM metadata for memory operations that are in a parallel loop.
216  void setAccessGroupsMetadata(AccessGroupOpInterface op,
217  llvm::Instruction *inst);
218 
219  // Sets LLVM metadata for memory operations that have alias scope information.
220  void setAliasScopeMetadata(AliasAnalysisOpInterface op,
221  llvm::Instruction *inst);
222 
223  /// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
224  void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
225 
226  /// Sets LLVM dereferenceable metadata for operations that have
227  /// dereferenceable attributes.
228  void setDereferenceableMetadata(DereferenceableOpInterface op,
229  llvm::Instruction *inst);
230 
231  /// Sets LLVM profiling metadata for operations that have branch weights.
232  void setBranchWeightsMetadata(WeightedBranchOpInterface op);
233 
234  /// Sets LLVM loop metadata for branch operations that have a loop annotation
235  /// attribute.
236  void setLoopMetadata(Operation *op, llvm::Instruction *inst);
237 
238  /// Sets the disjoint flag attribute for the exported instruction `value`
239  /// given the original operation `op`. Asserts if the operation does
240  /// not implement the disjoint flag interface, and asserts if the value
241  /// is an instruction that implements the disjoint flag.
242  void setDisjointFlag(Operation *op, llvm::Value *value);
243 
244  /// Converts the type from MLIR LLVM dialect to LLVM.
245  llvm::Type *convertType(Type type);
246 
247  /// Returns the MLIR context of the module being translated.
248  MLIRContext &getContext() { return *mlirModule->getContext(); }
249 
250  /// Returns the LLVM context in which the IR is being constructed.
251  llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }
252 
253  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
254  /// defining a global value.
255  llvm::GlobalValue *lookupGlobal(Operation *op) {
256  return globalsMapping.lookup(op);
257  }
258 
259  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
260  /// defining a global alias value.
261  llvm::GlobalValue *lookupAlias(Operation *op) {
262  return aliasesMapping.lookup(op);
263  }
264 
265  /// Finds an LLVM IR global value that corresponds to the given MLIR operation
266  /// defining an IFunc.
267  llvm::GlobalValue *lookupIFunc(Operation *op) {
268  return ifuncMapping.lookup(op);
269  }
270 
271  /// Returns the OpenMP IR builder associated with the LLVM IR module being
272  /// constructed.
273  llvm::OpenMPIRBuilder *getOpenMPBuilder();
274 
275  /// Returns the LLVM module in which the IR is being constructed.
276  llvm::Module *getLLVMModule() { return llvmModule.get(); }
277 
278  /// Translates the given location.
279  llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
280 
281  /// Translates the given LLVM DWARF expression metadata.
282  llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
283 
284  /// Translates the given LLVM global variable expression metadata.
285  llvm::DIGlobalVariableExpression *
286  translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
287 
288  /// Translates the given LLVM debug info metadata.
289  llvm::Metadata *translateDebugInfo(LLVM::DINodeAttr attr);
290 
291  /// Translates the given LLVM rounding mode metadata.
292  llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding);
293 
294  /// Translates the given LLVM FP exception behavior metadata.
295  llvm::fp::ExceptionBehavior
296  translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior);
297 
298  /// Translates the contents of the given block to LLVM IR using this
299  /// translator. The LLVM IR basic block corresponding to the given block is
300  /// expected to exist in the mapping of this translator. Uses `builder` to
301  /// translate the IR, leaving it at the end of the block. If `ignoreArguments`
302  /// is set, does not produce PHI nodes for the block arguments. Otherwise, the
303  /// PHI nodes are constructed for block arguments but are _not_ connected to
304  /// the predecessors that may not exist yet.
305  LogicalResult convertBlock(Block &bb, bool ignoreArguments,
306  llvm::IRBuilderBase &builder) {
307  return convertBlockImpl(bb, ignoreArguments, builder,
308  /*recordInsertions=*/false);
309  }
310 
311  /// Converts argument and result attributes from `attrsOp` to LLVM IR
312  /// attributes on the `call` instruction. Returns failure if conversion fails.
313  /// The `immArgPositions` parameter is only relevant for intrinsics. It
314  /// specifies the positions of immediate arguments, which do not have
315  /// associated argument attributes in MLIR and should be skipped during
316  /// attribute mapping.
317  LogicalResult
318  convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp,
319  llvm::CallBase *call,
320  ArrayRef<unsigned> immArgPositions = {});
321 
322  /// Gets the named metadata in the LLVM IR module being constructed, creating
323  /// it if it does not exist.
324  llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
325 
326  /// Creates a stack frame of type `T` on ModuleTranslation stack. `T` must
327  /// be derived from `StackFrameBase<T>` and constructible from the provided
328  /// arguments. Doing this before entering the region of the op being
329  /// translated makes the frame available when translating ops within that
330  /// region.
331  template <typename T, typename... Args>
332  void stackPush(Args &&...args) {
333  stack.stackPush<T>(std::forward<Args>(args)...);
334  }
335 
336  /// Pops the last element from the ModuleTranslation stack.
337  void stackPop() { stack.stackPop(); }
338 
339  /// Calls `callback` for every ModuleTranslation stack frame of type `T`
340  /// starting from the top of the stack.
341  template <typename T>
343  return stack.stackWalk(callback);
344  }
345 
346  /// RAII object calling stackPush/stackPop on construction/destruction.
347  template <typename T>
349 
350  SymbolTableCollection &symbolTable() { return symbolTableCollection; }
351 
352 private:
354  std::unique_ptr<llvm::Module> llvmModule);
356 
357  /// Converts individual components.
358  LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder,
359  bool recordInsertions = false);
360  LogicalResult convertFunctionSignatures();
361  LogicalResult convertFunctions();
362  LogicalResult convertIFuncs();
363  LogicalResult convertComdats();
364 
365  LogicalResult convertUnresolvedBlockAddress();
366 
367  /// Handle conversion for both globals and global aliases.
368  ///
369  /// - Create named global variables that correspond to llvm.mlir.global
370  /// definitions, similarly Convert llvm.global_ctors and global_dtors ops.
371  /// - Create global alias that correspond to llvm.mlir.alias.
372  LogicalResult convertGlobalsAndAliases();
373  LogicalResult convertOneFunction(LLVMFuncOp func);
374  LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,
375  llvm::IRBuilderBase &builder,
376  bool recordInsertions);
377 
378  /// Returns the LLVM metadata corresponding to the given mlir LLVM dialect
379  /// TBAATagAttr.
380  llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;
381 
382  /// Process tbaa LLVM Metadata operations and create LLVM
383  /// metadata nodes for them.
384  LogicalResult createTBAAMetadata();
385 
386  /// Process the ident LLVM Metadata, if it exists.
387  LogicalResult createIdentMetadata();
388 
389  /// Process the llvm.commandline LLVM Metadata, if it exists.
390  LogicalResult createCommandlineMetadata();
391 
392  /// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
393  LogicalResult createDependentLibrariesMetadata();
394 
395  /// Translates dialect attributes attached to the given operation.
396  LogicalResult
397  convertDialectAttributes(Operation *op,
398  ArrayRef<llvm::Instruction *> instructions);
399 
400  /// Translates parameter attributes of a call and adds them to the returned
401  /// AttrBuilder. Returns failure if any of the translations failed.
402  FailureOr<llvm::AttrBuilder> convertParameterAttrs(mlir::Location loc,
403  DictionaryAttr paramAttrs);
404 
405  /// Translates parameter attributes of a function and adds them to the
406  /// returned AttrBuilder. Returns failure if any of the translations failed.
407  FailureOr<llvm::AttrBuilder>
408  convertParameterAttrs(LLVMFuncOp func, int argIdx, DictionaryAttr paramAttrs);
409 
410  /// Original and translated module.
411  Operation *mlirModule;
412  std::unique_ptr<llvm::Module> llvmModule;
413  /// A converter for translating debug information.
414  std::unique_ptr<detail::DebugTranslation> debugTranslation;
415 
416  /// A converter for translating loop annotations.
417  std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
418 
419  /// Builder for LLVM IR generation of OpenMP constructs.
420  std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
421 
422  /// Mappings between llvm.mlir.global definitions and corresponding globals.
424 
425  /// Mappings between llvm.mlir.alias definitions and corresponding global
426  /// aliases.
428 
429  /// Mappings between llvm.mlir.ifunc definitions and corresponding global
430  /// ifuncs.
432 
433  /// A stateful object used to translate types.
434  TypeToLLVMIRTranslator typeTranslator;
435 
436  /// A dialect interface collection used for dispatching the translation to
437  /// specific dialects.
439 
440  /// Mappings between original and translated values, used for lookups.
441  llvm::StringMap<llvm::Function *> functionMapping;
442  DenseMap<Value, llvm::Value *> valueMapping;
444 
445  /// List of not yet consumed MLIR loop handles (represented by an omp.new_cli
446  /// operation which creates a value of type CanonicalLoopInfoType) and their
447  /// LLVM-IR representation as CanonicalLoopInfo which is managed by the
448  /// OpenMPIRBuilder.
450 
451  /// A mapping between MLIR LLVM dialect terminators and LLVM IR terminators
452  /// they are converted to. This allows for connecting PHI nodes to the source
453  /// values after all operations are converted.
455 
456  /// A mapping between MLIR LLVM dialect call operations and LLVM IR call
457  /// instructions. This allows for adding branch weights after the operations
458  /// have been converted.
460 
461  /// Mapping from an alias scope attribute to its LLVM metadata.
462  /// This map is populated lazily.
463  DenseMap<AliasScopeAttr, llvm::MDNode *> aliasScopeMetadataMapping;
464 
465  /// Mapping from an alias scope domain attribute to its LLVM metadata.
466  /// This map is populated lazily.
467  DenseMap<AliasScopeDomainAttr, llvm::MDNode *> aliasDomainMetadataMapping;
468 
469  /// Mapping from a tbaa attribute to its LLVM metadata.
470  /// This map is populated on module entry.
471  DenseMap<Attribute, llvm::MDNode *> tbaaMetadataMapping;
472 
473  /// Mapping from a comdat selector operation to its LLVM comdat struct.
474  /// This map is populated on module entry.
476 
477  /// Mapping from llvm.blockaddress operations to their corresponding LLVM
478  /// constant placeholders. After all basic blocks are translated, this
479  /// mapping is used to replace the placeholders with the LLVM block addresses.
480  DenseMap<BlockAddressOp, llvm::Value *> unresolvedBlockAddressMapping;
481 
482  /// Mapping from a BlockAddressAttr attribute to it's matching LLVM basic
483  /// block.
484  DenseMap<BlockAddressAttr, llvm::BasicBlock *> blockAddressToLLVMMapping;
485 
486  /// Stack of user-specified state elements, useful when translating operations
487  /// with regions.
488  StateStack stack;
489 
490  /// A cache for the symbol tables constructed during symbols lookup.
491  SymbolTableCollection symbolTableCollection;
492 };
493 
494 namespace detail {
495 /// For all blocks in the region that were converted to LLVM IR using the given
496 /// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
497 /// to the results of preceding blocks.
498 void connectPHINodes(Region &region, const ModuleTranslation &state);
499 
500 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
501 /// This currently supports integer, floating point, splat and dense element
502 /// attributes and combinations thereof. Also, an array attribute with two
503 /// elements is supported to represent a complex constant. In case of error,
504 /// report it to `loc` and return nullptr.
505 llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
506  Location loc,
507  const ModuleTranslation &moduleTranslation);
508 
509 /// Creates a call to an LLVM IR intrinsic function with the given arguments.
510 llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
511  llvm::Intrinsic::ID intrinsic,
512  ArrayRef<llvm::Value *> args = {},
513  ArrayRef<llvm::Type *> tys = {});
514 
515 /// Creates a call to a LLVM IR intrinsic defined by LLVM_IntrOpBase. This
516 /// resolves the overloads, and maps mixed MLIR value and attribute arguments to
517 /// LLVM values.
518 llvm::CallInst *createIntrinsicCall(
519  llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
520  Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
521  ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
522  ArrayRef<unsigned> immArgPositions,
523  ArrayRef<StringLiteral> immArgAttrNames);
524 
525 } // namespace detail
526 
527 } // namespace LLVM
528 } // namespace mlir
529 
530 #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:29
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.
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.
void mapOmpLoop(Value mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
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()
void invalidateOmpLoop(omp::NewCliOp mlir)
Mark an OpenMP loop as having been consumed.
LogicalResult convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp, llvm::CallBase *call, ArrayRef< unsigned > immArgPositions={})
Converts argument and result attributes from attrsOp to LLVM IR attributes on the call instruction.
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...
void invalidateOmpLoop(Value mlir)
Mark an OpenMP loop as having been consumed.
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.
void mapOmpLoop(omp::NewCliOp mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
llvm::GlobalValue * lookupIFunc(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining an IFunc.
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::CanonicalLoopInfo * lookupOMPLoop(omp::NewCliOp mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
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.
llvm::CanonicalLoopInfo * lookupOMPLoop(Value mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
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:63
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
WalkResult stackWalk(llvm::function_ref< WalkResult(T &)> callback)
Calls callback for every StateStack frame of type T starting from the top of the stack.
Definition: StateStack.h:72
void stackPop()
Pops the last element from the StateStack.
Definition: StateStack.h:67
void stackPush(Args &&...args)
Creates a stack frame of type T on StateStack.
Definition: StateStack.h:60
This class represents a collection of SymbolTables.
Definition: SymbolTable.h:283
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: WalkResult.h:29
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.
RAII object calling stackPush/stackPop on construction/destruction.
Definition: StateStack.h:106