tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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/ir/module.h>
30 
31 #include <sstream>
32 #include <string>
33 
34 namespace tvm {
35 
37 
39 enum class DiagnosticLevel : int {
40  kBug = 10,
41  kError = 20,
42  kWarning = 30,
43  kNote = 40,
44  kHelp = 50,
45 };
46 
47 class DiagnosticBuilder;
48 
50 class Diagnostic;
51 
53 class DiagnosticNode : public Object {
54  public:
69 
70  // override attr visitor
72  v->Visit("level", &level);
73  v->Visit("span", &span);
74  v->Visit("message", &message);
75  }
76 
77  bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const {
78  return equal(this->level, other->level) && equal(this->span, other->span) &&
79  equal(this->message, other->message);
80  }
81 
82  static constexpr const char* _type_key = "Diagnostic";
84 };
85 
86 class Diagnostic : public ObjectRef {
87  public:
88  TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message);
89 
90  static DiagnosticBuilder Bug(Span span);
91  static DiagnosticBuilder Error(Span span);
92  static DiagnosticBuilder Warning(Span span);
93  static DiagnosticBuilder Note(Span span);
94  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);
107 
109 };
110 
115  public:
118 
121 
124 
129 
130  template <typename T>
131  DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*)
132  stream_ << val;
133  return *this;
134  }
135 
136  DiagnosticBuilder() : level(DiagnosticLevel::kError), source_name(), span(Span()) {}
137 
139  : level(builder.level), source_name(builder.source_name), span(builder.span) {}
140 
141  DiagnosticBuilder(DiagnosticLevel level, Span span) : level(level), span(span) {}
142 
143  DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc) : level(level), loc(loc) {}
144 
145  operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); }
146 
147  private:
148  std::stringstream stream_;
149  friend class Diagnostic;
150 };
151 
155 class DiagnosticContext;
156 
167  public:
169 
170  // override attr visitor
172 
173  static constexpr const char* _type_key = "DiagnosticRenderer";
175 };
176 
178  public:
179  TVM_DLL DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)> render);
181  : DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)>()) {}
182 
183  void Render(const DiagnosticContext& ctx);
184 
186  ICHECK(get() != nullptr);
187  return static_cast<DiagnosticRendererNode*>(get_mutable());
188  }
189 
191 };
192 
194  public:
197 
200 
203 
205  v->Visit("module", &module);
206  v->Visit("diagnostics", &diagnostics);
207  }
208 
210  return equal(module, other->module) && equal(diagnostics, other->diagnostics);
211  }
212 
213  static constexpr const char* _type_key = "DiagnosticContext";
215 };
216 
217 class DiagnosticContext : public ObjectRef {
218  public:
219  TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer);
220  TVM_DLL static DiagnosticContext Default(const IRModule& source_map);
221 
225  void Emit(const Diagnostic& diagnostic);
226 
234  void EmitFatal(const Diagnostic& diagnostic);
235 
237  void Render();
238 
240  ICHECK(get() != nullptr);
241  return static_cast<DiagnosticContextNode*>(get_mutable());
242  }
243 
245 };
246 
247 DiagnosticRenderer TerminalRenderer(std::ostream& ostream);
248 
249 } // namespace tvm
250 #endif // TVM_IR_DIAGNOSTIC_H_
Display diagnostics in a given display format.
Definition: diagnostic.h:166
A compiler diagnostic message.
Definition: diagnostic.h:53
DiagnosticLevel level
The level.
Definition: diagnostic.h:56
Span span
The span at which to report an error.
Definition: diagnostic.h:58
Definition: diagnostic.h:86
DiagnosticContextNode * operator->()
Definition: diagnostic.h:239
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:171
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
SourceName source_name
The source name.
Definition: diagnostic.h:120
DiagnosticBuilder()
Definition: diagnostic.h:136
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
DiagnosticLevel level
The level.
Definition: diagnostic.h:117
Definition: diagnostic.h:193
DiagnosticBuilder(DiagnosticLevel level, Span span)
Definition: diagnostic.h:141
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:66
base class of all object containers.
Definition: object.h:167
DiagnosticRendererNode * operator->()
Definition: diagnostic.h:185
ObjectRef loc
The object location at which to report an error.
Definition: diagnostic.h:128
Definition: diagnostic.h:177
DiagnosticBuilder & operator<<(const T &val)
Definition: diagnostic.h:131
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Definition: source_map.h:120
A wrapper around std::stringstream to build a diagnostic.
Definition: diagnostic.h:114
String message
The diagnostic message.
Definition: diagnostic.h:68
The source name of a file span.
Definition: source_map.h:67
IRModule module
The Module to report against.
Definition: diagnostic.h:196
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
DiagnosticRenderer TerminalRenderer(std::ostream &ostream)
DiagnosticLevel
The diagnostic level, controls the printing of the message.
Definition: diagnostic.h:39
Definition: diagnostic.h:217
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
Array< Diagnostic > diagnostics
The set of diagnostics to report.
Definition: diagnostic.h:199
Base class of all object reference.
Definition: object.h:511
DiagnosticRenderer renderer
The renderer set for the context.
Definition: diagnostic.h:202
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Managed reference class to IRModuleNode.
Definition: module.h:348
Span span
The span of the diagnostic.
Definition: diagnostic.h:123
DiagnosticBuilder(const DiagnosticBuilder &builder)
Definition: diagnostic.h:138
bool SEqualReduce(const DiagnosticContextNode *other, SEqualReducer equal) const
Definition: diagnostic.h:209
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:204
void VisitAttrs(AttrVisitor *v)
Definition: diagnostic.h:71
bool SEqualReduce(const DiagnosticNode *other, SEqualReducer equal) const
Definition: diagnostic.h:77
DiagnosticRenderer()
Definition: diagnostic.h:180
TypedPackedFunc< void(DiagnosticContext ctx)> renderer
Definition: diagnostic.h:168
DiagnosticBuilder(DiagnosticLevel level, ObjectRef loc)
Definition: diagnostic.h:143
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728