From f2f9d208870e4bacac563ccd36e3be1c13d4f46a Mon Sep 17 00:00:00 2001 From: Robert Kimball Date: Sat, 17 Oct 2020 17:42:38 -0700 Subject: [PATCH] Refactor diagnostic to avoid circular dependencies (#6692) --- include/tvm/ir/diagnostic.h | 48 +------------------------------ include/tvm/ir/type_relation.h | 1 + include/tvm/relay/analysis.h | 2 +- include/tvm/runtime/object.h | 16 +++++------ include/tvm/support/logging.h | 46 +++++++++++++++++++++++++++++ src/ir/diagnostic.cc | 11 ++----- src/parser/parser.cc | 2 +- src/parser/span_check.h | 2 +- src/relay/analysis/well_formed.cc | 2 +- src/relay/op/nn/convolution.h | 2 +- 10 files changed, 63 insertions(+), 69 deletions(-) diff --git a/include/tvm/ir/diagnostic.h b/include/tvm/ir/diagnostic.h index 6b9807487bae7..2a2a6cd4e8678 100644 --- a/include/tvm/ir/diagnostic.h +++ b/include/tvm/ir/diagnostic.h @@ -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 -#include #include -#include -#include -#include -#include +#include #include -#include -#include 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. */ diff --git a/include/tvm/ir/type_relation.h b/include/tvm/ir/type_relation.h index 83323b01e419d..462588006c9ba 100644 --- a/include/tvm/ir/type_relation.h +++ b/include/tvm/ir/type_relation.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace tvm { diff --git a/include/tvm/relay/analysis.h b/include/tvm/relay/analysis.h index 26e5a65ddb5e4..5dd8370387319 100644 --- a/include/tvm/relay/analysis.h +++ b/include/tvm/relay/analysis.h @@ -24,12 +24,12 @@ #ifndef TVM_RELAY_ANALYSIS_H_ #define TVM_RELAY_ANALYSIS_H_ -#include #include #include #include #include #include +#include #include #include diff --git a/include/tvm/runtime/object.h b/include/tvm/runtime/object.h index e6ca832c70c21..b5cf77d590f60 100644 --- a/include/tvm/runtime/object.h +++ b/include/tvm/runtime/object.h @@ -23,8 +23,8 @@ #ifndef TVM_RUNTIME_OBJECT_H_ #define TVM_RUNTIME_OBJECT_H_ -#include #include +#include #include #include @@ -153,9 +153,9 @@ struct TypeIndex { * ObjectRef leaf_ref(make_object()); * // cast to a specific instance * const LeafObj* leaf_ptr = leaf_ref.as(); - * CHECK(leaf_ptr != nullptr); + * ICHECK(leaf_ptr != nullptr); * // can also cast to the base class. - * CHECK(leaf_ref.as() != nullptr); + * ICHECK(leaf_ref.as() != nullptr); * } * * \endcode @@ -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(*(operator->())); \ ObjectPtr(std::move(n)).swap(data_); \ @@ -845,7 +845,7 @@ inline RefType GetRef(const ObjType* ptr) { static_assert(std::is_base_of::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(const_cast(static_cast(ptr)))); } @@ -860,12 +860,12 @@ inline ObjectPtr GetObjectPtr(ObjType* ptr) { template inline SubRef Downcast(BaseRef ref) { if (ref.defined()) { - CHECK(ref->template IsInstance()) + ICHECK(ref->template IsInstance()) << "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_)); } diff --git a/include/tvm/support/logging.h b/include/tvm/support/logging.h index c318b89e5c51f..4322435c06b06 100644 --- a/include/tvm/support/logging.h +++ b/include/tvm/support/logging.h @@ -24,6 +24,8 @@ #ifndef TVM_SUPPORT_LOGGING_H_ #define TVM_SUPPORT_LOGGING_H_ +#include + // 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 @@ -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_ diff --git a/src/ir/diagnostic.cc b/src/ir/diagnostic.cc index ceadf78e2cfcc..148831dc3ab63 100644 --- a/src/ir/diagnostic.cc +++ b/src/ir/diagnostic.cc @@ -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 #include @@ -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 */ diff --git a/src/parser/parser.cc b/src/parser/parser.cc index 7dc55b0b519a0..9c9965ca588f1 100644 --- a/src/parser/parser.cc +++ b/src/parser/parser.cc @@ -21,7 +21,6 @@ * \file parser.cc * \brief A parser for TVM IR. */ -#include #include #include #include @@ -31,6 +30,7 @@ #include #include #include +#include #include diff --git a/src/parser/span_check.h b/src/parser/span_check.h index b9ba76df4b8fa..9a887474fe670 100644 --- a/src/parser/span_check.h +++ b/src/parser/span_check.h @@ -25,13 +25,13 @@ #ifndef TVM_PARSER_SPAN_CHECK_H_ #define TVM_PARSER_SPAN_CHECK_H_ -#include #include #include #include #include #include #include +#include #include #include diff --git a/src/relay/analysis/well_formed.cc b/src/relay/analysis/well_formed.cc index 5abbbc94fb364..0b6e043a0d216 100644 --- a/src/relay/analysis/well_formed.cc +++ b/src/relay/analysis/well_formed.cc @@ -21,10 +21,10 @@ * \file well_formed.cc * \brief check that expression is well formed. */ -#include #include #include #include +#include #include diff --git a/src/relay/op/nn/convolution.h b/src/relay/op/nn/convolution.h index cd334d7269ab1..935058c1a5b3b 100644 --- a/src/relay/op/nn/convolution.h +++ b/src/relay/op/nn/convolution.h @@ -24,7 +24,7 @@ #ifndef TVM_RELAY_OP_NN_CONVOLUTION_H_ #define TVM_RELAY_OP_NN_CONVOLUTION_H_ -#include +#include #include #include