tqchen commented on code in PR #649: URL: https://github.com/apache/tvm-ffi/pull/649#discussion_r3626357882
########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; + + /*! \brief Identity-substitution environment owned by this vtable instance. */ + Map<ObjectRef, Any> var_remap_; Review Comment: map should not be part of VTable signature, other functions are static across all impl, var_remap is local to the mutator, it should be member of StructuralMapMutatorObj ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); Review Comment: inline usage ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; Review Comment: readability: var_remap_get/set since otherwise it may read as set/get var remap member itself ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; + + /*! \brief Identity-substitution environment owned by this vtable instance. */ + Map<ObjectRef, Any> var_remap_; +}; + +/*! + * \brief Object node of a structural mutator. + */ +class StructuralMutatorObj : public Object { + public: + /*! + * \brief Construct a structural mutator from a derived-instance-owned vtable. + * \param vtable The non-null dispatch table for this mutator. It must outlive this object. + */ + explicit StructuralMutatorObj(StructuralMutatorVTable* vtable) : vtable_(vtable) {} + + /*! + * \brief Mutate a value through the mutator vtable. + * + * \param value The value to mutate. + * \return The mutated owning value. + * \throws Error if mutation fails. + * + * This entry point never intentionally mutates \p value in place. Recursive transformations + * also use \ref Mutate. + */ + TVM_FFI_INLINE Any Mutate(AnyView value) { return MutateExpected(value).value(); } + + /*! + * \brief Exception-free form of \ref Mutate. + * + * \param value The value to mutate. + * \return The mutated owning value, or an Error if mutation failed. + */ + TVM_FFI_INLINE Expected<Any> MutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->mutate)(this, value)); + } + + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param value The borrowed value to transform. + * \return The transformed owning value. + * \throws Error if transformation fails. + * + * The returned value may refer to the same object as \p value. Callers must use the return value + * as the result of the transformation rather than assuming that the input object was reused. + */ + TVM_FFI_INLINE Any MaybeInplaceMutate(AnyView value) { + return MaybeInplaceMutateExpected(value).value(); + } + + /*! + * \brief Exception-free form of \ref MaybeInplaceMutate. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>( + (*vtable_->maybe_inplace_mutate)(this, value)); + } + + /*! + * \brief Apply the default structural mutation with copy-on-write behavior. + * + * \param value The value to mutate. + * \return The mutated value, or an Error if hook dispatch, copying, or field mutation failed. + */ + TVM_FFI_INLINE Expected<Any> DefaultMutateExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); Review Comment: the first step should not ber query free var, instead, we should first check if customized kStructuralMutate exists and call that as fast path. Then freevar hanlding is should be part of fallback path, aka right before MutateReflectedFieldsExpected ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; + + /*! \brief Identity-substitution environment owned by this vtable instance. */ + Map<ObjectRef, Any> var_remap_; +}; + +/*! + * \brief Object node of a structural mutator. + */ +class StructuralMutatorObj : public Object { + public: + /*! + * \brief Construct a structural mutator from a derived-instance-owned vtable. + * \param vtable The non-null dispatch table for this mutator. It must outlive this object. + */ + explicit StructuralMutatorObj(StructuralMutatorVTable* vtable) : vtable_(vtable) {} + + /*! + * \brief Mutate a value through the mutator vtable. + * + * \param value The value to mutate. + * \return The mutated owning value. + * \throws Error if mutation fails. + * + * This entry point never intentionally mutates \p value in place. Recursive transformations + * also use \ref Mutate. + */ + TVM_FFI_INLINE Any Mutate(AnyView value) { return MutateExpected(value).value(); } + + /*! + * \brief Exception-free form of \ref Mutate. + * + * \param value The value to mutate. + * \return The mutated owning value, or an Error if mutation failed. + */ + TVM_FFI_INLINE Expected<Any> MutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->mutate)(this, value)); + } + + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param value The borrowed value to transform. + * \return The transformed owning value. + * \throws Error if transformation fails. + * + * The returned value may refer to the same object as \p value. Callers must use the return value + * as the result of the transformation rather than assuming that the input object was reused. + */ + TVM_FFI_INLINE Any MaybeInplaceMutate(AnyView value) { + return MaybeInplaceMutateExpected(value).value(); + } + + /*! + * \brief Exception-free form of \ref MaybeInplaceMutate. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>( + (*vtable_->maybe_inplace_mutate)(this, value)); + } + + /*! + * \brief Apply the default structural mutation with copy-on-write behavior. + * + * \param value The value to mutate. + * \return The mutated value, or an Error if hook dispatch, copying, or field mutation failed. + */ + TVM_FFI_INLINE Expected<Any> DefaultMutateExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); + if (is_free_var) { + Expected<Any> mapped_value = GetVarRemapExpected(value); + if (TVM_FFI_PREDICT_FALSE(mapped_value.is_err())) { + return Unexpected(std::move(mapped_value).error()); + } + if (details::ExpectedUnsafe::GetData(mapped_value).type_index() != TypeIndex::kTVMFFINone) { + return mapped_value; + } + } + + int32_t type_index = value.type_index(); + static reflection::TypeAttrColumn column(reflection::type_attr::kStructuralMutate); + AnyView attr = column[type_index]; + Expected<Any> result = [&]() -> Expected<Any> { + if (attr.type_index() != TypeIndex::kTVMFFINone) { + return details::DispatchTypeAttrHookExpected(this, value, attr, + reflection::type_attr::kStructuralMutate); + } + if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) { + return Any(value); + } + return details::MutateReflectedFieldsExpected(this, value); + }(); + if (TVM_FFI_PREDICT_FALSE(result.is_err()) || !is_free_var) { + return result; + } + + Expected<void> set_result = + SetVarRemapExpected(value, details::ExpectedUnsafe::GetData(result)); + if (TVM_FFI_PREDICT_FALSE(set_result.is_err())) { + return Unexpected(std::move(set_result).error()); + } + return result; + } + + /*! + * \brief Apply custom maybe-in-place mutation, or use uniqueness for reflected mutation. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. In-place changes + * completed before an Error are not rolled back. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateIfUniqueExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); + if (is_free_var) { + Expected<Any> mapped_value = GetVarRemapExpected(value); + if (TVM_FFI_PREDICT_FALSE(mapped_value.is_err())) { + return Unexpected(std::move(mapped_value).error()); + } + if (details::ExpectedUnsafe::GetData(mapped_value).type_index() != TypeIndex::kTVMFFINone) { + return mapped_value; + } + } + + Expected<Any> result = [&]() -> Expected<Any> { + int32_t type_index = value.type_index(); + if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) { + return Any(value); + } + + static reflection::TypeAttrColumn mutate_column(reflection::type_attr::kStructuralMutate); + static reflection::TypeAttrColumn maybe_inplace_mutate_column( + reflection::type_attr::kStructuralMaybeInplaceMutate); + AnyView mutate_attr = mutate_column[type_index]; + AnyView maybe_inplace_mutate_attr = maybe_inplace_mutate_column[type_index]; + bool has_mutate = mutate_attr.type_index() != TypeIndex::kTVMFFINone; + bool has_maybe_inplace_mutate = + maybe_inplace_mutate_attr.type_index() != TypeIndex::kTVMFFINone; + if (TVM_FFI_PREDICT_FALSE(has_maybe_inplace_mutate && !has_mutate)) { + return Unexpected(Error("TypeError", + std::string(reflection::type_attr::kStructuralMaybeInplaceMutate) + + " requires " + + std::string(reflection::type_attr::kStructuralMutate) + + " to be defined for the same type", + "")); + } + if (has_maybe_inplace_mutate) { + return details::DispatchTypeAttrHookExpected( + this, value, maybe_inplace_mutate_attr, + reflection::type_attr::kStructuralMaybeInplaceMutate); + } + if (has_mutate) { + return MutateExpected(value); + } + if (value.as<Object>()->unique()) { + return details::MaybeInplaceMutateReflectedFieldsExpected(this, value); + } + return MutateExpected(value); + }(); + if (TVM_FFI_PREDICT_FALSE(result.is_err()) || !is_free_var) { + return result; + } + + Expected<void> set_result = + SetVarRemapExpected(value, details::ExpectedUnsafe::GetData(result)); + if (TVM_FFI_PREDICT_FALSE(set_result.is_err())) { + return Unexpected(std::move(set_result).error()); + } + return result; + } + + /*! + * \brief Look up the replacement recorded for a variable identity. + * + * \param var The borrowed variable identity to look up. + * \return The owning replacement, FFI None if no replacement exists, or an Error if lookup + * fails. + * + * \note The variable identity must have + * ``kTVMFFISEqHashKindFreeVar`` structural-equality metadata. + */ + TVM_FFI_INLINE Expected<Any> GetVarRemapExpected(AnyView var) noexcept { + if (vtable_->get_var_remap == nullptr) { + return DefaultGetVarRemapExpected(var); + } + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->get_var_remap)(this, var)); + } + + /*! + * \brief Record the replacement for a variable identity. + * + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Successful completion, or an Error if the binding is invalid or cannot be stored. + * + * \note The variable identity must have + * ``kTVMFFISEqHashKindFreeVar`` structural-equality metadata. + */ + TVM_FFI_INLINE Expected<void> SetVarRemapExpected(AnyView var, AnyView mapped_value) noexcept { + if (vtable_->set_var_remap == nullptr) { + return DefaultSetVarRemapExpected(var, mapped_value); + } + return details::ExpectedUnsafe::MoveFromTVMFFIAny<void>( + (*vtable_->set_var_remap)(this, var, mapped_value)); + } + + /*! + * \brief Apply the default variable-remap lookup directly. + * + * \param var The borrowed variable identity to look up. + * \return The owning replacement, FFI None if no replacement exists, or an Error if lookup + * fails. + */ + TVM_FFI_INLINE Expected<Any> DefaultGetVarRemapExpected(AnyView var) noexcept { + if (var.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) { + return Unexpected( + Error("TypeError", "Variable-remap key must be an object-backed value", "")); + } + try { + ObjectRef var_ref = var.cast<ObjectRef>(); + std::optional<Any> result = vtable_->var_remap_.Get(var_ref); + if (!result.has_value()) { + return Any(nullptr); + } + return *std::move(result); + } catch (const Error& err) { + return Unexpected(err); + } + } + + /*! + * \brief Apply the default variable-remap insertion directly. + * + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Successful completion, or an Error if the binding is invalid or cannot be stored. + */ + TVM_FFI_INLINE Expected<void> DefaultSetVarRemapExpected(AnyView var, + AnyView mapped_value) noexcept { + if (var.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) { + return Unexpected( + Error("TypeError", "Variable-remap key must be an object-backed value", "")); + } + try { + ObjectRef var_ref = var.cast<ObjectRef>(); + Any owned_mapped_value(mapped_value); + vtable_->var_remap_.Set(var_ref, owned_mapped_value); + return Expected<void>(); + } catch (const Error& err) { + return Unexpected(err); + } + } + + /*! + * \brief Return the current def-region context. + * \return The active def-region kind. + */ + TVM_FFI_INLINE TVMFFIDefRegionKind def_region_kind() const { return def_region_mode_; } + + /*! + * \brief Temporarily switch the def-region context while invoking \p callback. + * + * \param kind The def-region kind to set during the callback. + * \param callback A nullary callable that performs recursive transformation. + * \return The value returned by \p callback. + */ + template <typename Callback> + TVM_FFI_INLINE auto WithDefRegionKind(TVMFFIDefRegionKind kind, Callback&& callback) + -> decltype(std::forward<Callback>(callback)()) { + class Scope { + public: + Scope(StructuralMutatorObj* mutator, TVMFFIDefRegionKind kind) + : mutator_(mutator), old_kind_(mutator->def_region_mode_) { + mutator_->def_region_mode_ = kind; + } + ~Scope() { mutator_->def_region_mode_ = old_kind_; } + Scope(const Scope&) = delete; + Scope& operator=(const Scope&) = delete; + + private: + StructuralMutatorObj* mutator_; + TVMFFIDefRegionKind old_kind_; + }; + Scope scope(this, kind); + return std::forward<Callback>(callback)(); + } + + /// \cond Doxygen_Suppress + static constexpr const bool _type_mutable = true; + TVM_FFI_DECLARE_OBJECT_INFO("ffi.StructuralMutator", StructuralMutatorObj, Object); + /// \endcond + + protected: + /*! + * \brief Non-owning pointer to the required ABI dispatch table. + */ + StructuralMutatorVTable* vtable_ = nullptr; + + /*! + * \brief Current def-region context for def-region-aware structural transformation. + */ + TVMFFIDefRegionKind def_region_mode_ = kTVMFFIDefRegionKindNone; +}; + +/*! + * \brief ObjectRef wrapper for \ref StructuralMutatorObj. + * + * \sa StructuralMutatorObj + */ +class StructuralMutator : public ObjectRef { + public: + /*! + * \brief Construct from an existing mutator object pointer. + * \param n The object pointer to wrap. + */ + explicit StructuralMutator(ObjectPtr<StructuralMutatorObj> n) : ObjectRef(std::move(n)) {} + + /// \cond Doxygen_Suppress + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StructuralMutator, ObjectRef, StructuralMutatorObj); + /// \endcond +}; + +namespace details { + +/*! + * \brief Dispatch a type-specific structural transformation hook. + * + * \param mutator The active structural mutator. + * \param value The borrowed value passed to the hook. + * \param attr The registered type attribute value. + * \param attr_name The attribute name used in type errors. + * \return The hook result, or an Error if the hook fails or \p attr has an invalid type. + */ +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, Review Comment: just inline it ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; + + /*! \brief Identity-substitution environment owned by this vtable instance. */ + Map<ObjectRef, Any> var_remap_; +}; + +/*! + * \brief Object node of a structural mutator. + */ +class StructuralMutatorObj : public Object { + public: + /*! + * \brief Construct a structural mutator from a derived-instance-owned vtable. + * \param vtable The non-null dispatch table for this mutator. It must outlive this object. + */ + explicit StructuralMutatorObj(StructuralMutatorVTable* vtable) : vtable_(vtable) {} + + /*! + * \brief Mutate a value through the mutator vtable. + * + * \param value The value to mutate. + * \return The mutated owning value. + * \throws Error if mutation fails. + * + * This entry point never intentionally mutates \p value in place. Recursive transformations + * also use \ref Mutate. + */ + TVM_FFI_INLINE Any Mutate(AnyView value) { return MutateExpected(value).value(); } + + /*! + * \brief Exception-free form of \ref Mutate. + * + * \param value The value to mutate. + * \return The mutated owning value, or an Error if mutation failed. + */ + TVM_FFI_INLINE Expected<Any> MutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->mutate)(this, value)); + } + + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param value The borrowed value to transform. + * \return The transformed owning value. + * \throws Error if transformation fails. + * + * The returned value may refer to the same object as \p value. Callers must use the return value + * as the result of the transformation rather than assuming that the input object was reused. + */ + TVM_FFI_INLINE Any MaybeInplaceMutate(AnyView value) { + return MaybeInplaceMutateExpected(value).value(); + } + + /*! + * \brief Exception-free form of \ref MaybeInplaceMutate. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>( + (*vtable_->maybe_inplace_mutate)(this, value)); + } + + /*! + * \brief Apply the default structural mutation with copy-on-write behavior. + * + * \param value The value to mutate. + * \return The mutated value, or an Error if hook dispatch, copying, or field mutation failed. + */ + TVM_FFI_INLINE Expected<Any> DefaultMutateExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); + if (is_free_var) { + Expected<Any> mapped_value = GetVarRemapExpected(value); + if (TVM_FFI_PREDICT_FALSE(mapped_value.is_err())) { + return Unexpected(std::move(mapped_value).error()); + } + if (details::ExpectedUnsafe::GetData(mapped_value).type_index() != TypeIndex::kTVMFFINone) { + return mapped_value; + } + } + + int32_t type_index = value.type_index(); + static reflection::TypeAttrColumn column(reflection::type_attr::kStructuralMutate); + AnyView attr = column[type_index]; + Expected<Any> result = [&]() -> Expected<Any> { + if (attr.type_index() != TypeIndex::kTVMFFINone) { + return details::DispatchTypeAttrHookExpected(this, value, attr, + reflection::type_attr::kStructuralMutate); + } + if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) { + return Any(value); + } + return details::MutateReflectedFieldsExpected(this, value); + }(); + if (TVM_FFI_PREDICT_FALSE(result.is_err()) || !is_free_var) { + return result; + } + + Expected<void> set_result = + SetVarRemapExpected(value, details::ExpectedUnsafe::GetData(result)); + if (TVM_FFI_PREDICT_FALSE(set_result.is_err())) { + return Unexpected(std::move(set_result).error()); + } + return result; + } + + /*! + * \brief Apply custom maybe-in-place mutation, or use uniqueness for reflected mutation. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. In-place changes + * completed before an Error are not rolled back. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateIfUniqueExpected(AnyView value) noexcept { Review Comment: the implementation means it is MaybeInplaceMutateExpected? since no IfUnique check here ########## include/tvm/ffi/extra/structural_mutate.h: ########## @@ -0,0 +1,1058 @@ +/* + * 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 tvm/ffi/extra/structural_mutate.h + * \brief Structural mutation API with optional in-place optimization. + */ +#ifndef TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ +#define TVM_FFI_EXTRA_STRUCTURAL_MUTATE_H_ + +#include <tvm/ffi/any.h> +#include <tvm/ffi/c_api.h> +#include <tvm/ffi/cast.h> +#include <tvm/ffi/container/array.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/container/tuple.h> +#include <tvm/ffi/container/variant.h> +#include <tvm/ffi/expected.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/extra/visit_error_context.h> +#include <tvm/ffi/function.h> +#include <tvm/ffi/function_details.h> +#include <tvm/ffi/optional.h> +#include <tvm/ffi/reflection/accessor.h> + +#include <cstddef> +#include <exception> +#include <optional> +#include <string> +#include <string_view> +#include <tuple> +#include <type_traits> +#include <utility> + +namespace tvm { +namespace ffi { + +class StructuralMutatorObj; + +/*! + * \brief ABI callback type for structural mutation. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` containing the transformed value or an Error. + */ +using FStructuralMutate = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView value) noexcept; + +/*! + * \brief ABI callback type for looking up an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` containing the owning mapped value, FFI None when no mapping exists, + * or an Error. + */ +using FStructuralGetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var) noexcept; + +/*! + * \brief ABI callback type for recording an identity substitution. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` containing FFI None on success or an Error. + */ +using FStructuralSetVarRemap = TVMFFIAny (*)(StructuralMutatorObj* mutator, AnyView var, + AnyView mapped_value) noexcept; + +namespace details { + +/*! + * \brief Check whether a value carries a free-variable identity. + * + * \param value The borrowed value to inspect. + * \return Whether the value's object type is registered as a FreeVar. + */ +TVM_FFI_INLINE static bool IsFreeVar(AnyView value) noexcept { + const Object* obj = value.as<Object>(); + if (obj == nullptr) { + return false; + } + const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(obj->type_index()); + return type_info->metadata != nullptr && + type_info->metadata->structural_eq_hash_kind == kTVMFFISEqHashKindFreeVar; +} + +// Dispatch a type-specific structural mutation hook. +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept; + +// Copy and structurally mutate the reflected fields of an object-backed value. +TVM_FFI_INLINE static Expected<Any> MutateReflectedFieldsExpected(StructuralMutatorObj* mutator, + AnyView value) noexcept; + +// Structurally transform the reflected fields of a unique object-backed value in place. +TVM_FFI_INLINE static Expected<Any> MaybeInplaceMutateReflectedFieldsExpected( + StructuralMutatorObj* mutator, AnyView value) noexcept; + +} // namespace details + +/*! + * \brief VTable ABI for \ref StructuralMutator dispatch. + */ +struct StructuralMutatorVTable { + /*! + * \brief Mutate a value without modifying the source in place. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to mutate. + * \return Raw ``TVMFFIAny`` carrying the transformed value or Error. + */ + FStructuralMutate mutate = nullptr; + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param mutator The active structural mutator. + * \param value The borrowed value to transform. + * \return Raw ``TVMFFIAny`` carrying the mutated value or Error. + * + * The returned value may refer to the same object as \p value when the implementation mutates + * that object in place. + */ + FStructuralMutate maybe_inplace_mutate = nullptr; + /*! + * \brief Look up the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to look up. + * \return Raw ``TVMFFIAny`` carrying the owning replacement, FFI None on a miss, or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralGetVarRemap get_var_remap = nullptr; + /*! + * \brief Record the replacement for a variable identity. + * + * \param mutator The active structural mutator. + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Raw ``TVMFFIAny`` carrying None or Error. + * \note A null entry selects the mutator's default identity-substitution environment. + */ + FStructuralSetVarRemap set_var_remap = nullptr; + + /*! \brief Identity-substitution environment owned by this vtable instance. */ + Map<ObjectRef, Any> var_remap_; +}; + +/*! + * \brief Object node of a structural mutator. + */ +class StructuralMutatorObj : public Object { + public: + /*! + * \brief Construct a structural mutator from a derived-instance-owned vtable. + * \param vtable The non-null dispatch table for this mutator. It must outlive this object. + */ + explicit StructuralMutatorObj(StructuralMutatorVTable* vtable) : vtable_(vtable) {} + + /*! + * \brief Mutate a value through the mutator vtable. + * + * \param value The value to mutate. + * \return The mutated owning value. + * \throws Error if mutation fails. + * + * This entry point never intentionally mutates \p value in place. Recursive transformations + * also use \ref Mutate. + */ + TVM_FFI_INLINE Any Mutate(AnyView value) { return MutateExpected(value).value(); } + + /*! + * \brief Exception-free form of \ref Mutate. + * + * \param value The value to mutate. + * \return The mutated owning value, or an Error if mutation failed. + */ + TVM_FFI_INLINE Expected<Any> MutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->mutate)(this, value)); + } + + /*! + * \brief Mutate a value, permitting an in-place implementation when it is safe. + * + * \param value The borrowed value to transform. + * \return The transformed owning value. + * \throws Error if transformation fails. + * + * The returned value may refer to the same object as \p value. Callers must use the return value + * as the result of the transformation rather than assuming that the input object was reused. + */ + TVM_FFI_INLINE Any MaybeInplaceMutate(AnyView value) { + return MaybeInplaceMutateExpected(value).value(); + } + + /*! + * \brief Exception-free form of \ref MaybeInplaceMutate. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateExpected(AnyView value) noexcept { + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>( + (*vtable_->maybe_inplace_mutate)(this, value)); + } + + /*! + * \brief Apply the default structural mutation with copy-on-write behavior. + * + * \param value The value to mutate. + * \return The mutated value, or an Error if hook dispatch, copying, or field mutation failed. + */ + TVM_FFI_INLINE Expected<Any> DefaultMutateExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); + if (is_free_var) { + Expected<Any> mapped_value = GetVarRemapExpected(value); + if (TVM_FFI_PREDICT_FALSE(mapped_value.is_err())) { + return Unexpected(std::move(mapped_value).error()); + } + if (details::ExpectedUnsafe::GetData(mapped_value).type_index() != TypeIndex::kTVMFFINone) { + return mapped_value; + } + } + + int32_t type_index = value.type_index(); + static reflection::TypeAttrColumn column(reflection::type_attr::kStructuralMutate); + AnyView attr = column[type_index]; + Expected<Any> result = [&]() -> Expected<Any> { + if (attr.type_index() != TypeIndex::kTVMFFINone) { + return details::DispatchTypeAttrHookExpected(this, value, attr, + reflection::type_attr::kStructuralMutate); + } + if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) { + return Any(value); + } + return details::MutateReflectedFieldsExpected(this, value); + }(); + if (TVM_FFI_PREDICT_FALSE(result.is_err()) || !is_free_var) { + return result; + } + + Expected<void> set_result = + SetVarRemapExpected(value, details::ExpectedUnsafe::GetData(result)); + if (TVM_FFI_PREDICT_FALSE(set_result.is_err())) { + return Unexpected(std::move(set_result).error()); + } + return result; + } + + /*! + * \brief Apply custom maybe-in-place mutation, or use uniqueness for reflected mutation. + * + * \param value The borrowed value to transform. + * \return The transformed owning value, or an Error if transformation failed. In-place changes + * completed before an Error are not rolled back. + */ + TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateIfUniqueExpected(AnyView value) noexcept { + bool is_free_var = details::IsFreeVar(value); + if (is_free_var) { + Expected<Any> mapped_value = GetVarRemapExpected(value); + if (TVM_FFI_PREDICT_FALSE(mapped_value.is_err())) { + return Unexpected(std::move(mapped_value).error()); + } + if (details::ExpectedUnsafe::GetData(mapped_value).type_index() != TypeIndex::kTVMFFINone) { + return mapped_value; + } + } + + Expected<Any> result = [&]() -> Expected<Any> { + int32_t type_index = value.type_index(); + if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) { + return Any(value); + } + + static reflection::TypeAttrColumn mutate_column(reflection::type_attr::kStructuralMutate); + static reflection::TypeAttrColumn maybe_inplace_mutate_column( + reflection::type_attr::kStructuralMaybeInplaceMutate); + AnyView mutate_attr = mutate_column[type_index]; + AnyView maybe_inplace_mutate_attr = maybe_inplace_mutate_column[type_index]; + bool has_mutate = mutate_attr.type_index() != TypeIndex::kTVMFFINone; + bool has_maybe_inplace_mutate = + maybe_inplace_mutate_attr.type_index() != TypeIndex::kTVMFFINone; + if (TVM_FFI_PREDICT_FALSE(has_maybe_inplace_mutate && !has_mutate)) { + return Unexpected(Error("TypeError", + std::string(reflection::type_attr::kStructuralMaybeInplaceMutate) + + " requires " + + std::string(reflection::type_attr::kStructuralMutate) + + " to be defined for the same type", + "")); + } + if (has_maybe_inplace_mutate) { + return details::DispatchTypeAttrHookExpected( + this, value, maybe_inplace_mutate_attr, + reflection::type_attr::kStructuralMaybeInplaceMutate); + } + if (has_mutate) { + return MutateExpected(value); + } + if (value.as<Object>()->unique()) { + return details::MaybeInplaceMutateReflectedFieldsExpected(this, value); + } + return MutateExpected(value); + }(); + if (TVM_FFI_PREDICT_FALSE(result.is_err()) || !is_free_var) { + return result; + } + + Expected<void> set_result = + SetVarRemapExpected(value, details::ExpectedUnsafe::GetData(result)); + if (TVM_FFI_PREDICT_FALSE(set_result.is_err())) { + return Unexpected(std::move(set_result).error()); + } + return result; + } + + /*! + * \brief Look up the replacement recorded for a variable identity. + * + * \param var The borrowed variable identity to look up. + * \return The owning replacement, FFI None if no replacement exists, or an Error if lookup + * fails. + * + * \note The variable identity must have + * ``kTVMFFISEqHashKindFreeVar`` structural-equality metadata. + */ + TVM_FFI_INLINE Expected<Any> GetVarRemapExpected(AnyView var) noexcept { + if (vtable_->get_var_remap == nullptr) { + return DefaultGetVarRemapExpected(var); + } + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->get_var_remap)(this, var)); + } + + /*! + * \brief Record the replacement for a variable identity. + * + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Successful completion, or an Error if the binding is invalid or cannot be stored. + * + * \note The variable identity must have + * ``kTVMFFISEqHashKindFreeVar`` structural-equality metadata. + */ + TVM_FFI_INLINE Expected<void> SetVarRemapExpected(AnyView var, AnyView mapped_value) noexcept { + if (vtable_->set_var_remap == nullptr) { + return DefaultSetVarRemapExpected(var, mapped_value); + } + return details::ExpectedUnsafe::MoveFromTVMFFIAny<void>( + (*vtable_->set_var_remap)(this, var, mapped_value)); + } + + /*! + * \brief Apply the default variable-remap lookup directly. + * + * \param var The borrowed variable identity to look up. + * \return The owning replacement, FFI None if no replacement exists, or an Error if lookup + * fails. + */ + TVM_FFI_INLINE Expected<Any> DefaultGetVarRemapExpected(AnyView var) noexcept { + if (var.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) { + return Unexpected( + Error("TypeError", "Variable-remap key must be an object-backed value", "")); + } + try { + ObjectRef var_ref = var.cast<ObjectRef>(); + std::optional<Any> result = vtable_->var_remap_.Get(var_ref); + if (!result.has_value()) { + return Any(nullptr); + } + return *std::move(result); + } catch (const Error& err) { + return Unexpected(err); + } + } + + /*! + * \brief Apply the default variable-remap insertion directly. + * + * \param var The borrowed variable identity to bind. + * \param mapped_value The borrowed replacement value. + * \return Successful completion, or an Error if the binding is invalid or cannot be stored. + */ + TVM_FFI_INLINE Expected<void> DefaultSetVarRemapExpected(AnyView var, + AnyView mapped_value) noexcept { + if (var.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) { + return Unexpected( + Error("TypeError", "Variable-remap key must be an object-backed value", "")); + } + try { + ObjectRef var_ref = var.cast<ObjectRef>(); + Any owned_mapped_value(mapped_value); + vtable_->var_remap_.Set(var_ref, owned_mapped_value); + return Expected<void>(); + } catch (const Error& err) { + return Unexpected(err); + } + } + + /*! + * \brief Return the current def-region context. + * \return The active def-region kind. + */ + TVM_FFI_INLINE TVMFFIDefRegionKind def_region_kind() const { return def_region_mode_; } + + /*! + * \brief Temporarily switch the def-region context while invoking \p callback. + * + * \param kind The def-region kind to set during the callback. + * \param callback A nullary callable that performs recursive transformation. + * \return The value returned by \p callback. + */ + template <typename Callback> + TVM_FFI_INLINE auto WithDefRegionKind(TVMFFIDefRegionKind kind, Callback&& callback) + -> decltype(std::forward<Callback>(callback)()) { + class Scope { + public: + Scope(StructuralMutatorObj* mutator, TVMFFIDefRegionKind kind) + : mutator_(mutator), old_kind_(mutator->def_region_mode_) { + mutator_->def_region_mode_ = kind; + } + ~Scope() { mutator_->def_region_mode_ = old_kind_; } + Scope(const Scope&) = delete; + Scope& operator=(const Scope&) = delete; + + private: + StructuralMutatorObj* mutator_; + TVMFFIDefRegionKind old_kind_; + }; + Scope scope(this, kind); + return std::forward<Callback>(callback)(); + } + + /// \cond Doxygen_Suppress + static constexpr const bool _type_mutable = true; + TVM_FFI_DECLARE_OBJECT_INFO("ffi.StructuralMutator", StructuralMutatorObj, Object); + /// \endcond + + protected: + /*! + * \brief Non-owning pointer to the required ABI dispatch table. + */ + StructuralMutatorVTable* vtable_ = nullptr; + + /*! + * \brief Current def-region context for def-region-aware structural transformation. + */ + TVMFFIDefRegionKind def_region_mode_ = kTVMFFIDefRegionKindNone; +}; + +/*! + * \brief ObjectRef wrapper for \ref StructuralMutatorObj. + * + * \sa StructuralMutatorObj + */ +class StructuralMutator : public ObjectRef { + public: + /*! + * \brief Construct from an existing mutator object pointer. + * \param n The object pointer to wrap. + */ + explicit StructuralMutator(ObjectPtr<StructuralMutatorObj> n) : ObjectRef(std::move(n)) {} + + /// \cond Doxygen_Suppress + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StructuralMutator, ObjectRef, StructuralMutatorObj); + /// \endcond +}; + +namespace details { + +/*! + * \brief Dispatch a type-specific structural transformation hook. + * + * \param mutator The active structural mutator. + * \param value The borrowed value passed to the hook. + * \param attr The registered type attribute value. + * \param attr_name The attribute name used in type errors. + * \return The hook result, or an Error if the hook fails or \p attr has an invalid type. + */ +TVM_FFI_INLINE static Expected<Any> DispatchTypeAttrHookExpected( + StructuralMutatorObj* mutator, AnyView value, AnyView attr, + std::string_view attr_name) noexcept { + // case 1: Type-specific override registered as an opaque ABI function pointer. + if (attr.type_index() == TypeIndex::kTVMFFIOpaquePtr) { + auto* hook = reinterpret_cast<FStructuralMutate>(attr.cast<void*>()); + return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*hook)(mutator, value)); + } + + // case 2: Type-specific override registered as an ffi::Function. + if (attr.type_index() == TypeIndex::kTVMFFIFunction) { + return attr.cast<Function>().CallExpected<Any>(mutator, value); + } + + return Unexpected( + Error("TypeError", + std::string(attr_name) + " must be an opaque function pointer or ffi.Function", "")); +} + +/*! + * \brief Transform every reflected structural field of an object. + * + * \tparam Callback A callable compatible with ``Expected<Any>(AnyView)``. + * \param mutator The active structural mutator. + * \param value The original object-backed value. + * \param result The owning shallow-copy result in copy-on-write mode, or the owning input value in + * in-place mode. It is also used to propagate the first Error. + * \param copy_on_write Whether to transform a distinct shallow copy instead of \p value. + * \param callback The recursive field transformation callback. + * \return The original value when copy-on-write mutation changes no fields, otherwise \p result, + * or an Error on failure. + */ +template <typename Callback> +TVM_FFI_INLINE static Expected<Any> TransformReflectedFieldsExpected(StructuralMutatorObj* mutator, Review Comment: MutateReflectedFieldsExpected -- 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]
