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