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  ffi::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;
79 };
80 
81 class Diagnostic : public ObjectRef {
82  public:
83  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
84 
85  static DiagnosticBuilder Bug(Span span);
88  static DiagnosticBuilder Note(Span span);
89  static DiagnosticBuilder Help(Span span);
90  // variants uses object location
91  static DiagnosticBuilder Bug(ObjectRef loc);
92  static DiagnosticBuilder Error(ObjectRef loc);
93  static DiagnosticBuilder Warning(ObjectRef loc);
94  static DiagnosticBuilder Note(ObjectRef loc);
95  static DiagnosticBuilder Help(ObjectRef loc);
96  // variants uses object ptr.
97  static DiagnosticBuilder Bug(const Object* loc);
98  static DiagnosticBuilder Error(const Object* loc);
99  static DiagnosticBuilder Warning(const Object* loc);
100  static DiagnosticBuilder Note(const Object* loc);
101  static DiagnosticBuilder Help(const Object* loc);
102 
104 };
105 
110  public:
113 
116 
119 
123  ObjectRef loc;
124 
125  template <typename T>
126  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
127  stream_ << val;
128  return *this;
129  }
130 
132 
134  : level(builder.level), source_name(builder.source_name), span(builder.span) {}
135 
137 
139 
140  operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
141 
142  private:
143  std::stringstream stream_;
144  friend class Diagnostic;
145 };
146 
150 class DiagnosticContext;
151 
161 class DiagnosticRendererNode : public Object {
162  public:
163  ffi::TypedFunction<void(DiagnosticContext ctx)> renderer;
164 
165  static void RegisterReflection() {
166  namespace refl = tvm::ffi::reflection;
167  refl::ObjectDef<DiagnosticRendererNode>().def_ro("renderer", &DiagnosticRendererNode::renderer);
168  }
170 };
171 
172 class DiagnosticRenderer : public ObjectRef {
173  public:
174  TVM_DLL DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)> render);
176  : DiagnosticRenderer(ffi::TypedFunction<void(DiagnosticContext ctx)>()) {}
177 
178  void Render(const DiagnosticContext& ctx);
179 
181  ICHECK(get() != nullptr);
182  return static_cast<DiagnosticRendererNode*>(get_mutable());
183  }
184 
187 };
188 
189 class DiagnosticContextNode : public Object {
190  public:
193 
195  ffi::Array<Diagnostic> diagnostics;
196 
199 
200  static void RegisterReflection() {
201  namespace refl = tvm::ffi::reflection;
202  refl::ObjectDef<DiagnosticContextNode>()
203  .def_ro("module", &DiagnosticContextNode::module)
204  .def_ro("diagnostics", &DiagnosticContextNode::diagnostics);
205  }
206 
207  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
209 };
210 
211 class DiagnosticContext : public ObjectRef {
212  public:
213  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
214  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
215 
219  void Emit(const Diagnostic& diagnostic);
220 
228  void EmitFatal(const Diagnostic& diagnostic);
229 
231  void Render();
232 
234  ICHECK(get() != nullptr);
235  return static_cast<DiagnosticContextNode*>(get_mutable());
236  }
237 
240 };
241 
242 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
243 
244 } // namespace tvm
245 #endif // TVM_IR_DIAGNOSTIC_H_
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:109
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc)
Definition: diagnostic.h:138
friend class Diagnostic
Definition: diagnostic.h:144
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:133
Span span
The span of the diagnostic.
Definition: diagnostic.h:118
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:136
SourceName source_name
The source name.
Definition: diagnostic.h:115
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:126
DiagnosticLevel level
The level.
Definition: diagnostic.h:112
DiagnosticBuilder()
Definition: diagnostic.h:131
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:123
Definition: diagnostic.h:189
ffi::Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:195
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:207
static void RegisterReflection()
Definition: diagnostic.h:200
IRModule module
The Module to report against.
Definition: diagnostic.h:192
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:198
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("DiagnosticContext", DiagnosticContextNode, Object)
Definition: diagnostic.h:211
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.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DiagnosticContext, ObjectRef, DiagnosticContextNode)
DiagnosticContextNode * operator->()
Definition: diagnostic.h:233
DiagnosticContext(const IRModule &module, const DiagnosticRenderer &renderer)
static DiagnosticContext Default(const IRModule &source_map)
A compiler diagnostic message.
Definition: diagnostic.h:52
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: diagnostic.h:77
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:65
ffi::String message
The diagnostic message.
Definition: diagnostic.h:67
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
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("Diagnostic", DiagnosticNode, Object)
Display diagnostics in a given display format.
Definition: diagnostic.h:161
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("DiagnosticRenderer", DiagnosticRendererNode, Object)
ffi::TypedFunction< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:163
static void RegisterReflection()
Definition: diagnostic.h:165
Definition: diagnostic.h:172
DiagnosticRenderer(ffi::TypedFunction< void(DiagnosticContext ctx)> render)
void Render(const DiagnosticContext &ctx)
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:180
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode)
DiagnosticRenderer()
Definition: diagnostic.h:175
Definition: diagnostic.h:81
static DiagnosticBuilder Error(const Object *loc)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(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:256
The source name of a file span.
Definition: source_map.h:64
Definition: source_map.h:111
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