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 12 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * Copyright 2019-2021 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_CORE_UTILS_INFO_H_
  17. #define MINDSPORE_CORE_UTILS_INFO_H_
  18. #include <string>
  19. #include <memory>
  20. #include <utility>
  21. #include <vector>
  22. #include "base/base.h"
  23. #include "utils/visible.h"
  24. #include "ir/scope.h"
  25. #include "utils/trace_info.h"
  26. namespace mindspore {
  27. enum SourceLineTip { kSourceLineTipDiscard = 0, kSourceLineTipNextLine = 1, kSourceLineTipInLine = 2 };
  28. // Location class record the location in source code.
  29. class Location {
  30. public:
  31. Location(const std::string &file_name, int line, int column, int line_end, int column_end)
  32. : file_name_(file_name), line_(line), column_(column), line_end_(line_end), column_end_(column_end) {}
  33. ~Location() = default;
  34. std::string ToString(SourceLineTip tip = kSourceLineTipNextLine) const;
  35. std::string file_name() const { return file_name_; }
  36. int line() const { return line_; }
  37. int line_end() const { return line_end_; }
  38. int column() const { return column_; }
  39. int column_end() const { return column_end_; }
  40. bool operator<(const Location &other) const;
  41. private:
  42. std::string file_name_;
  43. int line_;
  44. int column_;
  45. int line_end_;
  46. int column_end_;
  47. };
  48. class TraceContext {
  49. public:
  50. explicit TraceContext(const LocationPtr &loc);
  51. explicit TraceContext(const std::string &func_name);
  52. explicit TraceContext(const TraceInfoPtr &trace_info);
  53. TraceContext(const LocationPtr &loc, const std::string &func_name);
  54. ~TraceContext() = default;
  55. const LocationPtr &location() const { return location_; }
  56. const TraceInfoPtr &trace_info() const { return trace_info_; }
  57. const std::string &func_name() const { return func_name_; }
  58. private:
  59. LocationPtr location_;
  60. TraceInfoPtr trace_info_;
  61. std::string func_name_;
  62. };
  63. using TraceContextPtr = TraceContext *;
  64. /// \brief TraceManager defines interface for debug trace management.
  65. class MS_CORE_API TraceManager {
  66. public:
  67. /// \brief Constructor of TraceManager.
  68. TraceManager() = default;
  69. /// \brief Destructor of TraceManager.
  70. ~TraceManager() = default;
  71. /// \brief Get current trace context.
  72. ///
  73. /// \return The current trace context.
  74. static TraceContextPtr CurrentContextInfo() {
  75. if (!trace_context_stack_.empty()) {
  76. return &trace_context_stack_.back();
  77. }
  78. return nullptr;
  79. }
  80. /// \brief Debug trace with the given function name and location.
  81. ///
  82. /// \param[in] func_name The function name for debug trace.
  83. /// \param[in] location The source code location for debug trace.
  84. static void DebugTrace(const std::string &func_name, const LocationPtr &location);
  85. /// \brief Debug trace with the given location.
  86. ///
  87. /// \param[in] location The source code location for debug trace.
  88. static void DebugTrace(const LocationPtr &location);
  89. /// \brief Debug trace with the given trace info.
  90. ///
  91. /// \param[in] trace_info The trace info for debug.
  92. static void DebugTrace(const TraceInfoPtr &trace_info);
  93. /// \brief Debug trace with a cloned trace info and debug info.
  94. ///
  95. /// \param[in] debug_info The debug info for debug trace.
  96. /// \param[in] trace_info The trace info for debug trace.
  97. static void DebugTrace(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info);
  98. /// \brief End current debug trace.
  99. static void EndTrace() { trace_context_stack_.pop_back(); }
  100. /// \brief Clear debug info for parse or resolve.
  101. static void ClearParseOrResolveDebugInfo();
  102. /// \brief Get debug info for parse or resolve.
  103. ///
  104. /// \return The debug info for parse or resolve.
  105. static DebugInfoPtr GetParseOrResolveDebugInfo();
  106. private:
  107. /// \brief Trace context stack for current thread.
  108. thread_local static std::vector<TraceContext> trace_context_stack_;
  109. /// \brief Debug info for parse or resolve for current thread.
  110. thread_local static DebugInfoPtr parse_or_resolve_debug_info_;
  111. };
  112. class TraceGuard {
  113. public:
  114. TraceGuard(const std::string &func_name, const LocationPtr &location) {
  115. TraceManager::DebugTrace(func_name, location);
  116. }
  117. explicit TraceGuard(const LocationPtr &location) { TraceManager::DebugTrace(location); }
  118. explicit TraceGuard(const TraceInfoPtr &trace_info) { TraceManager::DebugTrace(trace_info); }
  119. TraceGuard(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info) {
  120. TraceManager::DebugTrace(debug_info, trace_info);
  121. }
  122. ~TraceGuard() { TraceManager::EndTrace(); }
  123. };
  124. /// \brief DebugInfo defines information for debug trace.
  125. class MS_CORE_API DebugInfo {
  126. public:
  127. /// \brief Construct a default DebugInfo.
  128. DebugInfo() : DebugInfo("") {}
  129. /// \brief Construct DebugInfo with the given name.
  130. ///
  131. /// \param[in] name The DebugInfo name.
  132. explicit DebugInfo(const std::string &name) : unique_id_(gen_unique_id()), name_(name) {
  133. auto top = TraceManager::CurrentContextInfo();
  134. if (top != nullptr) {
  135. trace_info_ = top->trace_info();
  136. location_ = top->location();
  137. }
  138. }
  139. /// \brief Construct DebugInfo with the given location.
  140. ///
  141. /// \param[in] loc The location for DebugInfo.
  142. explicit DebugInfo(const LocationPtr &loc) : unique_id_(gen_unique_id()), location_(loc) {
  143. auto top = TraceManager::CurrentContextInfo();
  144. if (top != nullptr) {
  145. trace_info_ = top->trace_info();
  146. }
  147. }
  148. /// \brief Construct DebugInfo with the given trace info.
  149. ///
  150. /// \param[in] trace_info The trace info for DebugInfo.
  151. explicit DebugInfo(TraceInfoPtr &&trace_info) : unique_id_(gen_unique_id()), trace_info_(std::move(trace_info)) {}
  152. /// \brief Destructor of DebugInfo.
  153. virtual ~DebugInfo() = default;
  154. /// \brief Get the id.
  155. ///
  156. /// \return The id of the debug info.
  157. int64_t get_id() const;
  158. /// \brief Get the unique id.
  159. ///
  160. /// \return The unique id.
  161. int64_t unique_id() const { return unique_id_; }
  162. /// \brief Get the unique id through copy.
  163. ///
  164. /// \return The unique id through copy.
  165. int64_t unique_id_through_copy() const;
  166. /// \brief Set the trace info.
  167. ///
  168. /// \param[in] trace_info The trace info to be set.
  169. void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
  170. /// \brief Get the trace info.
  171. ///
  172. /// \return The trace info.
  173. TraceInfoPtr trace_info() const { return trace_info_; }
  174. /// \brief Set the location.
  175. ///
  176. /// \param[in] loc The location to be set.
  177. void set_location(const LocationPtr &loc) { location_ = loc; }
  178. /// \brief Get the location.
  179. ///
  180. /// \return The location.
  181. virtual LocationPtr location() const { return location_; }
  182. /// \brief Get the name.
  183. ///
  184. /// \return The name of the DebugInfo.
  185. std::string name() { return name_; }
  186. /// \brief Set the name.
  187. ///
  188. /// \param[in] name The name to be set.
  189. void set_name(const std::string &name) { name_ = name; }
  190. /// \brief Get the debug name.
  191. ///
  192. /// \return The debug name of the DebugInfo.
  193. virtual std::string debug_name() { return name_; }
  194. /// \brief Get the python function name that this DebugInfo belongs to.
  195. ///
  196. /// \return The python function name that this DebugInfo belongs to.
  197. virtual std::string get_python_func_belonged() { return ""; }
  198. protected:
  199. static int64_t gen_unique_id() {
  200. static int64_t cur_unique_id = 0;
  201. return cur_unique_id++;
  202. }
  203. mutable int64_t id_ = 0;
  204. int64_t unique_id_;
  205. TraceInfoPtr trace_info_;
  206. LocationPtr location_;
  207. std::string name_;
  208. };
  209. /// \brief NodeDebugInfo defines debug information for a node.
  210. class MS_CORE_API NodeDebugInfo : public DebugInfo {
  211. public:
  212. /// \brief Construct a default NodeDebugInfo.
  213. NodeDebugInfo() : DebugInfo() {
  214. auto top = TraceManager::CurrentContextInfo();
  215. if (top != nullptr) {
  216. py_func_belonged_ = top->func_name();
  217. }
  218. }
  219. /// \brief Construct NodeDebugInfo with a given name.
  220. ///
  221. /// \param[in] name the name of the NodeDebugInfo.
  222. explicit NodeDebugInfo(const std::string &name) : DebugInfo(name) {
  223. auto top = TraceManager::CurrentContextInfo();
  224. if (top != nullptr) {
  225. py_func_belonged_ = top->func_name();
  226. }
  227. }
  228. /// \brief Construct NodeDebugInfo with the given trace info.
  229. ///
  230. /// \param[in] trace_info The trace info for NodeDebugInfo.
  231. explicit NodeDebugInfo(TraceInfoPtr &&trace_info) : DebugInfo(std::move(trace_info)) {}
  232. /// \brief Destructor of the NodeDebugInfo.
  233. ~NodeDebugInfo() override = default;
  234. std::string debug_name() override;
  235. /// \brief Set the node.
  236. ///
  237. /// \param[in] node The node to be set.
  238. void set_node(const AnfNodePtr &node) { node_ = AnfNodeWeakPtr(node); }
  239. /// \brief Get the node.
  240. ///
  241. /// \return The node.
  242. AnfNodePtr get_node() const { return node_.lock(); }
  243. /// \brief Set python function name that this NodeDebugInfo belongs to.
  244. ///
  245. /// \param[in] name The python function name to be set.
  246. void set_py_func_belonged(const std::string &name) { py_func_belonged_ = name; }
  247. std::string get_python_func_belonged() override { return py_func_belonged_; }
  248. private:
  249. AnfNodeWeakPtr node_;
  250. std::string py_func_belonged_;
  251. };
  252. using NodeDebugInfoPtr = std::shared_ptr<NodeDebugInfo>;
  253. class GraphDebugInfo : public DebugInfo {
  254. public:
  255. GraphDebugInfo() : DebugInfo() {
  256. auto top = TraceManager::CurrentContextInfo();
  257. if (top != nullptr) {
  258. py_func_name_ = top->func_name();
  259. }
  260. }
  261. explicit GraphDebugInfo(const std::string &name) : DebugInfo(name) {
  262. auto top = TraceManager::CurrentContextInfo();
  263. if (top != nullptr) {
  264. py_func_name_ = top->func_name();
  265. }
  266. }
  267. explicit GraphDebugInfo(TraceInfoPtr &&trace_info) : DebugInfo(std::move(trace_info)) {}
  268. ~GraphDebugInfo() override = default;
  269. std::string debug_name() override;
  270. LocationPtr location() const override;
  271. LocationPtr deco_location() { return deco_loc_; }
  272. void set_graph(const FuncGraphPtr &func_graph) { func_graph_ = FuncGraphWeakPtr(func_graph); }
  273. FuncGraphPtr get_graph() const { return func_graph_.lock(); }
  274. void set_full_name(const std::string &name) { full_name_ = name; }
  275. std::string get_full_name() { return full_name_; }
  276. void set_deco_location(const LocationPtr &deco_list_loc);
  277. std::string get_python_func_belonged() override { return py_func_name_; }
  278. private:
  279. FuncGraphWeakPtr func_graph_;
  280. LocationPtr deco_loc_;
  281. std::string py_func_name_;
  282. std::string full_name_;
  283. };
  284. using GraphDebugInfoPtr = std::shared_ptr<GraphDebugInfo>;
  285. inline TraceContext::TraceContext(const LocationPtr &loc) : location_(loc) {
  286. auto top = TraceManager::CurrentContextInfo();
  287. if (top != nullptr) {
  288. trace_info_ = top->trace_info();
  289. func_name_ = top->func_name();
  290. }
  291. }
  292. inline TraceContext::TraceContext(const std::string &func_name) : func_name_(func_name) {
  293. auto top = TraceManager::CurrentContextInfo();
  294. if (top != nullptr) {
  295. location_ = top->location();
  296. trace_info_ = top->trace_info();
  297. }
  298. }
  299. inline TraceContext::TraceContext(const TraceInfoPtr &trace_info) : trace_info_(trace_info) {
  300. auto top = TraceManager::CurrentContextInfo();
  301. if (top != nullptr) {
  302. location_ = top->location();
  303. func_name_ = top->func_name();
  304. }
  305. }
  306. inline TraceContext::TraceContext(const LocationPtr &loc, const std::string &func_name)
  307. : location_(loc), func_name_(func_name) {
  308. auto top = TraceManager::CurrentContextInfo();
  309. if (top != nullptr) {
  310. trace_info_ = top->trace_info();
  311. }
  312. }
  313. struct DebugInfoCompare {
  314. bool operator()(const DebugInfoPtr &left, const DebugInfoPtr &right) const;
  315. };
  316. void UpdateDebugInfo(const FuncGraphPtr &func_graph, const ScopePtr &scope, const DebugInfoPtr &debug_info);
  317. } // namespace mindspore
  318. #endif // MINDSPORE_CORE_UTILS_INFO_H_