tqchen commented on code in PR #601: URL: https://github.com/apache/tvm-ffi/pull/601#discussion_r3349628232
########## src/ffi/extra/structural_visit.cc: ########## @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/*! + * \file src/ffi/extra/structural_visit.cc + * \brief Structural visit implementation. + */ +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/dict.h> +#include <tvm/ffi/container/list.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/reflection/accessor.h> +#include <tvm/ffi/reflection/registry.h> + +namespace tvm { +namespace ffi { + +namespace details { + +// --------------------------------------------------------------------------- +// StructuralVisitor helpers. +// --------------------------------------------------------------------------- + +// Type-attr hooks store an ABI structural visit callback as an opaque pointer. +using FStructuralVisit = TVMFFIAny (*)(StructuralVisitorObj* visitor, const Object* value) noexcept; + +/*! + * \brief Walk reflected structural fields of \p value. + * + * Fields marked with ``kTVMFFIFieldFlagBitMaskSEqHashIgnore`` are skipped. + * Def-region field flags are scoped around recursive child visits. + * + * \param visitor The active visitor. + * \param value The object whose reflected fields should be visited. + * \return Expected interrupt state. An error means traversal failed. + */ +Expected<Optional<VisitInterrupt>> VisitReflectedFields(StructuralVisitorObj* visitor, + const ObjectRef& value) noexcept { + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(value.type_index()); + + Expected<Optional<VisitInterrupt>> result = Optional<VisitInterrupt>(std::nullopt); + reflection::ForEachFieldInfoWithEarlyStop( + type_info, [&](const TVMFFIFieldInfo* field_info) -> bool { + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashIgnore) { + return false; + } + + Any field_value; + const void* field_addr = reinterpret_cast<const char*>(value.get()) + field_info->offset; + int ret_code = field_info->getter(const_cast<void*>(field_addr), + reinterpret_cast<TVMFFIAny*>(&field_value)); + if (TVM_FFI_PREDICT_FALSE(ret_code != 0)) { + result = Unexpected(details::MoveFromSafeCallRaised()); + return true; + } + + TVMFFIDefRegionKind kind = kTVMFFIDefRegionKindNone; + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefNonRecursive) { + kind = kTVMFFIDefRegionKindNonRecursive; + } else if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefRecursive) { + kind = kTVMFFIDefRegionKindRecursive; + } + + if (kind != kTVMFFIDefRegionKindNone) { + result = visitor->WithDefRegionKind( + kind, [&]() { return visitor->VisitExpected(field_value); }); + } else { + result = visitor->VisitExpected(field_value); + } + return StructuralVisitNeedEarlyReturn(result); + }); + return result; +} + +} // namespace details + +// --------------------------------------------------------------------------- +// StructuralVisitor Object implementation. +// --------------------------------------------------------------------------- + +/*! + * \brief Default structural visit implementation. + * + * The default path first checks for a type-specific structural visit hook, then + * falls back to reflected structural fields. + */ +Expected<Optional<VisitInterrupt>> StructuralVisitorObj::DefaultVisitExpected( + AnyView value) noexcept { + if (TVM_FFI_PREDICT_FALSE(value.type_index() < TypeIndex::kTVMFFIStaticObjectBegin)) { Review Comment: don't predict here since POD handling path can be common depending on branch predictator we can choose to do the job ########## src/ffi/extra/structural_visit.cc: ########## @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/*! + * \file src/ffi/extra/structural_visit.cc + * \brief Structural visit implementation. + */ +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/dict.h> +#include <tvm/ffi/container/list.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/reflection/accessor.h> +#include <tvm/ffi/reflection/registry.h> + +namespace tvm { +namespace ffi { + +namespace details { + +// --------------------------------------------------------------------------- +// StructuralVisitor helpers. +// --------------------------------------------------------------------------- + +// Type-attr hooks store an ABI structural visit callback as an opaque pointer. +using FStructuralVisit = TVMFFIAny (*)(StructuralVisitorObj* visitor, const Object* value) noexcept; + +/*! + * \brief Walk reflected structural fields of \p value. + * + * Fields marked with ``kTVMFFIFieldFlagBitMaskSEqHashIgnore`` are skipped. + * Def-region field flags are scoped around recursive child visits. + * + * \param visitor The active visitor. + * \param value The object whose reflected fields should be visited. + * \return Expected interrupt state. An error means traversal failed. + */ +Expected<Optional<VisitInterrupt>> VisitReflectedFields(StructuralVisitorObj* visitor, + const ObjectRef& value) noexcept { + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(value.type_index()); + + Expected<Optional<VisitInterrupt>> result = Optional<VisitInterrupt>(std::nullopt); + reflection::ForEachFieldInfoWithEarlyStop( + type_info, [&](const TVMFFIFieldInfo* field_info) -> bool { + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashIgnore) { + return false; + } + + Any field_value; + const void* field_addr = reinterpret_cast<const char*>(value.get()) + field_info->offset; + int ret_code = field_info->getter(const_cast<void*>(field_addr), + reinterpret_cast<TVMFFIAny*>(&field_value)); + if (TVM_FFI_PREDICT_FALSE(ret_code != 0)) { + result = Unexpected(details::MoveFromSafeCallRaised()); + return true; + } + + TVMFFIDefRegionKind kind = kTVMFFIDefRegionKindNone; + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefNonRecursive) { + kind = kTVMFFIDefRegionKindNonRecursive; + } else if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefRecursive) { + kind = kTVMFFIDefRegionKindRecursive; + } + + if (kind != kTVMFFIDefRegionKindNone) { + result = visitor->WithDefRegionKind( + kind, [&]() { return visitor->VisitExpected(field_value); }); + } else { + result = visitor->VisitExpected(field_value); + } + return StructuralVisitNeedEarlyReturn(result); + }); + return result; +} + +} // namespace details + +// --------------------------------------------------------------------------- +// StructuralVisitor Object implementation. +// --------------------------------------------------------------------------- + +/*! + * \brief Default structural visit implementation. + * + * The default path first checks for a type-specific structural visit hook, then + * falls back to reflected structural fields. + */ +Expected<Optional<VisitInterrupt>> StructuralVisitorObj::DefaultVisitExpected( + AnyView value) noexcept { + if (TVM_FFI_PREDICT_FALSE(value.type_index() < TypeIndex::kTVMFFIStaticObjectBegin)) { + return Optional<VisitInterrupt>(std::nullopt); + } + ObjectRef value_ref = value.cast<ObjectRef>(); Review Comment: likely we can forward value to function without obtaining value ref(only need to cast to const Object* likely in VisitReflectedFields ########## src/ffi/extra/structural_visit.cc: ########## @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/*! + * \file src/ffi/extra/structural_visit.cc + * \brief Structural visit implementation. + */ +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/dict.h> +#include <tvm/ffi/container/list.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/reflection/accessor.h> +#include <tvm/ffi/reflection/registry.h> + +namespace tvm { +namespace ffi { + +namespace details { + +// --------------------------------------------------------------------------- +// StructuralVisitor helpers. +// --------------------------------------------------------------------------- + +// Type-attr hooks store an ABI structural visit callback as an opaque pointer. +using FStructuralVisit = TVMFFIAny (*)(StructuralVisitorObj* visitor, const Object* value) noexcept; + +/*! + * \brief Walk reflected structural fields of \p value. + * + * Fields marked with ``kTVMFFIFieldFlagBitMaskSEqHashIgnore`` are skipped. + * Def-region field flags are scoped around recursive child visits. + * + * \param visitor The active visitor. + * \param value The object whose reflected fields should be visited. + * \return Expected interrupt state. An error means traversal failed. + */ +Expected<Optional<VisitInterrupt>> VisitReflectedFields(StructuralVisitorObj* visitor, + const ObjectRef& value) noexcept { + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(value.type_index()); + + Expected<Optional<VisitInterrupt>> result = Optional<VisitInterrupt>(std::nullopt); + reflection::ForEachFieldInfoWithEarlyStop( + type_info, [&](const TVMFFIFieldInfo* field_info) -> bool { + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashIgnore) { + return false; + } + + Any field_value; + const void* field_addr = reinterpret_cast<const char*>(value.get()) + field_info->offset; + int ret_code = field_info->getter(const_cast<void*>(field_addr), + reinterpret_cast<TVMFFIAny*>(&field_value)); + if (TVM_FFI_PREDICT_FALSE(ret_code != 0)) { + result = Unexpected(details::MoveFromSafeCallRaised()); + return true; + } + + TVMFFIDefRegionKind kind = kTVMFFIDefRegionKindNone; + if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefNonRecursive) { + kind = kTVMFFIDefRegionKindNonRecursive; + } else if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefRecursive) { + kind = kTVMFFIDefRegionKindRecursive; + } + + if (kind != kTVMFFIDefRegionKindNone) { + result = visitor->WithDefRegionKind( + kind, [&]() { return visitor->VisitExpected(field_value); }); + } else { + result = visitor->VisitExpected(field_value); + } + return StructuralVisitNeedEarlyReturn(result); + }); + return result; +} + +} // namespace details + +// --------------------------------------------------------------------------- +// StructuralVisitor Object implementation. +// --------------------------------------------------------------------------- + +/*! + * \brief Default structural visit implementation. + * + * The default path first checks for a type-specific structural visit hook, then + * falls back to reflected structural fields. + */ +Expected<Optional<VisitInterrupt>> StructuralVisitorObj::DefaultVisitExpected( + AnyView value) noexcept { + if (TVM_FFI_PREDICT_FALSE(value.type_index() < TypeIndex::kTVMFFIStaticObjectBegin)) { + return Optional<VisitInterrupt>(std::nullopt); + } + ObjectRef value_ref = value.cast<ObjectRef>(); Review Comment: you can take benefit fo value.type_index() directly -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
