-
Notifications
You must be signed in to change notification settings - Fork 850
Expand file tree
/
Copy pathspan_utils.h
More file actions
78 lines (69 loc) · 2.46 KB
/
Copy pathspan_utils.h
File metadata and controls
78 lines (69 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*!
* \file tl/span_utils.h
* \brief Helpers to read/write source spans on tirx IR nodes and to format
* spans into compiler error messages.
*
* `StmtNode::span` / `BufferNode::span` / `PrimFuncNode::span` are mutable
* fields that are reflected read-only to Python. These helpers write them
* directly from C++, so spans can be injected during script parsing without
* touching the vendored TVM fork. Spans never participate in structural
* equality or hashing.
*/
#ifndef TVM_TL_SPAN_UTILS_H_
#define TVM_TL_SPAN_UTILS_H_
#include <tvm/ir/source_map.h>
#include <tvm/tirx/buffer.h>
#include <tvm/tirx/expr.h>
#include <tvm/tirx/function.h>
#include <tvm/tirx/stmt.h>
#include <initializer_list>
#include <sstream>
#include <string>
namespace tvm {
namespace tl {
/*!
* \brief Format a span as "file:line:col" for error messages.
* \return Empty string when the span or its source name is undefined.
*/
inline std::string FormatSpan(const Span &span) {
if (!span.defined() || !span->source_name.defined()) {
return "";
}
std::ostringstream os;
os << span->source_name->name << ":" << span->line << ":" << span->column;
return os.str();
}
/*!
* \brief Fill in the given span on every Stmt node in the subtree whose span
* is undefined. Nodes that already carry a span are left untouched, so
* lowered code inherits the source line of the statement it was
* lowered from without overriding finer-grained spans.
* \note No-op when `span` is undefined. Spans never participate in structural
* equality or hashing, so stamping does not change compilation results.
*/
void StampSubtreeSpans(const tirx::Stmt &stmt, const Span &span);
/*!
* \brief Suffix for error messages pointing at a source location, e.g.
* "\n --> /path/to/kernel.py:21:1". Empty when no span is available.
*/
inline std::string SpanHintSuffix(const Span &span) {
std::string loc = FormatSpan(span);
return loc.empty() ? "" : "\n --> " + loc;
}
/*!
* \brief SpanHintSuffix over multiple candidates; the first defined span wins.
* Useful when an error involves several buffers (e.g. src/dst) and any one of
* their declaration sites helps the user locate the problem.
*/
inline std::string SpanHintSuffix(std::initializer_list<Span> spans) {
for (const auto &span : spans) {
std::string hint = SpanHintSuffix(span);
if (!hint.empty()) {
return hint;
}
}
return "";
}
} // namespace tl
} // namespace tvm
#endif // TVM_TL_SPAN_UTILS_H_