Skip to content

Commit 3045f1f

Browse files
committed
[FFI] Provide Field Visit bridge so we can do gradual transition (apache#18091)
This PR provides functions that adapts old VisitAttrs reflection utilities to use new reflection mechanism when available. These adapter would allow us to gradually transition the object def from old VisitAttrs based mechanism to new mechanism. - For all objects - Replace VisitAttrs with static void RegisterReflection() that registers the fields - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file - For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead - Do the same steps as above and replace TVM_ATTRS - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO We will send followup PRs to do the gradual transition. Once all transition is completed, we will remove AttrsVisitor and only go through the new mechanism.
1 parent b91ef66 commit 3045f1f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

include/tvm/ffi/reflection/reflection.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ inline Function GetMethod(std::string_view type_key, const char* method_name) {
404404
*/
405405
template <typename Callback>
406406
inline void ForEachFieldInfo(const TypeInfo* type_info, Callback callback) {
407+
using ResultType = decltype(callback(type_info->fields));
408+
static_assert(std::is_same_v<ResultType, void>, "Callback must return void");
407409
// iterate through acenstors in parent to child order
408410
// skip the first one since it is always the root object
409411
for (int i = 1; i < type_info->type_depth; ++i) {
@@ -417,6 +419,34 @@ inline void ForEachFieldInfo(const TypeInfo* type_info, Callback callback) {
417419
}
418420
}
419421

422+
/*!
423+
* \brief Visit each field info of the type info and run callback which returns bool for early stop.
424+
*
425+
* \tparam Callback The callback function type, which returns bool for early stop.
426+
*
427+
* \param type_info The type info.
428+
* \param callback_with_early_stop The callback function.
429+
* \return true if any of early stop is triggered.
430+
*
431+
* \note This function calls both the child and parent type info and can be used for searching.
432+
*/
433+
template <typename Callback>
434+
inline bool ForEachFieldInfoWithEarlyStop(const TypeInfo* type_info,
435+
Callback callback_with_early_stop) {
436+
// iterate through acenstors in parent to child order
437+
// skip the first one since it is always the root object
438+
for (int i = 1; i < type_info->type_depth; ++i) {
439+
const TVMFFITypeInfo* parent_info = type_info->type_acenstors[i];
440+
for (int j = 0; j < parent_info->num_fields; ++j) {
441+
if (callback_with_early_stop(parent_info->fields + j)) return true;
442+
}
443+
}
444+
for (int i = 0; i < type_info->num_fields; ++i) {
445+
if (callback_with_early_stop(type_info->fields + i)) return true;
446+
}
447+
return false;
448+
}
449+
420450
} // namespace reflection
421451
} // namespace ffi
422452
} // namespace tvm

0 commit comments

Comments
 (0)