debug_info_manager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. #include "source/opt/debug_info_manager.h"
  15. #include <cassert>
  16. #include "source/opt/ir_context.h"
  17. // Constants for OpenCL.DebugInfo.100 extension instructions.
  18. static const uint32_t kOpLineOperandLineIndex = 1;
  19. static const uint32_t kLineOperandIndexDebugFunction = 7;
  20. static const uint32_t kLineOperandIndexDebugLexicalBlock = 5;
  21. static const uint32_t kDebugFunctionOperandFunctionIndex = 13;
  22. static const uint32_t kDebugFunctionOperandParentIndex = 9;
  23. static const uint32_t kDebugTypeCompositeOperandParentIndex = 9;
  24. static const uint32_t kDebugLexicalBlockOperandParentIndex = 7;
  25. static const uint32_t kDebugInlinedAtOperandInlinedIndex = 6;
  26. static const uint32_t kDebugExpressOperandOperationIndex = 4;
  27. static const uint32_t kDebugDeclareOperandLocalVariableIndex = 4;
  28. static const uint32_t kDebugDeclareOperandVariableIndex = 5;
  29. static const uint32_t kDebugValueOperandLocalVariableIndex = 4;
  30. static const uint32_t kDebugValueOperandExpressionIndex = 6;
  31. static const uint32_t kDebugOperationOperandOperationIndex = 4;
  32. static const uint32_t kOpVariableOperandStorageClassIndex = 2;
  33. static const uint32_t kDebugLocalVariableOperandParentIndex = 9;
  34. namespace spvtools {
  35. namespace opt {
  36. namespace analysis {
  37. namespace {
  38. void SetInlinedOperand(Instruction* dbg_inlined_at, uint32_t inlined_operand) {
  39. assert(dbg_inlined_at);
  40. assert(dbg_inlined_at->GetOpenCL100DebugOpcode() ==
  41. OpenCLDebugInfo100DebugInlinedAt);
  42. if (dbg_inlined_at->NumOperands() <= kDebugInlinedAtOperandInlinedIndex) {
  43. dbg_inlined_at->AddOperand(
  44. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inlined_operand}});
  45. } else {
  46. dbg_inlined_at->SetOperand(kDebugInlinedAtOperandInlinedIndex,
  47. {inlined_operand});
  48. }
  49. }
  50. uint32_t GetInlinedOperand(Instruction* dbg_inlined_at) {
  51. assert(dbg_inlined_at);
  52. assert(dbg_inlined_at->GetOpenCL100DebugOpcode() ==
  53. OpenCLDebugInfo100DebugInlinedAt);
  54. if (dbg_inlined_at->NumOperands() <= kDebugInlinedAtOperandInlinedIndex)
  55. return kNoInlinedAt;
  56. return dbg_inlined_at->GetSingleWordOperand(
  57. kDebugInlinedAtOperandInlinedIndex);
  58. }
  59. bool IsEmptyDebugExpression(Instruction* instr) {
  60. return instr->GetOpenCL100DebugOpcode() ==
  61. OpenCLDebugInfo100DebugExpression &&
  62. instr->NumOperands() == kDebugExpressOperandOperationIndex;
  63. }
  64. } // namespace
  65. DebugInfoManager::DebugInfoManager(IRContext* c) : context_(c) {
  66. AnalyzeDebugInsts(*c->module());
  67. }
  68. Instruction* DebugInfoManager::GetDbgInst(uint32_t id) {
  69. auto dbg_inst_it = id_to_dbg_inst_.find(id);
  70. return dbg_inst_it == id_to_dbg_inst_.end() ? nullptr : dbg_inst_it->second;
  71. }
  72. void DebugInfoManager::RegisterDbgInst(Instruction* inst) {
  73. assert(
  74. inst->NumInOperands() != 0 &&
  75. context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo() ==
  76. inst->GetInOperand(0).words[0] &&
  77. "Given instruction is not a debug instruction");
  78. id_to_dbg_inst_[inst->result_id()] = inst;
  79. }
  80. void DebugInfoManager::RegisterDbgFunction(Instruction* inst) {
  81. assert(inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction &&
  82. "inst is not a DebugFunction");
  83. auto fn_id = inst->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex);
  84. assert(
  85. fn_id_to_dbg_fn_.find(fn_id) == fn_id_to_dbg_fn_.end() &&
  86. "Register DebugFunction for a function that already has DebugFunction");
  87. fn_id_to_dbg_fn_[fn_id] = inst;
  88. }
  89. void DebugInfoManager::RegisterDbgDeclare(uint32_t var_id,
  90. Instruction* dbg_declare) {
  91. assert(dbg_declare->GetOpenCL100DebugOpcode() ==
  92. OpenCLDebugInfo100DebugDeclare ||
  93. dbg_declare->GetOpenCL100DebugOpcode() ==
  94. OpenCLDebugInfo100DebugValue);
  95. auto dbg_decl_itr = var_id_to_dbg_decl_.find(var_id);
  96. if (dbg_decl_itr == var_id_to_dbg_decl_.end()) {
  97. var_id_to_dbg_decl_[var_id] = {dbg_declare};
  98. } else {
  99. dbg_decl_itr->second.insert(dbg_declare);
  100. }
  101. }
  102. uint32_t DebugInfoManager::CreateDebugInlinedAt(const Instruction* line,
  103. const DebugScope& scope) {
  104. if (context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo() ==
  105. 0)
  106. return kNoInlinedAt;
  107. uint32_t line_number = 0;
  108. if (line == nullptr) {
  109. auto* lexical_scope_inst = GetDbgInst(scope.GetLexicalScope());
  110. if (lexical_scope_inst == nullptr) return kNoInlinedAt;
  111. OpenCLDebugInfo100Instructions debug_opcode =
  112. lexical_scope_inst->GetOpenCL100DebugOpcode();
  113. switch (debug_opcode) {
  114. case OpenCLDebugInfo100DebugFunction:
  115. line_number = lexical_scope_inst->GetSingleWordOperand(
  116. kLineOperandIndexDebugFunction);
  117. break;
  118. case OpenCLDebugInfo100DebugLexicalBlock:
  119. line_number = lexical_scope_inst->GetSingleWordOperand(
  120. kLineOperandIndexDebugLexicalBlock);
  121. break;
  122. case OpenCLDebugInfo100DebugTypeComposite:
  123. case OpenCLDebugInfo100DebugCompilationUnit:
  124. assert(false &&
  125. "DebugTypeComposite and DebugCompilationUnit are lexical "
  126. "scopes, but we inline functions into a function or a block "
  127. "of a function, not into a struct/class or a global scope.");
  128. break;
  129. default:
  130. assert(false &&
  131. "Unreachable. a debug extension instruction for a "
  132. "lexical scope must be DebugFunction, DebugTypeComposite, "
  133. "DebugLexicalBlock, or DebugCompilationUnit.");
  134. break;
  135. }
  136. } else {
  137. line_number = line->GetSingleWordOperand(kOpLineOperandLineIndex);
  138. }
  139. uint32_t result_id = context()->TakeNextId();
  140. std::unique_ptr<Instruction> inlined_at(new Instruction(
  141. context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
  142. result_id,
  143. {
  144. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  145. {context()
  146. ->get_feature_mgr()
  147. ->GetExtInstImportId_OpenCL100DebugInfo()}},
  148. {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
  149. {static_cast<uint32_t>(OpenCLDebugInfo100DebugInlinedAt)}},
  150. {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {line_number}},
  151. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {scope.GetLexicalScope()}},
  152. }));
  153. // |scope| already has DebugInlinedAt. We put the existing DebugInlinedAt
  154. // into the Inlined operand of this new DebugInlinedAt.
  155. if (scope.GetInlinedAt() != kNoInlinedAt) {
  156. inlined_at->AddOperand(
  157. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {scope.GetInlinedAt()}});
  158. }
  159. RegisterDbgInst(inlined_at.get());
  160. if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
  161. context()->get_def_use_mgr()->AnalyzeInstDefUse(inlined_at.get());
  162. context()->module()->AddExtInstDebugInfo(std::move(inlined_at));
  163. return result_id;
  164. }
  165. DebugScope DebugInfoManager::BuildDebugScope(
  166. const DebugScope& callee_instr_scope,
  167. DebugInlinedAtContext* inlined_at_ctx) {
  168. return DebugScope(callee_instr_scope.GetLexicalScope(),
  169. BuildDebugInlinedAtChain(callee_instr_scope.GetInlinedAt(),
  170. inlined_at_ctx));
  171. }
  172. uint32_t DebugInfoManager::BuildDebugInlinedAtChain(
  173. uint32_t callee_inlined_at, DebugInlinedAtContext* inlined_at_ctx) {
  174. if (inlined_at_ctx->GetScopeOfCallInstruction().GetLexicalScope() ==
  175. kNoDebugScope)
  176. return kNoInlinedAt;
  177. // Reuse the already generated DebugInlinedAt chain if exists.
  178. uint32_t already_generated_chain_head_id =
  179. inlined_at_ctx->GetDebugInlinedAtChain(callee_inlined_at);
  180. if (already_generated_chain_head_id != kNoInlinedAt) {
  181. return already_generated_chain_head_id;
  182. }
  183. const uint32_t new_dbg_inlined_at_id =
  184. CreateDebugInlinedAt(inlined_at_ctx->GetLineOfCallInstruction(),
  185. inlined_at_ctx->GetScopeOfCallInstruction());
  186. if (new_dbg_inlined_at_id == kNoInlinedAt) return kNoInlinedAt;
  187. if (callee_inlined_at == kNoInlinedAt) {
  188. inlined_at_ctx->SetDebugInlinedAtChain(kNoInlinedAt, new_dbg_inlined_at_id);
  189. return new_dbg_inlined_at_id;
  190. }
  191. uint32_t chain_head_id = kNoInlinedAt;
  192. uint32_t chain_iter_id = callee_inlined_at;
  193. Instruction* last_inlined_at_in_chain = nullptr;
  194. do {
  195. Instruction* new_inlined_at_in_chain = CloneDebugInlinedAt(
  196. chain_iter_id, /* insert_before */ last_inlined_at_in_chain);
  197. assert(new_inlined_at_in_chain != nullptr);
  198. // Set DebugInlinedAt of the new scope as the head of the chain.
  199. if (chain_head_id == kNoInlinedAt)
  200. chain_head_id = new_inlined_at_in_chain->result_id();
  201. // Previous DebugInlinedAt of the chain must point to the new
  202. // DebugInlinedAt as its Inlined operand to build a recursive
  203. // chain.
  204. if (last_inlined_at_in_chain != nullptr) {
  205. SetInlinedOperand(last_inlined_at_in_chain,
  206. new_inlined_at_in_chain->result_id());
  207. }
  208. last_inlined_at_in_chain = new_inlined_at_in_chain;
  209. chain_iter_id = GetInlinedOperand(new_inlined_at_in_chain);
  210. } while (chain_iter_id != kNoInlinedAt);
  211. // Put |new_dbg_inlined_at_id| into the end of the chain.
  212. SetInlinedOperand(last_inlined_at_in_chain, new_dbg_inlined_at_id);
  213. // Keep the new chain information that will be reused it.
  214. inlined_at_ctx->SetDebugInlinedAtChain(callee_inlined_at, chain_head_id);
  215. return chain_head_id;
  216. }
  217. Instruction* DebugInfoManager::GetDebugInfoNone() {
  218. if (debug_info_none_inst_ != nullptr) return debug_info_none_inst_;
  219. uint32_t result_id = context()->TakeNextId();
  220. std::unique_ptr<Instruction> dbg_info_none_inst(new Instruction(
  221. context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
  222. result_id,
  223. {
  224. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  225. {context()
  226. ->get_feature_mgr()
  227. ->GetExtInstImportId_OpenCL100DebugInfo()}},
  228. {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
  229. {static_cast<uint32_t>(OpenCLDebugInfo100DebugInfoNone)}},
  230. }));
  231. // Add to the front of |ext_inst_debuginfo_|.
  232. debug_info_none_inst_ =
  233. context()->module()->ext_inst_debuginfo_begin()->InsertBefore(
  234. std::move(dbg_info_none_inst));
  235. RegisterDbgInst(debug_info_none_inst_);
  236. if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
  237. context()->get_def_use_mgr()->AnalyzeInstDefUse(debug_info_none_inst_);
  238. return debug_info_none_inst_;
  239. }
  240. Instruction* DebugInfoManager::GetEmptyDebugExpression() {
  241. if (empty_debug_expr_inst_ != nullptr) return empty_debug_expr_inst_;
  242. uint32_t result_id = context()->TakeNextId();
  243. std::unique_ptr<Instruction> empty_debug_expr(new Instruction(
  244. context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
  245. result_id,
  246. {
  247. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  248. {context()
  249. ->get_feature_mgr()
  250. ->GetExtInstImportId_OpenCL100DebugInfo()}},
  251. {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
  252. {static_cast<uint32_t>(OpenCLDebugInfo100DebugExpression)}},
  253. }));
  254. // Add to the front of |ext_inst_debuginfo_|.
  255. empty_debug_expr_inst_ =
  256. context()->module()->ext_inst_debuginfo_begin()->InsertBefore(
  257. std::move(empty_debug_expr));
  258. RegisterDbgInst(empty_debug_expr_inst_);
  259. if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
  260. context()->get_def_use_mgr()->AnalyzeInstDefUse(empty_debug_expr_inst_);
  261. return empty_debug_expr_inst_;
  262. }
  263. Instruction* DebugInfoManager::GetDebugInlinedAt(uint32_t dbg_inlined_at_id) {
  264. auto* inlined_at = GetDbgInst(dbg_inlined_at_id);
  265. if (inlined_at == nullptr) return nullptr;
  266. if (inlined_at->GetOpenCL100DebugOpcode() !=
  267. OpenCLDebugInfo100DebugInlinedAt) {
  268. return nullptr;
  269. }
  270. return inlined_at;
  271. }
  272. Instruction* DebugInfoManager::CloneDebugInlinedAt(uint32_t clone_inlined_at_id,
  273. Instruction* insert_before) {
  274. auto* inlined_at = GetDebugInlinedAt(clone_inlined_at_id);
  275. if (inlined_at == nullptr) return nullptr;
  276. std::unique_ptr<Instruction> new_inlined_at(inlined_at->Clone(context()));
  277. new_inlined_at->SetResultId(context()->TakeNextId());
  278. RegisterDbgInst(new_inlined_at.get());
  279. if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
  280. context()->get_def_use_mgr()->AnalyzeInstDefUse(new_inlined_at.get());
  281. if (insert_before != nullptr)
  282. return insert_before->InsertBefore(std::move(new_inlined_at));
  283. return context()->module()->ext_inst_debuginfo_end()->InsertBefore(
  284. std::move(new_inlined_at));
  285. }
  286. uint32_t DebugInfoManager::GetParentScope(uint32_t child_scope) {
  287. auto dbg_scope_itr = id_to_dbg_inst_.find(child_scope);
  288. assert(dbg_scope_itr != id_to_dbg_inst_.end());
  289. OpenCLDebugInfo100Instructions debug_opcode =
  290. dbg_scope_itr->second->GetOpenCL100DebugOpcode();
  291. uint32_t parent_scope = kNoDebugScope;
  292. switch (debug_opcode) {
  293. case OpenCLDebugInfo100DebugFunction:
  294. parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
  295. kDebugFunctionOperandParentIndex);
  296. break;
  297. case OpenCLDebugInfo100DebugLexicalBlock:
  298. parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
  299. kDebugLexicalBlockOperandParentIndex);
  300. break;
  301. case OpenCLDebugInfo100DebugTypeComposite:
  302. parent_scope = dbg_scope_itr->second->GetSingleWordOperand(
  303. kDebugTypeCompositeOperandParentIndex);
  304. break;
  305. case OpenCLDebugInfo100DebugCompilationUnit:
  306. // DebugCompilationUnit does not have a parent scope.
  307. break;
  308. default:
  309. assert(false &&
  310. "Unreachable. A debug scope instruction must be "
  311. "DebugFunction, DebugTypeComposite, DebugLexicalBlock, "
  312. "or DebugCompilationUnit.");
  313. break;
  314. }
  315. return parent_scope;
  316. }
  317. bool DebugInfoManager::IsAncestorOfScope(uint32_t scope, uint32_t ancestor) {
  318. uint32_t ancestor_scope_itr = scope;
  319. while (ancestor_scope_itr != kNoDebugScope) {
  320. if (ancestor == ancestor_scope_itr) return true;
  321. ancestor_scope_itr = GetParentScope(ancestor_scope_itr);
  322. }
  323. return false;
  324. }
  325. bool DebugInfoManager::IsDeclareVisibleToInstr(Instruction* dbg_declare,
  326. uint32_t instr_scope_id) {
  327. if (instr_scope_id == kNoDebugScope) return false;
  328. uint32_t dbg_local_var_id =
  329. dbg_declare->GetSingleWordOperand(kDebugDeclareOperandLocalVariableIndex);
  330. auto dbg_local_var_itr = id_to_dbg_inst_.find(dbg_local_var_id);
  331. assert(dbg_local_var_itr != id_to_dbg_inst_.end());
  332. uint32_t decl_scope_id = dbg_local_var_itr->second->GetSingleWordOperand(
  333. kDebugLocalVariableOperandParentIndex);
  334. // If the scope of DebugDeclare is an ancestor scope of the instruction's
  335. // scope, the local variable is visible to the instruction.
  336. return IsAncestorOfScope(instr_scope_id, decl_scope_id);
  337. }
  338. void DebugInfoManager::AddDebugValue(Instruction* scope_and_line,
  339. uint32_t variable_id, uint32_t value_id,
  340. Instruction* insert_pos) {
  341. auto dbg_decl_itr = var_id_to_dbg_decl_.find(variable_id);
  342. if (dbg_decl_itr == var_id_to_dbg_decl_.end()) return;
  343. uint32_t instr_scope_id = scope_and_line->GetDebugScope().GetLexicalScope();
  344. for (auto* dbg_decl_or_val : dbg_decl_itr->second) {
  345. if (!IsDeclareVisibleToInstr(dbg_decl_or_val, instr_scope_id)) continue;
  346. uint32_t result_id = context()->TakeNextId();
  347. std::unique_ptr<Instruction> new_dbg_value(new Instruction(
  348. context(), SpvOpExtInst, context()->get_type_mgr()->GetVoidTypeId(),
  349. result_id,
  350. {
  351. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  352. {context()
  353. ->get_feature_mgr()
  354. ->GetExtInstImportId_OpenCL100DebugInfo()}},
  355. {spv_operand_type_t::SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
  356. {static_cast<uint32_t>(OpenCLDebugInfo100DebugValue)}},
  357. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  358. {dbg_decl_or_val->GetSingleWordOperand(
  359. kDebugValueOperandLocalVariableIndex)}},
  360. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {value_id}},
  361. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  362. {GetEmptyDebugExpression()->result_id()}},
  363. }));
  364. if (dbg_decl_or_val->NumOperands() >
  365. kDebugValueOperandExpressionIndex + 1) {
  366. assert(dbg_decl_or_val->GetOpenCL100DebugOpcode() ==
  367. OpenCLDebugInfo100DebugValue);
  368. for (uint32_t i = kDebugValueOperandExpressionIndex + 1;
  369. i < dbg_decl_or_val->NumOperands(); ++i) {
  370. new_dbg_value->AddOperand({spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  371. {dbg_decl_or_val->GetSingleWordOperand(i)}});
  372. }
  373. }
  374. // Avoid inserting the new DebugValue between OpPhi or OpVariable
  375. // instructions.
  376. Instruction* insert_before = insert_pos->NextNode();
  377. while (insert_before->opcode() == SpvOpPhi ||
  378. insert_before->opcode() == SpvOpVariable) {
  379. insert_before = insert_before->NextNode();
  380. }
  381. Instruction* added_dbg_value =
  382. insert_before->InsertBefore(std::move(new_dbg_value));
  383. added_dbg_value->UpdateDebugInfo(scope_and_line);
  384. AnalyzeDebugInst(added_dbg_value);
  385. if (context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse))
  386. context()->get_def_use_mgr()->AnalyzeInstDefUse(added_dbg_value);
  387. }
  388. }
  389. uint32_t DebugInfoManager::GetVariableIdOfDebugValueUsedForDeclare(
  390. Instruction* inst) {
  391. if (inst->GetOpenCL100DebugOpcode() != OpenCLDebugInfo100DebugValue) return 0;
  392. auto* expr =
  393. GetDbgInst(inst->GetSingleWordOperand(kDebugValueOperandExpressionIndex));
  394. if (expr == nullptr) return 0;
  395. if (expr->NumOperands() != kDebugExpressOperandOperationIndex + 1) return 0;
  396. auto* operation = GetDbgInst(
  397. expr->GetSingleWordOperand(kDebugExpressOperandOperationIndex));
  398. if (operation == nullptr) return 0;
  399. if (operation->GetSingleWordOperand(kDebugOperationOperandOperationIndex) !=
  400. OpenCLDebugInfo100Deref) {
  401. return 0;
  402. }
  403. uint32_t var_id =
  404. inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
  405. if (!context()->AreAnalysesValid(IRContext::Analysis::kAnalysisDefUse)) {
  406. assert(false &&
  407. "Checking a DebugValue can be used for declare needs DefUseManager");
  408. return 0;
  409. }
  410. auto* var = context()->get_def_use_mgr()->GetDef(var_id);
  411. if (var->opcode() == SpvOpVariable &&
  412. SpvStorageClass(var->GetSingleWordOperand(
  413. kOpVariableOperandStorageClassIndex)) == SpvStorageClassFunction) {
  414. return var_id;
  415. }
  416. return 0;
  417. }
  418. void DebugInfoManager::AnalyzeDebugInst(Instruction* dbg_inst) {
  419. if (dbg_inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100InstructionsMax)
  420. return;
  421. RegisterDbgInst(dbg_inst);
  422. if (dbg_inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction) {
  423. assert(GetDebugFunction(dbg_inst->GetSingleWordOperand(
  424. kDebugFunctionOperandFunctionIndex)) == nullptr &&
  425. "Two DebugFunction instruction exists for a single OpFunction.");
  426. RegisterDbgFunction(dbg_inst);
  427. }
  428. if (debug_info_none_inst_ == nullptr &&
  429. dbg_inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugInfoNone) {
  430. debug_info_none_inst_ = dbg_inst;
  431. }
  432. if (empty_debug_expr_inst_ == nullptr && IsEmptyDebugExpression(dbg_inst)) {
  433. empty_debug_expr_inst_ = dbg_inst;
  434. }
  435. if (dbg_inst->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugDeclare) {
  436. uint32_t var_id =
  437. dbg_inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
  438. RegisterDbgDeclare(var_id, dbg_inst);
  439. }
  440. if (uint32_t var_id = GetVariableIdOfDebugValueUsedForDeclare(dbg_inst)) {
  441. RegisterDbgDeclare(var_id, dbg_inst);
  442. }
  443. }
  444. void DebugInfoManager::AnalyzeDebugInsts(Module& module) {
  445. debug_info_none_inst_ = nullptr;
  446. empty_debug_expr_inst_ = nullptr;
  447. module.ForEachInst([this](Instruction* cpi) { AnalyzeDebugInst(cpi); });
  448. // Move |empty_debug_expr_inst_| to the beginning of the debug instruction
  449. // list.
  450. if (empty_debug_expr_inst_ != nullptr &&
  451. empty_debug_expr_inst_->PreviousNode() != nullptr &&
  452. empty_debug_expr_inst_->PreviousNode()->GetOpenCL100DebugOpcode() !=
  453. OpenCLDebugInfo100InstructionsMax) {
  454. empty_debug_expr_inst_->InsertBefore(
  455. &*context()->module()->ext_inst_debuginfo_begin());
  456. }
  457. // Move |debug_info_none_inst_| to the beginning of the debug instruction
  458. // list.
  459. if (debug_info_none_inst_ != nullptr &&
  460. debug_info_none_inst_->PreviousNode() != nullptr &&
  461. debug_info_none_inst_->PreviousNode()->GetOpenCL100DebugOpcode() !=
  462. OpenCLDebugInfo100InstructionsMax) {
  463. debug_info_none_inst_->InsertBefore(
  464. &*context()->module()->ext_inst_debuginfo_begin());
  465. }
  466. }
  467. void DebugInfoManager::ClearDebugInfo(Instruction* instr) {
  468. if (instr == nullptr ||
  469. instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100InstructionsMax) {
  470. return;
  471. }
  472. id_to_dbg_inst_.erase(instr->result_id());
  473. if (instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction) {
  474. auto fn_id =
  475. instr->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex);
  476. fn_id_to_dbg_fn_.erase(fn_id);
  477. }
  478. if (instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugDeclare ||
  479. instr->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugValue) {
  480. auto var_or_value_id =
  481. instr->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
  482. auto dbg_decl_itr = var_id_to_dbg_decl_.find(var_or_value_id);
  483. if (dbg_decl_itr != var_id_to_dbg_decl_.end()) {
  484. dbg_decl_itr->second.erase(instr);
  485. }
  486. }
  487. if (debug_info_none_inst_ == instr) {
  488. debug_info_none_inst_ = nullptr;
  489. for (auto dbg_instr_itr = context()->module()->ext_inst_debuginfo_begin();
  490. dbg_instr_itr != context()->module()->ext_inst_debuginfo_end();
  491. ++dbg_instr_itr) {
  492. if (instr != &*dbg_instr_itr &&
  493. dbg_instr_itr->GetOpenCL100DebugOpcode() ==
  494. OpenCLDebugInfo100DebugInfoNone) {
  495. debug_info_none_inst_ = &*dbg_instr_itr;
  496. }
  497. }
  498. }
  499. if (empty_debug_expr_inst_ == instr) {
  500. empty_debug_expr_inst_ = nullptr;
  501. for (auto dbg_instr_itr = context()->module()->ext_inst_debuginfo_begin();
  502. dbg_instr_itr != context()->module()->ext_inst_debuginfo_end();
  503. ++dbg_instr_itr) {
  504. if (instr != &*dbg_instr_itr && IsEmptyDebugExpression(&*dbg_instr_itr)) {
  505. empty_debug_expr_inst_ = &*dbg_instr_itr;
  506. }
  507. }
  508. }
  509. }
  510. } // namespace analysis
  511. } // namespace opt
  512. } // namespace spvtools