MLIR  18.0.0git
DebugTranslation.h
Go to the documentation of this file.
1 //===- DebugTranslation.h - MLIR to LLVM 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 an MLIR debug information and
10 // the corresponding LLVMIR representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
16 
18 #include "mlir/IR/Location.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/IR/DIBuilder.h"
22 
23 namespace mlir {
24 class Operation;
25 
26 namespace LLVM {
27 class LLVMFuncOp;
28 
29 namespace detail {
31 public:
32  DebugTranslation(Operation *module, llvm::Module &llvmModule);
33 
34  /// Finalize the translation of debug information.
35  void finalize();
36 
37  /// Translate the given location to an llvm debug location.
38  llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
39 
40  /// Translate the debug information for the given function.
41  void translate(LLVMFuncOp func, llvm::Function &llvmFunc);
42 
43  /// Translate the given LLVM debug metadata to LLVM.
44  llvm::DINode *translate(DINodeAttr attr);
45 
46  /// Translate the given derived LLVM debug metadata to LLVM.
47  template <typename DIAttrT>
48  auto translate(DIAttrT attr) {
49  // Infer the LLVM type from the attribute type.
50  using LLVMTypeT = std::remove_pointer_t<decltype(translateImpl(attr))>;
51  return cast_or_null<LLVMTypeT>(translate(DINodeAttr(attr)));
52  }
53 
54 private:
55  /// Translate the given location to an llvm debug location with the given
56  /// scope and inlinedAt parameters.
57  llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope,
58  llvm::DILocation *inlinedAt);
59 
60  /// Create an llvm debug file for the given file path.
61  llvm::DIFile *translateFile(StringRef fileName);
62 
63  /// Translate the given attribute to the corresponding llvm debug metadata.
64  llvm::DIType *translateImpl(DINullTypeAttr attr);
65  llvm::DIBasicType *translateImpl(DIBasicTypeAttr attr);
66  llvm::DICompileUnit *translateImpl(DICompileUnitAttr attr);
67  llvm::DICompositeType *translateImpl(DICompositeTypeAttr attr);
68  llvm::DIDerivedType *translateImpl(DIDerivedTypeAttr attr);
69  llvm::DIFile *translateImpl(DIFileAttr attr);
70  llvm::DILabel *translateImpl(DILabelAttr attr);
71  llvm::DILexicalBlock *translateImpl(DILexicalBlockAttr attr);
72  llvm::DILexicalBlockFile *translateImpl(DILexicalBlockFileAttr attr);
73  llvm::DILocalScope *translateImpl(DILocalScopeAttr attr);
74  llvm::DILocalVariable *translateImpl(DILocalVariableAttr attr);
75  llvm::DIModule *translateImpl(DIModuleAttr attr);
76  llvm::DINamespace *translateImpl(DINamespaceAttr attr);
77  llvm::DIScope *translateImpl(DIScopeAttr attr);
78  llvm::DISubprogram *translateImpl(DISubprogramAttr attr);
79  llvm::DISubrange *translateImpl(DISubrangeAttr attr);
80  llvm::DISubroutineType *translateImpl(DISubroutineTypeAttr attr);
81  llvm::DIType *translateImpl(DITypeAttr attr);
82 
83  /// Constructs a string metadata node from the string attribute. Returns
84  /// nullptr if `stringAttr` is null or contains and empty string.
85  llvm::MDString *getMDStringOrNull(StringAttr stringAttr);
86 
87  /// A mapping between mlir location+scope and the corresponding llvm debug
88  /// metadata.
90  llvm::DILocation *>
91  locationToLoc;
92 
93  /// A mapping between debug attribute and the corresponding llvm debug
94  /// metadata.
96 
97  /// A mapping between filename and llvm debug file.
98  /// TODO: Change this to DenseMap<Identifier, ...> when we can
99  /// access the Identifier filename in FileLineColLoc.
100  llvm::StringMap<llvm::DIFile *> fileMap;
101 
102  /// A string containing the current working directory of the compiler.
103  SmallString<256> currentWorkingDir;
104 
105  /// Flag indicating if debug information should be emitted.
106  bool debugEmissionIsEnabled;
107 
108  /// Debug information fields.
109  llvm::Module &llvmModule;
110  llvm::LLVMContext &llvmCtx;
111 };
112 
113 } // namespace detail
114 } // namespace LLVM
115 } // namespace mlir
116 
117 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
This class represents a LLVM attribute that describes a local debug info scope.
Definition: LLVMAttrs.h:46
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
void finalize()
Finalize the translation of debug information.
void translate(LLVMFuncOp func, llvm::Function &llvmFunc)
Translate the debug information for the given function.
DebugTranslation(Operation *module, llvm::Module &llvmModule)
auto translate(DIAttrT attr)
Translate the given derived LLVM debug metadata to LLVM.
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translate the given location to an llvm debug location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Include the generated interface declarations.