junrushao1994 commented on code in PR #12101:
URL: https://github.com/apache/tvm/pull/12101#discussion_r922706434
##########
src/node/reflection.cc:
##########
@@ -281,4 +281,43 @@
TVM_REGISTER_GLOBAL("node.NodeGetAttr").set_body(NodeGetAttr);
TVM_REGISTER_GLOBAL("node.NodeListAttrNames").set_body(NodeListAttrNames);
TVM_REGISTER_GLOBAL("node.MakeNode").set_body(MakeNode);
+
+namespace {
+// Attribute visitor class for finding the attribute key by its address
+class GetAttrKeyByAddressVisitor : public AttrVisitor {
+ public:
+ explicit GetAttrKeyByAddressVisitor(const void* attr_address)
+ : attr_address_(attr_address), key_(nullptr) {}
+
+ void Visit(const char* key, double* value) final { DoVisit(key, value); }
+ void Visit(const char* key, int64_t* value) final { DoVisit(key, value); }
+ void Visit(const char* key, uint64_t* value) final { DoVisit(key, value); }
+ void Visit(const char* key, int* value) final { DoVisit(key, value); }
+ void Visit(const char* key, bool* value) final { DoVisit(key, value); }
+ void Visit(const char* key, std::string* value) final { DoVisit(key, value);
}
+ void Visit(const char* key, void** value) final { DoVisit(key, value); }
+ void Visit(const char* key, DataType* value) final { DoVisit(key, value); }
+ void Visit(const char* key, runtime::NDArray* value) final { DoVisit(key,
value); }
+ void Visit(const char* key, runtime::ObjectRef* value) final { DoVisit(key,
value); }
+
+ const char* GetKey() const { return key_; }
+
+ private:
+ const void* attr_address_;
+ const char* key_;
+
+ void DoVisit(const char* key, const void* candidate) {
+ if (attr_address_ == candidate) {
+ key_ = key;
+ }
+ }
+};
+} // anonymous namespace
+
+const char* GetAttrKeyByAddress(const Object* object, const void*
attr_address) {
+ GetAttrKeyByAddressVisitor visitor(attr_address);
+ ReflectionVTable::Global()->VisitAttrs(const_cast<Object*>(object),
&visitor);
+ return visitor.GetKey();
Review Comment:
quick question: what is the expected behavior if `GetKey()` returns nullptr?
--
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]