-
Notifications
You must be signed in to change notification settings - Fork 4k
Unify name mangling in TVM #12066
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
Unify name mangling in TVM #12066
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a752322
Add NameSupply and GlobalVarSupply
gigiblender 11418f2
Build GlobalVarSupply from IRModules instead of having it attached to…
gigiblender 3ad2d08
Pass GlobalVarSupply when lowering shape funcs
8593707
Partially replace instantiations of GlobalVar with GlobalVarSupply
b606261
Construct GlobalVarSupply from IRModule
806d415
Add tests for supply
5df20bb
Add documentation for NameSupply and GlobalVarSupply
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
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,125 @@ | ||
| /* | ||
| * 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/ir/global_var_supply.h | ||
| * \brief GlobalVarSupply that can be used to generate unique \class GlobalVar. | ||
| */ | ||
| #ifndef TVM_IR_GLOBAL_VAR_SUPPLY_H_ | ||
| #define TVM_IR_GLOBAL_VAR_SUPPLY_H_ | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "tvm/ir/expr.h" | ||
| #include "tvm/ir/module.h" | ||
| #include "tvm/ir/name_supply.h" | ||
|
|
||
| namespace tvm { | ||
|
|
||
| /*! | ||
| * \brief GlobalVarSupply can be used to generate unique GlobalVars. | ||
| */ | ||
| class GlobalVarSupplyNode : public Object { | ||
| public: | ||
| /*! | ||
| * \brief Empty constructor. Will use an empty NameSupply. | ||
| */ | ||
| GlobalVarSupplyNode() : GlobalVarSupplyNode(NameSupply("")) {} | ||
|
|
||
| /*! | ||
| * \brief Constructor. | ||
| * \param name_supply The NameSupply to use for generating the names of fresh GlobalVars. | ||
| * \param name_to_var_map An optional map. | ||
| */ | ||
| explicit GlobalVarSupplyNode(NameSupply name_supply, | ||
| std::unordered_map<std::string, GlobalVar> name_to_var_map = {}); | ||
|
|
||
| /*! | ||
| * \brief Generates a unique GlobalVar from this supply. | ||
| * \param name The name from which the name of the GlobalVar is derived. | ||
| * \param add_prefix If set to true, then the prefix of the contained NameSupply will be prepended | ||
| * to the name. \return A unique GlobalVar. | ||
| */ | ||
| GlobalVar FreshGlobal(String name, bool add_prefix = true); | ||
|
|
||
| /*! | ||
| * \brief Looks up for a GlobalVar with the given name in this supply. | ||
| * If no entry is found, creates one, places it in the cache and returns it. | ||
| * \param name The name of the GlobalVar to search for. | ||
| * \param add_prefix If set to true, the prefix of the contained NameSupply will be prepended to | ||
| * the name before performing the search. \return A cached GlobalVar. | ||
| */ | ||
| GlobalVar UniqueGlobalFor(const String& name, bool add_prefix = true); | ||
|
|
||
| /*! | ||
| * \brief Reserves an existing GlobalVar with this supply. | ||
| * \param var The GlobalVar to be registered. | ||
| * \param allow_conflict Allow conflict with other GlobalVars that have the same name. | ||
| */ | ||
| void ReserveGlobalVar(const GlobalVar& var, bool allow_conflict = false); | ||
|
|
||
| void VisitAttrs(AttrVisitor* v) {} | ||
|
|
||
| /*! \brief The NameSupply used to generate unique name hints to GlobalVars. */ | ||
| NameSupply name_supply_; | ||
|
|
||
| static constexpr const char* _type_key = "GlobalVarSupply"; | ||
| static constexpr const bool _type_has_method_sequal_reduce = false; | ||
| static constexpr const bool _type_has_method_shash_reduce = false; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(GlobalVarSupplyNode, Object); | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, GlobalVar> name_to_var_map_; | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief Managed reference class to GlobalVarSupplyNode. | ||
| * \sa GlobalVarSupplyNode | ||
| */ | ||
| class GlobalVarSupply : public ObjectRef { | ||
| public: | ||
| /*! | ||
| * \brief Constructor. | ||
| * \param name_supply The NameSupply to be used when generating new GlobalVars. | ||
| * \param name_to_var_map An optional map. | ||
| */ | ||
| TVM_DLL explicit GlobalVarSupply(const NameSupply& name_supply, | ||
| std::unordered_map<std::string, GlobalVar> name_to_var_map = {}); | ||
|
|
||
| /*! | ||
| * \brief Constructs a supply from an array of IRModules. GlobalVars generated by this supply are | ||
| * guaranteed not to conflict with any GlobalVars that belong to the modules. \param modules Array | ||
| * of IRModules. | ||
| */ | ||
| TVM_DLL explicit GlobalVarSupply(const Array<IRModule>& modules); | ||
|
|
||
| /*! | ||
| * \brief Constructs a GlobalVarSupply from an IRModule. GlobalVars generated by this supply are | ||
| * guaranteed not to conflict with GlobalVars that belong to the modules. \param module The | ||
| * IRModule. | ||
| */ | ||
| TVM_DLL explicit GlobalVarSupply(const IRModule module); | ||
|
|
||
| TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(GlobalVarSupply, ObjectRef, GlobalVarSupplyNode); | ||
| }; | ||
|
|
||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_IR_GLOBAL_VAR_SUPPLY_H_ | ||
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
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,123 @@ | ||
| /* | ||
| * 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/ir/name_supply.h | ||
| * \brief NameSupply that can be used to generate unique variable names. | ||
| */ | ||
| #ifndef TVM_IR_NAME_SUPPLY_H_ | ||
| #define TVM_IR_NAME_SUPPLY_H_ | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
|
|
||
| #include "tvm/ir/expr.h" | ||
|
|
||
| namespace tvm { | ||
|
|
||
| /*! | ||
| * \brief NameSupply can be used to generate unique names. | ||
| */ | ||
| class NameSupplyNode : public Object { | ||
| public: | ||
| /*! | ||
| * \brief Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro. | ||
| */ | ||
| NameSupplyNode() = default; | ||
|
|
||
| /*! | ||
| * \brief Constructor. | ||
| * \param prefix The prefix to be used with this NameSupply. | ||
| * \param name_map The map used to guarantee uniqueness. | ||
| */ | ||
| NameSupplyNode(const String& prefix, std::unordered_map<std::string, int> name_map) | ||
| : prefix_(prefix), name_map(std::move(name_map)) {} | ||
|
|
||
| /*! | ||
| * \brief Generates a unique name from this NameSupply. | ||
| * \param name The name from which the generated name is derived. | ||
| * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the | ||
| * name. \return A unique name. | ||
| */ | ||
| String FreshName(const String& name, bool add_prefix = true); | ||
|
gigiblender marked this conversation as resolved.
|
||
|
|
||
| /*! | ||
| * \brief Reserves an existing name with this NameSupply. | ||
| * \param name The name to be reserved. | ||
| * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the | ||
| * name before reserving it. \return The name that was reserved with the NameSupply. It can be | ||
| * different if a prefix is added. | ||
| */ | ||
| String ReserveName(const String& name, bool add_prefix = true); | ||
|
|
||
| /*! | ||
| * \brief Checks if this NameSupply already generated a name. | ||
| * \param name The name to check. | ||
| * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the | ||
| * name before checking for it. \return True if the name has already been generated. False | ||
| * otherwise. | ||
| */ | ||
| bool ContainsName(const String& name, bool add_prefix = true); | ||
|
|
||
| void VisitAttrs(AttrVisitor* v) {} | ||
|
|
||
| // Prefix for all GlobalVar names. It can be empty. | ||
| std::string prefix_; | ||
|
|
||
| static constexpr const char* _type_key = "NameSupply"; | ||
| static constexpr const bool _type_has_method_sequal_reduce = false; | ||
| static constexpr const bool _type_has_method_shash_reduce = false; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(NameSupplyNode, Object); | ||
|
|
||
| private: | ||
| /*! \brief Helper function to add the NameSupply prefix to the name. */ | ||
| String add_prefix_to_name(const String& name); | ||
|
|
||
| /*! | ||
| * \brief Function that will generate a unique name. | ||
| * \param name The name to be used as a base. | ||
| * \return A unique name. | ||
| */ | ||
| std::string GetUniqueName(std::string name); | ||
|
|
||
| /*! \brief A map that is used to generate unique names. */ | ||
| std::unordered_map<std::string, int> name_map; | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief Managed reference class to NameSupplyNode. | ||
| * \sa NameSupplyNode | ||
| */ | ||
| class NameSupply : public ObjectRef { | ||
| public: | ||
| /*! | ||
| * \brief Constructor. | ||
| * \param prefix The prefix to be used with this NameSupply. | ||
| * \param name_map An optional map. | ||
| */ | ||
| TVM_DLL explicit NameSupply(const String& prefix, | ||
| std::unordered_map<std::string, int> name_map = {}); | ||
|
|
||
| TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(NameSupply, ObjectRef, NameSupplyNode); | ||
| }; | ||
|
|
||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_IR_NAME_SUPPLY_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.
Uh oh!
There was an error while loading. Please reload this page.