This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 327e8cc [ERROR] TVM_FFI_CHECK throw error condition (#69)
327e8cc is described below
commit 327e8cc63c9517df7ec9f1abc5f9c599c1b78c4d
Author: Kathryn (Jinqi) Chen <[email protected]>
AuthorDate: Sun Sep 28 17:27:14 2025 -0700
[ERROR] TVM_FFI_CHECK throw error condition (#69)
---
include/tvm/ffi/error.h | 3 +++
tests/cpp/test_error.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/include/tvm/ffi/error.h b/include/tvm/ffi/error.h
index b4d9d3a..c22747e 100644
--- a/include/tvm/ffi/error.h
+++ b/include/tvm/ffi/error.h
@@ -362,6 +362,9 @@ TVM_FFI_CHECK_FUNC(_NE, !=)
#define TVM_FFI_ICHECK(x) \
if (!(x)) TVM_FFI_THROW(InternalError) << "Check failed: (" #x << ") is
false: "
+#define TVM_FFI_CHECK(ErrorKind, cond) \
+ if (!(cond)) TVM_FFI_THROW(ErrorKind) << "Check failed: (" #cond << ") is
false: "
+
#define TVM_FFI_ICHECK_LT(x, y) TVM_FFI_ICHECK_BINARY_OP(_LT, <, x, y)
#define TVM_FFI_ICHECK_GT(x, y) TVM_FFI_ICHECK_BINARY_OP(_GT, >, x, y)
#define TVM_FFI_ICHECK_LE(x, y) TVM_FFI_ICHECK_BINARY_OP(_LE, <=, x, y)
diff --git a/tests/cpp/test_error.cc b/tests/cpp/test_error.cc
index 157673d..f224af4 100644
--- a/tests/cpp/test_error.cc
+++ b/tests/cpp/test_error.cc
@@ -61,6 +61,50 @@ TEST(CheckError, Backtrace) {
::tvm::ffi::Error);
}
+TEST(CheckError, ValueError) {
+ int value = -5;
+ EXPECT_THROW(
+ {
+ try {
+ TVM_FFI_CHECK(ValueError, value >= 0) << "Value must be
non-negative, got " << value;
+ } catch (const Error& error) {
+ EXPECT_EQ(error.kind(), "ValueError");
+ std::string what = error.what();
+ EXPECT_NE(what.find("line"), std::string::npos);
+ EXPECT_NE(what.find("Check failed: (value >= 0) is false"),
std::string::npos);
+ EXPECT_NE(what.find("Value must be non-negative, got -5"),
std::string::npos);
+ throw;
+ }
+ },
+ ::tvm::ffi::Error);
+}
+
+TEST(CheckError, IndexError) {
+ int index = 10;
+ int array_size = 5;
+ EXPECT_THROW(
+ {
+ try {
+ TVM_FFI_CHECK(IndexError, index < array_size)
+ << "Index " << index << " out of bounds for array of size " <<
array_size;
+ } catch (const Error& error) {
+ EXPECT_EQ(error.kind(), "IndexError");
+ std::string what = error.what();
+ EXPECT_NE(what.find("line"), std::string::npos);
+ EXPECT_NE(what.find("Check failed: (index < array_size) is false"),
std::string::npos);
+ EXPECT_NE(what.find("Index 10 out of bounds for array of size 5"),
std::string::npos);
+ throw;
+ }
+ },
+ ::tvm::ffi::Error);
+}
+
+TEST(CheckError, PassingCondition) {
+ // This should not throw
+ EXPECT_NO_THROW(TVM_FFI_CHECK(ValueError, true));
+ EXPECT_NO_THROW(TVM_FFI_CHECK(IndexError, 5 < 10));
+}
+
TEST(Error, AnyConvert) {
Any any = Error("TypeError", "here", "test0");
Optional<Error> opt_err = any.as<Error>();