MLIR  19.0.0git
DebugImporter.h
Go to the documentation of this file.
1 //===- DebugImporter.h - LLVM to MLIR Debug 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 translation between LLVMIR debug information and
10 // the corresponding MLIR representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
16 
18 #include "mlir/IR/BuiltinOps.h"
19 #include "mlir/IR/MLIRContext.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 
22 namespace mlir {
23 class Operation;
24 
25 namespace LLVM {
26 class LLVMFuncOp;
27 
28 namespace detail {
29 
31 public:
32  DebugImporter(ModuleOp mlirModule)
33  : context(mlirModule.getContext()), mlirModule(mlirModule) {}
34 
35  /// Translates the given LLVM debug location to an MLIR location.
36  Location translateLoc(llvm::DILocation *loc);
37 
38  /// Translates the LLVM DWARF expression metadata to MLIR.
39  DIExpressionAttr translateExpression(llvm::DIExpression *node);
40 
41  /// Translates the LLVM DWARF global variable expression metadata to MLIR.
42  DIGlobalVariableExpressionAttr
43  translateGlobalVariableExpression(llvm::DIGlobalVariableExpression *node);
44 
45  /// Translates the debug information for the given function into a Location.
46  /// Returns UnknownLoc if `func` has no debug information attached to it.
47  Location translateFuncLocation(llvm::Function *func);
48 
49  /// Translates the given LLVM debug metadata to MLIR.
50  DINodeAttr translate(llvm::DINode *node);
51 
52  /// Infers the metadata type and translates it to MLIR.
53  template <typename DINodeT>
54  auto translate(DINodeT *node) {
55  // Infer the MLIR type from the LLVM metadata type.
56  using MLIRTypeT = decltype(translateImpl(node));
57  return cast_or_null<MLIRTypeT>(
58  translate(static_cast<llvm::DINode *>(node)));
59  }
60 
61 private:
62  /// Translates the given LLVM debug metadata to the corresponding attribute.
63  DIBasicTypeAttr translateImpl(llvm::DIBasicType *node);
64  DICompileUnitAttr translateImpl(llvm::DICompileUnit *node);
65  DICompositeTypeAttr translateImpl(llvm::DICompositeType *node);
66  DIDerivedTypeAttr translateImpl(llvm::DIDerivedType *node);
67  DIFileAttr translateImpl(llvm::DIFile *node);
68  DILabelAttr translateImpl(llvm::DILabel *node);
69  DILexicalBlockAttr translateImpl(llvm::DILexicalBlock *node);
70  DILexicalBlockFileAttr translateImpl(llvm::DILexicalBlockFile *node);
71  DIGlobalVariableAttr translateImpl(llvm::DIGlobalVariable *node);
72  DILocalVariableAttr translateImpl(llvm::DILocalVariable *node);
73  DIModuleAttr translateImpl(llvm::DIModule *node);
74  DINamespaceAttr translateImpl(llvm::DINamespace *node);
75  DIScopeAttr translateImpl(llvm::DIScope *node);
76  DISubprogramAttr translateImpl(llvm::DISubprogram *node);
77  DISubrangeAttr translateImpl(llvm::DISubrange *node);
78  DISubroutineTypeAttr translateImpl(llvm::DISubroutineType *node);
79  DITypeAttr translateImpl(llvm::DIType *node);
80 
81  /// Constructs a StringAttr from the MDString if it is non-null. Returns a
82  /// null attribute otherwise.
83  StringAttr getStringAttrOrNull(llvm::MDString *stringNode);
84 
85  /// Get the DistinctAttr used to represent `node` if one was already created
86  /// for it, or create a new one if not.
87  DistinctAttr getOrCreateDistinctID(llvm::DINode *node);
88 
89  /// Get the `getRecSelf` constructor for the translated type of `node` if its
90  /// translated DITypeAttr supports recursion. Otherwise, returns nullptr.
91  function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>
92  getRecSelfConstructor(llvm::DINode *node);
93 
94  /// A mapping between LLVM debug metadata and the corresponding attribute.
96  /// A mapping between distinct LLVM debug metadata nodes and the corresponding
97  /// distinct id attribute.
98  DenseMap<llvm::DINode *, DistinctAttr> nodeToDistinctAttr;
99 
100  /// A stack that stores the metadata nodes that are being traversed. The stack
101  /// is used to detect cyclic dependencies during the metadata translation.
102  /// A node is pushed with a null value. If it is ever seen twice, it is given
103  /// a recursive id attribute, indicating that it is a recursive node.
104  llvm::MapVector<llvm::DINode *, DistinctAttr> translationStack;
105  /// All the unbound recursive self references in the translation stack.
106  SmallVector<DenseSet<DistinctAttr>> unboundRecursiveSelfRefs;
107 
108  MLIRContext *context;
109  ModuleOp mlirModule;
110 };
111 
112 } // namespace detail
113 } // namespace LLVM
114 } // namespace mlir
115 
116 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
static MLIRContext * getContext(OpFoldResult val)
An attribute that associates a referenced attribute with a unique identifier.
This class represents the base attribute for all debug info attributes.
Definition: LLVMAttrs.h:27
This class represents a LLVM attribute that describes a debug info scope.
Definition: LLVMAttrs.h:36
This class represents a LLVM attribute that describes a debug info type.
Definition: LLVMAttrs.h:55
DebugImporter(ModuleOp mlirModule)
Definition: DebugImporter.h:32
DINodeAttr translate(llvm::DINode *node)
Translates the given LLVM debug metadata to MLIR.
DIExpressionAttr translateExpression(llvm::DIExpression *node)
Translates the LLVM DWARF expression metadata to MLIR.
auto translate(DINodeT *node)
Infers the metadata type and translates it to MLIR.
Definition: DebugImporter.h:54
DIGlobalVariableExpressionAttr translateGlobalVariableExpression(llvm::DIGlobalVariableExpression *node)
Translates the LLVM DWARF global variable expression metadata to MLIR.
Location translateLoc(llvm::DILocation *loc)
Translates the given LLVM debug location to an MLIR location.
Location translateFuncLocation(llvm::Function *func)
Translates the debug information for the given function into a Location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Include the generated interface declarations.