gemini-code-assist[bot] commented on code in PR #453:
URL: https://github.com/apache/tvm-ffi/pull/453#discussion_r2810496986
##########
src/ffi/extra/reflection_extra.cc:
##########
@@ -133,11 +134,46 @@ inline void AccessPathRegisterReflection() {
[](const AccessPath& self, const AccessPath& other) { return
self->PathEqual(other); });
}
+int64_t StructuralKeyHash(const Any& key) {
+ const auto* key_obj = key.as<StructuralKeyObj>();
+ TVM_FFI_ICHECK_NOTNULL(key_obj);
+ return key_obj->hash_i64;
+}
+
+bool StructuralKeyEqual(const Any& lhs, const Any& rhs) {
+ if (lhs.same_as(rhs)) {
+ return true;
+ }
+ const auto* lhs_obj = lhs.as<StructuralKeyObj>();
+ const auto* rhs_obj = rhs.as<StructuralKeyObj>();
+ TVM_FFI_ICHECK_NOTNULL(lhs_obj);
+ TVM_FFI_ICHECK_NOTNULL(rhs_obj);
+ if (lhs_obj->hash_i64 != rhs_obj->hash_i64) {
+ return false;
+ }
+ return StructuralEqual::Equal(lhs_obj->key, rhs_obj->key);
+}
+
+inline void StructuralKeyRegisterReflection() {
+ namespace refl = tvm::ffi::reflection;
+ refl::ObjectDef<StructuralKeyObj>()
+ .def_ro("key", &StructuralKeyObj::key)
+ .def_ro("hash_i64", &StructuralKeyObj::hash_i64);
+ refl::TypeAttrDef<StructuralKeyObj>()
+ .attr("__any_hash__", reinterpret_cast<void*>(&StructuralKeyHash))
+ .attr("__any_equal__", reinterpret_cast<void*>(&StructuralKeyEqual));
+}
+
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
AccessStepRegisterReflection();
AccessPathRegisterReflection();
- refl::GlobalDef().def_packed("ffi.MakeObjectFromPackedArgs",
MakeObjectFromPackedArgs);
+ StructuralKeyRegisterReflection();
+
+ refl::GlobalDef()
+ .def_packed("ffi.MakeObjectFromPackedArgs", MakeObjectFromPackedArgs)
+ .def("ffi.StructuralKey", [](Any key) { return
StructuralKey(std::move(key)); })
+ .def("ffi.StructuralKeyEqual", StructuralKeyEqual);
Review Comment:

The FFI function `StructuralKeyHash` is declared in
`python/tvm_ffi/_ffi_api.py` but it's not registered as a global function here.
This will cause a runtime error if `tvm_ffi._ffi_api.StructuralKeyHash` is
called from Python. Please register it to match the Python API definition.
```c
.def("ffi.StructuralKeyEqual", StructuralKeyEqual)
.def("ffi.StructuralKeyHash", StructuralKeyHash);
```
--
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]