Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.h

raw
1 //===- SPIRVToLLVMDbgTran.h - Converts SPIR-V DebugInfo to LLVM -*- C++ -*-===//
2 //
3 // The LLVM/SPIR-V Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2018 Intel Corporation. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Intel Corporation, nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 //
35 // This file implements translation of debug info from SPIR-V to LLVM metadata
36 //
37 //===----------------------------------------------------------------------===//
38
39 #ifndef SPIRVTOLLVMDBGTRAN_H
40 #define SPIRVTOLLVMDBGTRAN_H
41
42 #include "SPIRVInstruction.h"
43
44 #include "llvm/IR/DIBuilder.h"
45 #include "llvm/IR/DebugLoc.h"
46
47 #include <unordered_map>
48
49 namespace llvm {
50 class Module;
51 class Value;
52 class Instruction;
53 class Type;
54 } // namespace llvm
55 using namespace llvm;
56
57 namespace SPIRV {
58 class SPIRVToLLVM;
59 class SPIRVEntry;
60 class SPIRVFunction;
61 class SPIRVValue;
62
63 class SPIRVToLLVMDbgTran {
64 public:
65 typedef std::vector<SPIRVWord> SPIRVWordVec;
66
67 SPIRVToLLVMDbgTran(SPIRVModule *TBM, Module *TM, SPIRVToLLVM *Reader);
68 void addDbgInfoVersion();
69 void transDbgInfo(const SPIRVValue *SV, Value *V);
70 template <typename T = MDNode>
71 T *transDebugInst(const SPIRVExtInst *DebugInst) {
72 assert((DebugInst->getExtSetKind() == SPIRVEIS_Debug ||
73 DebugInst->getExtSetKind() == SPIRVEIS_OpenCL_DebugInfo_100 ||
74 DebugInst->getExtSetKind() ==
75 SPIRVEIS_NonSemantic_Shader_DebugInfo_100 ||
76 DebugInst->getExtSetKind() ==
77 SPIRVEIS_NonSemantic_Shader_DebugInfo_200) &&
78 "Unexpected extended instruction set");
79 auto It = DebugInstCache.find(DebugInst);
80 if (It != DebugInstCache.end())
81 return static_cast<T *>(It->second);
82 MDNode *Res = transDebugInstImpl(DebugInst);
83 DebugInstCache[DebugInst] = Res;
84 return static_cast<T *>(Res);
85 }
86
87 Instruction *transDebugIntrinsic(const SPIRVExtInst *DebugInst,
88 BasicBlock *BB);
89 void finalize();
90
91 private:
92 DIFile *getFile(const SPIRVId SourceId);
93
94 DIFile *getDIFile(const std::string &FileName,
95 std::optional<DIFile::ChecksumInfo<StringRef>> CS = std::nullopt,
96 std::optional<std::string> Source = std::nullopt);
97 DIFile *getDIFile(const SPIRVEntry *E);
98 unsigned getLineNo(const SPIRVEntry *E);
99
100 MDNode *transDebugInstImpl(const SPIRVExtInst *DebugInst);
101
102 DIType *transNonNullDebugType(const SPIRVExtInst *DebugInst);
103
104 llvm::DebugLoc transDebugLocation(const SPIRVExtInst *DebugInst);
105
106 llvm::DebugLoc transDebugScope(const SPIRVInstruction *Inst);
107
108 MDNode *transDebugInlined(const SPIRVExtInst *Inst);
109 MDNode *transDebugInlinedNonSemanticShader200(const SPIRVExtInst *Inst);
110
111 void appendToSourceLangLiteral(DICompileUnit *CompileUnit,
112 SPIRVWord SourceLang);
113
114 DICompileUnit *transCompilationUnit(const SPIRVExtInst *DebugInst,
115 const std::string CompilerVersion = "",
116 const std::string Flags = "");
117
118 DIBasicType *transTypeBasic(const SPIRVExtInst *DebugInst);
119
120 DIDerivedType *transTypeQualifier(const SPIRVExtInst *DebugInst);
121
122 DIType *transTypePointer(const SPIRVExtInst *DebugInst);
123
124 DICompositeType *transTypeArray(const SPIRVExtInst *DebugInst);
125 DICompositeType *transTypeArrayOpenCL(const SPIRVExtInst *DebugInst);
126 DICompositeType *transTypeArrayNonSemantic(const SPIRVExtInst *DebugInst);
127 DICompositeType *transTypeArrayDynamic(const SPIRVExtInst *DebugInst);
128
129 DICompositeType *transTypeVector(const SPIRVExtInst *DebugInst);
130
131 DICompositeType *transTypeComposite(const SPIRVExtInst *DebugInst);
132
133 DISubrange *transTypeSubrange(const SPIRVExtInst *DebugInst);
134
135 DIStringType *transTypeString(const SPIRVExtInst *DebugInst);
136
137 DINode *transTypeMember(const SPIRVExtInst *DebugInst,
138 const SPIRVExtInst *ParentInst = nullptr,
139 DIScope *Scope = nullptr);
140 DINode *transTypeMemberOpenCL(const SPIRVExtInst *DebugInst);
141 DINode *transTypeMemberNonSemantic(const SPIRVExtInst *DebugInst,
142 const SPIRVExtInst *ParentInst,
143 DIScope *Scope);
144
145 DINode *transTypeEnum(const SPIRVExtInst *DebugInst);
146
147 DINode *transTypeTemplateParameter(const SPIRVExtInst *DebugInst);
148 DINode *transTypeTemplateTemplateParameter(const SPIRVExtInst *DebugInst);
149 DINode *transTypeTemplateParameterPack(const SPIRVExtInst *DebugInst);
150
151 MDNode *transTypeTemplate(const SPIRVExtInst *DebugInst);
152
153 DINode *transTypeFunction(const SPIRVExtInst *DebugInst);
154
155 DINode *transTypePtrToMember(const SPIRVExtInst *DebugInst);
156
157 DINode *transLexicalBlock(const SPIRVExtInst *DebugInst);
158 DINode *transLexicalBlockDiscriminator(const SPIRVExtInst *DebugInst);
159
160 DINode *transFunction(const SPIRVExtInst *DebugInst,
161 bool IsMainSubprogram = false);
162 DINode *transFunctionDefinition(const SPIRVExtInst *DebugInst);
163 void transFunctionBody(DISubprogram *DIS, SPIRVId FuncId);
164
165 DINode *transFunctionDecl(const SPIRVExtInst *DebugInst);
166
167 MDNode *transEntryPoint(const SPIRVExtInst *DebugInst);
168
169 MDNode *transGlobalVariable(const SPIRVExtInst *DebugInst);
170
171 DINode *transLocalVariable(const SPIRVExtInst *DebugInst);
172
173 DINode *transTypedef(const SPIRVExtInst *DebugInst);
174
175 DINode *transTypeInheritance(const SPIRVExtInst *DebugInst,
176 DIType *ChildClass = nullptr);
177
178 DINode *transImportedEntry(const SPIRVExtInst *DebugInst);
179
180 DINode *transModule(const SPIRVExtInst *DebugInst);
181
182 MDNode *transExpression(const SPIRVExtInst *DebugInst);
183
184 SPIRVModule *BM;
185 Module *M;
186 std::unordered_map<SPIRVId, std::unique_ptr<DIBuilder>> BuilderMap;
187 SPIRVToLLVM *SPIRVReader;
188 bool Enable;
189 std::unordered_map<std::string, DIFile *> FileMap;
190 std::unordered_map<SPIRVId, DISubprogram *> FuncMap;
191 std::unordered_map<const SPIRVExtInst *, MDNode *> DebugInstCache;
192
193 struct SplitFileName {
194 SplitFileName(const std::string &FileName);
195 std::string BaseName;
196 std::string Path;
197 };
198
199 DIScope *getScope(const SPIRVEntry *ScopeInst);
200 SPIRVExtInst *getDbgInst(const SPIRVId Id);
201
202 DIBuilder &getDIBuilder(const SPIRVExtInst *DebugInst);
203
204 template <SPIRVWord OpCode> SPIRVExtInst *getDbgInst(const SPIRVId Id) {
205 if (SPIRVExtInst *DI = getDbgInst(Id)) {
206 if (DI->getExtOp() == OpCode) {
207 return DI;
208 }
209 }
210 return nullptr;
211 }
212 const std::string &getString(const SPIRVId Id);
213 std::optional<std::string> getStringSourceContinued(const SPIRVId Id,
214 SPIRVExtInst *DebugInst);
215 SPIRVWord getConstantValueOrLiteral(const std::vector<SPIRVWord> &,
216 const SPIRVWord,
217 const SPIRVExtInstSetKind);
218 std::string findModuleProducer();
219 std::optional<DIFile::ChecksumInfo<StringRef>> ParseChecksum(StringRef Text);
220
221 // BuildIdentifier and StoragePath must both be set or both unset.
222 // If StoragePath is empty both variables are unset and not valid.
223 uint64_t BuildIdentifier{0};
224 std::string StoragePath{};
225 void setBuildIdentifierAndStoragePath();
226 };
227 } // namespace SPIRV
228
229 #endif // SPIRVTOLLVMDBGTRAN_H