-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add ffi::Expected<T> for exception-free error handling #399
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @guan404ming, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the error handling capabilities within the FFI layer by introducing a new Expected type. This type provides a robust, exception-free mechanism for FFI functions to communicate either a successful result or a detailed error, aligning with modern C++ practices and similar patterns in other languages. The changes ensure that FFI calls can explicitly manage potential failures without relying on exceptions for control flow, leading to more predictable and maintainable code. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces ffi::Expected<T> for exception-free error handling, a valuable addition for creating more robust FFI functions. The implementation is comprehensive, covering the Expected<T> class, integration with tvm::ffi::Function via CallExpected, and support for packed functions returning Expected<T>. The tests are thorough and cover many important use cases.
The overall approach of using tvm::ffi::Any for storage within Expected<T> is a clever way to leverage the existing FFI infrastructure. I have a few suggestions to simplify the TypeTraits specialization and to add move semantics for value/error accessors, which would improve code clarity and performance. Overall, this is a well-executed feature.
include/tvm/ffi/expected.h
Outdated
| TVM_FFI_INLINE T value() const { | ||
| if (is_err()) { | ||
| TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains error"; | ||
| } | ||
| return data_.cast<T>(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value() method is const-qualified, which means it always returns a copy of the contained value. This can be inefficient when the Expected object is an rvalue (e.g., a temporary returned from a function). Consider adding an rvalue-qualified overload to allow moving the value out. This would improve performance in scenarios like std::move(expected).value().
You can change the existing method to be const&-qualified and add a &&-qualified overload:
TVM_FFI_INLINE T value() const& {
if (is_err()) {
TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains error";
}
return data_.cast<T>();
}
TVM_FFI_INLINE T value() && {
if (is_err()) {
TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains error";
}
return std::move(data_).cast<T>();
}
include/tvm/ffi/expected.h
Outdated
| TVM_FFI_INLINE Error error() const { | ||
| TVM_FFI_ICHECK(is_err()) << "Expected does not contain an error"; | ||
| return data_.cast<Error>(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to value(), the error() method is const-qualified and always returns a copy. You can add an rvalue-qualified overload to allow moving the Error object out, which is more efficient.
TVM_FFI_INLINE Error error() const& {
TVM_FFI_ICHECK(is_err()) << "Expected does not contain an error";
return data_.cast<Error>();
}
TVM_FFI_INLINE Error error() && {
TVM_FFI_ICHECK(is_err()) << "Expected does not contain an error";
return std::move(data_).cast<Error>();
}| TVM_FFI_INLINE static void CopyToAnyView(const Expected<T>& src, TVMFFIAny* result) { | ||
| // Extract value from src.data_ and copy it properly | ||
| const TVMFFIAny* src_any = reinterpret_cast<const TVMFFIAny*>(&src.data_); | ||
|
|
||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| // It contains T, copy it out and move to result | ||
| T value = TypeTraits<T>::CopyFromAnyViewAfterCheck(src_any); | ||
| TypeTraits<T>::MoveToAny(std::move(value), result); | ||
| } else { | ||
| // It contains Error, copy it out and move to result | ||
| Error err = TypeTraits<Error>::CopyFromAnyViewAfterCheck(src_any); | ||
| TypeTraits<Error>::MoveToAny(std::move(err), result); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of CopyToAnyView is correct but can be simplified. Instead of checking the type and manually performing a copy-then-move for each case, you can leverage the Any type's copy constructor to handle the logic. This makes the code more concise and less error-prone, as it relies on the already-tested Any copy semantics.
TVM_FFI_INLINE static void CopyToAnyView(const Expected<T>& src, TVMFFIAny* result) {
// An Expected<T> is represented by its underlying value (T or Error)
// at the FFI boundary. We can simply copy the contained Any.
Any copied_any = src.data_;
*result = details::AnyUnsafe::MoveAnyToTVMFFIAny(std::move(copied_any));
}| TVM_FFI_INLINE static void MoveToAny(Expected<T> src, TVMFFIAny* result) { | ||
| // Extract value from src.data_ and move it properly | ||
| TVMFFIAny* src_any = reinterpret_cast<TVMFFIAny*>(&src.data_); | ||
|
|
||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| // It contains T, move it out and move to result | ||
| T value = TypeTraits<T>::MoveFromAnyAfterCheck(src_any); | ||
| TypeTraits<T>::MoveToAny(std::move(value), result); | ||
| } else { | ||
| // It contains Error, move it out and move to result | ||
| Error err = TypeTraits<Error>::MoveFromAnyAfterCheck(src_any); | ||
| TypeTraits<Error>::MoveToAny(std::move(err), result); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to CopyToAnyView, the MoveToAny implementation can be greatly simplified. Since src is passed by value (effectively a move), you can move the underlying Any object directly. This is more readable and directly expresses the intent of moving the contained value.
TVM_FFI_INLINE static void MoveToAny(Expected<T> src, TVMFFIAny* result) {
// An Expected<T> is represented by its underlying value (T or Error)
// at the FFI boundary. We can simply move the contained Any.
*result = details::AnyUnsafe::MoveAnyToTVMFFIAny(std::move(src.data_));
}2f70b38 to
f1a4dc9
Compare
|
Thanks a lot for the contribution, this would benefit from careful reviews, @junrushao @DarkSharpness @Ubospica can you help |
|
@guan404ming Thanks for the contribution! I will take a closer look sometime this week to provide a round of review. |
include/tvm/ffi/expected.h
Outdated
| */ | ||
| TVM_FFI_INLINE T value() const { | ||
| if (is_err()) { | ||
| TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains error"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If value() failed, it appears that it throws an error here but doesn't preserve the original error. Is it possible to retain the original error information?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Changed value() to throw the contained error directly instead of a generic RuntimeError:
if (is_err()) {
throw data_.cast<Error>();
}
| if (is_err()) { | ||
| TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains error"; | ||
| } | ||
| return data_.cast<T>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For large data, we can use move instead of copy, so I agree with Gemini that we can add an overload function here:
TVM_FFI_INLINE T value() && {
if (is_err()) { throw error(); }
return std::move(data_).cast<T>();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree as well. I've added both const& and && qualified overloads for value():
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we might not need both here, @tqchen would like to hear your opinion if we wanna keep one of them
include/tvm/ffi/expected.h
Outdated
| * \brief Check if the Expected contains an error. | ||
| * \return True if contains error, false if contains success value. | ||
| */ | ||
| TVM_FFI_INLINE bool is_err() const { return data_.as<Error>().has_value(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is likely that ObjectRef as a base class of Error can have both is_ok()=True and is_err()=True, since CheckAnyStrict relies on inheritance checking, not type matching. I would suggest returning !is_ok() here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it would be helpful to enhance the test cases on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed is_err() to return !is_ok() and also fixed is_ok() to check for Error first.
include/tvm/ffi/expected.h
Outdated
| /*! | ||
| * \brief Access the error value. | ||
| * \return The error value. | ||
| * \note Behavior is undefined if the Expected contains a success value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's instead of saying "behavior is undefined", say it throws a RuntimeError on bad access.
Perhaps change the function body into
if (!is_err()) {
TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains value, not error";
}
to mimic value() impl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I've updated the docstring and changed from TVM_FFI_ICHECK to TVM_FFI_THROW(RuntimeError)
f1a4dc9 to
486f68e
Compare
486f68e to
ad48e1b
Compare
include/tvm/ffi/expected.h
Outdated
| /*! | ||
| * \brief Helper function to create Expected::Err. | ||
| * \param error The error value. | ||
| * \return Expected<Any> containing the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's fix the comment here, should return Expected<T> same as the ExpectedOk case.
include/tvm/ffi/expected.h
Outdated
| * \return Expected<Any> containing the error. | ||
| * \note Returns Expected<Any> to allow usage in contexts where T is inferred. | ||
| */ | ||
| template <typename T = Any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps template <typename T> is cleaner? Do we have to default to Any here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be better. Thanks.
| * \return The success value if present, otherwise the default value. | ||
| */ | ||
| template <typename U = std::remove_cv_t<T>> | ||
| TVM_FFI_INLINE T value_or(U&& default_value) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function might be rarely used, because the philosophy of Expected<T> is handling error explicitly. Let's leave it for discussion here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe we should keep this since that is kind of standard practice std::optional and C++23 std::expected both have value_or.
include/tvm/ffi/expected.h
Outdated
| TVM_FFI_INLINE static void CopyToAnyView(const Expected<T>& src, TVMFFIAny* result) { | ||
| const TVMFFIAny* src_any = reinterpret_cast<const TVMFFIAny*>(&src.data_); | ||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| TypeTraits<T>::MoveToAny(TypeTraits<T>::CopyFromAnyViewAfterCheck(src_any), result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We possibly don't need to create a copy of T then move it.
Possible simplification:
if (src.is_err()) {
TypeTraits<Error>::CopyToAnyView(src.error(), result);
} else {
TypeTraits<T>::CopyToAnyView(src.value(), result);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is more clearer.
include/tvm/ffi/expected.h
Outdated
| TVM_FFI_INLINE static void MoveToAny(Expected<T> src, TVMFFIAny* result) { | ||
| TVMFFIAny* src_any = reinterpret_cast<TVMFFIAny*>(&src.data_); | ||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| TypeTraits<T>::MoveToAny(TypeTraits<T>::MoveFromAnyAfterCheck(src_any), result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto,
if (src.is_err()) {
TypeTraits<Error>::MoveToAny(std::move(src).error(), result);
} else {
TypeTraits<T>::MoveToAny(std::move(src).value(), result);
}
include/tvm/ffi/expected.h
Outdated
| } | ||
|
|
||
| TVM_FFI_INLINE static std::string TypeSchema() { | ||
| return R"({"type":"Expected","args":[)" + details::TypeSchema<T>::v() + "]}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also mention {"type":"ffi.Error"} in type schema?
include/tvm/ffi/expected.h
Outdated
| return std::nullopt; | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this
include/tvm/ffi/function.h
Outdated
|
|
||
| if (ret_code == 0) { | ||
| // Success - cast result to T and return Ok | ||
| return Expected<T>::Ok(std::move(result).cast<T>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a tricky part. If the result does not match the type T, cast<T>() will throw, and this error will escape CallExpected.
Some proposed change: (feel free to brainstorm better ones)
if (ret_code == 0) {
if (auto val = std::move(result).template as<T>()) {
return Expected<T>::Ok(std::move(*val));
} else {
return Expected<T>::Err(Error("TypeError",
"CallExpected: result type mismatch, expected " + TypeTraits<T>::TypeStr(), ""));
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. The implementation looks good. I think there is a potential edge case that we probably don't want ot try casting if T = Any. Here is updated implementation
if (ret_code == 0) {
if constexpr (std::is_same_v<T, Any>) {
return Expected<T>::Ok(std::move(result));
} else {
if (auto val = std::move(result).template as<T>()) {
return Expected<T>::Ok(std::move(*val));
} else {
return Expected<T>::Err(Error(
"TypeError",
"CallExpected: result type mismatch, expected " + TypeTraits<T>::TypeStr(), ""));
}
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @guan404ming, thanks for addressing the comments! The PR implements the proposal cleanly and the functionalities are in good state. My last advice are below, cc' @tqchen for a final round of discussion.
Since it introduces new function calling API to the users, @guan404ming could you document the usage? I think your test case RegisterExpectedReturning shows a good example on registering and calling Expected functions, and when to use the throw exception vs Expected flows. (cc' @junrushao if you could provide a pointer to where to add the docs)
include/tvm/ffi/function_details.h
Outdated
| static constexpr bool RetSupported = | ||
| (std::is_same_v<T, Any> || std::is_void_v<T> || TypeTraits<T>::convert_enabled); | ||
| static constexpr bool RetSupported = (std::is_same_v<T, Any> || std::is_void_v<T> || | ||
| TypeTraits<T>::convert_enabled || is_expected_v<T>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think convert_enabled is already True here, is_expected_v might be redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, let me remove it.
include/tvm/ffi/function_details.h
Outdated
| if (expected_result.is_ok()) { | ||
| *rv = expected_result.value(); | ||
| } else { | ||
| throw expected_result.error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps use move?
if (expected_result.is_ok()) {
*rv = std::move(expected_result).value();
} else {
throw std::move(expected_result).error();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, updated!
Sure, I think I could do it as a follow up after this one! |
806fe56 to
e40b425
Compare
|
@tqchen the CI fails because tvm-ffi's DLPack version is 1.0.2, while dlpack exchange api is tested against PyTorch's native impl (DLPack 1.0.3), perhaps we need to upgrade tvm-ffi's DLPack version |
Thanks @guan404ming , I think the PR is in good state, let's wait for CI to be fixed and get it in! |
|
Here is the upgrade pr #420. I've checked there is no any compat issue need to be updated in our src code. |
Why
Enable exception-free C++ API similar to Rust's Result or C++23's std::expected, as requested in #234.
How
Expected<T>class holding either success value T or ErrorFunction::CallExpected<T>()for exception-free function callsis_expectedtype trait and Expected handling inunpack_callExpected<T>"