debug_info_manager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 extension
  61. // 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 OpenCL.DebugInfo.100 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, scope, and
  131. // operands with DebugDeclare but it uses |value_id| for value. Returns
  132. // the added DebugValue, or nullptr if it does not add a DebugValue.
  133. Instruction* AddDebugValueForDecl(Instruction* dbg_decl, uint32_t value_id,
  134. Instruction* insert_before);
  135. // Erases |instr| from data structures of this class.
  136. void ClearDebugInfo(Instruction* instr);
  137. // Returns the id of Value operand if |inst| is DebugValue who has Deref
  138. // operation and its Value operand is a result id of OpVariable with
  139. // Function storage class. Otherwise, returns 0.
  140. uint32_t GetVariableIdOfDebugValueUsedForDeclare(Instruction* inst);
  141. // Converts DebugGlobalVariable |dbg_global_var| to a DebugLocalVariable and
  142. // creates a DebugDeclare mapping the new DebugLocalVariable to |local_var|.
  143. void ConvertDebugGlobalToLocalVariable(Instruction* dbg_global_var,
  144. Instruction* local_var);
  145. // Returns true if |instr| is a debug declaration instruction.
  146. bool IsDebugDeclare(Instruction* instr);
  147. // Replace all uses of |before| id that is an operand of a DebugScope with
  148. // |after| id if those uses (instruction) return true for |predicate|.
  149. void ReplaceAllUsesInDebugScopeWithPredicate(
  150. uint32_t before, uint32_t after,
  151. const std::function<bool(Instruction*)>& predicate);
  152. // Removes uses of DebugScope |inst| from |scope_id_to_users_| or uses of
  153. // DebugInlinedAt |inst| from |inlinedat_id_to_users_|.
  154. void ClearDebugScopeAndInlinedAtUses(Instruction* inst);
  155. private:
  156. IRContext* context() { return context_; }
  157. // Analyzes OpenCL.DebugInfo.100 instructions in the given |module| and
  158. // populates data structures in this class.
  159. void AnalyzeDebugInsts(Module& module);
  160. // Returns the debug instruction whose id is |id|. Returns |nullptr| if one
  161. // does not exists.
  162. Instruction* GetDbgInst(uint32_t id);
  163. // Returns a DebugOperation instruction with OpCode Deref.
  164. Instruction* GetDebugOperationWithDeref();
  165. // Registers the debug instruction |inst| into |id_to_dbg_inst_| using id of
  166. // |inst| as a key.
  167. void RegisterDbgInst(Instruction* inst);
  168. // Register the DebugFunction instruction |inst|. The function referenced
  169. // in |inst| must not already be registered.
  170. void RegisterDbgFunction(Instruction* inst);
  171. // Register the DebugDeclare or DebugValue with Deref operation
  172. // |dbg_declare| into |var_id_to_dbg_decl_| using OpVariable id
  173. // |var_id| as a key.
  174. void RegisterDbgDeclare(uint32_t var_id, Instruction* dbg_declare);
  175. // Returns a DebugExpression instruction without Operation operands.
  176. Instruction* GetEmptyDebugExpression();
  177. // Returns true if a scope |ancestor| is |scope| or an ancestor scope
  178. // of |scope|.
  179. bool IsAncestorOfScope(uint32_t scope, uint32_t ancestor);
  180. // Returns true if the declaration of a local variable |dbg_declare|
  181. // is visible in the scope of an instruction |instr_scope_id|.
  182. bool IsDeclareVisibleToInstr(Instruction* dbg_declare, Instruction* scope);
  183. // Returns the parent scope of the scope |child_scope|.
  184. uint32_t GetParentScope(uint32_t child_scope);
  185. IRContext* context_;
  186. // Mapping from ids of OpenCL.DebugInfo.100 extension instructions
  187. // to their Instruction instances.
  188. std::unordered_map<uint32_t, Instruction*> id_to_dbg_inst_;
  189. // Mapping from function's ids to DebugFunction instructions whose
  190. // operand is the function.
  191. std::unordered_map<uint32_t, Instruction*> fn_id_to_dbg_fn_;
  192. // Mapping from variable or value ids to DebugDeclare or DebugValue
  193. // instructions whose operand is the variable or value.
  194. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  195. var_id_to_dbg_decl_;
  196. // Mapping from DebugScope ids to users.
  197. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  198. scope_id_to_users_;
  199. // Mapping from DebugInlinedAt ids to users.
  200. std::unordered_map<uint32_t, std::unordered_set<Instruction*>>
  201. inlinedat_id_to_users_;
  202. // DebugOperation whose OpCode is OpenCLDebugInfo100Deref.
  203. Instruction* deref_operation_;
  204. // DebugInfoNone instruction. We need only a single DebugInfoNone.
  205. // To reuse the existing one, we keep it using this member variable.
  206. Instruction* debug_info_none_inst_;
  207. // DebugExpression instruction without Operation operands. We need only
  208. // a single DebugExpression without Operation operands. To reuse the
  209. // existing one, we keep it using this member variable.
  210. Instruction* empty_debug_expr_inst_;
  211. };
  212. } // namespace analysis
  213. } // namespace opt
  214. } // namespace spvtools
  215. #endif // SOURCE_OPT_DEBUG_INFO_MANAGER_H_