debug_info_manager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #ifndef SOURCE_OPT_DEBUG_INFO_MANAGER_H_
  15. #define SOURCE_OPT_DEBUG_INFO_MANAGER_H_
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #include "source/opt/instruction.h"
  19. #include "source/opt/module.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace analysis {
  23. // When an instruction of a callee function is inlined to its caller function,
  24. // we need the line and the scope information of the function call instruction
  25. // to generate DebugInlinedAt. This class keeps the data. For multiple inlining
  26. // of a single instruction, we have to create multiple DebugInlinedAt
  27. // instructions as a chain. This class keeps the information of the generated
  28. // DebugInlinedAt chains to reduce the number of chains.
  29. class DebugInlinedAtContext {
  30. public:
  31. explicit DebugInlinedAtContext(Instruction* call_inst)
  32. : call_inst_line_(call_inst->dbg_line_inst()),
  33. call_inst_scope_(call_inst->GetDebugScope()) {}
  34. const Instruction* GetLineOfCallInstruction() { return call_inst_line_; }
  35. const DebugScope& GetScopeOfCallInstruction() { return call_inst_scope_; }
  36. // Puts the DebugInlinedAt chain that is generated for the callee instruction
  37. // whose DebugInlinedAt of DebugScope is |callee_instr_inlined_at| into
  38. // |callee_inlined_at2chain_|.
  39. void SetDebugInlinedAtChain(uint32_t callee_instr_inlined_at,
  40. uint32_t chain_head_id) {
  41. callee_inlined_at2chain_[callee_instr_inlined_at] = chain_head_id;
  42. }
  43. // Gets the DebugInlinedAt chain from |callee_inlined_at2chain_|.
  44. uint32_t GetDebugInlinedAtChain(uint32_t callee_instr_inlined_at) {
  45. auto chain_itr = callee_inlined_at2chain_.find(callee_instr_inlined_at);
  46. if (chain_itr != callee_inlined_at2chain_.end()) return chain_itr->second;
  47. return kNoInlinedAt;
  48. }
  49. private:
  50. // The line information of the function call instruction that will be
  51. // replaced by the callee function.
  52. const Instruction* call_inst_line_;
  53. // The scope information of the function call instruction that will be
  54. // replaced by the callee function.
  55. const DebugScope call_inst_scope_;
  56. // Map from DebugInlinedAt ids of callee to head ids of new generated
  57. // DebugInlinedAt chain.
  58. std::unordered_map<uint32_t, uint32_t> callee_inlined_at2chain_;
  59. };
  60. // A class for analyzing, managing, and creating OpenCL.DebugInfo.100 and
  61. // NonSemantic.Shader.DebugInfo.100 extension instructions.
  62. class DebugInfoManager {
  63. public:
  64. // Constructs a debug information manager from the given |context|.
  65. DebugInfoManager(IRContext* context);
  66. DebugInfoManager(const DebugInfoManager&) = delete;
  67. DebugInfoManager(DebugInfoManager&&) = delete;
  68. DebugInfoManager& operator=(const DebugInfoManager&) = delete;
  69. DebugInfoManager& operator=(DebugInfoManager&&) = delete;
  70. friend bool operator==(const DebugInfoManager&, const DebugInfoManager&);
  71. friend bool operator!=(const DebugInfoManager& lhs,
  72. const DebugInfoManager& rhs) {
  73. return !(lhs == rhs);
  74. }
  75. // Analyzes DebugInfo instruction |dbg_inst|.
  76. void AnalyzeDebugInst(Instruction* dbg_inst);
  77. // Creates new DebugInlinedAt and returns its id. Its line operand is the
  78. // line number of |line| if |line| is not nullptr. Otherwise, its line operand
  79. // is the line number of lexical scope of |scope|. Its Scope and Inlined
  80. // operands are Scope and Inlined of |scope|.
  81. uint32_t CreateDebugInlinedAt(const Instruction* line,
  82. const DebugScope& scope);
  83. // Clones DebugExpress instruction |dbg_expr| and add Deref Operation
  84. // in the front of the Operation list of |dbg_expr|.
  85. Instruction* DerefDebugExpression(Instruction* dbg_expr);
  86. // Returns a DebugInfoNone instruction.
  87. Instruction* GetDebugInfoNone();
  88. // Returns DebugInlinedAt whose id is |dbg_inlined_at_id|. If it does not
  89. // exist or it is not a DebugInlinedAt instruction, return nullptr.
  90. Instruction* GetDebugInlinedAt(uint32_t dbg_inlined_at_id);
  91. // Returns DebugFunction whose Function operand is |fn_id|. If it does not
  92. // exist, return nullptr.
  93. Instruction* GetDebugFunction(uint32_t fn_id) {
  94. auto dbg_fn_it = fn_id_to_dbg_fn_.find(fn_id);
  95. return dbg_fn_it == fn_id_to_dbg_fn_.end() ? nullptr : dbg_fn_it->second;
  96. }
  97. // Clones DebugInlinedAt whose id is |clone_inlined_at_id|. If
  98. // |clone_inlined_at_id| is not an id of DebugInlinedAt, returns nullptr.
  99. // If |insert_before| is given, inserts the new DebugInlinedAt before it.
  100. // Otherwise, inserts the new DebugInlinedAt into the debug instruction
  101. // section of the module.
  102. Instruction* CloneDebugInlinedAt(uint32_t clone_inlined_at_id,
  103. Instruction* insert_before = nullptr);
  104. // Returns the debug scope corresponding to an inlining instruction in the
  105. // scope |callee_instr_scope| into |inlined_at_ctx|. Generates all new
  106. // debug instructions needed to represent the scope.
  107. DebugScope BuildDebugScope(const DebugScope& callee_instr_scope,
  108. DebugInlinedAtContext* inlined_at_ctx);
  109. // Returns DebugInlinedAt corresponding to inlining an instruction, which
  110. // was inlined at |callee_inlined_at|, into |inlined_at_ctx|. Generates all
  111. // new debug instructions needed to represent the DebugInlinedAt.
  112. uint32_t BuildDebugInlinedAtChain(uint32_t callee_inlined_at,
  113. DebugInlinedAtContext* inlined_at_ctx);
  114. // Returns true if there is a debug declaration instruction whose
  115. // 'Local Variable' operand is |variable_id|.
  116. bool IsVariableDebugDeclared(uint32_t variable_id);
  117. // Kills all debug declaration instructions with Deref whose 'Local Variable'
  118. // operand is |variable_id|. Returns whether it kills an instruction or not.
  119. bool KillDebugDeclares(uint32_t variable_id);
  120. // Generates a DebugValue instruction with value |value_id| for every local
  121. // variable that is in the scope of |scope_and_line| and whose memory is
  122. // |variable_id| and inserts it after the instruction |insert_pos|.
  123. // Returns whether a DebugValue is added or not. |invisible_decls| returns
  124. // DebugDeclares invisible to |scope_and_line|.
  125. bool AddDebugValueIfVarDeclIsVisible(
  126. Instruction* scope_and_line, uint32_t variable_id, uint32_t value_id,
  127. Instruction* insert_pos,
  128. std::unordered_set<Instruction*>* invisible_decls);
  129. // Creates a DebugValue for DebugDeclare |dbg_decl| and inserts it before
  130. // |insert_before|. The new DebugValue has the same line and scope as
  131. // |scope_and_line|, or no scope and line information if |scope_and_line|
  132. // is nullptr. The new DebugValue has the same operands as DebugDeclare
  133. // but it uses |value_id| for the value. Returns the created DebugValue,
  134. // or nullptr if fails to create one.
  135. Instruction* AddDebugValueForDecl(Instruction* dbg_decl, uint32_t value_id,
  136. Instruction* insert_before,
  137. Instruction* scope_and_line);
  138. // Erases |instr| from data structures of this class.
  139. void ClearDebugInfo(Instruction* instr);
  140. // Return the opcode for the Vulkan DebugOperation inst
  141. uint32_t GetVulkanDebugOperation(Instruction* inst);
  142. // Returns the id of Value operand if |inst| is DebugValue who has Deref
  143. // operation and its Value operand is a result id of OpVariable with
  144. // Function storage class. Otherwise, returns 0.
  145. uint32_t GetVariableIdOfDebugValueUsedForDeclare(Instruction* inst);
  146. // Converts DebugGlobalVariable |dbg_global_var| to a DebugLocalVariable and
  147. // creates a DebugDeclare mapping the new DebugLocalVariable to |local_var|.
  148. void ConvertDebugGlobalToLocalVariable(Instruction* dbg_global_var,
  149. Instruction* local_var);
  150. // Returns true if |instr| is a debug declaration instruction.
  151. bool IsDebugDeclare(Instruction* instr);
  152. // Replace all uses of |before| id that is an operand of a DebugScope with
  153. // |after| id if those uses (instruction) return true for |predicate|.
  154. void ReplaceAllUsesInDebugScopeWithPredicate(
  155. uint32_t before, uint32_t after,
  156. const std::function<bool(Instruction*)>& predicate);
  157. // Removes uses of DebugScope |inst| from |scope_id_to_users_| or uses of
  158. // DebugInlinedAt |inst| from |inlinedat_id_to_users_|.
  159. void ClearDebugScopeAndInlinedAtUses(Instruction* inst);
  160. private:
  161. IRContext* context() { return context_; }
  162. // Analyzes DebugInfo instructions in the given |module| and
  163. // populates data structures in this class.
  164. void AnalyzeDebugInsts(Module& module);
  165. // Get the DebugInfo ExtInstImport Id, or 0 if no DebugInfo is available.
  166. uint32_t GetDbgSetImportId();
  167. // Returns the debug instruction whose id is |id|. Returns |nullptr| if one
  168. // does not exists.
  169. Instruction* GetDbgInst(uint32_t id);
  170. // Returns a DebugOperation instruction with OpCode Deref.
  171. Instruction* GetDebugOperationWithDeref();
  172. // Registers the debug instruction |inst| into |id_to_dbg_inst_| using id of
  173. // |inst| as a key.
  174. void RegisterDbgInst(Instruction* inst);
  175. // Register the DebugFunction instruction |inst|. The function referenced
  176. // in |inst| must not already be registered.
  177. void RegisterDbgFunction(Instruction* inst);
  178. // Register the DebugDeclare or DebugValue with Deref operation
  179. // |dbg_declare| into |var_id_to_dbg_decl_| using OpVariable id
  180. // |var_id| as a key.
  181. void RegisterDbgDeclare(uint32_t var_id, Instruction* dbg_declare);
  182. // Returns a DebugExpression instruction without Operation operands.
  183. Instruction* GetEmptyDebugExpression();
  184. // Returns true if a scope |ancestor| is |scope| or an ancestor scope
  185. // of |scope|.
  186. bool IsAncestorOfScope(uint32_t scope, uint32_t ancestor);
  187. // Returns true if the declaration of a local variable |dbg_declare|
  188. // is visible in the scope of an instruction |instr_scope_id|.
  189. bool IsDeclareVisibleToInstr(Instruction* dbg_declare, Instruction* scope);
  190. // Returns the parent scope of the scope |child_scope|.
  191. uint32_t GetParentScope(uint32_t child_scope);
  192. IRContext* context_;
  193. // Mapping from ids of DebugInfo extension instructions.
  194. // to their Instruction instances.
  195. std::unordered_map<uint32_t, Instruction*> id_to_dbg_inst_;
  196. // Mapping from function's ids to DebugFunction instructions whose
  197. // operand is the function.
  198. std::unordered_map<uint32_t, Instruction*> fn_id_to_dbg_fn_;
  199. // Mapping from variable or value ids to DebugDeclare or DebugValue
  200. // instructions whose operand is the variable or value.
  201. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  202. var_id_to_dbg_decl_;
  203. // Mapping from DebugScope ids to users.
  204. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  205. scope_id_to_users_;
  206. // Mapping from DebugInlinedAt ids to users.
  207. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  208. inlinedat_id_to_users_;
  209. // DebugOperation whose OpCode is OpenCLDebugInfo100Deref.
  210. Instruction* deref_operation_;
  211. // DebugInfoNone instruction. We need only a single DebugInfoNone.
  212. // To reuse the existing one, we keep it using this member variable.
  213. Instruction* debug_info_none_inst_;
  214. // DebugExpression instruction without Operation operands. We need only
  215. // a single DebugExpression without Operation operands. To reuse the
  216. // existing one, we keep it using this member variable.
  217. Instruction* empty_debug_expr_inst_;
  218. };
  219. } // namespace analysis
  220. } // namespace opt
  221. } // namespace spvtools
  222. #endif // SOURCE_OPT_DEBUG_INFO_MANAGER_H_