MLIR  20.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"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/IR/DebugInfoMetadata.h"
23 
24 namespace mlir {
25 class Operation;
26 
27 namespace LLVM {
28 class LLVMFuncOp;
29 
30 namespace detail {
31 
33 public:
34  DebugImporter(ModuleOp mlirModule, bool dropDICompositeTypeElements);
35 
36  /// Translates the given LLVM debug location to an MLIR location.
37  Location translateLoc(llvm::DILocation *loc);
38 
39  /// Translates the LLVM DWARF expression metadata to MLIR.
40  DIExpressionAttr translateExpression(llvm::DIExpression *node);
41 
42  /// Translates the LLVM DWARF global variable expression metadata to MLIR.
43  DIGlobalVariableExpressionAttr
44  translateGlobalVariableExpression(llvm::DIGlobalVariableExpression *node);
45 
46  /// Translates the debug information for the given function into a Location.
47  /// Returns UnknownLoc if `func` has no debug information attached to it.
48  Location translateFuncLocation(llvm::Function *func);
49 
50  /// Translates the given LLVM debug metadata to MLIR.
51  DINodeAttr translate(llvm::DINode *node);
52 
53  /// Infers the metadata type and translates it to MLIR.
54  template <typename DINodeT>
55  auto translate(DINodeT *node) {
56  // Infer the MLIR type from the LLVM metadata type.
57  using MLIRTypeT = decltype(translateImpl(node));
58  return cast_or_null<MLIRTypeT>(
59  translate(static_cast<llvm::DINode *>(node)));
60  }
61 
62 private:
63  /// Translates the given LLVM debug metadata to the corresponding attribute.
64  DIBasicTypeAttr translateImpl(llvm::DIBasicType *node);
65  DICompileUnitAttr translateImpl(llvm::DICompileUnit *node);
66  DICompositeTypeAttr translateImpl(llvm::DICompositeType *node);
67  DIDerivedTypeAttr translateImpl(llvm::DIDerivedType *node);
68  DIStringTypeAttr translateImpl(llvm::DIStringType *node);
69  DIFileAttr translateImpl(llvm::DIFile *node);
70  DILabelAttr translateImpl(llvm::DILabel *node);
71  DILexicalBlockAttr translateImpl(llvm::DILexicalBlock *node);
72  DILexicalBlockFileAttr translateImpl(llvm::DILexicalBlockFile *node);
73  DIGlobalVariableAttr translateImpl(llvm::DIGlobalVariable *node);
74  DILocalVariableAttr translateImpl(llvm::DILocalVariable *node);
75  DIVariableAttr translateImpl(llvm::DIVariable *node);
76  DIModuleAttr translateImpl(llvm::DIModule *node);
77  DINamespaceAttr translateImpl(llvm::DINamespace *node);
78  DIScopeAttr translateImpl(llvm::DIScope *node);
79  DISubprogramAttr translateImpl(llvm::DISubprogram *node);
80  DISubrangeAttr translateImpl(llvm::DISubrange *node);
81  DISubroutineTypeAttr translateImpl(llvm::DISubroutineType *node);
82  DITypeAttr translateImpl(llvm::DIType *node);
83 
84  /// Constructs a StringAttr from the MDString if it is non-null. Returns a
85  /// null attribute otherwise.
86  StringAttr getStringAttrOrNull(llvm::MDString *stringNode);
87 
88  /// Get the DistinctAttr used to represent `node` if one was already created
89  /// for it, or create a new one if not.
90  DistinctAttr getOrCreateDistinctID(llvm::DINode *node);
91 
92  std::optional<DINodeAttr> createRecSelf(llvm::DINode *node);
93 
94  /// A mapping between distinct LLVM debug metadata nodes and the corresponding
95  /// distinct id attribute.
96  DenseMap<llvm::DINode *, DistinctAttr> nodeToDistinctAttr;
97 
98  /// A mapping between DINodes that are recursive, and their assigned recId.
99  /// This is kept so that repeated occurrences of the same node can reuse the
100  /// same ID and be deduplicated.
102 
104 
105  MLIRContext *context;
106  ModuleOp mlirModule;
107 
108  /// An option to control if DICompositeTypes should always be imported without
109  /// converting their elements. If set, the option avoids the recursive
110  /// traversal of composite type debug information, which can be expensive for
111  /// adversarial inputs.
112  bool dropDICompositeTypeElements;
113 };
114 
115 } // namespace detail
116 } // namespace LLVM
117 } // namespace mlir
118 
119 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
A cache for replacer-like functions that map values between two domains.
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
This class represents a LLVM attribute that describes a debug info variable.
Definition: LLVMAttrs.h:64
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:55
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.
DebugImporter(ModuleOp mlirModule, bool dropDICompositeTypeElements)
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.