Kathryn-cat commented on code in PR #649: URL: https://github.com/apache/tvm-ffi/pull/649#discussion_r3633628412
########## 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: addressed -- 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]
