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