Skip to content

Commit b5e0f6b

Browse files
MasterJH5574tqchen
andauthored
[Diagnostic] Support constructing Diagnostic Error through ObjectRef (#13977)
This PR supports creating a diagnostic error from an arbitrary Object. Given that we are bringing the diagnostic error for general uses in the long term, in which case * not every Expr necessarily has a span, * we have well-implemented elegant location-aware printer for an object itself, * we may need to print some object other than Expr, or does even not have a span field, we support diagnostic error with arbitrary object to denote the location. Co-authored-by: Tianqi Chen <[email protected]>
1 parent b371c46 commit b5e0f6b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

include/tvm/ir/diagnostic.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class DiagnosticNode : public Object {
5656
DiagnosticLevel level;
5757
/*! \brief The span at which to report an error. */
5858
Span span;
59+
/*!
60+
* \brief The object location at which to report an error.
61+
*
62+
* The object loc provides a location when span is not always
63+
* available during transformation. The error reporter can
64+
* still pick up loc->span if necessary.
65+
*/
66+
ObjectRef loc;
5967
/*! \brief The diagnostic message. */
6068
String message;
6169

@@ -84,6 +92,18 @@ class Diagnostic : public ObjectRef {
8492
static DiagnosticBuilder Warning(Span span);
8593
static DiagnosticBuilder Note(Span span);
8694
static DiagnosticBuilder Help(Span span);
95+
// variants uses object location
96+
static DiagnosticBuilder Bug(ObjectRef loc);
97+
static DiagnosticBuilder Error(ObjectRef loc);
98+
static DiagnosticBuilder Warning(ObjectRef loc);
99+
static DiagnosticBuilder Note(ObjectRef loc);
100+
static DiagnosticBuilder Help(ObjectRef loc);
101+
// variants uses object ptr.
102+
static DiagnosticBuilder Bug(const Object* loc);
103+
static DiagnosticBuilder Error(const Object* loc);
104+
static DiagnosticBuilder Warning(const Object* loc);
105+
static DiagnosticBuilder Note(const Object* loc);
106+
static DiagnosticBuilder Help(const Object* loc);
87107

88108
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Diagnostic, ObjectRef, DiagnosticNode);
89109
};
@@ -102,6 +122,11 @@ class DiagnosticBuilder {
102122
/*! \brief The span of the diagnostic. */
103123
Span span;
104124

125+
/*!
126+
* \brief The object location at which to report an error.
127+
*/
128+
ObjectRef loc;
129+
105130
template <typename T>
106131
DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
107132
stream_ << val;
@@ -115,6 +140,8 @@ class DiagnosticBuilder {
115140

116141
DiagnosticBuilder(DiagnosticLevel level, Span span) : level(level), span(span) {}
117142

143+
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc) : level(level), loc(loc) {}
144+
118145
operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
119146

120147
private:

src/ir/diagnostic.cc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ DiagnosticBuilder Diagnostic::Help(Span span) {
6666
return DiagnosticBuilder(DiagnosticLevel::kHelp, span);
6767
}
6868

69+
DiagnosticBuilder Diagnostic::Bug(ObjectRef loc) {
70+
return DiagnosticBuilder(DiagnosticLevel::kBug, loc);
71+
}
72+
73+
DiagnosticBuilder Diagnostic::Error(ObjectRef loc) {
74+
return DiagnosticBuilder(DiagnosticLevel::kError, loc);
75+
}
76+
77+
DiagnosticBuilder Diagnostic::Warning(ObjectRef loc) {
78+
return DiagnosticBuilder(DiagnosticLevel::kWarning, loc);
79+
}
80+
81+
DiagnosticBuilder Diagnostic::Note(ObjectRef loc) {
82+
return DiagnosticBuilder(DiagnosticLevel::kNote, loc);
83+
}
84+
85+
DiagnosticBuilder Diagnostic::Help(ObjectRef loc) {
86+
return DiagnosticBuilder(DiagnosticLevel::kHelp, loc);
87+
}
88+
89+
DiagnosticBuilder Diagnostic::Bug(const Object* loc) { return Bug(GetRef<ObjectRef>(loc)); }
90+
91+
DiagnosticBuilder Diagnostic::Error(const Object* loc) { return Error(GetRef<ObjectRef>(loc)); }
92+
93+
DiagnosticBuilder Diagnostic::Note(const Object* loc) { return Note(GetRef<ObjectRef>(loc)); }
94+
95+
DiagnosticBuilder Diagnostic::Help(const Object* loc) { return Help(GetRef<ObjectRef>(loc)); }
96+
6997
/* Diagnostic Renderer */
7098
TVM_REGISTER_NODE_TYPE(DiagnosticRendererNode);
7199

@@ -284,7 +312,7 @@ DiagnosticRenderer TerminalRenderer(std::ostream& out) {
284312
});
285313
}
286314

287-
TVM_REGISTER_GLOBAL(DEFAULT_RENDERER).set_body_typed([]() { return TerminalRenderer(std::cout); });
315+
TVM_REGISTER_GLOBAL(DEFAULT_RENDERER).set_body_typed([]() { return TerminalRenderer(std::cerr); });
288316

289317
TVM_REGISTER_GLOBAL("diagnostics.GetRenderer").set_body_typed([]() { return GetRenderer(); });
290318

0 commit comments

Comments
 (0)