You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

info.h 8.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. */
  16. #ifndef MINDSPORE_CCSRC_IR_INFO_H_
  17. #define MINDSPORE_CCSRC_IR_INFO_H_
  18. #include <iostream>
  19. #include <string>
  20. #include <memory>
  21. #include <stack>
  22. #include <utility>
  23. #include <vector>
  24. #include "ir/base.h"
  25. #include "debug/trace_info.h"
  26. namespace mindspore {
  27. // namespace to support intermediate representation definition
  28. enum SourceLineTip { kSourceLineTipDiscard = 0, kSourceLineTipNextLine = 1, kSourceLineTipInLine = 2 };
  29. // Location class record the location in source code.
  30. class Location {
  31. public:
  32. Location(const std::string &file_name, int line, int column, int line_end, int column_end)
  33. : file_name_(file_name), line_(line), column_(column), line_end_(line_end), column_end_(column_end) {}
  34. Location(const Location &loc)
  35. : file_name_(loc.file_name_),
  36. line_(loc.line_),
  37. column_(loc.column_),
  38. line_end_(loc.line_end_),
  39. column_end_(loc.column_end_) {}
  40. std::string ToString(SourceLineTip tip = kSourceLineTipNextLine);
  41. std::string file_name() { return file_name_; }
  42. int line() const { return line_; }
  43. void set_line(int line) { line_ = line; }
  44. int line_end() const { return line_end_; }
  45. void set_line_end(int line) { line_end_ = line; }
  46. int column() const { return column_; }
  47. void set_column(int column) { column_ = column; }
  48. int column_end() const { return column_end_; }
  49. void set_column_end(int column) { column_end_ = column; }
  50. ~Location() = default;
  51. private:
  52. std::string file_name_;
  53. int line_;
  54. int column_;
  55. int line_end_;
  56. int column_end_;
  57. };
  58. class TraceContext;
  59. using TraceContextPtr = std::shared_ptr<TraceContext>;
  60. class FuncGraph;
  61. using FuncGraphPtr = std::shared_ptr<FuncGraph>;
  62. using FuncGraphWeakPtr = std::weak_ptr<FuncGraph>;
  63. class AnfNode;
  64. using AnfNodeWeakPtr = std::weak_ptr<AnfNode>;
  65. class TraceManager {
  66. public:
  67. TraceManager() = default;
  68. ~TraceManager() = default;
  69. static TraceContextPtr CurrentContextInfo();
  70. static void DebugTrace(const std::string &func_name, const LocationPtr &location);
  71. static void DebugTrace(const LocationPtr &location);
  72. static void DebugTrace(const TraceInfoPtr &trace_info);
  73. // debug trace with a cloned trace info with debug_info
  74. static void DebugTrace(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info);
  75. static void EndTrace();
  76. static std::stack<TraceContextPtr> trace_context_stack_;
  77. };
  78. class TraceGuard {
  79. public:
  80. explicit TraceGuard(const std::string func_name, const LocationPtr &location) {
  81. TraceManager::DebugTrace(func_name, location);
  82. }
  83. explicit TraceGuard(const LocationPtr &location) { TraceManager::DebugTrace(location); }
  84. ~TraceGuard() { TraceManager::EndTrace(); }
  85. };
  86. class TraceContext {
  87. public:
  88. LocationPtr location_;
  89. TraceInfoPtr trace_info_;
  90. std::string func_name_;
  91. protected:
  92. void ProcessAttributeFromContext();
  93. public:
  94. ~TraceContext() = default;
  95. explicit TraceContext(const LocationPtr &loc) {
  96. ProcessAttributeFromContext();
  97. location_ = loc;
  98. }
  99. explicit TraceContext(const std::string &func_name) {
  100. ProcessAttributeFromContext();
  101. func_name_ = func_name;
  102. }
  103. explicit TraceContext(const TraceInfoPtr &trace_info) {
  104. ProcessAttributeFromContext();
  105. trace_info_ = trace_info;
  106. }
  107. void set_location(const LocationPtr &loc) { location_ = loc; }
  108. LocationPtr location() { return location_; }
  109. void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
  110. TraceInfoPtr trace_info() const { return trace_info_; }
  111. void set_func_name(const std::string &func_name) { func_name_ = func_name; }
  112. std::string func_name() { return func_name_; }
  113. };
  114. class DebugInfo : public Base {
  115. public:
  116. DebugInfo();
  117. explicit DebugInfo(const std::string &name);
  118. explicit DebugInfo(const LocationPtr &loc);
  119. ~DebugInfo() override = default;
  120. MS_DECLARE_PARENT(DebugInfo, Base);
  121. int64_t debug_id();
  122. int64_t unique_id() const { return unique_id_; }
  123. int64_t unique_id_through_copy() const;
  124. std::string get_id() { return std::to_string(debug_id()); }
  125. void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
  126. TraceInfoPtr trace_info() const { return trace_info_; }
  127. void set_location(const LocationPtr &loc) { location_ = loc; }
  128. virtual LocationPtr location() { return location_; }
  129. std::string name() { return name_; }
  130. void set_name(const std::string &name) { name_ = name; }
  131. virtual std::string debug_name();
  132. virtual std::string get_python_func_belonged() { return ""; }
  133. protected:
  134. template <typename Derived>
  135. std::shared_ptr<Derived> shared_from_base() {
  136. return std::static_pointer_cast<Derived>(shared_from_this());
  137. }
  138. private:
  139. void InitValueFromContext() {
  140. if (TraceManager::CurrentContextInfo() != nullptr) {
  141. auto context_info = TraceManager::CurrentContextInfo();
  142. trace_info_ = context_info->trace_info();
  143. location_ = context_info->location();
  144. }
  145. }
  146. static int64_t gen_unique_id() {
  147. static int64_t cur_unique_id = 0;
  148. return cur_unique_id++;
  149. }
  150. protected:
  151. int64_t unique_id_;
  152. int64_t debug_id_;
  153. TraceInfoPtr trace_info_;
  154. LocationPtr location_;
  155. std::string name_;
  156. };
  157. class NodeDebugInfo : public DebugInfo {
  158. public:
  159. NodeDebugInfo() {
  160. if (TraceManager::CurrentContextInfo() != nullptr) {
  161. auto context_info = TraceManager::CurrentContextInfo();
  162. py_func_belonged_ = context_info->func_name();
  163. }
  164. }
  165. explicit NodeDebugInfo(const std::string &name) : DebugInfo(name) {
  166. if (TraceManager::CurrentContextInfo() != nullptr) {
  167. auto context_info = TraceManager::CurrentContextInfo();
  168. py_func_belonged_ = context_info->func_name();
  169. }
  170. }
  171. ~NodeDebugInfo() override = default;
  172. std::string debug_name() override;
  173. void set_node(const std::shared_ptr<AnfNode> &node) { node_ = AnfNodeWeakPtr(node); }
  174. std::shared_ptr<AnfNode> get_node() const { return node_.lock(); }
  175. void set_py_func_belonged(const std::string &name) { py_func_belonged_ = name; }
  176. std::string get_python_func_belonged() override { return py_func_belonged_; }
  177. AnfNodeWeakPtr node_;
  178. std::string py_func_belonged_;
  179. };
  180. using NodeDebugInfoPtr = std::shared_ptr<NodeDebugInfo>;
  181. class GraphDebugInfo : public DebugInfo {
  182. public:
  183. GraphDebugInfo() {
  184. if (TraceManager::CurrentContextInfo() != nullptr) {
  185. auto context_info = TraceManager::CurrentContextInfo();
  186. py_func_name_ = context_info->func_name();
  187. deco_loc_ = nullptr;
  188. }
  189. }
  190. explicit GraphDebugInfo(const std::string &name) : DebugInfo(name) {
  191. if (TraceManager::CurrentContextInfo() != nullptr) {
  192. auto context_info = TraceManager::CurrentContextInfo();
  193. py_func_name_ = context_info->func_name();
  194. deco_loc_ = nullptr;
  195. }
  196. }
  197. ~GraphDebugInfo() override = default;
  198. std::string debug_name() override;
  199. LocationPtr location() override;
  200. LocationPtr deco_location() { return deco_loc_; }
  201. void set_graph(const FuncGraphPtr &func_graph) { func_graph_ = FuncGraphWeakPtr(func_graph); }
  202. FuncGraphPtr get_graph() const { return func_graph_.lock(); }
  203. void set_full_name(const std::string &name) { full_name_ = name; }
  204. std::string get_full_name() { return full_name_; }
  205. void set_deco_location(const LocationPtr &deco_list_loc);
  206. std::string get_python_func_belonged() override { return py_func_name_; }
  207. FuncGraphWeakPtr func_graph_;
  208. LocationPtr deco_loc_;
  209. std::string py_func_name_;
  210. std::string full_name_;
  211. };
  212. using GraphDebugInfoPtr = std::shared_ptr<GraphDebugInfo>;
  213. } // namespace mindspore
  214. #endif // MINDSPORE_CCSRC_IR_INFO_H_