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 c52a0178 [Rust] Preserve object-range facts in Any conversions (#910)
c52a0178 is described below

commit c52a017882549b1f7530db5e549242537180e8ed
Author: Tianqi Chen <[email protected]>
AuthorDate: Tue Nov 3 22:28:35 2026 -0500

    [Rust] Preserve object-range facts in Any conversions (#910)
    
    Preserve the known object-type range when Rust converts object
    references into Any/AnyView and reconstructs object rvalues. Document
    the live-object invariants and expose them to the optimizer with
    `core::hint::assert_unchecked`, allowing redundant round-trip range
    checks to disappear while retaining generic input validation.
---
 rust/tvm-ffi-macros/src/object_macros.rs | 12 ++++++++++++
 rust/tvm-ffi/src/rvalue_ref.rs           |  8 ++++++++
 2 files changed, 20 insertions(+)

diff --git a/rust/tvm-ffi-macros/src/object_macros.rs 
b/rust/tvm-ffi-macros/src/object_macros.rs
index 35cefce0..e51745dc 100644
--- a/rust/tvm-ffi-macros/src/object_macros.rs
+++ b/rust/tvm-ffi-macros/src/object_macros.rs
@@ -603,6 +603,12 @@ pub(crate) fn expand_object_ref(
                 let object_ptr =
                     data_ptr as *mut ContainerType as *mut 
#tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
                 data.type_index = (*object_ptr).type_index;
+                // SAFETY: ObjectRefCore guarantees a live allocation whose 
header
+                // contains its registered object-range dynamic type index.
+                ::core::hint::assert_unchecked(
+                    data.type_index >=
+                        
#tvm_ffi_crate::tvm_ffi_sys::TVMFFITypeIndex::kTVMFFIStaticObjectBegin as i32
+                );
                 data.small_str_len = 0;
                 data.data_union.v_obj = object_ptr;
             }
@@ -647,6 +653,12 @@ pub(crate) fn expand_object_ref(
                 let object_ptr =
                     data_ptr as *mut ContainerType as *mut 
#tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
                 data.type_index = (*object_ptr).type_index;
+                // SAFETY: ObjectRefCore guarantees a live allocation whose 
header
+                // contains its registered object-range dynamic type index.
+                ::core::hint::assert_unchecked(
+                    data.type_index >=
+                        
#tvm_ffi_crate::tvm_ffi_sys::TVMFFITypeIndex::kTVMFFIStaticObjectBegin as i32
+                );
                 data.small_str_len = 0;
                 data.data_union.v_obj = object_ptr;
             }
diff --git a/rust/tvm-ffi/src/rvalue_ref.rs b/rust/tvm-ffi/src/rvalue_ref.rs
index 62e98887..bb739271 100644
--- a/rust/tvm-ffi/src/rvalue_ref.rs
+++ b/rust/tvm-ffi/src/rvalue_ref.rs
@@ -156,9 +156,17 @@ where
     }
 }
 
+/// Reconstruct the owned object's ABI representation.
+///
+/// # Safety
+/// `object` must point to a live managed object with a registered object-range
+/// dynamic type index.
 unsafe fn object_any(object: *mut TVMFFIObject) -> TVMFFIAny {
     let mut raw = TVMFFIAny::new();
     raw.type_index = (*object).type_index;
+    // SAFETY: The caller provides a live object from an owning reference or
+    // a valid object-rvalue slot, so its dynamic type is in the object range.
+    core::hint::assert_unchecked(raw.type_index >= 
TypeIndex::kTVMFFIStaticObjectBegin as i32);
     raw.data_union.v_obj = object;
     raw
 }

Reply via email to