debug_info_manager.cpp 29 KB

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