debug_info_manager.cpp 35 KB

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