Skip to content

Commit 1fed0ae

Browse files
authored
[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.
1 parent 61f90eb commit 1fed0ae

File tree

1 file changed

+11
-5
lines changed
  • include/tvm/ffi/reflection

1 file changed

+11
-5
lines changed

include/tvm/ffi/reflection/init.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ TObjectRef FFIConvertFromAnyViewToObjectRef(AnyView input) {
6262
<< "` to `" << TypeTraits<TObjectRef>::TypeStr() << "`";
6363
}
6464

65+
/*!
66+
* \brief Return the lazily initialized KWARGS sentinel object.
67+
*/
68+
inline const ObjectRef& GetKwargsSentinel() {
69+
static ObjectRef kwargs_sentinel =
70+
Function::GetGlobalRequired("ffi.GetKwargsObject")().cast<ObjectRef>();
71+
return kwargs_sentinel;
72+
}
73+
6574
} // namespace details
6675

6776
/*!
@@ -118,12 +127,9 @@ inline Function MakeInit(int32_t type_index) {
118127
std::stable_partition(info->pos_indices.begin(), info->pos_indices.end(),
119128
[&](size_t idx) { return !info->all_fields[idx].has_default; });
120129

121-
// Eagerly resolve the KWARGS sentinel via global function registry.
122-
ObjectRef kwargs_sentinel =
123-
Function::GetGlobalRequired("ffi.GetKwargsObject")().cast<ObjectRef>();
124-
125130
return Function::FromPacked(
126-
[info, kwargs_sentinel, type_info](PackedArgs args, Any* rv) {
131+
[info, type_info](PackedArgs args, Any* rv) {
132+
const ObjectRef& kwargs_sentinel = details::GetKwargsSentinel();
127133
// ---- 1. Create object via CreateEmptyObject --------------------------
128134
ObjectPtr<Object> obj_ptr = CreateEmptyObject(type_info);
129135

0 commit comments

Comments
 (0)