Debian Codegraph @

third_party/spirv-tools/source/opt/debug_info_manager.cpp

raw
1 // Copyright (c) 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/opt/debug_info_manager.h"
16
17 #include <cassert>
18
19 #include "source/opt/ir_context.h"
20
21 // Constants for OpenCL.DebugInfo.100 extension instructions.
22
23 static const uint32_t kOpLineOperandLineIndex = 1;
24 static const uint32_t kLineOperandIndexDebugFunction = 7;
25 static const uint32_t kLineOperandIndexDebugLexicalBlock = 5;
26 static const uint32_t kDebugFunctionOperandFunctionIndex = 13;
27 static const uint32_t kDebugFunctionOperandParentIndex = 9;
28 static const uint32_t kDebugTypeCompositeOperandParentIndex = 9;
29 static const uint32_t kDebugLexicalBlockOperandParentIndex = 7;
30 static const uint32_t kDebugInlinedAtOperandInlinedIndex = 6;
31 static const uint32_t kDebugExpressOperandOperationIndex = 4;
32 static const uint32_t kDebugDeclareOperandLocalVariableIndex = 4;
33 static const uint32_t kDebugDeclareOperandVariableIndex = 5;
34 static const uint32_t kDebugValueOperandLocalVariableIndex = 4;
35 static const uint32_t kDebugValueOperandExpressionIndex = 6;
36 static const uint32_t kDebugValueOperandIndexesIndex = 7;
37 static const uint32_t kDebugOperationOperandOperationIndex = 4;
38 static const uint32_t kOpVariableOperandStorageClassIndex = 2;
39 static const uint32_t kDebugLocalVariableOperandParentIndex = 9;
40 static const uint32_t kExtInstInstructionInIdx = 1;
41 static const uint32_t kDebugGlobalVariableOperandFlagsIndex = 12;
42 static const uint32_t kDebugLocalVariableOperandFlagsIndex = 10;
43
44 namespace spvtools {
45 namespace opt {
46 namespace analysis {
47 namespace {
48
49 void SetInlinedOperand(Instruction* dbg_inlined_at, uint32_t inlined_operand) {
50 assert(dbg_inlined_at);
51 assert(dbg_inlined_at->GetOpenCL100DebugOpcode() ==
52 OpenCLDebugInfo100DebugInlinedAt);
53 if (dbg_inlined_at->NumOperands() <= kDebugInlinedAtOperandInlinedIndex) {
54 dbg_inlined_at->AddOperand(
55 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inlined_operand}});
56 } else {
57 dbg_inlined_at->SetOperand(kDebugInlinedAtOperandInlinedIndex,
58 {inlined_operand});
59 }
60 }
61
62 uint32_t GetInlinedOperand(Instruction* dbg_inlined_at) {
63 assert(dbg_inlined_at);
64 assert(dbg_inlined_at->GetOpenCL100DebugOpcode() ==
65 OpenCLDebugInfo100DebugInlinedAt);
66 if (dbg_inlined_at->NumOperands() <= kDebugInlinedAtOperandInlinedIndex)
67 return kNoInlinedAt;
68 return dbg_inlined_at->GetSingleWordOperand(
69 kDebugInlinedAtOperandInlinedIndex);
70 }
71
72 bool IsEmptyDebugExpression(Instruction* instr) {
73 return instr->GetOpenCL100DebugOpcode() ==
74 OpenCLDebugInfo100DebugExpression &&
75 instr->NumOperands() == kDebugExpressOperandOperationIndex;
76 }
77
78 } // namespace
79
80 DebugInfoManager::DebugInfoManager(IRContext* c) : context_(c) {
81 AnalyzeDebugInsts(*c->module());
82 }
83
84 Instruction* DebugInfoManager::GetDbgInst(uint32_t id) {
85 auto dbg_inst_it = id_to_dbg_inst_.find(id);
86 return dbg_inst_it == id_to_dbg_inst_.end() ? nullptr : dbg_inst_it->second;
87 }
88
89 void DebugInfoManager::RegisterDbgInst(Instruction* inst) {
90 assert(
91 inst->NumInOperands() != 0 &&
92 context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo() ==
93 inst->GetInOperand(0).words[0] &&
94 "Given instruction is not a debug instruction");
95 id_to_dbg_inst_[inst->result_id()] = inst;
96 }
97
98 void DebugInfoManager::RegisterDbgFunction(Instruction* inst) {
99 assert(inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction &&
100 "inst is not a DebugFunction");
101 auto fn_id = inst->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex);
102 // Do not register function that has been optimized away
103 auto fn_inst = GetDbgInst(fn_id);
104 if (fn_inst != nullptr) {
105 assert(GetDbgInst(fn_id)->GetOpenCL100DebugOpcode() ==
106 OpenCLDebugInfo100DebugInfoNone);
107 return;
108 }
109 assert(
110 fn_id_to_dbg_fn_.find(fn_id) == fn_id_to_dbg_fn_.end() &&
111 "Register DebugFunction for a function that already has DebugFunction");
112 fn_id_to_dbg_fn_[fn_id] = inst;
113 }
114
115 void DebugInfoManager::RegisterDbgDeclare(uint32_t var_id,
116 Instruction* dbg_declare) {
117 assert(dbg_declare->GetOpenCL100DebugOpcode() ==
118 OpenCLDebugInfo100DebugDeclare ||
119 dbg_declare->GetOpenCL100DebugOpcode() ==
120 OpenCLDebugInfo100DebugValue);
121 auto dbg_decl_itr = var_id_to_dbg_decl_.find(var_id);
122 if (dbg_decl_itr == var_id_to_dbg_decl_.end()) {
123 var_id_to_dbg_decl_[var_id] = {dbg_declare};
124 } else {
125 dbg_decl_itr->second.insert(dbg_declare);
126 }
127 }
128
129 uint32_t DebugInfoManager::CreateDebugInlinedAt(const Instruction* line,
130 const DebugScope& scope) {
131 if (context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo() ==
132 0)
133 return kNoInlinedAt;
134
135 uint32_t line_number = 0;
136 if (line == nullptr) {
137 auto* lexical_scope_inst = GetDbgInst(scope.GetLexicalScope());
138 if (lexical_scope_inst == nullptr) return kNoInlinedAt;
139 OpenCLDebugInfo100Instructions debug_opcode =
140 lexical_scope_inst->GetOpenCL100DebugOpcode();
141 switch (debug_opcode) {
142 case OpenCLDebugInfo100DebugFunction:
143 line_number = lexical_scope_inst->GetSingleWordOperand(
144 kLineOperandIndexDebugFunction);
145 break;
146 case OpenCLDebugInfo100DebugLexicalBlock:
147 line_number = lexical_scope_inst->GetSingleWordOperand(
148 kLineOperandIndexDebugLexicalBlock);
149 break;
150 case OpenCLDebugInfo100DebugTypeComposite:
151 case OpenCLDebugInfo100DebugCompilationUnit:
152 assert(false &&
153 "DebugTypeComposite and DebugCompilationUnit are lexical "
154 "scopes, but we inline functions into a function or a block "
155 "of a function, not into a struct/class or a global scope.");
156 break;
157 default:
158 assert(false &&
159 "Unreachable. a debug extension instruction for a "
160 "lexical scope must be DebugFunction, DebugTypeComposite, "
161 "DebugLexicalBlock, or DebugCompilationUnit.");
162 break;
163 }
164 } else {
165 line_number = line->GetSingleWordOperand(kOpLineOperandLineIndex);
166 }
167
168 uint32_t result_id = context()->TakeNextId();
169 std::unique_ptr<Instruction> inlined_at(new Instruction(
170 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
171 result_id,
172 {
173 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
174 {context()
175 ->get_feature_mgr()
176 ->GetExtInstImportId_OpenCL100DebugInfo()}},
177 {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
178 {static_cast<uint32_t>(OpenCLDebugInfo100DebugInlinedAt)}},
179 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {line_number}},
180 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {scope.GetLexicalScope()}},
181 }));
182 // |scope| already has DebugInlinedAt. We put the existing DebugInlinedAt
183 // into the Inlined operand of this new DebugInlinedAt.
184 if (scope.GetInlinedAt() != kNoInlinedAt) {
185 inlined_at->AddOperand(
186 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {scope.GetInlinedAt()}});
187 }
188 RegisterDbgInst(inlined_at.get());
189 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
190 context()->get_def_use_mgr()->AnalyzeInstDefUse(inlined_at.get());
191 context()->module()->AddExtInstDebugInfo(std::move(inlined_at));
192 return result_id;
193 }
194
195 DebugScope DebugInfoManager::BuildDebugScope(
196 const DebugScope& callee_instr_scope,
197 DebugInlinedAtContext* inlined_at_ctx) {
198 return DebugScope(callee_instr_scope.GetLexicalScope(),
199 BuildDebugInlinedAtChain(callee_instr_scope.GetInlinedAt(),
200 inlined_at_ctx));
201 }
202
203 uint32_t DebugInfoManager::BuildDebugInlinedAtChain(
204 uint32_t callee_inlined_at, DebugInlinedAtContext* inlined_at_ctx) {
205 if (inlined_at_ctx->GetScopeOfCallInstruction().GetLexicalScope() ==
206 kNoDebugScope)
207 return kNoInlinedAt;
208
209 // Reuse the already generated DebugInlinedAt chain if exists.
210 uint32_t already_generated_chain_head_id =
211 inlined_at_ctx->GetDebugInlinedAtChain(callee_inlined_at);
212 if (already_generated_chain_head_id != kNoInlinedAt) {
213 return already_generated_chain_head_id;
214 }
215
216 const uint32_t new_dbg_inlined_at_id =
217 CreateDebugInlinedAt(inlined_at_ctx->GetLineOfCallInstruction(),
218 inlined_at_ctx->GetScopeOfCallInstruction());
219 if (new_dbg_inlined_at_id == kNoInlinedAt) return kNoInlinedAt;
220
221 if (callee_inlined_at == kNoInlinedAt) {
222 inlined_at_ctx->SetDebugInlinedAtChain(kNoInlinedAt, new_dbg_inlined_at_id);
223 return new_dbg_inlined_at_id;
224 }
225
226 uint32_t chain_head_id = kNoInlinedAt;
227 uint32_t chain_iter_id = callee_inlined_at;
228 Instruction* last_inlined_at_in_chain = nullptr;
229 do {
230 Instruction* new_inlined_at_in_chain = CloneDebugInlinedAt(
231 chain_iter_id, /* insert_before */ last_inlined_at_in_chain);
232 assert(new_inlined_at_in_chain != nullptr);
233
234 // Set DebugInlinedAt of the new scope as the head of the chain.
235 if (chain_head_id == kNoInlinedAt)
236 chain_head_id = new_inlined_at_in_chain->result_id();
237
238 // Previous DebugInlinedAt of the chain must point to the new
239 // DebugInlinedAt as its Inlined operand to build a recursive
240 // chain.
241 if (last_inlined_at_in_chain != nullptr) {
242 SetInlinedOperand(last_inlined_at_in_chain,
243 new_inlined_at_in_chain->result_id());
244 }
245 last_inlined_at_in_chain = new_inlined_at_in_chain;
246
247 chain_iter_id = GetInlinedOperand(new_inlined_at_in_chain);
248 } while (chain_iter_id != kNoInlinedAt);
249
250 // Put |new_dbg_inlined_at_id| into the end of the chain.
251 SetInlinedOperand(last_inlined_at_in_chain, new_dbg_inlined_at_id);
252
253 // Keep the new chain information that will be reused it.
254 inlined_at_ctx->SetDebugInlinedAtChain(callee_inlined_at, chain_head_id);
255 return chain_head_id;
256 }
257
258 Instruction* DebugInfoManager::GetDebugOperationWithDeref() {
259 if (deref_operation_ != nullptr) return deref_operation_;
260
261 uint32_t result_id = context()->TakeNextId();
262 std::unique_ptr<Instruction> deref_operation(new Instruction(
263 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
264 result_id,
265 {
266 {SPV_OPERAND_TYPE_ID,
267 {context()
268 ->get_feature_mgr()
269 ->GetExtInstImportId_OpenCL100DebugInfo()}},
270 {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
271 {static_cast<uint32_t>(OpenCLDebugInfo100DebugOperation)}},
272 {SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION,
273 {static_cast<uint32_t>(OpenCLDebugInfo100Deref)}},
274 }));
275
276 // Add to the front of |ext_inst_debuginfo_|.
277 deref_operation_ =
278 context()->module()->ext_inst_debuginfo_begin()->InsertBefore(
279 std::move(deref_operation));
280
281 RegisterDbgInst(deref_operation_);
282 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
283 context()->get_def_use_mgr()->AnalyzeInstDefUse(deref_operation_);
284 return deref_operation_;
285 }
286
287 Instruction* DebugInfoManager::DerefDebugExpression(Instruction* dbg_expr) {
288 assert(dbg_expr->GetOpenCL100DebugOpcode() ==
289 OpenCLDebugInfo100DebugExpression);
290 std::unique_ptr<Instruction> deref_expr(dbg_expr->Clone(context()));
291 deref_expr->SetResultId(context()->TakeNextId());
292 deref_expr->InsertOperand(
293 kDebugExpressOperandOperationIndex,
294 {SPV_OPERAND_TYPE_ID, {GetDebugOperationWithDeref()->result_id()}});
295 auto* deref_expr_instr =
296 context()->ext_inst_debuginfo_end()->InsertBefore(std::move(deref_expr));
297 AnalyzeDebugInst(deref_expr_instr);
298 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
299 context()->get_def_use_mgr()->AnalyzeInstDefUse(deref_expr_instr);
300 return deref_expr_instr;
301 }
302
303 Instruction* DebugInfoManager::GetDebugInfoNone() {
304 if (debug_info_none_inst_ != nullptr) return debug_info_none_inst_;
305
306 uint32_t result_id = context()->TakeNextId();
307 std::unique_ptr<Instruction> dbg_info_none_inst(new Instruction(
308 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
309 result_id,
310 {
311 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
312 {context()
313 ->get_feature_mgr()
314 ->GetExtInstImportId_OpenCL100DebugInfo()}},
315 {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
316 {static_cast<uint32_t>(OpenCLDebugInfo100DebugInfoNone)}},
317 }));
318
319 // Add to the front of |ext_inst_debuginfo_|.
320 debug_info_none_inst_ =
321 context()->module()->ext_inst_debuginfo_begin()->InsertBefore(
322 std::move(dbg_info_none_inst));
323
324 RegisterDbgInst(debug_info_none_inst_);
325 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
326 context()->get_def_use_mgr()->AnalyzeInstDefUse(debug_info_none_inst_);
327 return debug_info_none_inst_;
328 }
329
330 Instruction* DebugInfoManager::GetEmptyDebugExpression() {
331 if (empty_debug_expr_inst_ != nullptr) return empty_debug_expr_inst_;
332
333 uint32_t result_id = context()->TakeNextId();
334 std::unique_ptr<Instruction> empty_debug_expr(new Instruction(
335 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
336 result_id,
337 {
338 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
339 {context()
340 ->get_feature_mgr()
341 ->GetExtInstImportId_OpenCL100DebugInfo()}},
342 {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
343 {static_cast<uint32_t>(OpenCLDebugInfo100DebugExpression)}},
344 }));
345
346 // Add to the front of |ext_inst_debuginfo_|.
347 empty_debug_expr_inst_ =
348 context()->module()->ext_inst_debuginfo_begin()->InsertBefore(
349 std::move(empty_debug_expr));
350
351 RegisterDbgInst(empty_debug_expr_inst_);
352 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
353 context()->get_def_use_mgr()->AnalyzeInstDefUse(empty_debug_expr_inst_);
354 return empty_debug_expr_inst_;
355 }
356
357 Instruction* DebugInfoManager::GetDebugInlinedAt(uint32_t dbg_inlined_at_id) {
358 auto* inlined_at = GetDbgInst(dbg_inlined_at_id);
359 if (inlined_at == nullptr) return nullptr;
360 if (inlined_at->GetOpenCL100DebugOpcode() !=
361 OpenCLDebugInfo100DebugInlinedAt) {
362 return nullptr;
363 }
364 return inlined_at;
365 }
366
367 Instruction* DebugInfoManager::CloneDebugInlinedAt(uint32_t clone_inlined_at_id,
368 Instruction* insert_before) {
369 auto* inlined_at = GetDebugInlinedAt(clone_inlined_at_id);
370 if (inlined_at == nullptr) return nullptr;
371 std::unique_ptr<Instruction> new_inlined_at(inlined_at->Clone(context()));
372 new_inlined_at->SetResultId(context()->TakeNextId());
373 RegisterDbgInst(new_inlined_at.get());
374 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
375 context()->get_def_use_mgr()->AnalyzeInstDefUse(new_inlined_at.get());
376 if (insert_before != nullptr)
377 return insert_before->InsertBefore(std::move(new_inlined_at));
378 return context()->module()->ext_inst_debuginfo_end()->InsertBefore(
379 std::move(new_inlined_at));
380 }
381
382 bool DebugInfoManager::IsVariableDebugDeclared(uint32_t variable_id) {
383 auto dbg_decl_itr = var_id_to_dbg_decl_.find(variable_id);
384 return dbg_decl_itr != var_id_to_dbg_decl_.end();
385 }
386
387 bool DebugInfoManager::KillDebugDeclares(uint32_t variable_id) {
388 bool modified = false;
389 auto dbg_decl_itr = var_id_to_dbg_decl_.find(variable_id);
390 if (dbg_decl_itr != var_id_to_dbg_decl_.end()) {
391 // We intentionally copy the list of DebugDeclare instructions because
392 // context()->KillInst(dbg_decl) will update |var_id_to_dbg_decl_|. If we
393 // directly use |dbg_decl_itr->second|, it accesses a dangling pointer.
394 auto copy_dbg_decls = dbg_decl_itr->second;
395
396 for (auto* dbg_decl : copy_dbg_decls) {
397 context()->KillInst(dbg_decl);
398 modified = true;
399 }
400 var_id_to_dbg_decl_.erase(dbg_decl_itr);
401 }
402 return modified;
403 }
404
405 uint32_t DebugInfoManager::GetParentScope(uint32_t child_scope) {
406 auto dbg_scope_itr = id_to_dbg_inst_.find(child_scope);
407 assert(dbg_scope_itr != id_to_dbg_inst_.end());
408 OpenCLDebugInfo100Instructions debug_opcode =
409 dbg_scope_itr->second->GetOpenCL100DebugOpcode();
410 uint32_t parent_scope = kNoDebugScope;
411 switch (debug_opcode) {
412 case OpenCLDebugInfo100DebugFunction:
413 parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
414 kDebugFunctionOperandParentIndex);
415 break;
416 case OpenCLDebugInfo100DebugLexicalBlock:
417 parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
418 kDebugLexicalBlockOperandParentIndex);
419 break;
420 case OpenCLDebugInfo100DebugTypeComposite:
421 parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
422 kDebugTypeCompositeOperandParentIndex);
423 break;
424 case OpenCLDebugInfo100DebugCompilationUnit:
425 // DebugCompilationUnit does not have a parent scope.
426 break;
427 default:
428 assert(false &&
429 "Unreachable. A debug scope instruction must be "
430 "DebugFunction, DebugTypeComposite, DebugLexicalBlock, "
431 "or DebugCompilationUnit.");
432 break;
433 }
434 return parent_scope;
435 }
436
437 bool DebugInfoManager::IsAncestorOfScope(uint32_t scope, uint32_t ancestor) {
438 uint32_t ancestor_scope_itr = scope;
439 while (ancestor_scope_itr != kNoDebugScope) {
440 if (ancestor == ancestor_scope_itr) return true;
441 ancestor_scope_itr = GetParentScope(ancestor_scope_itr);
442 }
443 return false;
444 }
445
446 bool DebugInfoManager::IsDeclareVisibleToInstr(Instruction* dbg_declare,
447 Instruction* scope) {
448 assert(dbg_declare != nullptr);
449 assert(scope != nullptr);
450
451 std::vector<uint32_t> scope_ids;
452 if (scope->opcode() == SpvOpPhi) {
453 scope_ids.push_back(scope->GetDebugScope().GetLexicalScope());
454 for (uint32_t i = 0; i < scope->NumInOperands(); i += 2) {
455 auto* value = context()->get_def_use_mgr()->GetDef(
456 scope->GetSingleWordInOperand(i));
457 if (value != nullptr)
458 scope_ids.push_back(value->GetDebugScope().GetLexicalScope());
459 }
460 } else {
461 scope_ids.push_back(scope->GetDebugScope().GetLexicalScope());
462 }
463
464 uint32_t dbg_local_var_id =
465 dbg_declare->GetSingleWordOperand(kDebugDeclareOperandLocalVariableIndex);
466 auto dbg_local_var_itr = id_to_dbg_inst_.find(dbg_local_var_id);
467 assert(dbg_local_var_itr != id_to_dbg_inst_.end());
468 uint32_t decl_scope_id = dbg_local_var_itr->second->GetSingleWordOperand(
469 kDebugLocalVariableOperandParentIndex);
470
471 // If the scope of DebugDeclare is an ancestor scope of the instruction's
472 // scope, the local variable is visible to the instruction.
473 for (uint32_t scope_id : scope_ids) {
474 if (scope_id != kNoDebugScope &&
475 IsAncestorOfScope(scope_id, decl_scope_id)) {
476 return true;
477 }
478 }
479 return false;
480 }
481
482 Instruction* DebugInfoManager::AddDebugValueWithIndex(
483 uint32_t dbg_local_var_id, uint32_t value_id, uint32_t expr_id,
484 uint32_t index_id, Instruction* insert_before) {
485 uint32_t result_id = context()->TakeNextId();
486 if (!result_id) return nullptr;
487 std::unique_ptr<Instruction> new_dbg_value(new Instruction(
488 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
489 result_id,
490 {
491 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
492 {context()
493 ->get_feature_mgr()
494 ->GetExtInstImportId_OpenCL100DebugInfo()}},
495 {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
496 {static_cast<uint32_t>(OpenCLDebugInfo100DebugValue)}},
497 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {dbg_local_var_id}},
498 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {value_id}},
499 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
500 {expr_id == 0 ? GetEmptyDebugExpression()->result_id() : expr_id}},
501 }));
502 if (index_id) {
503 new_dbg_value->AddOperand(
504 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {index_id}});
505 }
506
507 Instruction* added_dbg_value =
508 insert_before->InsertBefore(std::move(new_dbg_value));
509 AnalyzeDebugInst(added_dbg_value);
510 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
511 context()->get_def_use_mgr()->AnalyzeInstDefUse(added_dbg_value);
512 if (context()->AreAnalysesValid(
513 IRContext::Analysis::kAnalysisInstrToBlockMapping)) {
514 auto insert_blk = context()->get_instr_block(insert_before);
515 context()->set_instr_block(added_dbg_value, insert_blk);
516 }
517 return added_dbg_value;
518 }
519
520 bool DebugInfoManager::AddDebugValueIfVarDeclIsVisible(
521 Instruction* scope_and_line, uint32_t variable_id, uint32_t value_id,
522 Instruction* insert_pos,
523 std::unordered_set<Instruction*>* invisible_decls) {
524 assert(scope_and_line != nullptr);
525
526 auto dbg_decl_itr = var_id_to_dbg_decl_.find(variable_id);
527 if (dbg_decl_itr == var_id_to_dbg_decl_.end()) return false;
528
529 bool modified = false;
530 for (auto* dbg_decl_or_val : dbg_decl_itr->second) {
531 if (!IsDeclareVisibleToInstr(dbg_decl_or_val, scope_and_line)) {
532 if (invisible_decls) invisible_decls->insert(dbg_decl_or_val);
533 continue;
534 }
535
536 // Avoid inserting the new DebugValue between OpPhi or OpVariable
537 // instructions.
538 Instruction* insert_before = insert_pos->NextNode();
539 while (insert_before->opcode() == SpvOpPhi ||
540 insert_before->opcode() == SpvOpVariable) {
541 insert_before = insert_before->NextNode();
542 }
543
544 uint32_t index_id = 0;
545 if (dbg_decl_or_val->NumOperands() > kDebugValueOperandIndexesIndex) {
546 index_id =
547 dbg_decl_or_val->GetSingleWordOperand(kDebugValueOperandIndexesIndex);
548 }
549
550 Instruction* added_dbg_value =
551 AddDebugValueWithIndex(dbg_decl_or_val->GetSingleWordOperand(
552 kDebugValueOperandLocalVariableIndex),
553 value_id, 0, index_id, insert_before);
554 assert(added_dbg_value != nullptr);
555 added_dbg_value->UpdateDebugInfoFrom(scope_and_line);
556 AnalyzeDebugInst(added_dbg_value);
557 modified = true;
558 }
559 return modified;
560 }
561
562 bool DebugInfoManager::AddDebugValueForDecl(Instruction* dbg_decl,
563 uint32_t value_id) {
564 if (dbg_decl == nullptr || !IsDebugDeclare(dbg_decl)) return false;
565
566 std::unique_ptr<Instruction> dbg_val(dbg_decl->Clone(context()));
567 dbg_val->SetResultId(context()->TakeNextId());
568 dbg_val->SetInOperand(kExtInstInstructionInIdx,
569 {OpenCLDebugInfo100DebugValue});
570 dbg_val->SetOperand(kDebugDeclareOperandVariableIndex, {value_id});
571 dbg_val->SetOperand(kDebugValueOperandExpressionIndex,
572 {GetEmptyDebugExpression()->result_id()});
573
574 auto* added_dbg_val = dbg_decl->InsertBefore(std::move(dbg_val));
575 AnalyzeDebugInst(added_dbg_val);
576 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
577 context()->get_def_use_mgr()->AnalyzeInstDefUse(added_dbg_val);
578 if (context()->AreAnalysesValid(
579 IRContext::Analysis::kAnalysisInstrToBlockMapping)) {
580 auto insert_blk = context()->get_instr_block(dbg_decl);
581 context()->set_instr_block(added_dbg_val, insert_blk);
582 }
583 return true;
584 }
585
586 uint32_t DebugInfoManager::GetVariableIdOfDebugValueUsedForDeclare(
587 Instruction* inst) {
588 if (inst->GetOpenCL100DebugOpcode() != OpenCLDebugInfo100DebugValue) return 0;
589
590 auto* expr =
591 GetDbgInst(inst->GetSingleWordOperand(kDebugValueOperandExpressionIndex));
592 if (expr == nullptr) return 0;
593 if (expr->NumOperands() != kDebugExpressOperandOperationIndex + 1) return 0;
594
595 auto* operation = GetDbgInst(
596 expr->GetSingleWordOperand(kDebugExpressOperandOperationIndex));
597 if (operation == nullptr) return 0;
598 if (operation->GetSingleWordOperand(kDebugOperationOperandOperationIndex) !=
599 OpenCLDebugInfo100Deref) {
600 return 0;
601 }
602
603 uint32_t var_id =
604 inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
605 if (!context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse)) {
606 assert(false &&
607 "Checking a DebugValue can be used for declare needs DefUseManager");
608 return 0;
609 }
610
611 auto* var = context()->get_def_use_mgr()->GetDef(var_id);
612 if (var->opcode() == SpvOpVariable &&
613 SpvStorageClass(var->GetSingleWordOperand(
614 kOpVariableOperandStorageClassIndex)) == SpvStorageClassFunction) {
615 return var_id;
616 }
617 return 0;
618 }
619
620 bool DebugInfoManager::IsDebugDeclare(Instruction* instr) {
621 if (!instr->IsOpenCL100DebugInstr()) return false;
622 return instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugDeclare ||
623 GetVariableIdOfDebugValueUsedForDeclare(instr) != 0;
624 }
625
626 void DebugInfoManager::ReplaceAllUsesInDebugScopeWithPredicate(
627 uint32_t before, uint32_t after,
628 const std::function<bool(Instruction*)>& predicate) {
629 auto scope_id_to_users_itr = scope_id_to_users_.find(before);
630 if (scope_id_to_users_itr != scope_id_to_users_.end()) {
631 for (Instruction* inst : scope_id_to_users_itr->second) {
632 if (predicate(inst)) inst->UpdateLexicalScope(after);
633 }
634 scope_id_to_users_[after] = scope_id_to_users_itr->second;
635 scope_id_to_users_.erase(scope_id_to_users_itr);
636 }
637 auto inlinedat_id_to_users_itr = inlinedat_id_to_users_.find(before);
638 if (inlinedat_id_to_users_itr != inlinedat_id_to_users_.end()) {
639 for (Instruction* inst : inlinedat_id_to_users_itr->second) {
640 if (predicate(inst)) inst->UpdateDebugInlinedAt(after);
641 }
642 inlinedat_id_to_users_[after] = inlinedat_id_to_users_itr->second;
643 inlinedat_id_to_users_.erase(inlinedat_id_to_users_itr);
644 }
645 }
646
647 void DebugInfoManager::ClearDebugScopeAndInlinedAtUses(Instruction* inst) {
648 auto scope_id_to_users_itr = scope_id_to_users_.find(inst->result_id());
649 if (scope_id_to_users_itr != scope_id_to_users_.end()) {
650 scope_id_to_users_.erase(scope_id_to_users_itr);
651 }
652 auto inlinedat_id_to_users_itr =
653 inlinedat_id_to_users_.find(inst->result_id());
654 if (inlinedat_id_to_users_itr != inlinedat_id_to_users_.end()) {
655 inlinedat_id_to_users_.erase(inlinedat_id_to_users_itr);
656 }
657 }
658
659 void DebugInfoManager::AnalyzeDebugInst(Instruction* inst) {
660 if (inst->GetDebugScope().GetLexicalScope() != kNoDebugScope) {
661 auto& users = scope_id_to_users_[inst->GetDebugScope().GetLexicalScope()];
662 users.insert(inst);
663 }
664 if (inst->GetDebugInlinedAt() != kNoInlinedAt) {
665 auto& users = inlinedat_id_to_users_[inst->GetDebugInlinedAt()];
666 users.insert(inst);
667 }
668
669 if (!inst->IsOpenCL100DebugInstr()) return;
670
671 RegisterDbgInst(inst);
672
673 if (inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction) {
674 assert(GetDebugFunction(inst->GetSingleWordOperand(
675 kDebugFunctionOperandFunctionIndex)) == nullptr &&
676 "Two DebugFunction instruction exists for a single OpFunction.");
677 RegisterDbgFunction(inst);
678 }
679
680 if (deref_operation_ == nullptr &&
681 inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugOperation &&
682 inst->GetSingleWordOperand(kDebugOperationOperandOperationIndex) ==
683 OpenCLDebugInfo100Deref) {
684 deref_operation_ = inst;
685 }
686
687 if (debug_info_none_inst_ == nullptr &&
688 inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugInfoNone) {
689 debug_info_none_inst_ = inst;
690 }
691
692 if (empty_debug_expr_inst_ == nullptr && IsEmptyDebugExpression(inst)) {
693 empty_debug_expr_inst_ = inst;
694 }
695
696 if (inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugDeclare) {
697 uint32_t var_id =
698 inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
699 RegisterDbgDeclare(var_id, inst);
700 }
701
702 if (uint32_t var_id = GetVariableIdOfDebugValueUsedForDeclare(inst)) {
703 RegisterDbgDeclare(var_id, inst);
704 }
705 }
706
707 void DebugInfoManager::ConvertDebugGlobalToLocalVariable(
708 Instruction* dbg_global_var, Instruction* local_var) {
709 if (dbg_global_var->GetOpenCL100DebugOpcode() !=
710 OpenCLDebugInfo100DebugGlobalVariable) {
711 return;
712 }
713 assert(local_var->opcode() == SpvOpVariable ||
714 local_var->opcode() == SpvOpFunctionParameter);
715
716 // Convert |dbg_global_var| to DebugLocalVariable
717 dbg_global_var->SetInOperand(kExtInstInstructionInIdx,
718 {OpenCLDebugInfo100DebugLocalVariable});
719 auto flags = dbg_global_var->GetSingleWordOperand(
720 kDebugGlobalVariableOperandFlagsIndex);
721 for (uint32_t i = dbg_global_var->NumInOperands() - 1;
722 i >= kDebugLocalVariableOperandFlagsIndex; --i) {
723 dbg_global_var->RemoveOperand(i);
724 }
725 dbg_global_var->SetOperand(kDebugLocalVariableOperandFlagsIndex, {flags});
726 context()->ForgetUses(dbg_global_var);
727 context()->AnalyzeUses(dbg_global_var);
728
729 // Create a DebugDeclare
730 std::unique_ptr<Instruction> new_dbg_decl(new Instruction(
731 context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
732 context()->TakeNextId(),
733 {
734 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
735 {context()
736 ->get_feature_mgr()
737 ->GetExtInstImportId_OpenCL100DebugInfo()}},
738 {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
739 {static_cast<uint32_t>(OpenCLDebugInfo100DebugDeclare)}},
740 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
741 {dbg_global_var->result_id()}},
742 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {local_var->result_id()}},
743 {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
744 {GetEmptyDebugExpression()->result_id()}},
745 }));
746 auto* added_dbg_decl =
747 local_var->NextNode()->InsertBefore(std::move(new_dbg_decl));
748 if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
749 context()->get_def_use_mgr()->AnalyzeInstDefUse(added_dbg_decl);
750 if (context()->AreAnalysesValid(
751 IRContext::Analysis::kAnalysisInstrToBlockMapping)) {
752 auto insert_blk = context()->get_instr_block(local_var);
753 context()->set_instr_block(added_dbg_decl, insert_blk);
754 }
755 }
756
757 void DebugInfoManager::AnalyzeDebugInsts(Module& module) {
758 deref_operation_ = nullptr;
759 debug_info_none_inst_ = nullptr;
760 empty_debug_expr_inst_ = nullptr;
761 module.ForEachInst([this](Instruction* cpi) { AnalyzeDebugInst(cpi); });
762
763 // Move |empty_debug_expr_inst_| to the beginning of the debug instruction
764 // list.
765 if (empty_debug_expr_inst_ != nullptr &&
766 empty_debug_expr_inst_->PreviousNode() != nullptr &&
767 empty_debug_expr_inst_->PreviousNode()->IsOpenCL100DebugInstr()) {
768 empty_debug_expr_inst_->InsertBefore(
769 &*context()->module()->ext_inst_debuginfo_begin());
770 }
771
772 // Move |debug_info_none_inst_| to the beginning of the debug instruction
773 // list.
774 if (debug_info_none_inst_ != nullptr &&
775 debug_info_none_inst_->PreviousNode() != nullptr &&
776 debug_info_none_inst_->PreviousNode()->IsOpenCL100DebugInstr()) {
777 debug_info_none_inst_->InsertBefore(
778 &*context()->module()->ext_inst_debuginfo_begin());
779 }
780 }
781
782 void DebugInfoManager::ClearDebugInfo(Instruction* instr) {
783 auto scope_id_to_users_itr =
784 scope_id_to_users_.find(instr->GetDebugScope().GetLexicalScope());
785 if (scope_id_to_users_itr != scope_id_to_users_.end()) {
786 scope_id_to_users_itr->second.erase(instr);
787 }
788 auto inlinedat_id_to_users_itr =
789 inlinedat_id_to_users_.find(instr->GetDebugInlinedAt());
790 if (inlinedat_id_to_users_itr != inlinedat_id_to_users_.end()) {
791 inlinedat_id_to_users_itr->second.erase(instr);
792 }
793
794 if (instr == nullptr || !instr->IsOpenCL100DebugInstr()) {
795 return;
796 }
797
798 id_to_dbg_inst_.erase(instr->result_id());
799
800 if (instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction) {
801 auto fn_id =
802 instr->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex);
803 fn_id_to_dbg_fn_.erase(fn_id);
804 }
805
806 if (instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugDeclare ||
807 instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugValue) {
808 auto var_or_value_id =
809 instr->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
810 auto dbg_decl_itr = var_id_to_dbg_decl_.find(var_or_value_id);
811 if (dbg_decl_itr != var_id_to_dbg_decl_.end()) {
812 dbg_decl_itr->second.erase(instr);
813 }
814 }
815
816 if (deref_operation_ == instr) {
817 deref_operation_ = nullptr;
818 for (auto dbg_instr_itr = context()->module()->ext_inst_debuginfo_begin();
819 dbg_instr_itr != context()->module()->ext_inst_debuginfo_end();
820 ++dbg_instr_itr) {
821 if (instr != &*dbg_instr_itr &&
822 dbg_instr_itr->GetOpenCL100DebugOpcode() ==
823 OpenCLDebugInfo100DebugOperation &&
824 dbg_instr_itr->GetSingleWordOperand(
825 kDebugOperationOperandOperationIndex) ==
826 OpenCLDebugInfo100Deref) {
827 deref_operation_ = &*dbg_instr_itr;
828 break;
829 }
830 }
831 }
832
833 if (debug_info_none_inst_ == instr) {
834 debug_info_none_inst_ = nullptr;
835 for (auto dbg_instr_itr = context()->module()->ext_inst_debuginfo_begin();
836 dbg_instr_itr != context()->module()->ext_inst_debuginfo_end();
837 ++dbg_instr_itr) {
838 if (instr != &*dbg_instr_itr &&
839 dbg_instr_itr->GetOpenCL100DebugOpcode() ==
840 OpenCLDebugInfo100DebugInfoNone) {
841 debug_info_none_inst_ = &*dbg_instr_itr;
842 break;
843 }
844 }
845 }
846
847 if (empty_debug_expr_inst_ == instr) {
848 empty_debug_expr_inst_ = nullptr;
849 for (auto dbg_instr_itr = context()->module()->ext_inst_debuginfo_begin();
850 dbg_instr_itr != context()->module()->ext_inst_debuginfo_end();
851 ++dbg_instr_itr) {
852 if (instr != &*dbg_instr_itr && IsEmptyDebugExpression(&*dbg_instr_itr)) {
853 empty_debug_expr_inst_ = &*dbg_instr_itr;
854 break;
855 }
856 }
857 }
858 }
859
860 } // namespace analysis
861 } // namespace opt
862 } // namespace spvtools