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