tvm
diagnostic.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef TVM_IR_DIAGNOSTIC_H_
27 #define TVM_IR_DIAGNOSTIC_H_
28 
29 #include <tvm/ffi/reflection/registry.h>
30 #include <tvm/ir/module.h>
31 
32 #include <sstream>
33 #include <string>
34 
35 namespace tvm {
36 
38 enum class DiagnosticLevel : int {
39  kBug = 10,
40  kError = 20,
41  kWarning = 30,
42  kNote = 40,
43  kHelp = 50,
44 };
45 
46 class DiagnosticBuilder;
47 
49 class Diagnostic;
50 
52 class DiagnosticNode : public Object {
53  public:
65  ObjectRef loc;
67  String message;
68 
69  static void RegisterReflection() {
70  namespace refl = tvm::ffi::reflection;
71  refl::ObjectDef<DiagnosticNode>()
72  .def_ro("level", &DiagnosticNode::level)
73  .def_ro("span", &DiagnosticNode::span)
74  .def_ro("message", &DiagnosticNode::message);
75  }
76 
77  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
78  static constexpr const char* _type_key = "Diagnostic";
80 };
81 
82 class Diagnostic : public ObjectRef {
83  public:
84  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
85 
86  static DiagnosticBuilder Bug(Span span);
89  static DiagnosticBuilder Note(Span span);
90  static DiagnosticBuilder Help(Span span);
91  // variants uses object location
92  static DiagnosticBuilder Bug(ObjectRef loc);
93  static DiagnosticBuilder Error(ObjectRef loc);
94  static DiagnosticBuilder Warning(ObjectRef loc);
95  static DiagnosticBuilder Note(ObjectRef loc);
96  static DiagnosticBuilder Help(ObjectRef loc);
97  // variants uses object ptr.
98  static DiagnosticBuilder Bug(const Object* loc);
99  static DiagnosticBuilder Error(const Object* loc);
100  static DiagnosticBuilder Warning(const Object* loc);
101  static DiagnosticBuilder Note(const Object* loc);
102  static DiagnosticBuilder Help(const Object* loc);
103 
105 };
106 
111  public:
114 
117 
120 
124  ObjectRef loc;
125 
126  template <typename T>
127  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
128  stream_ << val;
129  return *this;
130  }
131 
133 
135  : level(builder.level), source_name(builder.source_name), span(builder.span) {}
136 
138 
140 
141  operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
142 
143  private:
144  std::stringstream stream_;
145  friend class Diagnostic;
146 };
147 
151 class DiagnosticContext;
152 
162 class DiagnosticRendererNode : public Object {
163  public:
164  ffi::TypedFunction<void(DiagnosticContext ctx)> renderer;
165 
166  static void RegisterReflection() {
167  namespace refl = tvm::ffi::reflection;
168  refl::ObjectDef<DiagnosticRendererNode>().def_ro("renderer", &DiagnosticRendererNode::renderer);
169  }
170 
171  static constexpr const char* _type_key = "DiagnosticRenderer";
173 };
174 
175 class DiagnosticRenderer : public ObjectRef {
176  public:
177  TVM_DLL DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)> render);
179  : DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)>()) {}
180 
181  void Render(const DiagnosticContext& ctx);
182 
184  ICHECK(get() != nullptr);
185  return static_cast<DiagnosticRendererNode*>(get_mutable());
186  }
187 
189 };
190 
191 class DiagnosticContextNode : public Object {
192  public:
195 
197  Array<Diagnostic> diagnostics;
198 
201 
202  static void RegisterReflection() {
203  namespace refl = tvm::ffi::reflection;
204  refl::ObjectDef<DiagnosticContextNode>()
205  .def_ro("module", &DiagnosticContextNode::module)
206  .def_ro("diagnostics", &DiagnosticContextNode::diagnostics);
207  }
208 
209  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
210  static constexpr const char* _type_key = "DiagnosticContext";
212 };
213 
214 class DiagnosticContext : public ObjectRef {
215  public:
216  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
217  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
218 
222  void Emit(const Diagnostic& diagnostic);
223 
231  void EmitFatal(const Diagnostic& diagnostic);
232 
234  void Render();
235 
237  ICHECK(get() != nullptr);
238  return static_cast<DiagnosticContextNode*>(get_mutable());
239  }
240 
242 };
243 
244 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
245 
246 } // namespace tvm
247 #endif // TVM_IR_DIAGNOSTIC_H_
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:110
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc)
Definition: diagnostic.h:139
friend class Diagnostic
Definition: diagnostic.h:145
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:134
Span span
The span of the diagnostic.
Definition: diagnostic.h:119
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:137
SourceName source_name
The source name.
Definition: diagnostic.h:116
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:127
DiagnosticLevel level
The level.
Definition: diagnostic.h:113
DiagnosticBuilder()
Definition: diagnostic.h:132
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:124
Definition: diagnostic.h:191
static constexpr const char * _type_key
Definition: diagnostic.h:210
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticContextNode, Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:209
static void RegisterReflection()
Definition: diagnostic.h:202
Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:197
IRModule module
The Module to report against.
Definition: diagnostic.h:194
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:200
Definition: diagnostic.h:214
void Render()
Render the errors and raise a DiagnosticError exception.
void Emit(const Diagnostic &diagnostic)
Emit a diagnostic.
void EmitFatal(const Diagnostic &diagnostic)
Emit a diagnostic and then immediately attempt to render all errors.
DiagnosticContextNode * operator->()
Definition: diagnostic.h:236
DiagnosticContext(const IRModule &module, const DiagnosticRenderer &renderer)
static DiagnosticContext Default(const IRModule &source_map)
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticContext, ObjectRef, DiagnosticContextNode)
A compiler diagnostic message.
Definition: diagnostic.h:52
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:77
static constexpr const char * _type_key
Definition: diagnostic.h:78
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:65
String message
The diagnostic message.
Definition: diagnostic.h:67
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticNode, Object)
DiagnosticLevel level
The level.
Definition: diagnostic.h:55
static void RegisterReflection()
Definition: diagnostic.h:69
Span span
The span at which to report an error.
Definition: diagnostic.h:57
Display diagnostics in a given display format.
Definition: diagnostic.h:162
static constexpr const char * _type_key
Definition: diagnostic.h:171
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticRendererNode, Object)
ffi::TypedFunction< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:164
static void RegisterReflection()
Definition: diagnostic.h:166
Definition: diagnostic.h:175
DiagnosticRenderer(ffi::TypedFunction< void(DiagnosticContext ctx)> render)
void Render(const DiagnosticContext &ctx)
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:183
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode)
DiagnosticRenderer()
Definition: diagnostic.h:178
Definition: diagnostic.h:82
static DiagnosticBuilder Error(const Object *loc)
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Diagnostic, ObjectRef, DiagnosticNode)
static DiagnosticBuilder Bug(ObjectRef loc)
Diagnostic(DiagnosticLevel level, Span span, const std::string &message)
static DiagnosticBuilder Warning(Span span)
static DiagnosticBuilder Note(ObjectRef loc)
static DiagnosticBuilder Warning(const Object *loc)
static DiagnosticBuilder Bug(Span span)
static DiagnosticBuilder Help(Span span)
static DiagnosticBuilder Note(const Object *loc)
static DiagnosticBuilder Bug(const Object *loc)
static DiagnosticBuilder Help(ObjectRef loc)
static DiagnosticBuilder Warning(ObjectRef loc)
static DiagnosticBuilder Error(Span span)
static DiagnosticBuilder Help(const Object *loc)
static DiagnosticBuilder Error(ObjectRef loc)
static DiagnosticBuilder Note(Span span)
Managed reference class to IRModuleNode.
Definition: module.h:257
The source name of a file span.
Definition: source_map.h:65
Definition: source_map.h:113
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
DiagnosticRenderer TerminalRenderer(std::ostream &ostream)
DiagnosticLevel
The diagnostic level, controls the printing of the message.
Definition: diagnostic.h:38