debug_info_manager.h 12 KB

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