junrushao1994 commented on code in PR #11977: URL: https://github.com/apache/tvm/pull/11977#discussion_r912440070
########## src/node/object_path.cc: ########## @@ -0,0 +1,322 @@ +/* + * 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. + */ + +#include <tvm/node/object_path.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/memory.h> +#include <tvm/runtime/registry.h> + +#include <algorithm> +#include <cstring> + +using namespace tvm::runtime; + +namespace tvm { + +// ============== ObjectPathNode ============== + +ObjectPathNode::ObjectPathNode(ObjectPathNode* parent) + : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : parent->length_ + 1) {} + +// --- GetParent --- + +ObjectPath ObjectPathNode::GetParent() const { return Downcast<ObjectPath>(parent_); } + +TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const ObjectPath& path) { + return path->GetParent(); +}); + +// --- Length --- + +size_t ObjectPathNode::Length() const { return length_; } + +TVM_REGISTER_GLOBAL("node.ObjectPathLength").set_body_typed([](const ObjectPath& path) { + return static_cast<int64_t>(path->Length()); +}); + +// --- GetPrefix --- + +ObjectPath ObjectPathNode::GetPrefix(size_t length) const { + if (length > Length()) { + throw std::out_of_range("Attempted to get a prefix longer than the path itself"); + } Review Comment: In TVM, we use CHECK (for user-facing errors) and ICHECK (for internal errors) series more often. For example: ```suggestion CHECK_LE(length, Length()) << "IndexError: Attempted to get a prefix longer than the path itself"; ``` The prefix `IndexError` will be converted automatically to python's native IndexError: https://github.com/apache/tvm/blob/6642c6e8b05534d3c08f8fa2969c31d553aa8476/python/tvm/error.py#L62 ########## src/node/object_path.cc: ########## @@ -0,0 +1,322 @@ +/* + * 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. + */ + +#include <tvm/node/object_path.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/memory.h> +#include <tvm/runtime/registry.h> + +#include <algorithm> +#include <cstring> + +using namespace tvm::runtime; + +namespace tvm { + +// ============== ObjectPathNode ============== + +ObjectPathNode::ObjectPathNode(ObjectPathNode* parent) + : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : parent->length_ + 1) {} + +// --- GetParent --- + +ObjectPath ObjectPathNode::GetParent() const { return Downcast<ObjectPath>(parent_); } + +TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const ObjectPath& path) { + return path->GetParent(); +}); + +// --- Length --- + +size_t ObjectPathNode::Length() const { return length_; } + +TVM_REGISTER_GLOBAL("node.ObjectPathLength").set_body_typed([](const ObjectPath& path) { + return static_cast<int64_t>(path->Length()); +}); + +// --- GetPrefix --- + +ObjectPath ObjectPathNode::GetPrefix(size_t length) const { + if (length > Length()) { + throw std::out_of_range("Attempted to get a prefix longer than the path itself"); + } + + const ObjectPathNode* node = this; + size_t suffix_len = Length() - length; + for (size_t i = 0; i < suffix_len; ++i) { + node = node->ParentNode(); + } + + return GetRef<ObjectPath>(node); +} + +TVM_REGISTER_GLOBAL("node.ObjectPathGetPrefix") + .set_body_typed([](const ObjectPath& path, int64_t length) { + if (length < 0) { + throw std::out_of_range("Prefix length can't be negative"); + } + return path->GetPrefix(static_cast<size_t>(length)); Review Comment: Just I would love to discuss two things here, mostly about styles: First, besides legacy in the codebase, in new APIs, we strive to make sure the C++ and Python API are consistent. Therefore, usually we do not prefer putting logic in registration to prevent potential discrepancy. The second is about the usage of `size_t`. It's more like personal preference so I dont have strong opinion: usually I would prefer `int64_t`/`int32_t` to `size_t` if there is no specific need (e.g. large uint, compatibility with STL), because unsigned types could cause some arithmetic issues and `size_t` could be platform-dependent (IIRC). Therefore, how about we prefer using `int` or `int64_t` throughout `ObjectPath` work if there is no specific need? I would imagine it simplify the implementation and eliminate API discrepancy. ########## src/node/object_path.cc: ########## @@ -0,0 +1,322 @@ +/* + * 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. + */ + +#include <tvm/node/object_path.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/memory.h> +#include <tvm/runtime/registry.h> + +#include <algorithm> +#include <cstring> + +using namespace tvm::runtime; + +namespace tvm { + +// ============== ObjectPathNode ============== + +ObjectPathNode::ObjectPathNode(ObjectPathNode* parent) + : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : parent->length_ + 1) {} + +// --- GetParent --- + +ObjectPath ObjectPathNode::GetParent() const { return Downcast<ObjectPath>(parent_); } + +TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const ObjectPath& path) { + return path->GetParent(); +}); + +// --- Length --- + +size_t ObjectPathNode::Length() const { return length_; } + +TVM_REGISTER_GLOBAL("node.ObjectPathLength").set_body_typed([](const ObjectPath& path) { + return static_cast<int64_t>(path->Length()); +}); + +// --- GetPrefix --- + +ObjectPath ObjectPathNode::GetPrefix(size_t length) const { + if (length > Length()) { + throw std::out_of_range("Attempted to get a prefix longer than the path itself"); + } + + const ObjectPathNode* node = this; + size_t suffix_len = Length() - length; + for (size_t i = 0; i < suffix_len; ++i) { + node = node->ParentNode(); + } + + return GetRef<ObjectPath>(node); +} + +TVM_REGISTER_GLOBAL("node.ObjectPathGetPrefix") + .set_body_typed([](const ObjectPath& path, int64_t length) { + if (length < 0) { + throw std::out_of_range("Prefix length can't be negative"); + } + return path->GetPrefix(static_cast<size_t>(length)); + }); + +// --- IsPrefixOf --- + +bool ObjectPathNode::IsPrefixOf(const ObjectPath& other) const { + if (!other.defined()) { + return false; + } Review Comment: Just curious: when will `other` be null? If there is such cases, in the newer codebase, we would prefer wrap `other` using `Optional<ObjectPath>` to be void-safe, i.e. ```suggestion bool ObjectPathNode::IsPrefixOf(const Optional<ObjectPath>& other) const { if (!other.defined()) { return false; } ``` ########## include/tvm/node/object_path.h: ########## @@ -0,0 +1,281 @@ +/* + * 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/node/object_path.h + * ObjectPath class that represents a path from a root object to one of its descendants + * via attribute access, array indexing etc. + */ + +#ifndef TVM_NODE_OBJECT_PATH_H_ +#define TVM_NODE_OBJECT_PATH_H_ + +#include <tvm/runtime/container/string.h> +#include <tvm/runtime/object.h> + +#include <string> + +namespace tvm { + +using runtime::Object; +using runtime::ObjectPtr; +using runtime::ObjectRef; + +class ObjectPath; + +/*! + * \brief Path to an object from some root object. + * + * Motivation: + * + * Same IR node object can be referenced in several different contexts inside a larger IR object. + * For example, a variable could be referenced in several statements within a block. + * + * This makes it impossible to use an object pointer to uniquely identify a "location" within + * the larger IR object for error reporting purposes. The ObjectPath class addresses this problem + * by serving as a unique "locator". + */ +class ObjectPathNode : public Object { + public: + /*! \brief Get the parent path */ + ObjectPath GetParent() const; + /*! + * \brief Get the length of the path. + * + * For example, the path returned by `ObjectPath::Root()` has length 1. + */ + size_t Length() const; + + /*! + * \brief Get a path prefix of the given length. + * + * Provided `length` must not exceed the `Length()` of this path. + */ + ObjectPath GetPrefix(size_t length) const; + + /*! + * \brief Check if this path is a prefix of another path. + * + * The prefix is not strict, i.e. a path is considered a prefix of itself. + */ + bool IsPrefixOf(const ObjectPath& other) const; + + /*! \brief Check if two paths are equal. */ + bool PathsEqual(const ObjectPath& other) const; + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(const char* attr_key); + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(String attr_key); + + /*! \brief Extend this path with access to an array element. */ + ObjectPath ArrayIndex(size_t index); + + /*! \brief Extend this path with access to a missing array element. */ + ObjectPath MissingArrayElement(size_t index); + + /*! \brief Extend this path with access to a map value. */ + ObjectPath MapValue(ObjectRef key); + + /*! \brief Extend this path with access to a missing map entry. */ + ObjectPath MissingMapEntry(); + + static constexpr const char* _type_key = "ObjectPath"; + TVM_DECLARE_BASE_OBJECT_INFO(ObjectPathNode, Object); + + protected: + explicit ObjectPathNode(ObjectPathNode* parent); + + friend class ObjectPath; + friend std::string GetObjectPathRepr(const ObjectPathNode* node); + + const ObjectPathNode* ParentNode() const; + + /*! Compares just the last node of the path, without comparing the whole path. */ + virtual bool LastNodeEqual(const ObjectPathNode* other) const = 0; + + virtual std::string LastNodeString() const = 0; + + private: + ObjectRef parent_; + size_t length_; +}; + +class ObjectPath : public ObjectRef { + public: + /*! \brief Create a path that represents the root object itself. */ + static ObjectPath Root(); + + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ObjectPath, ObjectRef, ObjectPathNode); +}; + +//------------------------------------------------------------------------- +//----- Concrete object path nodes ------------------------------------ +//------------------------------------------------------------------------- + +// ----- Root ----- + +class RootPathNode final : public ObjectPathNode { + public: + explicit RootPathNode(); + + static constexpr const char* _type_key = "RootPath"; + TVM_DECLARE_FINAL_OBJECT_INFO(RootPathNode, ObjectPathNode); + + protected: + bool LastNodeEqual(const ObjectPathNode* other) const final; + std::string LastNodeString() const final; +}; + +class RootPath : public ObjectPath { + public: + TVM_DEFINE_OBJECT_REF_METHODS(RootPath, ObjectPath, RootPathNode); +}; + +// ----- Attribute access ----- + +class AttributeAccessPathNode final : public ObjectPathNode { + public: + /*! \brief Name of the attribute being accessed. Must be a static string. */ + String attr_key; + + explicit AttributeAccessPathNode(ObjectPathNode* parent, String attr_key); Review Comment: A style issue: in TVM we prefer having constructor in `ObjectRef`s. In our case, it's like: ```C++ AttributeAccessPath::AttributeAccessPath(ObjectPathNode* parent, String attr_key) { ObjectPtr<AttributeAccessPathNode> n = make_object<AttributeAccessPath>(); n->parent = parent; n->attr_key = std::move(attr_key); return AttributeAccessPath(n); } ``` ########## include/tvm/node/object_path.h: ########## @@ -0,0 +1,281 @@ +/* + * 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/node/object_path.h + * ObjectPath class that represents a path from a root object to one of its descendants + * via attribute access, array indexing etc. + */ + +#ifndef TVM_NODE_OBJECT_PATH_H_ +#define TVM_NODE_OBJECT_PATH_H_ + +#include <tvm/runtime/container/string.h> +#include <tvm/runtime/object.h> + +#include <string> + +namespace tvm { + +using runtime::Object; +using runtime::ObjectPtr; +using runtime::ObjectRef; + +class ObjectPath; + +/*! + * \brief Path to an object from some root object. + * + * Motivation: + * + * Same IR node object can be referenced in several different contexts inside a larger IR object. + * For example, a variable could be referenced in several statements within a block. + * + * This makes it impossible to use an object pointer to uniquely identify a "location" within + * the larger IR object for error reporting purposes. The ObjectPath class addresses this problem + * by serving as a unique "locator". + */ +class ObjectPathNode : public Object { + public: + /*! \brief Get the parent path */ + ObjectPath GetParent() const; + /*! + * \brief Get the length of the path. + * + * For example, the path returned by `ObjectPath::Root()` has length 1. + */ + size_t Length() const; + + /*! + * \brief Get a path prefix of the given length. + * + * Provided `length` must not exceed the `Length()` of this path. + */ + ObjectPath GetPrefix(size_t length) const; + + /*! + * \brief Check if this path is a prefix of another path. + * + * The prefix is not strict, i.e. a path is considered a prefix of itself. + */ + bool IsPrefixOf(const ObjectPath& other) const; + + /*! \brief Check if two paths are equal. */ + bool PathsEqual(const ObjectPath& other) const; + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(const char* attr_key); + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(String attr_key); + + /*! \brief Extend this path with access to an array element. */ + ObjectPath ArrayIndex(size_t index); + + /*! \brief Extend this path with access to a missing array element. */ + ObjectPath MissingArrayElement(size_t index); + + /*! \brief Extend this path with access to a map value. */ + ObjectPath MapValue(ObjectRef key); + + /*! \brief Extend this path with access to a missing map entry. */ + ObjectPath MissingMapEntry(); + + static constexpr const char* _type_key = "ObjectPath"; + TVM_DECLARE_BASE_OBJECT_INFO(ObjectPathNode, Object); + + protected: + explicit ObjectPathNode(ObjectPathNode* parent); + + friend class ObjectPath; + friend std::string GetObjectPathRepr(const ObjectPathNode* node); + + const ObjectPathNode* ParentNode() const; + + /*! Compares just the last node of the path, without comparing the whole path. */ + virtual bool LastNodeEqual(const ObjectPathNode* other) const = 0; + + virtual std::string LastNodeString() const = 0; + + private: + ObjectRef parent_; Review Comment: Let's make it explicit that the `parent_` would be nullptr ```suggestion Optional<ObjectRef> parent_; ``` ########## include/tvm/node/object_path.h: ########## @@ -0,0 +1,281 @@ +/* + * 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/node/object_path.h + * ObjectPath class that represents a path from a root object to one of its descendants + * via attribute access, array indexing etc. + */ + +#ifndef TVM_NODE_OBJECT_PATH_H_ +#define TVM_NODE_OBJECT_PATH_H_ + +#include <tvm/runtime/container/string.h> +#include <tvm/runtime/object.h> + +#include <string> + +namespace tvm { + +using runtime::Object; +using runtime::ObjectPtr; +using runtime::ObjectRef; + +class ObjectPath; + +/*! + * \brief Path to an object from some root object. + * + * Motivation: + * + * Same IR node object can be referenced in several different contexts inside a larger IR object. + * For example, a variable could be referenced in several statements within a block. + * + * This makes it impossible to use an object pointer to uniquely identify a "location" within + * the larger IR object for error reporting purposes. The ObjectPath class addresses this problem + * by serving as a unique "locator". + */ +class ObjectPathNode : public Object { + public: + /*! \brief Get the parent path */ + ObjectPath GetParent() const; + /*! + * \brief Get the length of the path. + * + * For example, the path returned by `ObjectPath::Root()` has length 1. + */ + size_t Length() const; + + /*! + * \brief Get a path prefix of the given length. + * + * Provided `length` must not exceed the `Length()` of this path. + */ + ObjectPath GetPrefix(size_t length) const; + + /*! + * \brief Check if this path is a prefix of another path. + * + * The prefix is not strict, i.e. a path is considered a prefix of itself. + */ + bool IsPrefixOf(const ObjectPath& other) const; + + /*! \brief Check if two paths are equal. */ + bool PathsEqual(const ObjectPath& other) const; + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(const char* attr_key); + + /*! \brief Extend this path with access to an object attribute. */ + ObjectPath Attr(String attr_key); + + /*! \brief Extend this path with access to an array element. */ + ObjectPath ArrayIndex(size_t index); + + /*! \brief Extend this path with access to a missing array element. */ + ObjectPath MissingArrayElement(size_t index); + + /*! \brief Extend this path with access to a map value. */ + ObjectPath MapValue(ObjectRef key); + + /*! \brief Extend this path with access to a missing map entry. */ + ObjectPath MissingMapEntry(); + + static constexpr const char* _type_key = "ObjectPath"; + TVM_DECLARE_BASE_OBJECT_INFO(ObjectPathNode, Object); + + protected: + explicit ObjectPathNode(ObjectPathNode* parent); + + friend class ObjectPath; + friend std::string GetObjectPathRepr(const ObjectPathNode* node); + + const ObjectPathNode* ParentNode() const; + + /*! Compares just the last node of the path, without comparing the whole path. */ + virtual bool LastNodeEqual(const ObjectPathNode* other) const = 0; + + virtual std::string LastNodeString() const = 0; + + private: + ObjectRef parent_; + size_t length_; +}; + +class ObjectPath : public ObjectRef { + public: + /*! \brief Create a path that represents the root object itself. */ + static ObjectPath Root(); + + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ObjectPath, ObjectRef, ObjectPathNode); Review Comment: QQ: when will we want to get a mutable pointer to `ObjectPathNode`? If there isn't a case, shall we instead use `TVM_DEFINE_OBJECT_REF_METHODS`? Also, we increasingly intend to declare objects as "not-nullable" and have an `Optional<>` wrapper to ensure null-safety. In this case, does it make sense if we declare it using `TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS`? ########## src/node/object_path.cc: ########## @@ -0,0 +1,322 @@ +/* + * 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. + */ + +#include <tvm/node/object_path.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/memory.h> +#include <tvm/runtime/registry.h> + +#include <algorithm> +#include <cstring> + +using namespace tvm::runtime; + +namespace tvm { + +// ============== ObjectPathNode ============== + +ObjectPathNode::ObjectPathNode(ObjectPathNode* parent) + : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : parent->length_ + 1) {} + +// --- GetParent --- + +ObjectPath ObjectPathNode::GetParent() const { return Downcast<ObjectPath>(parent_); } + +TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const ObjectPath& path) { + return path->GetParent(); +}); Review Comment: it's possible to simplify the boilerplate here. [Example](https://github.com/apache/tvm/blob/6642c6e8b05534d3c08f8fa2969c31d553aa8476/src/tir/schedule/schedule.cc#L169-L170). ```suggestion TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_method<ObjectPath>(&ObjectPathNode::GetParent); ``` ########## src/node/object_path.cc: ########## @@ -0,0 +1,322 @@ +/* + * 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. + */ + +#include <tvm/node/object_path.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/memory.h> +#include <tvm/runtime/registry.h> + +#include <algorithm> +#include <cstring> + +using namespace tvm::runtime; + +namespace tvm { + +// ============== ObjectPathNode ============== + +ObjectPathNode::ObjectPathNode(ObjectPathNode* parent) + : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : parent->length_ + 1) {} + +// --- GetParent --- + +ObjectPath ObjectPathNode::GetParent() const { return Downcast<ObjectPath>(parent_); } + +TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const ObjectPath& path) { + return path->GetParent(); +}); + +// --- Length --- + +size_t ObjectPathNode::Length() const { return length_; } + +TVM_REGISTER_GLOBAL("node.ObjectPathLength").set_body_typed([](const ObjectPath& path) { + return static_cast<int64_t>(path->Length()); +}); + +// --- GetPrefix --- + +ObjectPath ObjectPathNode::GetPrefix(size_t length) const { + if (length > Length()) { + throw std::out_of_range("Attempted to get a prefix longer than the path itself"); + } + + const ObjectPathNode* node = this; + size_t suffix_len = Length() - length; + for (size_t i = 0; i < suffix_len; ++i) { + node = node->ParentNode(); + } + + return GetRef<ObjectPath>(node); +} + +TVM_REGISTER_GLOBAL("node.ObjectPathGetPrefix") + .set_body_typed([](const ObjectPath& path, int64_t length) { + if (length < 0) { + throw std::out_of_range("Prefix length can't be negative"); + } + return path->GetPrefix(static_cast<size_t>(length)); + }); + +// --- IsPrefixOf --- + +bool ObjectPathNode::IsPrefixOf(const ObjectPath& other) const { + if (!other.defined()) { + return false; + } + + size_t this_len = Length(); + if (this_len > other->Length()) { + return false; + } + return this->PathsEqual(other->GetPrefix(this_len)); +} + +TVM_REGISTER_GLOBAL("node.ObjectPathIsPrefixOf") + .set_body_typed([](const ObjectPath& a, const ObjectPath& b) { return a->IsPrefixOf(b); }); + +// --- Attr --- + +ObjectPath ObjectPathNode::Attr(const char* attr_key) { + if (attr_key != nullptr) { + return ObjectPath(make_object<AttributeAccessPathNode>(this, attr_key)); + } else { + return ObjectPath(make_object<UnknownAttributeAccessPathNode>(this)); + } +} + +ObjectPath ObjectPathNode::Attr(String attr_key) { + if (attr_key.defined()) { + return ObjectPath(make_object<AttributeAccessPathNode>(this, attr_key)); + } else { + return ObjectPath(make_object<UnknownAttributeAccessPathNode>(this)); + } +} Review Comment: let's make sure nullable values are handled explicitly ```suggestion ObjectPath ObjectPathNode::Attr(Optional<String> attr_key) { if (attr_key.defined()) { return ObjectPath(make_object<AttributeAccessPathNode>(this, attr_key.value())); } else { return ObjectPath(make_object<UnknownAttributeAccessPathNode>(this)); } } ``` -- 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]
