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 78c4e430 [API] Add type index query mode (#640)
78c4e430 is described below
commit 78c4e430546a172d4d0058e0a7e38b35432cc529
Author: Tianqi Chen <[email protected]>
AuthorDate: Sun Jun 21 06:38:51 2026 -0400
[API] Add type index query mode (#640)
## Summary
- Add query-only behavior to TVMFFITypeGetOrAllocIndex when
parent_type_index == -2.
- Return the existing registered type index on hit, or -2 on miss
without allocating/registering or mutating parent metadata.
- Document the concrete -2 C API behavior and add focused C++ coverage
for hit and miss/no-registration behavior.
This query mode is reserved for a future release update that may
optionally query type indices at startup.
## Validation
- ./build/cpp_tests/lib/tvm_ffi_tests
--gtest_filter=Object.TypeGetOrAllocIndexQuery*
- ctest -V -C RelWithDebugInfo --test-dir build/cpp_tests
--output-on-failure
- pre-commit run --files include/tvm/ffi/c_api.h src/ffi/object.cc
tests/cpp/test_object.cc
---
include/tvm/ffi/c_api.h | 6 ++++--
src/ffi/object.cc | 3 +++
tests/cpp/test_object.cc | 15 +++++++++++++++
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/include/tvm/ffi/c_api.h b/include/tvm/ffi/c_api.h
index 8af33b79..90d4853c 100644
--- a/include/tvm/ffi/c_api.h
+++ b/include/tvm/ffi/c_api.h
@@ -1358,15 +1358,17 @@ TVM_FFI_DLL const TVMFFIByteArray*
TVMFFIBacktrace(const char* filename, int lin
* If the static_tindex is non-negative, the function will
* allocate a runtime type index.
* Otherwise, we will populate the type table and return the static index.
+ * If parent_type_index is -2, the function queries the existing type index:
+ * it returns the registered index for type_key, or -2 if type_key is not
registered.
*
* \param type_key The type key.
* \param type_depth The type depth.
* \param static_type_index Static type index if any, can be -1, which means
this is a dynamic index
* \param num_child_slots Number of slots reserved for its children.
* \param child_slots_can_overflow Whether to allow child to overflow the
slots.
- * \param parent_type_index Parent type index, pass in -1 if it is root.
+ * \param parent_type_index Parent type index, pass in -1 if it is root, or -2
to query only.
*
- * \return The allocated type index.
+ * \return The existing or allocated type index; -2 when query-only mode
misses.
*/
TVM_FFI_DLL int32_t TVMFFITypeGetOrAllocIndex(const TVMFFIByteArray* type_key,
int32_t static_type_index,
int32_t type_depth,
diff --git a/src/ffi/object.cc b/src/ffi/object.cc
index dcfb1893..d6d04dca 100644
--- a/src/ffi/object.cc
+++ b/src/ffi/object.cc
@@ -126,6 +126,9 @@ class TypeTable {
if (it != type_key2index_.end()) {
return type_table_[(*it).second]->type_index;
}
+ if (parent_type_index == -2) {
+ return -2;
+ }
// get parent's entry
Entry* parent = [&]() -> Entry* {
diff --git a/tests/cpp/test_object.cc b/tests/cpp/test_object.cc
index 5a5d4162..fc621e77 100644
--- a/tests/cpp/test_object.cc
+++ b/tests/cpp/test_object.cc
@@ -86,6 +86,21 @@ TEST(Object, CRTPObjectInfo) {
EXPECT_GE(info->type_index, TypeIndex::kTVMFFIDynObjectBegin);
}
+TEST(Object, TypeGetOrAllocIndexQueryRegistered) {
+ TVMFFIByteArray type_key{TIntObj::_type_key,
std::char_traits<char>::length(TIntObj::_type_key)};
+ EXPECT_EQ(TVMFFITypeGetOrAllocIndex(&type_key, -1, 0, 0, 0, -2),
TIntObj::RuntimeTypeIndex());
+}
+
+TEST(Object, TypeGetOrAllocIndexQueryMissDoesNotRegister) {
+ const char* type_key_data = "test.TypeGetOrAllocIndexQueryMiss";
+ TVMFFIByteArray type_key{type_key_data,
std::char_traits<char>::length(type_key_data)};
+ EXPECT_EQ(TVMFFITypeGetOrAllocIndex(&type_key, -1, 0, 0, 0, -2), -2);
+
+ int32_t type_index = -1;
+ EXPECT_NE(TVMFFITypeKeyToIndex(&type_key, &type_index), 0);
+ EXPECT_EQ(type_index, -1);
+}
+
TEST(Object, InstanceCheck) {
ObjectPtr<Object> a = make_object<TIntObj>(11);
ObjectPtr<Object> b = make_object<TFloatObj>(11);