|
7 | 7 | #define TVM_RELAY_ERROR_H_ |
8 | 8 |
|
9 | 9 | #include <string> |
| 10 | +#include <vector> |
| 11 | +#include <sstream> |
10 | 12 | #include "./base.h" |
| 13 | +#include "./expr.h" |
| 14 | +#include "./module.h" |
11 | 15 |
|
12 | 16 | namespace tvm { |
13 | 17 | namespace relay { |
14 | 18 |
|
15 | | -struct Error : public dmlc::Error { |
16 | | - explicit Error(const std::string &msg) : dmlc::Error(msg) {} |
17 | | -}; |
| 19 | +#define RELAY_ERROR(msg) (RelayErrorStream() << msg) |
| 20 | + |
| 21 | +// Forward declaratio for RelayErrorStream. |
| 22 | +struct Error; |
| 23 | + |
| 24 | +/*! \brief A wrapper around std::stringstream. |
| 25 | + * |
| 26 | + * This is designed to avoid platform specific |
| 27 | + * issues compiling and using std::stringstream |
| 28 | + * for error reporting. |
| 29 | + */ |
| 30 | +struct RelayErrorStream { |
| 31 | + std::stringstream ss; |
| 32 | + |
| 33 | + template<typename T> |
| 34 | + RelayErrorStream& operator<<(const T& t) { |
| 35 | + ss << t; |
| 36 | + return *this; |
| 37 | + } |
18 | 38 |
|
19 | | -struct InternalError : public Error { |
20 | | - explicit InternalError(const std::string &msg) : Error(msg) {} |
| 39 | + std::string str() const { |
| 40 | + return ss.str(); |
| 41 | + } |
| 42 | + |
| 43 | + void Raise() const; |
21 | 44 | }; |
22 | 45 |
|
23 | | -struct FatalTypeError : public Error { |
24 | | - explicit FatalTypeError(const std::string &s) : Error(s) {} |
| 46 | +struct Error : public dmlc::Error { |
| 47 | + Span sp; |
| 48 | + explicit Error(const std::string& msg) : dmlc::Error(msg), sp() {} |
| 49 | + Error(const std::stringstream& msg) : dmlc::Error(msg.str()), sp() {} // NOLINT(*) |
| 50 | + Error(const RelayErrorStream& msg) : dmlc::Error(msg.str()), sp() {} // NOLINT(*) |
25 | 51 | }; |
26 | 52 |
|
27 | | -struct TypecheckerError : public Error { |
28 | | - explicit TypecheckerError(const std::string &msg) : Error(msg) {} |
| 53 | +/*! \brief An abstraction around how errors are stored and reported. |
| 54 | + * Designed to be opaque to users, so we can support a robust and simpler |
| 55 | + * error reporting mode, as well as a more complex mode. |
| 56 | + * |
| 57 | + * The first mode is the most accurate: we report a Relay error at a specific |
| 58 | + * Span, and then render the error message directly against a textual representation |
| 59 | + * of the program, highlighting the exact lines in which it occurs. This mode is not |
| 60 | + * implemented in this PR and will not work. |
| 61 | + * |
| 62 | + * The second mode is a general-purpose mode, which attempts to annotate the program's |
| 63 | + * textual format with errors. |
| 64 | + * |
| 65 | + * The final mode represents the old mode, if we report an error that has no span or |
| 66 | + * expression, we will default to throwing an exception with a textual representation |
| 67 | + * of the error and no indication of where it occured in the original program. |
| 68 | + * |
| 69 | + * The latter mode is not ideal, and the goal of the new error reporting machinery is |
| 70 | + * to avoid ever reporting errors in this style. |
| 71 | + */ |
| 72 | +class ErrorReporter { |
| 73 | + public: |
| 74 | + ErrorReporter() : errors_(), node_to_error_() {} |
| 75 | + |
| 76 | + /*! \brief Report a tvm::relay::Error. |
| 77 | + * |
| 78 | + * This API is useful for reporting spanned errors. |
| 79 | + * |
| 80 | + * \param err The error to report. |
| 81 | + */ |
| 82 | + void Report(const Error& err) { |
| 83 | + if (!err.sp.defined()) { |
| 84 | + throw err; |
| 85 | + } |
| 86 | + |
| 87 | + this->errors_.push_back(err); |
| 88 | + } |
| 89 | + |
| 90 | + /*! \brief Report an error against a program, using the full program |
| 91 | + * error reporting strategy. |
| 92 | + * |
| 93 | + * This error reporting method requires the global function in which |
| 94 | + * to report an error, the expression to report the error on, |
| 95 | + * and the error object. |
| 96 | + * |
| 97 | + * \param global The global function in which the expression is contained. |
| 98 | + * \param node The expression or type to report the error at. |
| 99 | + * \param err The error message to report. |
| 100 | + */ |
| 101 | + inline void ReportAt(const GlobalVar& global, const NodeRef& node, std::stringstream& err) { |
| 102 | + this->ReportAt(global, node, Error(err)); |
| 103 | + } |
| 104 | + |
| 105 | + /*! \brief Report an error against a program, using the full program |
| 106 | + * error reporting strategy. |
| 107 | + * |
| 108 | + * This error reporting method requires the global function in which |
| 109 | + * to report an error, the expression to report the error on, |
| 110 | + * and the error object. |
| 111 | + * |
| 112 | + * \param global The global function in which the expression is contained. |
| 113 | + * \param node The expression or type to report the error at. |
| 114 | + * \param err The error to report. |
| 115 | + */ |
| 116 | + void ReportAt(const GlobalVar& global, const NodeRef& node, const Error& err); |
| 117 | + |
| 118 | + /*! \brief Render all reported errors and exit the program. |
| 119 | + * |
| 120 | + * This function should be used after executing a pass to render reported errors. |
| 121 | + * |
| 122 | + * It will build an error message from the set of errors, depending on the error |
| 123 | + * reporting strategy. |
| 124 | + * |
| 125 | + * \param module The module to report errors on. |
| 126 | + * \param use_color Controls whether to colorize the output. |
| 127 | + */ |
| 128 | + void RenderErrors(const Module& module, bool use_color = true); |
| 129 | + |
| 130 | + inline bool AnyErrors() { |
| 131 | + return errors_.size() != 0; |
| 132 | + } |
| 133 | + |
| 134 | + private: |
| 135 | + std::vector<Error> errors_; |
| 136 | + std::unordered_map<NodeRef, std::vector<size_t>, NodeHash, NodeEqual> node_to_error_; |
| 137 | + std::unordered_map<NodeRef, GlobalVar, NodeHash, NodeEqual> node_to_gv_; |
29 | 138 | }; |
30 | 139 |
|
31 | 140 | } // namespace relay |
|
0 commit comments