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
17#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
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
23namespace mlir {
24class Operation;
25
26namespace LLVM {
27class LLVMFuncOp;
28
29namespace detail {
30class DebugTranslation {
31public:
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 /// Translates the given DWARF expression metadata to to LLVM.
41 llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
42
43 /// Translates the given DWARF global variable expression to LLVM.
44 llvm::DIGlobalVariableExpression *
45 translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
46
47 /// Translate the debug information for the given function.
48 void translate(LLVMFuncOp func, llvm::Function &llvmFunc);
49
50 /// Translate the given LLVM debug metadata to LLVM.
51 llvm::DINode *translate(DINodeAttr attr);
52
53 /// Translate the given derived LLVM debug metadata to LLVM.
54 template <typename DIAttrT>
55 auto translate(DIAttrT attr) {
56 // Infer the LLVM type from the attribute type.
57 using LLVMTypeT = std::remove_pointer_t<decltype(translateImpl(attr))>;
58 return cast_or_null<LLVMTypeT>(translate(attr: DINodeAttr(attr)));
59 }
60
61private:
62 /// Translate the given location to an llvm debug location with the given
63 /// scope and inlinedAt parameters.
64 llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope,
65 llvm::DILocation *inlinedAt);
66
67 /// Create an llvm debug file for the given file path.
68 llvm::DIFile *translateFile(StringRef fileName);
69
70 /// Translate the given attribute to the corresponding llvm debug metadata.
71 llvm::DIType *translateImpl(DINullTypeAttr attr);
72 llvm::DIBasicType *translateImpl(DIBasicTypeAttr attr);
73 llvm::DICompileUnit *translateImpl(DICompileUnitAttr attr);
74 llvm::DICompositeType *translateImpl(DICompositeTypeAttr attr);
75 llvm::DIDerivedType *translateImpl(DIDerivedTypeAttr attr);
76 llvm::DIFile *translateImpl(DIFileAttr attr);
77 llvm::DILabel *translateImpl(DILabelAttr attr);
78 llvm::DILexicalBlock *translateImpl(DILexicalBlockAttr attr);
79 llvm::DILexicalBlockFile *translateImpl(DILexicalBlockFileAttr attr);
80 llvm::DILocalScope *translateImpl(DILocalScopeAttr attr);
81 llvm::DILocalVariable *translateImpl(DILocalVariableAttr attr);
82 llvm::DIGlobalVariable *translateImpl(DIGlobalVariableAttr attr);
83 llvm::DIModule *translateImpl(DIModuleAttr attr);
84 llvm::DINamespace *translateImpl(DINamespaceAttr attr);
85 llvm::DIScope *translateImpl(DIScopeAttr attr);
86 llvm::DISubprogram *translateImpl(DISubprogramAttr attr);
87 llvm::DISubrange *translateImpl(DISubrangeAttr attr);
88 llvm::DISubroutineType *translateImpl(DISubroutineTypeAttr attr);
89 llvm::DIType *translateImpl(DITypeAttr attr);
90
91 /// Attributes that support self recursion need to implement an additional
92 /// method to hook into `translateRecursive`.
93 /// - `<temp llvm type> translateTemporaryImpl(<mlir type>)`:
94 /// Create a temporary translation of the DI attr without recursively
95 /// translating any nested DI attrs.
96 llvm::DIType *translateRecursive(DIRecursiveTypeAttrInterface attr);
97
98 /// Translate the given attribute to a temporary llvm debug metadata of the
99 /// corresponding type.
100 llvm::TempDICompositeType translateTemporaryImpl(DICompositeTypeAttr attr);
101
102 /// Constructs a string metadata node from the string attribute. Returns
103 /// nullptr if `stringAttr` is null or contains and empty string.
104 llvm::MDString *getMDStringOrNull(StringAttr stringAttr);
105
106 /// A mapping between mlir location+scope and the corresponding llvm debug
107 /// metadata.
108 DenseMap<std::tuple<Location, llvm::DILocalScope *, const llvm::DILocation *>,
109 llvm::DILocation *>
110 locationToLoc;
111
112 /// A mapping between debug attribute and the corresponding llvm debug
113 /// metadata.
114 DenseMap<Attribute, llvm::DINode *> attrToNode;
115
116 /// A mapping between recursive ID and the translated DIType.
117 llvm::MapVector<DistinctAttr, llvm::DIType *> recursiveTypeMap;
118
119 /// A mapping between a distinct ID and the translated LLVM metadata node.
120 /// This helps identify attrs that should translate into the same LLVM debug
121 /// node.
122 DenseMap<DistinctAttr, llvm::DINode *> distinctAttrToNode;
123
124 /// A mapping between filename and llvm debug file.
125 /// TODO: Change this to DenseMap<Identifier, ...> when we can
126 /// access the Identifier filename in FileLineColLoc.
127 llvm::StringMap<llvm::DIFile *> fileMap;
128
129 /// A string containing the current working directory of the compiler.
130 SmallString<256> currentWorkingDir;
131
132 /// Flag indicating if debug information should be emitted.
133 bool debugEmissionIsEnabled;
134
135 /// Debug information fields.
136 llvm::Module &llvmModule;
137 llvm::LLVMContext &llvmCtx;
138};
139
140} // namespace detail
141} // namespace LLVM
142} // namespace mlir
143
144#endif // MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
145

source code of mlir/lib/Target/LLVMIR/DebugTranslation.h