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 
24 #ifndef TVM_IR_ERROR_H_
25 #define TVM_IR_ERROR_H_
26 
27 #include <tvm/ir/module.h>
28 #include <tvm/ir/span.h>
29 
30 #include <sstream>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 namespace tvm {
52 struct ErrorBuilder {
53  public:
54  template <typename T>
55  ErrorBuilder& operator<<(const T& val) { // NOLINT(*)
56  stream_ << val;
57  return *this;
58  }
59 
60  private:
61  std::stringstream stream_;
62  friend class CompileError;
63 };
64 
68 class CompileError : public Error {
69  public:
76  explicit CompileError(const std::string& msg) : Error(msg), span(nullptr) {}
81  CompileError(const ErrorBuilder& err) : Error(err.stream_.str()), span(nullptr) {} // NOLINT(*)
86  CompileError(const CompileError& other) : Error(other.what()), span(other.span) {} // NOLINT(*)
89  CompileError() : Error(""), span(nullptr) {}
90 };
91 
113  public:
115  ErrorReporter() : errors_(), node_to_error_() {}
116 
124  void Report(const CompileError& err) {
125  if (!err.span.defined()) {
126  throw err;
127  }
128 
129  this->errors_.push_back(err);
130  }
131 
144  void ReportAt(const GlobalVar& global, const ObjectRef& node, std::stringstream& err) {
145  std::string err_msg = err.str();
146  this->ReportAt(global, node, CompileError(err_msg));
147  }
148 
161  void ReportAt(const GlobalVar& global, const ObjectRef& node, const CompileError& err);
162 
174  void RenderErrors(const IRModule& module, bool use_color = true);
175 
176  inline bool AnyErrors() { return errors_.size() != 0; }
177 
178  private:
179  std::vector<CompileError> errors_;
180  std::unordered_map<ObjectRef, std::vector<size_t>, ObjectPtrHash, ObjectPtrEqual> node_to_error_;
181  std::unordered_map<ObjectRef, GlobalVar, ObjectPtrHash, ObjectPtrEqual> node_to_gv_;
182 };
183 
184 } // namespace tvm
185 #endif // TVM_IR_ERROR_H_
ObjectRef equal functor.
Definition: object.h:634
Custom Error class to be thrown during compilation.
Definition: error.h:68
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
An abstraction around how errors are stored and reported. Designed to be opaque to users...
Definition: error.h:112
A wrapper around std::stringstream to build error.
Definition: error.h:52
Span information for debugging purposes.
Definition: span.h:115
bool defined() const
Definition: object.h:544
ErrorBuilder & operator<<(const T &val)
Definition: error.h:55
CompileError(const ErrorBuilder &err)
construct error from error builder.
Definition: error.h:81
ObjectRef hash functor.
Definition: object.h:624
Managed reference to GlobalVarNode.
Definition: expr.h:475
Span span
Location of the error.
Definition: error.h:71
friend class CompileError
Definition: error.h:62
Base class of all object reference.
Definition: object.h:511
void Report(const CompileError &err)
Report a CompileError.
Definition: error.h:124
CompileError(const std::string &msg)
construct error from message.
Definition: error.h:76
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:144
CompileError(const CompileError &other)
copy constructor.
Definition: error.h:86
Managed reference class to IRModuleNode.
Definition: module.h:352
CompileError()
default constructor.
Definition: error.h:89
ErrorReporter()
default constructor.
Definition: error.h:115
bool AnyErrors()
Definition: error.h:176