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);
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 an LLVM intrinsic to an MLIR LLVM dialect operation if an MLIR
334  /// counterpart exists. Otherwise, returns failure.
335  LogicalResult convertIntrinsic(llvm::CallInst *inst);
336  /// Converts an LLVM instruction to an MLIR LLVM dialect operation if an MLIR
337  /// counterpart exists. Otherwise, returns failure.
338  LogicalResult convertInstruction(llvm::Instruction *inst);
339  /// Converts the metadata attached to the original instruction `inst` if
340  /// a dialect interfaces supports the specific kind of metadata and attaches
341  /// the resulting dialect attributes to the converted operation `op`. Emits a
342  /// warning if the conversion of a supported metadata kind fails.
343  void setNonDebugMetadataAttrs(llvm::Instruction *inst, Operation *op);
344  /// Imports `inst` and populates valueMapping[inst] with the result of the
345  /// imported operation or noResultOpMapping[inst] with the imported operation
346  /// if it has no result.
347  LogicalResult processInstruction(llvm::Instruction *inst);
348  /// Converts the `branch` arguments in the order of the phi's found in
349  /// `target` and appends them to the `blockArguments` to attach to the
350  /// generated branch operation. The `blockArguments` thus have the same order
351  /// as the phi's in `target`.
352  LogicalResult convertBranchArgs(llvm::Instruction *branch,
353  llvm::BasicBlock *target,
354  SmallVectorImpl<Value> &blockArguments);
355  /// Convert `callInst` operands. For indirect calls, the method additionally
356  /// inserts the called function at the beginning of the returned `operands`
357  /// array. If `allowInlineAsm` is set to false (the default), it will return
358  /// failure if the called operand is an inline asm which isn't convertible to
359  /// MLIR as a value.
360  FailureOr<SmallVector<Value>>
361  convertCallOperands(llvm::CallBase *callInst, bool allowInlineAsm = false);
362  /// Converts the callee's function type. For direct calls, it converts the
363  /// actual function type, which may differ from the called operand type in
364  /// variadic functions. For indirect calls, it converts the function type
365  /// associated with the call instruction. Returns failure when the call and
366  /// the callee are not compatible or when nested type conversions failed.
367  FailureOr<LLVMFunctionType> convertFunctionType(llvm::CallBase *callInst);
368  /// Returns the callee name, or an empty symbol if the call is not direct.
369  FlatSymbolRefAttr convertCalleeName(llvm::CallBase *callInst);
370  /// Converts the parameter and result attributes attached to `func` and adds
371  /// them to the `funcOp`.
372  void convertParameterAttributes(llvm::Function *func, LLVMFuncOp funcOp,
373  OpBuilder &builder);
374  /// Converts the AttributeSet of one parameter in LLVM IR to a corresponding
375  /// DictionaryAttr for the LLVM dialect.
376  DictionaryAttr convertParameterAttribute(llvm::AttributeSet llvmParamAttrs,
377  OpBuilder &builder);
378  /// Converts the parameter and result attributes attached to `call` and adds
379  /// them to the `callOp`. Implemented in terms of the the public definition of
380  /// convertParameterAttributes.
381  void convertParameterAttributes(llvm::CallBase *call, CallOpInterface callOp,
382  OpBuilder &builder);
383  /// Converts the attributes attached to `inst` and adds them to the `op`.
384  LogicalResult convertCallAttributes(llvm::CallInst *inst, CallOp op);
385  /// Converts the attributes attached to `inst` and adds them to the `op`.
386  LogicalResult convertInvokeAttributes(llvm::InvokeInst *inst, InvokeOp op);
387  /// Returns the builtin type equivalent to the given LLVM dialect type or
388  /// nullptr if there is no equivalent. The returned type can be used to create
389  /// an attribute for a GlobalOp or a ConstantOp.
390  Type getBuiltinTypeForAttr(Type type);
391  /// Returns `constant` as an attribute to attach to a GlobalOp or ConstantOp
392  /// or nullptr if the constant is not convertible. It supports scalar integer
393  /// and float constants as well as shaped types thereof including strings.
394  Attribute getConstantAsAttr(llvm::Constant *constant);
395  /// Returns the topologically sorted set of transitive dependencies needed to
396  /// convert the given constant.
397  SetVector<llvm::Constant *> getConstantsToConvert(llvm::Constant *constant);
398  /// Converts an LLVM constant to an MLIR value, or returns failure if the
399  /// conversion fails. The MLIR value may be produced by a ConstantOp,
400  /// AddressOfOp, NullOp, or a side-effect free operation (for ConstantExprs or
401  /// ConstantGEPs).
402  FailureOr<Value> convertConstant(llvm::Constant *constant);
403  /// Converts an LLVM constant and its transitive constant dependencies to MLIR
404  /// operations by converting them in topological order using the
405  /// `convertConstant` method, or returns failure if the conversion of any of
406  /// them fails. All operations are inserted at the start of the current
407  /// function entry block.
408  FailureOr<Value> convertConstantExpr(llvm::Constant *constant);
409  /// Returns a global comdat operation that serves as a container for LLVM
410  /// comdat selectors. Creates the global comdat operation on the first
411  /// invocation.
412  ComdatOp getGlobalComdatOp();
413  /// Performs conversion of LLVM TBAA metadata starting from
414  /// `node`. On exit from this function all nodes reachable
415  /// from `node` are converted, and tbaaMapping map is updated
416  /// (unless all dependencies have been converted by a previous
417  /// invocation of this function).
418  LogicalResult processTBAAMetadata(const llvm::MDNode *node);
419  /// Converts all LLVM access groups starting from `node` to MLIR access group
420  /// operations and stores a mapping from every nested access group node to the
421  /// translated attribute. Returns success if all conversions succeed and
422  /// failure otherwise.
423  LogicalResult processAccessGroupMetadata(const llvm::MDNode *node);
424  /// Converts all LLVM alias scopes and domains starting from `node` to MLIR
425  /// alias scope and domain attributes and stores a mapping from every nested
426  /// alias scope or alias domain node to the translated attribute. Returns
427  /// success if all conversions succeed and failure otherwise.
428  LogicalResult processAliasScopeMetadata(const llvm::MDNode *node);
429  /// Converts the given LLVM comdat struct to an MLIR comdat selector operation
430  /// and stores a mapping from the struct to the symbol pointing to the
431  /// translated operation.
432  void processComdat(const llvm::Comdat *comdat);
433  /// Returns a symbol name for a nameless global. MLIR, in contrast to LLVM,
434  /// always requires a symbol name.
435  FlatSymbolRefAttr
436  getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
437  /// Returns the global insertion point for the next global operation. If the
438  /// `globalInsertionOp` is set, the insertion point is placed after the
439  /// specified operation. Otherwise, it defaults to the start of the module.
440  OpBuilder::InsertionGuard setGlobalInsertionPoint();
441 
442  /// Builder pointing at where the next instruction should be generated.
443  OpBuilder builder;
444  /// Block to insert the next constant into.
445  Block *constantInsertionBlock = nullptr;
446  /// Operation to insert the next constant after.
447  Operation *constantInsertionOp = nullptr;
448  /// Operation to insert the next global after.
449  Operation *globalInsertionOp = nullptr;
450  /// Operation to insert comdat selector operations into.
451  ComdatOp globalComdatOp = nullptr;
452  /// The current context.
453  MLIRContext *context;
454  /// The MLIR module being created.
455  ModuleOp mlirModule;
456  /// The LLVM module being imported.
457  std::unique_ptr<llvm::Module> llvmModule;
458  /// Nameless globals.
459  DenseMap<llvm::GlobalVariable *, FlatSymbolRefAttr> namelessGlobals;
460  /// Counter used to assign a unique ID to each nameless global.
461  unsigned namelessGlobalId = 0;
462 
463  /// A dialect interface collection used for dispatching the import to specific
464  /// dialects.
465  LLVMImportInterface iface;
466 
467  /// Function-local mapping between original and imported block.
468  DenseMap<llvm::BasicBlock *, Block *> blockMapping;
469  /// Function-local mapping between original and imported values.
470  DenseMap<llvm::Value *, Value> valueMapping;
471  /// Function-local mapping between original instructions and imported
472  /// operations for all operations that return no result. All operations that
473  /// return a result have a valueMapping entry instead.
474  DenseMap<llvm::Instruction *, Operation *> noResultOpMapping;
475  /// Function-local list of debug intrinsics that need to be imported after the
476  /// function conversion has finished.
477  SetVector<llvm::Instruction *> debugIntrinsics;
478  /// Mapping between LLVM alias scope and domain metadata nodes and
479  /// attributes in the LLVM dialect corresponding to these nodes.
480  DenseMap<const llvm::MDNode *, Attribute> aliasScopeMapping;
481  /// Mapping between LLVM TBAA metadata nodes and LLVM dialect TBAA attributes
482  /// corresponding to these nodes.
483  DenseMap<const llvm::MDNode *, Attribute> tbaaMapping;
484  /// Mapping between LLVM comdat structs and symbol references to LLVM dialect
485  /// comdat selector operations corresponding to these structs.
486  DenseMap<const llvm::Comdat *, SymbolRefAttr> comdatMapping;
487  /// The stateful type translator (contains named structs).
488  LLVM::TypeFromLLVMIRTranslator typeTranslator;
489  /// Stateful debug information importer.
490  std::unique_ptr<detail::DebugImporter> debugImporter;
491  /// Loop annotation importer.
492  std::unique_ptr<detail::LoopAnnotationImporter> loopAnnotationImporter;
493 
494  /// An option to control if expensive but uncritical diagnostics should be
495  /// emitted. Avoids generating warnings for unhandled debug intrinsics and
496  /// metadata that otherwise dominate the translation time for large inputs.
497  bool emitExpensiveWarnings;
498 
499  /// An option to control whether the importer should try to convert all
500  /// intrinsics to llvm.call_intrinsic instead of dialect supported operations.
501  bool preferUnregisteredIntrinsics;
502 };
503 
504 } // namespace LLVM
505 } // namespace mlir
506 
507 #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.
ModuleImport(ModuleOp mlirModule, std::unique_ptr< llvm::Module > llvmModule, bool emitExpensiveWarnings, bool importEmptyDICompositeTypes, bool preferUnregisteredIntrinsics)
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
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:66
This class helps build Operations.
Definition: Builders.h:205
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.