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