MLIR  22.0.0git
ModuleImport.h
Go to the documentation of this file.
1 //===- ModuleImport.h - LLVM to MLIR 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 import of an LLVM IR module into an LLVM dialect
10 // module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TARGET_LLVMIR_MODULEIMPORT_H
15 #define MLIR_TARGET_LLVMIR_MODULEIMPORT_H
16 
18 #include "mlir/IR/BuiltinOps.h"
22 #include "llvm/IR/Module.h"
23 
24 namespace llvm {
25 class BasicBlock;
26 class CallBase;
27 class DbgVariableIntrinsic;
28 class Function;
29 class Instruction;
30 class Value;
31 } // namespace llvm
32 
33 namespace mlir {
34 namespace LLVM {
35 
36 namespace detail {
37 class DataLayoutImporter;
38 class DebugImporter;
39 class LoopAnnotationImporter;
40 } // namespace detail
41 
42 /// Module import implementation class that provides methods to import globals
43 /// and functions from an LLVM module into an MLIR module. It holds mappings
44 /// between the original and translated globals, basic blocks, and values used
45 /// during the translation. Additionally, it keeps track of the current constant
46 /// insertion point since LLVM immediate values translate to MLIR operations
47 /// that are introduced at the beginning of the region.
48 class ModuleImport {
49 public:
50  ModuleImport(ModuleOp mlirModule, std::unique_ptr<llvm::Module> llvmModule,
51  bool emitExpensiveWarnings, bool importEmptyDICompositeTypes,
52  bool preferUnregisteredIntrinsics, bool importStructsAsLiterals);
53 
54  /// Calls the LLVMImportInterface initialization that queries the registered
55  /// dialect interfaces for the supported LLVM IR intrinsics and metadata kinds
56  /// and builds the dispatch tables. Returns failure if multiple dialect
57  /// interfaces translate the same LLVM IR intrinsic.
58  LogicalResult initializeImportInterface() {
59  return iface.initializeImport(llvmModule->getContext());
60  }
61 
62  /// Converts all functions of the LLVM module to MLIR functions.
63  LogicalResult convertFunctions();
64 
65  /// Converts all comdat selectors of the LLVM module to MLIR comdat
66  /// operations.
67  LogicalResult convertComdats();
68 
69  /// Converts all global variables of the LLVM module to MLIR global variables.
70  LogicalResult convertGlobals();
71 
72  /// Converts all aliases of the LLVM module to MLIR variables.
73  LogicalResult convertAliases();
74 
75  /// Converts all ifuncs of the LLVM module to MLIR variables.
76  LogicalResult convertIFuncs();
77 
78  /// Converts the data layout of the LLVM module to an MLIR data layout
79  /// specification.
80  LogicalResult convertDataLayout();
81 
82  /// Converts target triple of the LLVM module to an MLIR target triple
83  /// specification.
84  void convertTargetTriple();
85 
86  /// Converts the module level asm of the LLVM module to an MLIR module
87  /// level asm specification.
88  void convertModuleLevelAsm();
89 
90  /// Stores the mapping between an LLVM value and its MLIR counterpart.
91  void mapValue(llvm::Value *llvm, Value mlir) { mapValue(llvm) = mlir; }
92 
93  /// Provides write-once access to store the MLIR value corresponding to the
94  /// given LLVM value.
95  Value &mapValue(llvm::Value *value) {
96  Value &mlir = valueMapping[value];
97  assert(mlir == nullptr &&
98  "attempting to map a value that is already mapped");
99  return mlir;
100  }
101 
102  /// Returns the MLIR value mapped to the given LLVM value.
103  Value lookupValue(llvm::Value *value) { return valueMapping.lookup(value); }
104 
105  /// Stores a mapping between an LLVM instruction and the imported MLIR
106  /// operation if the operation returns no result. Asserts if the operation
107  /// returns a result and should be added to valueMapping instead.
108  void mapNoResultOp(llvm::Instruction *llvm, Operation *mlir) {
110  }
111 
112  /// Provides write-once access to store the MLIR operation corresponding to
113  /// the given LLVM instruction if the operation returns no result. Asserts if
114  /// the operation returns a result and should be added to valueMapping
115  /// instead.
116  Operation *&mapNoResultOp(llvm::Instruction *inst) {
117  Operation *&mlir = noResultOpMapping[inst];
118  assert(inst->getType()->isVoidTy() &&
119  "attempting to map an operation that returns a result");
120  assert(mlir == nullptr &&
121  "attempting to map an operation that is already mapped");
122  return mlir;
123  }
124 
125  /// Returns the MLIR operation mapped to the given LLVM instruction. Queries
126  /// valueMapping and noResultOpMapping to support operations with and without
127  /// result.
128  Operation *lookupOperation(llvm::Instruction *inst) {
129  if (Value value = lookupValue(inst))
130  return value.getDefiningOp();
131  return noResultOpMapping.lookup(inst);
132  }
133 
134  /// Stores the mapping between an LLVM block and its MLIR counterpart.
135  void mapBlock(llvm::BasicBlock *llvm, Block *mlir) {
136  auto result = blockMapping.try_emplace(llvm, mlir);
137  (void)result;
138  assert(result.second && "attempting to map a block that is already mapped");
139  }
140 
141  /// Returns the MLIR block mapped to the given LLVM block.
142  Block *lookupBlock(llvm::BasicBlock *block) const {
143  return blockMapping.lookup(block);
144  }
145 
146  /// Converts an LLVM value to an MLIR value, or returns failure if the
147  /// conversion fails. Uses the `convertConstant` method to translate constant
148  /// LLVM values.
149  FailureOr<Value> convertValue(llvm::Value *value);
150 
151  /// Converts an LLVM metadata value to an MLIR value, or returns failure if
152  /// the conversion fails. Uses the `convertConstant` method to translate
153  /// constant LLVM values.
154  FailureOr<Value> convertMetadataValue(llvm::Value *value);
155 
156  /// Converts a range of LLVM values to a range of MLIR values using the
157  /// `convertValue` method, or returns failure if the conversion fails.
158  FailureOr<SmallVector<Value>> convertValues(ArrayRef<llvm::Value *> values);
159 
160  /// Converts `value` to an integer attribute. Asserts if the matching fails.
161  IntegerAttr matchIntegerAttr(llvm::Value *value);
162 
163  /// Converts `value` to a float attribute. Asserts if the matching fails.
164  FloatAttr matchFloatAttr(llvm::Value *value);
165 
166  /// Converts `value` to a local variable attribute. Asserts if the matching
167  /// fails.
168  DILocalVariableAttr matchLocalVariableAttr(llvm::Value *value);
169 
170  /// Converts `value` to a label attribute. Asserts if the matching fails.
171  DILabelAttr matchLabelAttr(llvm::Value *value);
172 
173  /// Converts `value` to a FP exception behavior attribute. Asserts if the
174  /// matching fails.
175  FPExceptionBehaviorAttr matchFPExceptionBehaviorAttr(llvm::Value *value);
176 
177  /// Converts `value` to a rounding mode attribute. Asserts if the matching
178  /// fails.
179  RoundingModeAttr matchRoundingModeAttr(llvm::Value *value);
180 
181  /// Converts `value` to an array of alias scopes or returns failure if the
182  /// conversion fails.
183  FailureOr<SmallVector<AliasScopeAttr>>
184  matchAliasScopeAttrs(llvm::Value *value);
185 
186  /// Translates the debug location.
187  Location translateLoc(llvm::DILocation *loc);
188 
189  /// Converts the type from LLVM to MLIR LLVM dialect.
190  Type convertType(llvm::Type *type) {
191  return typeTranslator.translateType(type);
192  }
193 
194  /// Imports `func` into the current module.
195  LogicalResult processFunction(llvm::Function *func);
196 
197  /// Converts function attributes of LLVM Function `func` into LLVM dialect
198  /// attributes of LLVMFuncOp `funcOp`.
199  void processFunctionAttributes(llvm::Function *func, LLVMFuncOp funcOp);
200 
201  /// Sets the integer overflow flags (nsw/nuw) attribute for the imported
202  /// operation `op` given the original instruction `inst`. Asserts if the
203  /// operation does not implement the integer overflow flag interface.
204  void setIntegerOverflowFlags(llvm::Instruction *inst, Operation *op) const;
205 
206  /// Sets the exact flag attribute for the imported operation `op` given
207  /// the original instruction `inst`. Asserts if the operation does not
208  /// implement the exact flag interface.
209  void setExactFlag(llvm::Instruction *inst, Operation *op) const;
210 
211  /// Sets the disjoint flag attribute for the imported operation `op`
212  /// given the original instruction `inst`. Asserts if the operation does
213  /// not implement the disjoint flag interface.
214  void setDisjointFlag(llvm::Instruction *inst, Operation *op) const;
215 
216  /// Sets the nneg flag attribute for the imported operation `op` given
217  /// the original instruction `inst`. Asserts if the operation does not
218  /// implement the nneg flag interface.
219  void setNonNegFlag(llvm::Instruction *inst, Operation *op) const;
220 
221  /// Sets the fastmath flags attribute for the imported operation `op` given
222  /// the original instruction `inst`. Asserts if the operation does not
223  /// implement the fastmath interface.
224  void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const;
225 
226  /// Converts !llvm.linker.options metadata to the llvm.linker.options
227  /// LLVM dialect operation.
228  LogicalResult convertLinkerOptionsMetadata();
229 
230  /// Converts !llvm.module.flags metadata.
231  LogicalResult convertModuleFlagsMetadata();
232 
233  /// Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
234  LogicalResult convertIdentMetadata();
235 
236  /// Converts !llvm.commandline metadata to the llvm.commandline LLVM ModuleOp
237  /// attribute.
238  LogicalResult convertCommandlineMetadata();
239 
240  /// Converts !llvm.dependent-libraries metadata to llvm.dependent_libraries
241  /// LLVM ModuleOp attribute.
242  LogicalResult convertDependentLibrariesMetadata();
243 
244  /// Converts all LLVM metadata nodes that translate to attributes such as
245  /// alias analysis or access group metadata, and builds a map from the
246  /// metadata nodes to the converted attributes.
247  /// Returns success if all conversions succeed and failure otherwise.
248  LogicalResult convertMetadata();
249 
250  /// Returns the MLIR attribute mapped to the given LLVM TBAA
251  /// metadata `node`.
252  Attribute lookupTBAAAttr(const llvm::MDNode *node) const {
253  return tbaaMapping.lookup(node);
254  }
255 
256  /// Returns the access group attributes that map to the access group nodes
257  /// starting from the access group metadata `node`. Returns failure, if any of
258  /// the attributes cannot be found.
259  FailureOr<SmallVector<AccessGroupAttr>>
260  lookupAccessGroupAttrs(const llvm::MDNode *node) const;
261 
262  /// Returns the loop annotation attribute that corresponds to the given LLVM
263  /// loop metadata `node`.
264  LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node,
265  Location loc) const;
266 
267  /// Returns the dereferenceable attribute that corresponds to the given LLVM
268  /// dereferenceable or dereferenceable_or_null metadata `node`. `kindID`
269  /// specifies the kind of the metadata node (dereferenceable or
270  /// dereferenceable_or_null).
271  FailureOr<DereferenceableAttr>
272  translateDereferenceableAttr(const llvm::MDNode *node, unsigned kindID);
273 
274  /// Returns the alias scope attributes that map to the alias scope nodes
275  /// starting from the metadata `node`. Returns failure, if any of the
276  /// attributes cannot be found.
277  FailureOr<SmallVector<AliasScopeAttr>>
278  lookupAliasScopeAttrs(const llvm::MDNode *node) const;
279 
280  /// Adds a debug intrinsics to the list of intrinsics that should be converted
281  /// after the function conversion has finished.
282  void addDebugIntrinsic(llvm::CallInst *intrinsic);
283 
284  /// Converts the LLVM values for an intrinsic to mixed MLIR values and
285  /// attributes for LLVM_IntrOpBase. Attributes correspond to LLVM immargs. The
286  /// list `immArgPositions` contains the positions of immargs on the LLVM
287  /// intrinsic, and `immArgAttrNames` list (of the same length) contains the
288  /// corresponding MLIR attribute names.
289  LogicalResult
292  bool requiresOpBundles,
293  ArrayRef<unsigned> immArgPositions,
294  ArrayRef<StringLiteral> immArgAttrNames,
295  SmallVectorImpl<Value> &valuesOut,
297 
298  /// Converts the argument and result attributes attached to `call` and adds
299  /// them to `attrsOp`. For intrinsic calls, filters out attributes
300  /// corresponding to immediate arguments specified by `immArgPositions`.
301  void convertArgAndResultAttrs(llvm::CallBase *call,
302  ArgAndResultAttrsOpInterface attrsOp,
303  ArrayRef<unsigned> immArgPositions = {});
304 
305  /// Whether the importer should try to convert all intrinsics to
306  /// llvm.call_intrinsic instead of dialect supported operations.
308  return preferUnregisteredIntrinsics;
309  }
310 
311 private:
312  /// Clears the accumulated state before processing a new region.
313  void clearRegionState() {
314  valueMapping.clear();
315  noResultOpMapping.clear();
316  blockMapping.clear();
317  debugIntrinsics.clear();
318  }
319  /// Sets the constant insertion point to the start of the given block.
320  void setConstantInsertionPointToStart(Block *block) {
321  constantInsertionBlock = block;
322  constantInsertionOp = nullptr;
323  }
324 
325  /// Converts an LLVM global variable into an MLIR LLVM dialect global
326  /// operation if a conversion exists. Otherwise, returns failure.
327  LogicalResult convertGlobal(llvm::GlobalVariable *globalVar);
328  /// Imports the magic globals "global_ctors" and "global_dtors".
329  LogicalResult convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar);
330  /// Converts an LLVM global alias variable into an MLIR LLVM dialect alias
331  /// operation if a conversion exists. Otherwise, returns failure.
332  LogicalResult convertAlias(llvm::GlobalAlias *alias);
333  // Converts an LLVM global ifunc into an MLIR LLVM dialect ifunc operation.
334  LogicalResult convertIFunc(llvm::GlobalIFunc *ifunc);
335  /// Returns personality of `func` as a FlatSymbolRefAttr.
336  FlatSymbolRefAttr getPersonalityAsAttr(llvm::Function *func);
337  /// Imports `bb` into `block`, which must be initially empty.
338  LogicalResult processBasicBlock(llvm::BasicBlock *bb, Block *block);
339  /// Converts all debug intrinsics in `debugIntrinsics`. Assumes that the
340  /// function containing the intrinsics has been fully converted to MLIR.
341  LogicalResult processDebugIntrinsics();
342  /// Converts a single debug intrinsic.
343  LogicalResult processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
344  DominanceInfo &domInfo);
345  /// Converts LLMV IR asm inline call operand's attributes into an array of
346  /// MLIR attributes to be utilized in `llvm.inline_asm`.
347  ArrayAttr convertAsmInlineOperandAttrs(const llvm::CallBase &llvmCall);
348  /// Converts an LLVM intrinsic to an MLIR LLVM dialect operation if an MLIR
349  /// counterpart exists. Otherwise, returns failure.
350  LogicalResult convertIntrinsic(llvm::CallInst *inst);
351  /// Converts an LLVM instruction to an MLIR LLVM dialect operation if an MLIR
352  /// counterpart exists. Otherwise, returns failure.
353  LogicalResult convertInstruction(llvm::Instruction *inst);
354  /// Converts the metadata attached to the original instruction `inst` if
355  /// a dialect interfaces supports the specific kind of metadata and attaches
356  /// the resulting dialect attributes to the converted operation `op`. Emits a
357  /// warning if the conversion of a supported metadata kind fails.
358  void setNonDebugMetadataAttrs(llvm::Instruction *inst, Operation *op);
359  /// Imports `inst` and populates valueMapping[inst] with the result of the
360  /// imported operation or noResultOpMapping[inst] with the imported operation
361  /// if it has no result.
362  LogicalResult processInstruction(llvm::Instruction *inst);
363  /// Converts the `branch` arguments in the order of the phi's found in
364  /// `target` and appends them to the `blockArguments` to attach to the
365  /// generated branch operation. The `blockArguments` thus have the same order
366  /// as the phi's in `target`.
367  LogicalResult convertBranchArgs(llvm::Instruction *branch,
368  llvm::BasicBlock *target,
369  SmallVectorImpl<Value> &blockArguments);
370  /// Convert `callInst` operands. For indirect calls, the method additionally
371  /// inserts the called function at the beginning of the returned `operands`
372  /// array. If `allowInlineAsm` is set to false (the default), it will return
373  /// failure if the called operand is an inline asm which isn't convertible to
374  /// MLIR as a value.
375  FailureOr<SmallVector<Value>>
376  convertCallOperands(llvm::CallBase *callInst, bool allowInlineAsm = false);
377  /// Converts the callee's function type. For direct calls, it converts the
378  /// actual function type, which may differ from the called operand type in
379  /// variadic functions. For indirect calls, it converts the function type
380  /// associated with the call instruction. When the call and the callee are not
381  /// compatible (or when nested type conversions failed), emit a warning and
382  /// update `isIncompatibleCall` to indicate it.
383  FailureOr<LLVMFunctionType> convertFunctionType(llvm::CallBase *callInst,
384  bool &isIncompatibleCall);
385  /// Returns the callee name, or an empty symbol if the call is not direct.
386  FlatSymbolRefAttr convertCalleeName(llvm::CallBase *callInst);
387  /// Converts the argument and result attributes attached to `func` and adds
388  /// them to the `funcOp`.
389  void convertArgAndResultAttrs(llvm::Function *func, LLVMFuncOp funcOp);
390  /// Converts the argument or result attributes in `llvmAttrSet` to a
391  /// corresponding MLIR LLVM dialect attribute dictionary.
392  DictionaryAttr convertArgOrResultAttrSet(llvm::AttributeSet llvmAttrSet);
393  /// Converts the attributes attached to `inst` and adds them to the `op`.
394  LogicalResult convertCallAttributes(llvm::CallInst *inst, CallOp op);
395  /// Converts the attributes attached to `inst` and adds them to the `op`.
396  LogicalResult convertInvokeAttributes(llvm::InvokeInst *inst, InvokeOp op);
397  /// Returns the builtin type equivalent to the given LLVM dialect type or
398  /// nullptr if there is no equivalent. The returned type can be used to create
399  /// an attribute for a GlobalOp or a ConstantOp.
400  Type getBuiltinTypeForAttr(Type type);
401  /// Returns `constant` as an attribute to attach to a GlobalOp or ConstantOp
402  /// or nullptr if the constant is not convertible. It supports scalar integer
403  /// and float constants as well as shaped types thereof including strings.
404  Attribute getConstantAsAttr(llvm::Constant *constant);
405  /// Returns the topologically sorted set of transitive dependencies needed to
406  /// convert the given constant.
407  SetVector<llvm::Constant *> getConstantsToConvert(llvm::Constant *constant);
408  /// Converts an LLVM constant to an MLIR value, or returns failure if the
409  /// conversion fails. The MLIR value may be produced by a ConstantOp,
410  /// AddressOfOp, NullOp, or a side-effect free operation (for ConstantExprs or
411  /// ConstantGEPs).
412  FailureOr<Value> convertConstant(llvm::Constant *constant);
413  /// Converts an LLVM constant and its transitive constant dependencies to MLIR
414  /// operations by converting them in topological order using the
415  /// `convertConstant` method, or returns failure if the conversion of any of
416  /// them fails. All operations are inserted at the start of the current
417  /// function entry block.
418  FailureOr<Value> convertConstantExpr(llvm::Constant *constant);
419  /// Returns a global comdat operation that serves as a container for LLVM
420  /// comdat selectors. Creates the global comdat operation on the first
421  /// invocation.
422  ComdatOp getGlobalComdatOp();
423  /// Performs conversion of LLVM TBAA metadata starting from
424  /// `node`. On exit from this function all nodes reachable
425  /// from `node` are converted, and tbaaMapping map is updated
426  /// (unless all dependencies have been converted by a previous
427  /// invocation of this function).
428  LogicalResult processTBAAMetadata(const llvm::MDNode *node);
429  /// Converts all LLVM access groups starting from `node` to MLIR access group
430  /// operations and stores a mapping from every nested access group node to the
431  /// translated attribute. Returns success if all conversions succeed and
432  /// failure otherwise.
433  LogicalResult processAccessGroupMetadata(const llvm::MDNode *node);
434  /// Converts all LLVM alias scopes and domains starting from `node` to MLIR
435  /// alias scope and domain attributes and stores a mapping from every nested
436  /// alias scope or alias domain node to the translated attribute. Returns
437  /// success if all conversions succeed and failure otherwise.
438  LogicalResult processAliasScopeMetadata(const llvm::MDNode *node);
439  /// Converts the given LLVM comdat struct to an MLIR comdat selector operation
440  /// and stores a mapping from the struct to the symbol pointing to the
441  /// translated operation.
442  void processComdat(const llvm::Comdat *comdat);
443  /// Returns a symbol name for a nameless global. MLIR, in contrast to LLVM,
444  /// always requires a symbol name.
445  FlatSymbolRefAttr
446  getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
447  /// Returns the global insertion point for the next global operation. If the
448  /// `globalInsertionOp` is set, the insertion point is placed after the
449  /// specified operation. Otherwise, it defaults to the start of the module.
450  OpBuilder::InsertionGuard setGlobalInsertionPoint();
451 
452  /// Builder pointing at where the next instruction should be generated.
453  OpBuilder builder;
454  /// Block to insert the next constant into.
455  Block *constantInsertionBlock = nullptr;
456  /// Operation to insert the next constant after.
457  Operation *constantInsertionOp = nullptr;
458  /// Operation to insert the next global after.
459  Operation *globalInsertionOp = nullptr;
460  /// Operation to insert comdat selector operations into.
461  ComdatOp globalComdatOp = nullptr;
462  /// The current context.
463  MLIRContext *context;
464  /// The MLIR module being created.
465  ModuleOp mlirModule;
466  /// The LLVM module being imported.
467  std::unique_ptr<llvm::Module> llvmModule;
468  /// Nameless globals.
469  DenseMap<llvm::GlobalVariable *, FlatSymbolRefAttr> namelessGlobals;
470  /// Counter used to assign a unique ID to each nameless global.
471  unsigned namelessGlobalId = 0;
472 
473  /// A dialect interface collection used for dispatching the import to specific
474  /// dialects.
475  LLVMImportInterface iface;
476 
477  /// Function-local mapping between original and imported block.
478  DenseMap<llvm::BasicBlock *, Block *> blockMapping;
479  /// Function-local mapping between original and imported values.
480  DenseMap<llvm::Value *, Value> valueMapping;
481  /// Function-local mapping between original instructions and imported
482  /// operations for all operations that return no result. All operations that
483  /// return a result have a valueMapping entry instead.
484  DenseMap<llvm::Instruction *, Operation *> noResultOpMapping;
485  /// Function-local list of debug intrinsics that need to be imported after the
486  /// function conversion has finished.
487  SetVector<llvm::Instruction *> debugIntrinsics;
488  /// Mapping between LLVM alias scope and domain metadata nodes and
489  /// attributes in the LLVM dialect corresponding to these nodes.
490  DenseMap<const llvm::MDNode *, Attribute> aliasScopeMapping;
491  /// Mapping between LLVM TBAA metadata nodes and LLVM dialect TBAA attributes
492  /// corresponding to these nodes.
493  DenseMap<const llvm::MDNode *, Attribute> tbaaMapping;
494  /// Mapping between LLVM comdat structs and symbol references to LLVM dialect
495  /// comdat selector operations corresponding to these structs.
496  DenseMap<const llvm::Comdat *, SymbolRefAttr> comdatMapping;
497  /// The stateful type translator (contains named structs).
498  LLVM::TypeFromLLVMIRTranslator typeTranslator;
499  /// Stateful debug information importer.
500  std::unique_ptr<detail::DebugImporter> debugImporter;
501  /// Loop annotation importer.
502  std::unique_ptr<detail::LoopAnnotationImporter> loopAnnotationImporter;
503 
504  /// An option to control if expensive but uncritical diagnostics should be
505  /// emitted. Avoids generating warnings for unhandled debug intrinsics and
506  /// metadata that otherwise dominate the translation time for large inputs.
507  bool emitExpensiveWarnings;
508 
509  /// An option to control whether the importer should try to convert all
510  /// intrinsics to llvm.call_intrinsic instead of dialect supported operations.
511  bool preferUnregisteredIntrinsics;
512 };
513 
514 } // namespace LLVM
515 } // namespace mlir
516 
517 #endif // MLIR_TARGET_LLVMIR_MODULEIMPORT_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Block represents an ordered list of Operations.
Definition: Block.h:33
LogicalResult initializeImport(llvm::LLVMContext &llvmContext)
Queries all registered dialect interfaces for the supported LLVM IR intrinsic and metadata kinds and ...
Module import implementation class that provides methods to import globals and functions from an LLVM...
Definition: ModuleImport.h:48
LogicalResult convertIFuncs()
Converts all ifuncs of the LLVM module to MLIR variables.
LogicalResult convertIntrinsicArguments(ArrayRef< llvm::Value * > values, ArrayRef< llvm::OperandBundleUse > opBundles, bool requiresOpBundles, ArrayRef< unsigned > immArgPositions, ArrayRef< StringLiteral > immArgAttrNames, SmallVectorImpl< Value > &valuesOut, SmallVectorImpl< NamedAttribute > &attrsOut)
Converts the LLVM values for an intrinsic to mixed MLIR values and attributes for LLVM_IntrOpBase.
Location translateLoc(llvm::DILocation *loc)
Translates the debug location.
LogicalResult convertComdats()
Converts all comdat selectors of the LLVM module to MLIR comdat operations.
LogicalResult convertAliases()
Converts all aliases of the LLVM module to MLIR variables.
LogicalResult convertFunctions()
Converts all functions of the LLVM module to MLIR functions.
FailureOr< SmallVector< Value > > convertValues(ArrayRef< llvm::Value * > values)
Converts a range of LLVM values to a range of MLIR values using the convertValue method,...
Attribute lookupTBAAAttr(const llvm::MDNode *node) const
Returns the MLIR attribute mapped to the given LLVM TBAA metadata node.
Definition: ModuleImport.h:252
bool useUnregisteredIntrinsicsOnly() const
Whether the importer should try to convert all intrinsics to llvm.call_intrinsic instead of dialect s...
Definition: ModuleImport.h:307
DILocalVariableAttr matchLocalVariableAttr(llvm::Value *value)
Converts value to a local variable attribute.
LogicalResult convertLinkerOptionsMetadata()
Converts !llvm.linker.options metadata to the llvm.linker.options LLVM dialect operation.
void mapBlock(llvm::BasicBlock *llvm, Block *mlir)
Stores the mapping between an LLVM block and its MLIR counterpart.
Definition: ModuleImport.h:135
void processFunctionAttributes(llvm::Function *func, LLVMFuncOp funcOp)
Converts function attributes of LLVM Function func into LLVM dialect attributes of LLVMFuncOp funcOp.
LogicalResult convertMetadata()
Converts all LLVM metadata nodes that translate to attributes such as alias analysis or access group ...
Value & mapValue(llvm::Value *value)
Provides write-once access to store the MLIR value corresponding to the given LLVM value.
Definition: ModuleImport.h:95
FailureOr< Value > convertValue(llvm::Value *value)
Converts an LLVM value to an MLIR value, or returns failure if the conversion fails.
LogicalResult initializeImportInterface()
Calls the LLVMImportInterface initialization that queries the registered dialect interfaces for the s...
Definition: ModuleImport.h:58
void addDebugIntrinsic(llvm::CallInst *intrinsic)
Adds a debug intrinsics to the list of intrinsics that should be converted after the function convers...
LogicalResult convertIdentMetadata()
Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
Block * lookupBlock(llvm::BasicBlock *block) const
Returns the MLIR block mapped to the given LLVM block.
Definition: ModuleImport.h:142
FailureOr< Value > convertMetadataValue(llvm::Value *value)
Converts an LLVM metadata value to an MLIR value, or returns failure if the conversion fails.
FailureOr< SmallVector< AliasScopeAttr > > lookupAliasScopeAttrs(const llvm::MDNode *node) const
Returns the alias scope attributes that map to the alias scope nodes starting from the metadata node.
void setDisjointFlag(llvm::Instruction *inst, Operation *op) const
Sets the disjoint flag attribute for the imported operation op given the original instruction inst.
void mapNoResultOp(llvm::Instruction *llvm, Operation *mlir)
Stores a mapping between an LLVM instruction and the imported MLIR operation if the operation returns...
Definition: ModuleImport.h:108
void convertModuleLevelAsm()
Converts the module level asm of the LLVM module to an MLIR module level asm specification.
void setExactFlag(llvm::Instruction *inst, Operation *op) const
Sets the exact flag attribute for the imported operation op given the original instruction inst.
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
Definition: ModuleImport.h:190
Operation * lookupOperation(llvm::Instruction *inst)
Returns the MLIR operation mapped to the given LLVM instruction.
Definition: ModuleImport.h:128
ModuleImport(ModuleOp mlirModule, std::unique_ptr< llvm::Module > llvmModule, bool emitExpensiveWarnings, bool importEmptyDICompositeTypes, bool preferUnregisteredIntrinsics, bool importStructsAsLiterals)
DILabelAttr matchLabelAttr(llvm::Value *value)
Converts value to a label attribute. Asserts if the matching fails.
FloatAttr matchFloatAttr(llvm::Value *value)
Converts value to a float attribute. Asserts if the matching fails.
LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node, Location loc) const
Returns the loop annotation attribute that corresponds to the given LLVM loop metadata node.
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const
Sets the fastmath flags attribute for the imported operation op given the original instruction inst.
FailureOr< SmallVector< AliasScopeAttr > > matchAliasScopeAttrs(llvm::Value *value)
Converts value to an array of alias scopes or returns failure if the conversion fails.
Value lookupValue(llvm::Value *value)
Returns the MLIR value mapped to the given LLVM value.
Definition: ModuleImport.h:103
LogicalResult processFunction(llvm::Function *func)
Imports func into the current module.
LogicalResult convertDependentLibrariesMetadata()
Converts !llvm.dependent-libraries metadata to llvm.dependent_libraries LLVM ModuleOp attribute.
RoundingModeAttr matchRoundingModeAttr(llvm::Value *value)
Converts value to a rounding mode attribute.
void convertTargetTriple()
Converts target triple of the LLVM module to an MLIR target triple specification.
void convertArgAndResultAttrs(llvm::CallBase *call, ArgAndResultAttrsOpInterface attrsOp, ArrayRef< unsigned > immArgPositions={})
Converts the argument and result attributes attached to call and adds them to attrsOp.
Operation *& mapNoResultOp(llvm::Instruction *inst)
Provides write-once access to store the MLIR operation corresponding to the given LLVM instruction if...
Definition: ModuleImport.h:116
LogicalResult convertModuleFlagsMetadata()
Converts !llvm.module.flags metadata.
void mapValue(llvm::Value *llvm, Value mlir)
Stores the mapping between an LLVM value and its MLIR counterpart.
Definition: ModuleImport.h:91
FailureOr< SmallVector< AccessGroupAttr > > lookupAccessGroupAttrs(const llvm::MDNode *node) const
Returns the access group attributes that map to the access group nodes starting from the access group...
LogicalResult convertGlobals()
Converts all global variables of the LLVM module to MLIR global variables.
void setIntegerOverflowFlags(llvm::Instruction *inst, Operation *op) const
Sets the integer overflow flags (nsw/nuw) attribute for the imported operation op given the original ...
LogicalResult convertCommandlineMetadata()
Converts !llvm.commandline metadata to the llvm.commandline LLVM ModuleOp attribute.
FPExceptionBehaviorAttr matchFPExceptionBehaviorAttr(llvm::Value *value)
Converts value to a FP exception behavior attribute.
void setNonNegFlag(llvm::Instruction *inst, Operation *op) const
Sets the nneg flag attribute for the imported operation op given the original instruction inst.
FailureOr< DereferenceableAttr > translateDereferenceableAttr(const llvm::MDNode *node, unsigned kindID)
Returns the dereferenceable attribute that corresponds to the given LLVM dereferenceable or dereferen...
LogicalResult convertDataLayout()
Converts the data layout of the LLVM module to an MLIR data layout specification.
IntegerAttr matchIntegerAttr(llvm::Value *value)
Converts value to an integer attribute. Asserts if the matching fails.
Type translateType(llvm::Type *type)
Translates the given LLVM IR type to the MLIR LLVM dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.