Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor diagnostic to avoid circular dependencies #6692

Merged
merged 7 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 1 addition & 47 deletions include/tvm/ir/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,22 @@
* \file diagnostic.h
* \brief A new diagnostic interface for TVM error reporting.
*
* A prototype of the new diagnostic reporting interface for TVM.
*
* Eventually we hope to promote this file to the top-level and
* replace the existing errors.h.
*/

#ifndef TVM_IR_DIAGNOSTIC_H_
#define TVM_IR_DIAGNOSTIC_H_

#include <tvm/ir/module.h>
#include <tvm/ir/span.h>
#include <tvm/parser/source_map.h>
#include <tvm/runtime/container.h>
#include <tvm/runtime/object.h>
#include <tvm/support/logging.h>

#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace tvm {

using tvm::parser::SourceMap;
using tvm::runtime::TypedPackedFunc;

extern const char* kTVM_INTERNAL_ERROR_MESSAGE;

#define ICHECK_INDENT " "

#define ICHECK_BINARY_OP(name, op, x, y) \
if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << std::endl \
<< ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": "

#define ICHECK(x) \
if (!(x)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT << "Check failed: " #x << " == false: "

#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y)
#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y)
#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y)
#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y)
#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y)
#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y)
#define ICHECK_NOTNULL(x) \
((x) == nullptr ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << __INDENT << "Check not null: " #x \
<< ' ', \
(x) : (x)) // NOLINT(*)

/*! \brief The diagnostic level, controls the printing of the message. */
enum class DiagnosticLevel : int {
kBug = 10,
kError = 20,
kWarning = 30,
kNote = 40,
kHelp = 50,
};

class DiagnosticBuilder;

/*! \brief A compiler diagnostic. */
Expand Down
1 change: 1 addition & 0 deletions include/tvm/ir/type_relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <tvm/ir/env_func.h>
#include <tvm/ir/module.h>
#include <tvm/ir/type.h>
#include <tvm/support/logging.h>

namespace tvm {

Expand Down
2 changes: 1 addition & 1 deletion include/tvm/relay/analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#ifndef TVM_RELAY_ANALYSIS_H_
#define TVM_RELAY_ANALYSIS_H_

#include <tvm/ir/diagnostic.h>
#include <tvm/ir/module.h>
#include <tvm/relay/adt.h>
#include <tvm/relay/expr.h>
#include <tvm/relay/function.h>
#include <tvm/relay/type.h>
#include <tvm/support/logging.h>

#include <string>
#include <unordered_map>
Expand Down
16 changes: 8 additions & 8 deletions include/tvm/runtime/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#ifndef TVM_RUNTIME_OBJECT_H_
#define TVM_RUNTIME_OBJECT_H_

#include <dmlc/logging.h>
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/support/logging.h>

#include <string>
#include <type_traits>
Expand Down Expand Up @@ -153,9 +153,9 @@ struct TypeIndex {
* ObjectRef leaf_ref(make_object<LeafObj>());
* // cast to a specific instance
* const LeafObj* leaf_ptr = leaf_ref.as<LeafObj>();
* CHECK(leaf_ptr != nullptr);
* ICHECK(leaf_ptr != nullptr);
* // can also cast to the base class.
* CHECK(leaf_ref.as<BaseObj>() != nullptr);
* ICHECK(leaf_ref.as<BaseObj>() != nullptr);
* }
*
* \endcode
Expand Down Expand Up @@ -756,7 +756,7 @@ struct ObjectPtrEqual {
*/
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \
ObjectName* CopyOnWrite() { \
CHECK(data_ != nullptr); \
ICHECK(data_ != nullptr); \
if (!data_.unique()) { \
auto n = make_object<ObjectName>(*(operator->())); \
ObjectPtr<Object>(std::move(n)).swap(data_); \
Expand Down Expand Up @@ -845,7 +845,7 @@ inline RefType GetRef(const ObjType* ptr) {
static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
"Can only cast to the ref of same container type");
if (!RefType::_type_is_nullable) {
CHECK(ptr != nullptr);
ICHECK(ptr != nullptr);
}
return RefType(ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
}
Expand All @@ -860,12 +860,12 @@ inline ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr) {
template <typename SubRef, typename BaseRef>
inline SubRef Downcast(BaseRef ref) {
if (ref.defined()) {
CHECK(ref->template IsInstance<typename SubRef::ContainerType>())
ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
<< "Downcast from " << ref->GetTypeKey() << " to " << SubRef::ContainerType::_type_key
<< " failed.";
} else {
CHECK(SubRef::_type_is_nullable) << "Downcast from nullptr to not nullable reference of "
<< SubRef::ContainerType::_type_key;
ICHECK(SubRef::_type_is_nullable) << "Downcast from nullptr to not nullable reference of "
<< SubRef::ContainerType::_type_key;
}
return SubRef(std::move(ref.data_));
}
Expand Down
46 changes: 46 additions & 0 deletions include/tvm/support/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#ifndef TVM_SUPPORT_LOGGING_H_
#define TVM_SUPPORT_LOGGING_H_

#include <dmlc/logging.h>

// a technique that enables overriding macro names on the number of parameters. This is used
// to define other macros below
#define GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
Expand Down Expand Up @@ -109,4 +111,48 @@
#define COND_CHECK_2(quit_on_assert, x) COND_CHECK_3(quit_on_assert, x, return false)
#define COND_LOG_2(quit_on_assert, x) COND_LOG_3(quit_on_assert, x, return false)

namespace tvm {

constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE =
"\n---------------------------------------------------------------\n"
"An internal invariant was violated during the execution of TVM.\n"
"Please read TVM's error reporting guidelines.\n"
"More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n"
"---------------------------------------------------------------\n";

#define ICHECK_INDENT " "

#define ICHECK_BINARY_OP(name, op, x, y) \
if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << std::endl \
<< ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": "

#define ICHECK(x) \
if (!(x)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT << "Check failed: " #x << " == false: "

#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y)
#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y)
#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y)
#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y)
#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y)
#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y)
#define ICHECK_NOTNULL(x) \
((x) == nullptr ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< kTVM_INTERNAL_ERROR_MESSAGE << __INDENT << "Check not null: " #x \
<< ' ', \
(x) : (x)) // NOLINT(*)

/*! \brief The diagnostic level, controls the printing of the message. */
enum class DiagnosticLevel : int {
kBug = 10,
kError = 20,
kWarning = 30,
kNote = 40,
kHelp = 50,
};

} // namespace tvm
#endif // TVM_SUPPORT_LOGGING_H_
11 changes: 2 additions & 9 deletions src/ir/diagnostic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

/*!
* \file src/ir/transform.cc
* \brief Infrastructure for transformation passes.
* \file src/ir/diagnostic.cc
* \brief Implementation of DiagnosticContext and friends.
*/
#include <tvm/ir/diagnostic.h>
#include <tvm/parser/source_map.h>
Expand All @@ -30,13 +30,6 @@ namespace tvm {

using tvm::parser::Source;

const char* kTVM_INTERNAL_ERROR_MESSAGE =
"\n---------------------------------------------------------------\n"
"An internal invariant was violated during the execution of TVM.\n"
"Please read TVM's error reporting guidelines.\n"
"More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n"
"---------------------------------------------------------------\n";

// failed to check to argument arg0.dims[0] != 0

/* Diagnostic */
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* \file parser.cc
* \brief A parser for TVM IR.
*/
#include <tvm/ir/diagnostic.h>
#include <tvm/ir/module.h>
#include <tvm/node/reflection.h>
#include <tvm/parser/parser.h>
Expand All @@ -31,6 +30,7 @@
#include <tvm/relay/transform.h>
#include <tvm/runtime/object.h>
#include <tvm/runtime/registry.h>
#include <tvm/support/logging.h>

#include <fstream>

Expand Down
2 changes: 1 addition & 1 deletion src/parser/span_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#ifndef TVM_PARSER_SPAN_CHECK_H_
#define TVM_PARSER_SPAN_CHECK_H_

#include <tvm/ir/diagnostic.h>
#include <tvm/ir/transform.h>
#include <tvm/ir/type_functor.h>
#include <tvm/relay/expr.h>
#include <tvm/relay/expr_functor.h>
#include <tvm/runtime/container.h>
#include <tvm/runtime/object.h>
#include <tvm/support/logging.h>

#include <fstream>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion src/relay/analysis/well_formed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* \file well_formed.cc
* \brief check that expression is well formed.
*/
#include <tvm/ir/diagnostic.h>
#include <tvm/relay/analysis.h>
#include <tvm/relay/expr_functor.h>
#include <tvm/relay/pattern_functor.h>
#include <tvm/support/logging.h>

#include <unordered_set>

Expand Down
2 changes: 1 addition & 1 deletion src/relay/op/nn/convolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef TVM_RELAY_OP_NN_CONVOLUTION_H_
#define TVM_RELAY_OP_NN_CONVOLUTION_H_

#include <tvm/ir/diagnostic.h>
#include <tvm/support/logging.h>
#include <tvm/tir/analysis.h>

#include <string>
Expand Down