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