You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "tqchen (via GitHub)" <gi...@apache.org> on 2026/06/20 22:51:17 UTC
[PR] [FFI] Add ref-qualified strict ObjectRef casts [tvm-ffi]
tqchen opened a new pull request, #639:
URL: https://github.com/apache/tvm-ffi/pull/639
This PR refactors strict ObjectRef casting to use the Any TypeTraits strict-check path and adds strict throwing APIs.\n\nChanges:\n- Add ref-qualified ObjectRef::as<T>() and ObjectRef::as_or_throw<T>() overloads for const and rvalue receivers.\n- Add Any::as_or_throw<T>() const/rvalue helpers.\n- Keep ObjectRef null handling explicit while using temporary TVMFFIAny views for rich TypeTraits checks and mismatch messages.\n- Add focused tests for const and move variants of as<T>() and as_or_throw<T>().\n- Update TVM_FFI_UNSAFE_ASSUME lowering to be more robust for GCC.\n\nValidation:\n- cmake --build build/cpp_tests -j2 --target tvm_ffi_tests\n- ctest --test-dir build/cpp_tests --output-on-failure -R '^(Any\.AsOrThrow|ObjectRef\.(as|AsUsesTypeTraitsCheckAnyStrict|AsOrThrow)|Expected\..*)$' (29/29 tests)\n- pre-commit run --files include/tvm/ffi/any.h include/tvm/ffi/object.h tests/cpp/test_any.cc tests/cpp/test_object.cc\n- Reconfirmed GCC/Clang assembly for ObjectRef::as<TInt>() an
d as_or_throw<TInt>() const/rvalue probes: hot paths use direct object-header loads/type-index comparisons; temporary TVMFFIAny views are optimized away.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [FFI] Add ref-qualified strict ObjectRef casts [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #639:
URL: https://github.com/apache/tvm-ffi/pull/639#discussion_r3447617522
##########
include/tvm/ffi/any.h:
##########
@@ -824,6 +870,69 @@ struct AnyEqual {
}
};
+// Defer this definition until any.h so the throwing path can depend on
+// TVM_FFI_THROW(TypeError), while object.h stays below the error layer.
+template <typename ObjectRefType, typename>
+TVM_FFI_INLINE ObjectRefType ObjectRef::as_or_throw() const& {
+ if (data_ != nullptr) {
+ // Piggy back to Any TypeTraits for rich ObjectRef check, temp any_data will optimize away.
+ TVMFFIAny any_data;
+ any_data.type_index = data_->type_index();
+ TVM_FFI_UNSAFE_ASSUME(any_data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin);
+ any_data.zero_padding = 0;
+ TVM_FFI_CLEAR_PTR_PADDING_IN_FFI_ANY(&any_data);
+ any_data.v_obj = reinterpret_cast<TVMFFIObject*>(const_cast<Object*>(data_.get()));
+ if (TypeTraits<ObjectRefType>::CheckAnyStrict(&any_data)) {
+ ObjectRefType result(UnsafeInit{});
+ result.data_ = data_;
+ return result;
+ } else {
+ TVM_FFI_THROW(TypeError) << "Cannot treat type `"
+ << TypeTraits<ObjectRefType>::GetMismatchTypeInfo(&any_data)
+ << "` as type `" << TypeTraits<ObjectRefType>::TypeStr() << "`";
+ }
+ } else {
+ if constexpr (ObjectRefType::_type_is_nullable) {
+ ObjectRefType result(UnsafeInit{});
+ return result;
+ } else {
+ TVM_FFI_THROW(TypeError) << "Cannot treat type `" << StaticTypeKey::kTVMFFINone
+ << "` as type `" << TypeTraits<ObjectRefType>::TypeStr() << "`";
+ }
+ }
+}
+
+template <typename ObjectRefType, typename>
+TVM_FFI_INLINE ObjectRefType ObjectRef::as_or_throw() && {
+ if (data_ != nullptr) {
+ // Piggy back to Any TypeTraits for rich ObjectRef check, temp any_data will optimize away.
+ TVMFFIAny any_data;
+ any_data.type_index = data_->type_index();
+ TVM_FFI_UNSAFE_ASSUME(any_data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin);
+ any_data.zero_padding = 0;
+ TVM_FFI_CLEAR_PTR_PADDING_IN_FFI_ANY(&any_data);
+ any_data.v_obj = reinterpret_cast<TVMFFIObject*>(const_cast<Object*>(data_.get()));
+ if (TypeTraits<ObjectRefType>::CheckAnyStrict(&any_data)) {
+ ObjectRefType result(UnsafeInit{});
+ result.data_ = std::move(data_);
+ data_ = nullptr;
+ return result;
+ } else {
+ TVM_FFI_THROW(TypeError) << "Cannot treat type `"
+ << TypeTraits<ObjectRefType>::GetMismatchTypeInfo(&any_data)
+ << "` as type `" << TypeTraits<ObjectRefType>::TypeStr() << "`";
+ }
+ } else {
+ if constexpr (ObjectRefType::_type_is_nullable) {
+ ObjectRefType result(UnsafeInit{});
+ return result;
+ } else {
+ TVM_FFI_THROW(TypeError) << "Cannot treat type `" << StaticTypeKey::kTVMFFINone
+ << "` as type `" << TypeTraits<ObjectRefType>::TypeStr() << "`";
+ }
+ }
+}
Review Comment:

The implementations of `ObjectRef::as_or_throw() const&` and `ObjectRef::as_or_throw() &&` duplicate a significant amount of low-level logic (such as setting up `any_data`, checking strict types, and handling null pointers) that is already fully implemented in `ObjectRef::as() const&` and `ObjectRef::as() &&`.
We can simplify these definitions by delegating directly to `as()`, matching the elegant pattern already used for `Any::as_or_throw`. This significantly improves maintainability and readability while preserving all performance optimizations (like avoiding atomic reference count changes on rvalue moves).
```c
template <typename ObjectRefType, typename>
TVM_FFI_INLINE ObjectRefType ObjectRef::as_or_throw() const& {
std::optional<ObjectRefType> result = this->as<ObjectRefType>();
if (TVM_FFI_PREDICT_FALSE(!result.has_value())) {
TVM_FFI_THROW(TypeError) << "Cannot treat type '" << GetTypeKey()
<< "' as type '" << TypeTraits<ObjectRefType>::TypeStr() << "'";
}
return *std::move(result);
}
template <typename ObjectRefType, typename>
TVM_FFI_INLINE ObjectRefType ObjectRef::as_or_throw() && {
std::optional<ObjectRefType> result = std::move(*this).as<ObjectRefType>();
if (TVM_FFI_PREDICT_FALSE(!result.has_value())) {
TVM_FFI_THROW(TypeError) << "Cannot treat type '" << GetTypeKey()
<< "' as type '" << TypeTraits<ObjectRefType>::TypeStr() << "'";
}
return *std::move(result);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [FFI] Add ref-qualified strict ObjectRef casts [tvm-ffi]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen merged PR #639:
URL: https://github.com/apache/tvm-ffi/pull/639
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org