tqchen commented on code in PR #601:
URL: https://github.com/apache/tvm-ffi/pull/601#discussion_r3328746325


##########
src/ffi/extra/structural_visit.cc:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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) {
+  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;
+        }
+
+        reflection::FieldGetter getter(field_info);
+        Any field_value = getter(value);
+
+        TVMFFIDefRegionKind kind = kTVMFFIDefRegionKindNone;
+        if (field_info->flags & kTVMFFIFieldFlagBitMaskSEqHashDefNonRecursive) 
{
+          kind = kTVMFFIDefRegionKindNonRecursive;
+        } else if (field_info->flags & 
kTVMFFIFieldFlagBitMaskSEqHashDefRecursive) {
+          kind = kTVMFFIDefRegionKindRecursive;
+        }
+
+        if (auto child = field_value.as<ObjectRef>()) {
+          if (TVM_FFI_PREDICT_FALSE(kind != kTVMFFIDefRegionKindNone)) {
+            result =
+                visitor->WithDefRegionKind(kind, [&]() { return 
visitor->VisitExpected(*child); });
+          } else {
+            result = visitor->VisitExpected(*child);
+          }
+        }
+        return TVM_FFI_PREDICT_FALSE(StructuralVisitNeedEarlyReturn(result));

Review Comment:
   this does not involve branch and do not need predict



-- 
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