Kathryn-cat commented on code in PR #649:
URL: https://github.com/apache/tvm-ffi/pull/649#discussion_r3627349371


##########
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:
   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]

Reply via email to