tvm
error.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 #ifndef TVM_RELAY_ERROR_H_
20 #define TVM_RELAY_ERROR_H_
21 
22 #include <tvm/ir/module.h>
23 
24 #include <sstream>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace tvm {
30 namespace relay {
47 struct ErrorBuilder {
48  public:
49  template <typename T>
50  ErrorBuilder& operator<<(const T& val) { // NOLINT(*)
51  stream_ << val;
52  return *this;
53  }
54 
55  private:
56  std::stringstream stream_;
57  friend class CompileError;
58 };
59 
63 class CompileError : public Error {
64  public:
71  explicit CompileError(const std::string& msg) : Error(msg), span(nullptr) {}
76  CompileError(const ErrorBuilder& err) : Error(err.stream_.str()), span(nullptr) {} // NOLINT(*)
81  CompileError(const CompileError& other) : Error(other.what()), span(other.span) {} // NOLINT(*)
84  CompileError() : Error(""), span(nullptr) {}
85 };
86 
108  public:
110  ErrorReporter() : errors_(), node_to_error_() {}
111 
119  void Report(const CompileError& err) {
120  if (!err.span.defined()) {
121  throw err;
122  }
123 
124  this->errors_.push_back(err);
125  }
126 
139  void ReportAt(const GlobalVar& global, const ObjectRef& node, std::stringstream& err) {
140  std::string err_msg = err.str();
141  this->ReportAt(global, node, CompileError(err_msg));
142  }
143 
156  void ReportAt(const GlobalVar& global, const ObjectRef& node, const CompileError& err);
157 
169  void RenderErrors(const IRModule& module, bool use_color = true);
170 
171  inline bool AnyErrors() { return errors_.size() != 0; }
172 
173  private:
174  std::vector<CompileError> errors_;
175  std::unordered_map<ObjectRef, std::vector<size_t>, ObjectPtrHash, ObjectPtrEqual> node_to_error_;
176  std::unordered_map<ObjectRef, GlobalVar, ObjectPtrHash, ObjectPtrEqual> node_to_gv_;
177 };
178 
179 } // namespace relay
180 } // namespace tvm
181 #endif // TVM_RELAY_ERROR_H_
Managed reference to GlobalVarNode.
Definition: expr.h:487
Managed reference class to IRModuleNode.
Definition: module.h:366
Definition: source_map.h:120
Custom Error class to be thrown during compilation.
Definition: error.h:63
CompileError()
default constructor.
Definition: error.h:84
CompileError(const ErrorBuilder &err)
construct error from error builder.
Definition: error.h:76
Span span
Location of the error.
Definition: error.h:66
CompileError(const CompileError &other)
copy constructor.
Definition: error.h:81
CompileError(const std::string &msg)
construct error from message.
Definition: error.h:71
An abstraction around how errors are stored and reported. Designed to be opaque to users,...
Definition: error.h:107
ErrorReporter()
default constructor.
Definition: error.h:110
void ReportAt(const GlobalVar &global, const ObjectRef &node, const CompileError &err)
Report an error against a program, using the full program error reporting strategy.
void RenderErrors(const IRModule &module, bool use_color=true)
Render all reported errors and exit the program.
void Report(const CompileError &err)
Report a CompileError.
Definition: error.h:119
bool AnyErrors()
Definition: error.h:171
void ReportAt(const GlobalVar &global, const ObjectRef &node, std::stringstream &err)
Report an error against a program, using the full program error reporting strategy.
Definition: error.h:139
Base class of all object reference.
Definition: object.h:519
bool defined() const
Definition: object.h:552
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A wrapper around std::stringstream to build error. include/tvm/ir/type.h Can be consumed by CompileEr...
Definition: error.h:47
ErrorBuilder & operator<<(const T &val)
Definition: error.h:50
ObjectRef equal functor.
Definition: object.h:665
ObjectRef hash functor.
Definition: object.h:655