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 "Seven-Streams (via GitHub)" <gi...@apache.org> on 2026/07/23 18:25:31 UTC
[PR] [FEAT][RUST] support `try_cast` between any object-based structs. [tvm-ffi]
Seven-Streams opened a new pull request, #684:
URL: https://github.com/apache/tvm-ffi/pull/684
# [Rust][Feat] Add runtime-checked casting between ObjectRef types
## Summary
Add runtime-checked casting between arbitrary `ObjectRef` types in the Rust
bindings, mirroring C++ `ObjectRef::as<T>` / `Downcast<T>`.
- **`ObjectRefCast`** (blanket impl for all `ObjectRefCore` types):
- `try_cast::<B>(self)` — consumes `self`, rewraps the object pointer without
copying; fails with a `TypeError` naming both type keys.
- `cast_clone::<B>(&self)` — borrowing variant, shares ownership via inc_ref.
- Succeeds iff the **runtime** type of the underlying object is
`B::ContainerType` or a subtype, so downcasting through a parent reference
works as expected.
```rust
let base: TestBase = derived.try_cast()?; // upcast
let obj: ObjectRef = base.try_cast()?; // upcast to root
let d: TestDerived = obj.try_cast()?; // checked downcast
```
- **`is_instance_of(a_idx, b_idx)`** — O(1) subtype check via the process-wide
type table (equal-index fast path + one ancestor-table lookup), the Rust
counterpart of C++ `IsObjectInstance`.
- **Hierarchy-aware `Any`/`AnyView` conversions**: derive-generated
`check_any_strict` / `try_cast_from_any_view` now use `is_instance_of`,
aligning with C++ `CheckAnyStrict` — converting an `Any` holding a derived
object to a parent reference (incl. `ObjectRef`) now succeeds.
- **Derive fixes** (uncovered by first external-crate use): generated code no
longer references `proc_macro_error`; `type_str()` return type fully
qualified; `object::unsafe_` made `#[doc(hidden)] pub`.
- **Tests**: `tests/test_cast.rs` covers multi-level up/downcast round-trips,
failure messages, refcounts, `Any` subtype conversions/rejections, and
leak-free destruction. Exact-match fast path unchanged (~1 compare); slow
path ~8ns, only on previously-failing paths.
--
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] [FEAT][RUST] support `try_cast` between any object-based structs. [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#issuecomment-5061933654
> [!CAUTION]
> The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.
--
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] [FEAT][RUST] support `try_cast` between any object-based structs. [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#issuecomment-5062061546
> [!CAUTION]
> The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642494889
##########
rust/tvm-ffi-macros/src/object_macros.rs:
##########
@@ -192,23 +195,24 @@ pub fn derive_object_ref(input: proc_macro::TokenStream) -> TokenStream {
}
}
+ #[inline(always)]
unsafe fn move_to_any(
src: Self,
data: &mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIAny
) {
type ContainerType = <#struct_name as #tvm_ffi_crate::object::ObjectRefCore>
::ContainerType;
- let type_index =
- <ContainerType as #tvm_ffi_crate::object::ObjectCore>::type_index();
- data.type_index = type_index as i32;
- data.small_str_len = 0;
let data_ptr = #tvm_ffi_crate::object::ObjectArc::into_raw(
src.data
);
- data.data_union.v_obj =
- data_ptr as *mut ContainerType as *mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
+ let object_ptr =
+ data_ptr as *mut ContainerType as *mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
Review Comment:
We need the object pointer first because the type index is now read from the runtime object header instead of ContainerType::type_index(), which preserves the dynamic subtype after an upcast.
##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
}
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+ if object_type_index == target_type_index {
+ return true;
+ }
+ let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+ // Every registered object is an instance of the root Object type.
+ if target_type_index == object_begin {
Review Comment:
I think it is. this is the root object fast path. Every object-range type must cast to objectref, and the C++ uses the same range check while avoiding type-table lookups.
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#issuecomment-5066341126
> [!CAUTION]
> The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642912491
##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
}
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+ if object_type_index == target_type_index {
+ return true;
+ }
+ let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+ // Every registered object is an instance of the root Object type.
+ if target_type_index == object_begin {
Review Comment:
agreed
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642888220
##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
}
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+ if object_type_index == target_type_index {
+ return true;
+ }
+ let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+ // Every registered object is an instance of the root Object type.
+ if target_type_index == object_begin {
+ return object_type_index >= object_begin;
+ }
+ // Only object types participate in the type hierarchy.
+ if object_type_index < object_begin || target_type_index < object_begin {
+ return false;
+ }
+ // Parent indices are always smaller than their descendants.
+ if object_type_index < target_type_index {
+ return false;
+ }
+ unsafe {
+ let object_info = TVMFFIGetTypeInfo(object_type_index);
+ let target_info = TVMFFIGetTypeInfo(target_type_index);
+ if object_info.is_null() || target_info.is_null() {
+ return false;
+ }
+ let target_depth = (*target_info).type_depth;
Review Comment:
might worthcheck other template trick for target_type_index. Note in the case of C++ TargetType can be a type T, where things like type_depth can be directly queried as a compile time constant rather than runtime
##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
}
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+ if object_type_index == target_type_index {
+ return true;
+ }
+ let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+ // Every registered object is an instance of the root Object type.
+ if target_type_index == object_begin {
Review Comment:
given this is runtime check, likely not having the specialization is ok, depending on how freq conversion to ObjectRef is.
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "Seven-Streams (via GitHub)" <gi...@apache.org>.
Seven-Streams commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642353822
##########
rust/tvm-ffi-macros/src/object_macros.rs:
##########
@@ -192,23 +195,24 @@ pub fn derive_object_ref(input: proc_macro::TokenStream) -> TokenStream {
}
}
+ #[inline(always)]
unsafe fn move_to_any(
src: Self,
data: &mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIAny
) {
type ContainerType = <#struct_name as #tvm_ffi_crate::object::ObjectRefCore>
::ContainerType;
- let type_index =
- <ContainerType as #tvm_ffi_crate::object::ObjectCore>::type_index();
- data.type_index = type_index as i32;
- data.small_str_len = 0;
let data_ptr = #tvm_ffi_crate::object::ObjectArc::into_raw(
src.data
);
- data.data_union.v_obj =
- data_ptr as *mut ContainerType as *mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
+ let object_ptr =
+ data_ptr as *mut ContainerType as *mut #tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
Review Comment:
Why the order is changed
##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
}
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+ if object_type_index == target_type_index {
+ return true;
+ }
+ let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+ // Every registered object is an instance of the root Object type.
+ if target_type_index == object_begin {
Review Comment:
not sure if it's necessary
--
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] [FEAT][RUST] Support runtime-checked try_cast between ObjectRef types [tvm-ffi]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684
--
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