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