debug_info_manager.cpp 31 KB

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