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


##########
include/tvm/ffi/extra/structural_mutate.h:
##########
@@ -0,0 +1,904 @@
+/*
+ * 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 <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 FStructuralVarRemapGet = 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 FStructuralVarRemapSet = TVMFFIAny (*)(StructuralMutatorObj* mutator, 
AnyView var,
+                                             AnyView mapped_value) noexcept;
+
+namespace details {
+
+// Copy and structurally mutate the reflected fields of an object-backed value.
+TVM_FFI_INLINE static Expected<Any> 
MutateReflectedFieldsExpected(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.
+   */
+  FStructuralVarRemapGet var_remap_get = 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.
+   */
+  FStructuralVarRemapSet var_remap_set = nullptr;
+};
+
+/*!
+ * \brief Object node of a structural mutator.
+ */
+class StructuralMutatorObj : public Object {
+ public:
+  /*!
+   * \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 Mutate a value, in-place mutate only when it is uniquely owned.
+   *
+   * \param value The borrowed value to transform.
+   * \return The transformed owning value, or an Error if transformation 
failed.
+   */
+  TVM_FFI_INLINE Expected<Any> MaybeInplaceMutateIfUniqueExpected(AnyView 
value) noexcept {
+    const Object* obj = value.as<Object>();
+    if (obj == nullptr || obj->unique()) {

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