|
| 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 | + |
| 20 | +/*! |
| 21 | + * \file diagnostic.h |
| 22 | + * \brief A new diagnostic interface for TVM error reporting. |
| 23 | + * |
| 24 | + * A prototype of the new diagnostic reporting interface for TVM. |
| 25 | + * |
| 26 | + * Eventually we hope to promote this file to the top-level and |
| 27 | + * replace the existing errors.h. |
| 28 | + */ |
| 29 | + |
| 30 | +#ifndef TVM_IR_DIAGNOSTIC_H_ |
| 31 | +#define TVM_IR_DIAGNOSTIC_H_ |
| 32 | + |
| 33 | +#include <tvm/ir/module.h> |
| 34 | +#include <tvm/ir/span.h> |
| 35 | +#include <tvm/parser/source_map.h> |
| 36 | +#include <tvm/runtime/container.h> |
| 37 | +#include <tvm/runtime/object.h> |
| 38 | +#include <tvm/support/logging.h> |
| 39 | + |
| 40 | +#include <fstream> |
| 41 | +#include <string> |
| 42 | +#include <utility> |
| 43 | +#include <vector> |
| 44 | + |
| 45 | +namespace tvm { |
| 46 | + |
| 47 | +using tvm::parser::SourceMap; |
| 48 | +using tvm::runtime::TypedPackedFunc; |
| 49 | + |
| 50 | +extern const char* kTVM_INTERNAL_ERROR_MESSAGE; |
| 51 | + |
| 52 | +#define ICHECK_INDENT " " |
| 53 | + |
| 54 | +#define ICHECK_BINARY_OP(name, op, x, y) \ |
| 55 | + if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \ |
| 56 | + dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ |
| 57 | + << kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ |
| 58 | + << ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": " |
| 59 | + |
| 60 | +#define ICHECK(x) \ |
| 61 | + if (!(x)) \ |
| 62 | + dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ |
| 63 | + << kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT << "Check failed: " #x << " == false: " |
| 64 | + |
| 65 | +#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y) |
| 66 | +#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y) |
| 67 | +#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y) |
| 68 | +#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y) |
| 69 | +#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y) |
| 70 | +#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y) |
| 71 | +#define ICHECK_NOTNULL(x) \ |
| 72 | + ((x) == nullptr ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ |
| 73 | + << kTVM_INTERNAL_ERROR_MESSAGE << __INDENT << "Check not null: " #x \ |
| 74 | + << ' ', \ |
| 75 | + (x) : (x)) // NOLINT(*) |
| 76 | + |
| 77 | +/*! \brief The diagnostic level, controls the printing of the message. */ |
| 78 | +enum class DiagnosticLevel : int { |
| 79 | + kBug = 10, |
| 80 | + kError = 20, |
| 81 | + kWarning = 30, |
| 82 | + kNote = 40, |
| 83 | + kHelp = 50, |
| 84 | +}; |
| 85 | + |
| 86 | +class DiagnosticBuilder; |
| 87 | + |
| 88 | +/*! \brief A compiler diagnostic. */ |
| 89 | +class Diagnostic; |
| 90 | + |
| 91 | +/*! \brief A compiler diagnostic message. */ |
| 92 | +class DiagnosticNode : public Object { |
| 93 | + public: |
| 94 | + /*! \brief The level. */ |
| 95 | + DiagnosticLevel level; |
| 96 | + /*! \brief The span at which to report an error. */ |
| 97 | + Span span; |
| 98 | + /*! \brief The diagnostic message. */ |
| 99 | + String message; |
| 100 | + |
| 101 | + // override attr visitor |
| 102 | + void VisitAttrs(AttrVisitor* v) { |
| 103 | + v->Visit("level", &level); |
| 104 | + v->Visit("span", &span); |
| 105 | + v->Visit("message", &message); |
| 106 | + } |
| 107 | + |
| 108 | + bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const { |
| 109 | + return equal(this->level, other->level) && equal(this->span, other->span) && |
| 110 | + equal(this->message, other->message); |
| 111 | + } |
| 112 | + |
| 113 | + static constexpr const char* _type_key = "Diagnostic"; |
| 114 | + TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticNode, Object); |
| 115 | +}; |
| 116 | + |
| 117 | +class Diagnostic : public ObjectRef { |
| 118 | + public: |
| 119 | + TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message); |
| 120 | + |
| 121 | + static DiagnosticBuilder Bug(Span span); |
| 122 | + static DiagnosticBuilder Error(Span span); |
| 123 | + static DiagnosticBuilder Warning(Span span); |
| 124 | + static DiagnosticBuilder Note(Span span); |
| 125 | + static DiagnosticBuilder Help(Span span); |
| 126 | + |
| 127 | + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Diagnostic, ObjectRef, DiagnosticNode); |
| 128 | +}; |
| 129 | + |
| 130 | +/*! |
| 131 | + * \brief A wrapper around std::stringstream to build a diagnostic. |
| 132 | + */ |
| 133 | +class DiagnosticBuilder { |
| 134 | + public: |
| 135 | + /*! \brief The level. */ |
| 136 | + DiagnosticLevel level; |
| 137 | + |
| 138 | + /*! \brief The source name. */ |
| 139 | + SourceName source_name; |
| 140 | + |
| 141 | + /*! \brief The span of the diagnostic. */ |
| 142 | + Span span; |
| 143 | + |
| 144 | + template <typename T> |
| 145 | + DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*) |
| 146 | + stream_ << val; |
| 147 | + return *this; |
| 148 | + } |
| 149 | + |
| 150 | + DiagnosticBuilder() : level(DiagnosticLevel::kError), source_name(), span(Span()) {} |
| 151 | + |
| 152 | + DiagnosticBuilder(const DiagnosticBuilder& builder) |
| 153 | + : level(builder.level), source_name(builder.source_name), span(builder.span) {} |
| 154 | + |
| 155 | + DiagnosticBuilder(DiagnosticLevel level, Span span) : level(level), span(span) {} |
| 156 | + |
| 157 | + operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); } |
| 158 | + |
| 159 | + private: |
| 160 | + std::stringstream stream_; |
| 161 | + friend class Diagnostic; |
| 162 | +}; |
| 163 | + |
| 164 | +/*! |
| 165 | + * \brief A diagnostic context for recording errors against a source file. |
| 166 | + */ |
| 167 | +class DiagnosticContext; |
| 168 | + |
| 169 | +/*! \brief Display diagnostics in a given display format. |
| 170 | + * |
| 171 | + * A diagnostic renderer is responsible for converting the |
| 172 | + * raw diagnostics into consumable output. |
| 173 | + * |
| 174 | + * For example the terminal renderer will render a sequence |
| 175 | + * of compiler diagnostics to std::out and std::err in |
| 176 | + * a human readable form. |
| 177 | + */ |
| 178 | +class DiagnosticRendererNode : public Object { |
| 179 | + public: |
| 180 | + TypedPackedFunc<void(DiagnosticContext ctx)> renderer; |
| 181 | + |
| 182 | + // override attr visitor |
| 183 | + void VisitAttrs(AttrVisitor* v) {} |
| 184 | + |
| 185 | + static constexpr const char* _type_key = "DiagnosticRenderer"; |
| 186 | + TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticRendererNode, Object); |
| 187 | +}; |
| 188 | + |
| 189 | +class DiagnosticRenderer : public ObjectRef { |
| 190 | + public: |
| 191 | + TVM_DLL DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)> render); |
| 192 | + TVM_DLL DiagnosticRenderer() |
| 193 | + : DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)>()) {} |
| 194 | + |
| 195 | + void Render(const DiagnosticContext& ctx); |
| 196 | + |
| 197 | + DiagnosticRendererNode* operator->() { |
| 198 | + CHECK(get() != nullptr); |
| 199 | + return static_cast<DiagnosticRendererNode*>(get_mutable()); |
| 200 | + } |
| 201 | + |
| 202 | + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode); |
| 203 | +}; |
| 204 | + |
| 205 | +class DiagnosticContextNode : public Object { |
| 206 | + public: |
| 207 | + /*! \brief The Module to report against. */ |
| 208 | + IRModule module; |
| 209 | + |
| 210 | + /*! \brief The set of diagnostics to report. */ |
| 211 | + Array<Diagnostic> diagnostics; |
| 212 | + |
| 213 | + /*! \brief The renderer set for the context. */ |
| 214 | + DiagnosticRenderer renderer; |
| 215 | + |
| 216 | + void VisitAttrs(AttrVisitor* v) { |
| 217 | + v->Visit("module", &module); |
| 218 | + v->Visit("diagnostics", &diagnostics); |
| 219 | + } |
| 220 | + |
| 221 | + bool SEqualReduce(const DiagnosticContextNode* other, SEqualReducer equal) const { |
| 222 | + return equal(module, other->module) && equal(diagnostics, other->diagnostics); |
| 223 | + } |
| 224 | + |
| 225 | + static constexpr const char* _type_key = "DiagnosticContext"; |
| 226 | + TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticContextNode, Object); |
| 227 | +}; |
| 228 | + |
| 229 | +class DiagnosticContext : public ObjectRef { |
| 230 | + public: |
| 231 | + TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer); |
| 232 | + TVM_DLL static DiagnosticContext Default(const IRModule& source_map); |
| 233 | + |
| 234 | + /*! \brief Emit a diagnostic. |
| 235 | + * \param diagnostic The diagnostic to emit. |
| 236 | + */ |
| 237 | + void Emit(const Diagnostic& diagnostic); |
| 238 | + |
| 239 | + /*! \brief Emit a diagnostic and then immediately attempt to render all errors. |
| 240 | + * |
| 241 | + * \param diagnostic The diagnostic to emit. |
| 242 | + * |
| 243 | + * Note: this will raise an exception if you would like to instead continue execution |
| 244 | + * use the Emit method instead. |
| 245 | + */ |
| 246 | + void EmitFatal(const Diagnostic& diagnostic); |
| 247 | + |
| 248 | + /*! \brief Render the errors and raise a DiagnosticError exception. */ |
| 249 | + void Render(); |
| 250 | + |
| 251 | + DiagnosticContextNode* operator->() { |
| 252 | + CHECK(get() != nullptr); |
| 253 | + return static_cast<DiagnosticContextNode*>(get_mutable()); |
| 254 | + } |
| 255 | + |
| 256 | + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticContext, ObjectRef, DiagnosticContextNode); |
| 257 | +}; |
| 258 | + |
| 259 | +DiagnosticRenderer TerminalRenderer(std::ostream& ostream); |
| 260 | + |
| 261 | +} // namespace tvm |
| 262 | +#endif // TVM_IR_DIAGNOSTIC_H_ |
0 commit comments