This is an automated email from the ASF dual-hosted git repository.
junrushao 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 1fed0ae [FFI][Reflection] Lazily resolve KWARGS sentinel in auto-init
(#519)
1fed0ae is described below
commit 1fed0ae0421e614d45662e8ee6bcae353d3ab2ea
Author: Akaash Parthasarathy <[email protected]>
AuthorDate: Wed Apr 1 15:29:51 2026 -0400
[FFI][Reflection] Lazily resolve KWARGS sentinel in auto-init (#519)
This PR intends to avoid the eager lookup of `ffi.GetKwargsObject`
during `MakeInit` construction. Instead, it resolves and caches the
KWARGS sentinel via a function-local static (`GetKwargsSentinel`) when
`__ffi_init__` is invoked. This removes static-init order dependence
between reflection auto-init registration and global function
registration, preventing `Function ffi.GetKwargsObject not found`
failures.
---
include/tvm/ffi/reflection/init.h | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/include/tvm/ffi/reflection/init.h
b/include/tvm/ffi/reflection/init.h
index 009d1ac..369be7c 100644
--- a/include/tvm/ffi/reflection/init.h
+++ b/include/tvm/ffi/reflection/init.h
@@ -62,6 +62,15 @@ TObjectRef FFIConvertFromAnyViewToObjectRef(AnyView input) {
<< "` to `" << TypeTraits<TObjectRef>::TypeStr() <<
"`";
}
+/*!
+ * \brief Return the lazily initialized KWARGS sentinel object.
+ */
+inline const ObjectRef& GetKwargsSentinel() {
+ static ObjectRef kwargs_sentinel =
+ Function::GetGlobalRequired("ffi.GetKwargsObject")().cast<ObjectRef>();
+ return kwargs_sentinel;
+}
+
} // namespace details
/*!
@@ -118,12 +127,9 @@ inline Function MakeInit(int32_t type_index) {
std::stable_partition(info->pos_indices.begin(), info->pos_indices.end(),
[&](size_t idx) { return
!info->all_fields[idx].has_default; });
- // Eagerly resolve the KWARGS sentinel via global function registry.
- ObjectRef kwargs_sentinel =
- Function::GetGlobalRequired("ffi.GetKwargsObject")().cast<ObjectRef>();
-
return Function::FromPacked(
- [info, kwargs_sentinel, type_info](PackedArgs args, Any* rv) {
+ [info, type_info](PackedArgs args, Any* rv) {
+ const ObjectRef& kwargs_sentinel = details::GetKwargsSentinel();
// ---- 1. Create object via CreateEmptyObject
--------------------------
ObjectPtr<Object> obj_ptr = CreateEmptyObject(type_info);