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