forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Trace object paths in StructuralEqual #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| /* | ||
| * 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 "location" identifier. | ||
| */ | ||
| class ObjectPathNode : public Object { | ||
| public: | ||
| /*! \brief Get the parent path */ | ||
| ObjectPath GetParent() 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: | ||
| size_t Length() const; | ||
|
|
||
| ObjectPath GetPrefix(size_t length) const; | ||
|
|
||
| bool IsPrefixOf(const ObjectPath& other) const; | ||
|
|
||
| bool PathsEqual(const ObjectPath& other) const; | ||
|
|
||
| /*! \brief Create a path that represents the root object itself. */ | ||
| static ObjectPath Root(); | ||
|
|
||
| TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ObjectPath, ObjectRef, ObjectPathNode); | ||
| }; | ||
|
|
||
| struct ObjectPathPair { | ||
| ObjectPath lhs_path; | ||
| ObjectPath rhs_path; | ||
| }; | ||
|
|
||
| //------------------------------------------------------------------------- | ||
| //----- 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); | ||
|
|
||
| static constexpr const char* _type_key = "AttributeAccessPath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(AttributeAccessPathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class AttributeAccessPath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(AttributeAccessPath, ObjectPath, AttributeAccessPathNode); | ||
| }; | ||
|
|
||
| // ----- Unknown attribute access ----- | ||
|
|
||
| class UnknownAttributeAccessPathNode final : public ObjectPathNode { | ||
| public: | ||
| explicit UnknownAttributeAccessPathNode(ObjectPathNode* parent); | ||
|
|
||
| static constexpr const char* _type_key = "UnknownAttributeAccessPath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(UnknownAttributeAccessPathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class UnknownAttributeAccessPath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(UnknownAttributeAccessPath, ObjectPath, | ||
| UnknownAttributeAccessPathNode); | ||
| }; | ||
|
|
||
| // ----- Array element access by index ----- | ||
|
|
||
| class ArrayIndexPathNode : public ObjectPathNode { | ||
| public: | ||
| /*! \brief Index of the array element that is being accessed. */ | ||
| size_t index; | ||
|
|
||
| explicit ArrayIndexPathNode(ObjectPathNode* parent, size_t index); | ||
|
|
||
| static constexpr const char* _type_key = "ArrayIndexPath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(ArrayIndexPathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class ArrayIndexPath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(ArrayIndexPath, ObjectPath, ArrayIndexPathNode); | ||
| }; | ||
|
|
||
| // ----- Missing array element ----- | ||
|
|
||
| class MissingArrayElementPathNode : public ObjectPathNode { | ||
| public: | ||
| /*! \brief Index of the array element that is missing. */ | ||
| size_t index; | ||
|
|
||
| explicit MissingArrayElementPathNode(ObjectPathNode* parent, size_t index); | ||
|
|
||
| static constexpr const char* _type_key = "MissingArrayElementPath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(MissingArrayElementPathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class MissingArrayElementPath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(MissingArrayElementPath, ObjectPath, MissingArrayElementPathNode); | ||
| }; | ||
|
|
||
| // ----- Map value ----- | ||
|
|
||
| class MapValuePathNode : public ObjectPathNode { | ||
| public: | ||
| /*! \brief Key of the map entry that is being accessed */ | ||
| ObjectRef key; | ||
|
|
||
| explicit MapValuePathNode(ObjectPathNode* parent, ObjectRef key); | ||
|
|
||
| static constexpr const char* _type_key = "MapValuePath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(MapValuePathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class MapValuePath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(MapValuePath, ObjectPath, MapValuePathNode); | ||
| }; | ||
|
|
||
| // ----- Missing map entry ----- | ||
|
|
||
| class MissingMapEntryPathNode : public ObjectPathNode { | ||
| public: | ||
| explicit MissingMapEntryPathNode(ObjectPathNode* parent); | ||
|
|
||
| static constexpr const char* _type_key = "MissingMapEntryPath"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(MissingMapEntryPathNode, ObjectPathNode); | ||
|
|
||
| protected: | ||
| bool LastNodeEqual(const ObjectPathNode& other) const final; | ||
| std::string LastNodeString() const final; | ||
| }; | ||
|
|
||
| class MissingMapEntryPath : public ObjectPath { | ||
| public: | ||
| TVM_DEFINE_OBJECT_REF_METHODS(MissingMapEntryPath, ObjectPath, MissingMapEntryPathNode); | ||
| }; | ||
|
|
||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_NODE_OBJECT_PATH_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be better to have a virtual
PathEqualmethod? Doing so will add one or two lines of duplicate code to do the downcast and recursive call, but the function name seems to be more intuitive to understand. (This also applies to theLastNodeString()method below.Also, is it possible to accept
const ObjectPathNode*to keep up with the convention in TVM?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually perform the comparison in a loop, not with a recursive call, so I believe there would be more duplication that one or two lines.
In general, I'm not sure if that's the right trade off to make here.
LastNodeEqual()is a protected function and is not expected to be used anywhere outsideobject_path.h/.cc, so we probably shouldn't be worried too much about its intuitiveness. On the other hand, introducing code duplication would make the code significantly more fragile, so I would say it's not worth it.I think the root of the problem stems from a shortcut I took here with object decomposition. Ideally, we would have a separate
PathItemclass that would only store one element of the path, and then thePathobject would be responsible for storing aPathItemat the end of the path, as well as a parent pointer. However, under current circumstances (TVM's object model boilerplate overhead + lack of C++17's std::variant) this approach would be cumbersome and inefficient.Regarding your second suggestion: Done (changed the arg type to pointer).