debug_info_manager.cpp 37 KB

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