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
17 #ifndef MINDSPORE_CORE_UTILS_INFO_H_
18 #define MINDSPORE_CORE_UTILS_INFO_H_
19
20 #include <string>
21 #include <limits>
22 #include <memory>
23 #include <utility>
24 #include <vector>
25
26 #include "base/base.h"
27 #include "mindapi/base/macros.h"
28 #include "ir/scope.h"
29 #include "utils/trace_info.h"
30 namespace mindspore {
31 enum SourceLineTip {
32 kSourceLineTipDiscard = 0,
33 kSourceLineTipNextLine = 1,
34 kSourceLineTipInLine = 2,
35 kSourceSectionTipNextLineHere = 3
36 };
37
38 // typedef enum CacheBool { UNCACHED = -1, FALSE, TRUE } CacheBool;
39 using CacheBool = int32_t;
40 const CacheBool Uncached = -1;
41 const CacheBool False = 0;
42 const CacheBool True = 1;
43
44 MS_CORE_API void ClearThreadLocal();
45
46 // Location class record the location in source code.
47 class Location {
48 public:
Location(const std::string & file_name,int line,int column,int line_end,int column_end,const std::string & expr,std::vector<std::string> && comments)49 Location(const std::string &file_name, int line, int column, int line_end, int column_end, const std::string &expr,
50 std::vector<std::string> &&comments)
51 : file_name_(file_name),
52 line_(line),
53 column_(column),
54 line_end_(line_end),
55 column_end_(column_end),
56 expr_src_(expr),
57 comments_(std::move(comments)) {}
58 ~Location() = default;
59 MS_CORE_API std::string ToString(SourceLineTip tip = kSourceLineTipNextLine, int start_line = 0);
60 MS_CORE_API std::string DebugString() const;
file_name()61 std::string file_name() const { return file_name_; }
set_file_name(const std::string & file_name)62 void set_file_name(const std::string &file_name) { file_name_ = file_name; }
line()63 int line() const { return line_; }
set_line(int line)64 void set_line(int line) { line_ = line; }
line_end()65 int line_end() const { return line_end_; }
set_line_end(int line_end)66 void set_line_end(int line_end) { line_end_ = line_end; }
column()67 int column() const { return column_; }
column_end()68 int column_end() const { return column_end_; }
expr_src()69 const std::string &expr_src() const { return expr_src_; }
set_expr_src(const std::string & expr_src)70 void set_expr_src(const std::string &expr_src) { expr_src_ = expr_src; }
comments()71 const std::vector<std::string> &comments() const { return comments_; }
72
invalid()73 bool invalid() const { return line() == 0 && line_end() == 0 && column() == 0 && column_end() == 0; }
74
75 bool operator<(const Location &other) const;
76
77 private:
78 std::string file_name_;
79 int line_;
80 int column_;
81 int line_end_;
82 int column_end_;
83 std::string expr_src_;
84 std::vector<std::string> comments_;
85 std::string line_str_;
86 };
87
88 class TraceContext {
89 public:
90 explicit TraceContext(const LocationPtr &loc);
91 explicit TraceContext(const TraceInfoPtr &trace_info);
92 ~TraceContext() = default;
location()93 const LocationPtr &location() const { return location_; }
trace_info()94 const TraceInfoPtr &trace_info() const { return trace_info_; }
95
96 private:
97 LocationPtr location_;
98 TraceInfoPtr trace_info_;
99 };
100
101 using TraceContextPtr = TraceContext *;
102
103 /// \brief TraceManager defines interface for debug trace management.
104 class MS_CORE_API TraceManager {
105 public:
106 /// \brief Constructor of TraceManager.
107 TraceManager() = default;
108
109 /// \brief Destructor of TraceManager.
110 ~TraceManager() = default;
111
112 /// \brief Get current trace context.
113 ///
114 /// \return The current trace context.
115 static TraceContextPtr CurrentContextInfo();
116
117 /// \brief Debug trace with the given location.
118 ///
119 /// \param[in] location The source code location for debug trace.
120 /// \return If trace successfully.
121 static bool DebugTrace(const LocationPtr &location);
122
123 /// \brief Debug trace with the given trace info.
124 ///
125 /// \param[in] trace_info The trace info for debug.
126 /// \return If trace successfully.
127 static bool DebugTrace(const TraceInfoPtr &trace_info);
128
129 /// \brief End current debug trace.
130 static void EndTrace() noexcept;
131
132 /// \brief Clear debug info for parse or resolve.
133 static void ClearParserDebugInfo();
134
135 /// \brief Get debug info for parse or resolve.
136 ///
137 /// \return The debug info for parse or resolve.
138 static DebugInfoPtr parser_debug_info();
139
140 /// \brief Get the flag of recording a debug info.
141 ///
142 /// \return A bool.
143 static bool parser_debug_info_flag();
144
145 /// \brief Set the flag to false for not recording a debug info.
146 static void CloseParserDebugInfoFlag();
147
148 /// \brief Set the flag to true for recording a debug info.
149 static void OpenParserDebugInfoFlag();
150 };
151
152 class TraceGuard {
153 public:
TraceGuard(const LocationPtr & location)154 explicit TraceGuard(const LocationPtr &location) { tracing_ = TraceManager::DebugTrace(location); }
TraceGuard(const TraceInfoPtr & trace_info)155 explicit TraceGuard(const TraceInfoPtr &trace_info) { tracing_ = TraceManager::DebugTrace(trace_info); }
~TraceGuard()156 ~TraceGuard() {
157 if (tracing_) {
158 TraceManager::EndTrace();
159 }
160 }
161
162 private:
163 bool tracing_{false};
164 };
165
166 /// \brief DebugInfo defines information for debug trace.
167 class MS_CORE_API DebugInfo {
168 public:
169 /// \brief Construct a default DebugInfo.
DebugInfo()170 DebugInfo() : DebugInfo("") {}
171
172 /// \brief Construct DebugInfo with the given name.
173 ///
174 /// \param[in] name The DebugInfo name.
175 explicit DebugInfo(const std::string &name);
176
177 /// \brief Construct DebugInfo with the given location.
178 ///
179 /// \param[in] loc The location for DebugInfo.
180 explicit DebugInfo(const LocationPtr &loc);
181
182 /// \brief Construct DebugInfo with the given trace info.
183 ///
184 /// \param[in] trace_info The trace info for DebugInfo.
DebugInfo(TraceInfoPtr && trace_info)185 explicit DebugInfo(TraceInfoPtr &&trace_info) : unique_id_(gen_unique_id()), trace_info_(std::move(trace_info)) {}
186
187 /// \brief Destructor of DebugInfo.
188 virtual ~DebugInfo() = default;
189
190 /// \brief Get the id.
191 ///
192 /// \return The id of the debug info.
193 size_t get_id() const;
194
195 /// \brief Get the unique id.
196 ///
197 /// \return The unique id.
unique_id()198 size_t unique_id() const { return unique_id_; }
199
200 /// \brief Get the unique id through copy.
201 ///
202 /// \return The unique id through copy.
203 size_t unique_id_through_copy() const;
204
205 /// \brief Set the trace info.
206 ///
207 /// \param[in] trace_info The trace info to be set.
set_trace_info(const TraceInfoPtr & trace_info)208 void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
209
210 /// \brief Get the trace info.
211 ///
212 /// \return The trace info.
trace_info()213 TraceInfoPtr trace_info() const { return trace_info_; }
214
215 /// \brief Set the location.
216 ///
217 /// \param[in] loc The location to be set.
set_location(const LocationPtr & loc)218 void set_location(const LocationPtr &loc) { location_ = loc; }
219
220 /// \brief Get the location.
221 ///
222 /// \return The location.
location()223 virtual LocationPtr location() const { return location_; }
224
225 /// \brief Get the name.
226 ///
227 /// \return The name of the DebugInfo.
name()228 std::string name() const { return name_; }
229
230 /// \brief Set the name.
231 ///
232 /// \param[in] name The name to be set.
set_name(const std::string & name)233 void set_name(const std::string &name) { name_ = name; }
234
235 /// \brief Get the debug name.
236 ///
237 /// \return The debug name of the DebugInfo.
debug_name()238 virtual std::string debug_name() { return debug_name_; }
239
240 /// \brief Set the debug name.
241 ///
242 /// \param[in] debug_name The name to be set.
set_debug_name(const std::string & debug_name)243 void set_debug_name(const std::string &debug_name) { debug_name_ = debug_name; }
244
shadow_debug_infos_map()245 HashMap<DebugInfoPtr, DebugInfoPtr> &shadow_debug_infos_map() { return shadow_debug_infos_map_; }
246
247 static DebugInfoPtr UpdateInlineCNodeDebugInfo(const DebugInfoPtr &call_debug_info, const DebugInfoPtr &debug_info);
248
249 protected:
gen_unique_id()250 static size_t gen_unique_id() {
251 static size_t cur_unique_id = 0;
252 return cur_unique_id++;
253 }
254
255 mutable size_t id_ = 0;
256 size_t unique_id_;
257 size_t through_copy_unique_id_{std::numeric_limits<size_t>::max()};
258 TraceInfoPtr trace_info_;
259 LocationPtr location_;
260 std::string name_;
261 std::string debug_name_;
262 HashMap<DebugInfoPtr, DebugInfoPtr> shadow_debug_infos_map_;
263 };
264
265 /// \brief NodeDebugInfo defines debug information for a node.
266 class MS_CORE_API NodeDebugInfo : public DebugInfo {
267 public:
268 /// \brief Construct a default NodeDebugInfo.
NodeDebugInfo()269 NodeDebugInfo() : DebugInfo() {}
270
271 /// \brief Construct NodeDebugInfo with a given name.
272 ///
273 /// \param[in] name the name of the NodeDebugInfo.
NodeDebugInfo(const std::string & name)274 explicit NodeDebugInfo(const std::string &name) : DebugInfo(name) {}
275
276 /// \brief Construct NodeDebugInfo with the given trace info.
277 ///
278 /// \param[in] trace_info The trace info for NodeDebugInfo.
NodeDebugInfo(TraceInfoPtr && trace_info)279 explicit NodeDebugInfo(TraceInfoPtr &&trace_info) : DebugInfo(std::move(trace_info)) {}
280
281 /// \brief Destructor of the NodeDebugInfo.
282 ~NodeDebugInfo() override = default;
283
284 std::string debug_name() override;
285
286 /// \brief Set the node's type name.
287 ///
288 /// \param[in] type_name The node type name to be set.
set_type_name(const std::string & type_name)289 void set_type_name(const std::string &type_name) { type_name_ = type_name; }
290
291 private:
292 std::string type_name_;
293 };
294
295 using NodeDebugInfoPtr = std::shared_ptr<NodeDebugInfo>;
296
297 class MS_CORE_API GraphDebugInfo : public DebugInfo {
298 public:
GraphDebugInfo()299 GraphDebugInfo() : DebugInfo() {}
300
GraphDebugInfo(const std::string & name)301 explicit GraphDebugInfo(const std::string &name) : DebugInfo(name) {}
302
GraphDebugInfo(TraceInfoPtr && trace_info)303 explicit GraphDebugInfo(TraceInfoPtr &&trace_info) : DebugInfo(std::move(trace_info)) {}
304
305 ~GraphDebugInfo() override = default;
306
307 std::string debug_name() override;
308 LocationPtr location() const override;
deco_location()309 LocationPtr deco_location() { return deco_loc_; }
set_graph(const FuncGraphPtr & func_graph)310 void set_graph(const FuncGraphPtr &func_graph) { func_graph_ = FuncGraphWeakPtr(func_graph); }
get_graph()311 FuncGraphPtr get_graph() const { return func_graph_.lock(); }
312 void set_deco_location(const LocationPtr &deco_list_loc);
313
314 private:
315 FuncGraphWeakPtr func_graph_;
316 LocationPtr deco_loc_;
317 };
318
319 using GraphDebugInfoPtr = std::shared_ptr<GraphDebugInfo>;
320
TraceContext(const LocationPtr & loc)321 inline TraceContext::TraceContext(const LocationPtr &loc) : location_(loc) {
322 auto top = TraceManager::CurrentContextInfo();
323 if (top != nullptr) {
324 trace_info_ = top->trace_info();
325 }
326 if (location_ != nullptr) {
327 MS_LOG(DEBUG) << "location_: " << location_->DebugString();
328 } else {
329 MS_LOG(DEBUG) << "location_ is null";
330 }
331 }
332
TraceContext(const TraceInfoPtr & trace_info)333 inline TraceContext::TraceContext(const TraceInfoPtr &trace_info) : trace_info_(trace_info) {}
334
335 struct MS_CORE_API DebugInfoCompare {
336 bool operator()(const DebugInfoPtr &left, const DebugInfoPtr &right) const;
337 };
338
339 MS_CORE_API void UpdateDebugInfo(const FuncGraphPtr &func_graph, const ScopePtr &scope, const DebugInfoPtr &debug_info);
340 MS_CORE_API void UpdateInlineCNodeDebugInfo(const AnfNodePtr &caller, const AnfNodePtr &callee);
341
342 MS_CORE_API std::vector<DebugInfoPtr> GetDebugInfoList(const DebugInfoPtr &debug_info);
343
344 } // namespace mindspore
345
346 #endif // MINDSPORE_CORE_UTILS_INFO_H_
347