1717 * under the License.
1818 */
1919
20+ /* !
21+ * \file tvm/ir/name_supply.h
22+ * \brief NameSupply that can be used to generate unique variable names.
23+ */
2024#ifndef TVM_IR_NAME_SUPPLY_H_
2125#define TVM_IR_NAME_SUPPLY_H_
2226
2327#include < string>
2428#include < unordered_map>
29+ #include < utility>
2530
2631#include " tvm/ir/expr.h"
2732
2833namespace tvm {
2934
35+ /* !
36+ * \brief NameSupply can be used to generate unique names.
37+ */
3038class NameSupplyNode : public Object {
3139 public:
32- NameSupplyNode () : NameSupplyNode(" " ) {}
33-
34- explicit NameSupplyNode (const String& prefix);
35-
40+ /* !
41+ * \brief Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro.
42+ */
43+ NameSupplyNode () = default ;
44+
45+ /* !
46+ * \brief Constructor.
47+ * \param prefix The prefix to be used with this NameSupply.
48+ * \param name_map The map used to guarantee uniqueness.
49+ */
50+ NameSupplyNode (const String& prefix, std::unordered_map<std::string, int > name_map)
51+ : prefix_(prefix), name_map(std::move(name_map)) {}
52+
53+ /* !
54+ * \brief Generates a unique name from this NameSupply.
55+ * \param name The name from which the generated name is derived.
56+ * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
57+ * name. \return A unique name.
58+ */
3659 String FreshName (const String& name, bool add_prefix = true );
3760
61+ /* !
62+ * \brief Reserves an existing name with this NameSupply.
63+ * \param name The name to be reserved.
64+ * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
65+ * name before reserving it. \return The name that was reserved with the NameSupply. It can be
66+ * different if a prefix is added.
67+ */
3868 String ReserveName (const String& name, bool add_prefix = true );
3969
70+ /* !
71+ * \brief Checks if this NameSupply already generated a name.
72+ * \param name The name to check.
73+ * \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
74+ * name before checking for it. \return True if the name has already been generated. False
75+ * otherwise.
76+ */
4077 bool ContainsName (const String& name, bool add_prefix = true );
4178
42- void Clear ();
43-
44- void VisitAttrs (AttrVisitor* v) { v->Visit (" prefix" , &prefix_); }
79+ void VisitAttrs (AttrVisitor* v) {}
4580
4681 // Prefix for all GlobalVar names. It can be empty.
4782 std::string prefix_;
@@ -52,32 +87,35 @@ class NameSupplyNode : public Object {
5287 TVM_DECLARE_FINAL_OBJECT_INFO (NameSupplyNode, Object);
5388
5489 private:
55- String prefix_module_name (const String& name);
56-
90+ /* ! \brief Helper function to add the NameSupply prefix to the name. */
91+ String add_prefix_to_name (const String& name);
92+
93+ /* !
94+ * \brief Function that will generate a unique name.
95+ * \param name The name to be used as a base.
96+ * \return A unique name.
97+ */
5798 std::string GetUniqueName (std::string name);
5899
59- // Key is function_name. Value is a counter.
100+ /* ! \brief A map that is used to generate unique names. */
60101 std::unordered_map<std::string, int > name_map;
61-
62- friend class NameSupply ;
63102};
64103
104+ /* !
105+ * \brief Managed reference class to NameSupplyNode.
106+ * \sa NameSupplyNode
107+ */
65108class NameSupply : public ObjectRef {
66109 public:
67- TVM_DLL explicit NameSupply ();
68-
110+ /* !
111+ * \brief Constructor.
112+ * \param prefix The prefix to be used with this NameSupply.
113+ * \param name_map An optional map.
114+ */
69115 TVM_DLL explicit NameSupply (const String& prefix,
70116 std::unordered_map<std::string, int > name_map = {});
71117
72- explicit NameSupply (ObjectPtr<Object> n) : ObjectRef(n) {}
73- /* ! \return mutable pointers to the node. */
74- NameSupplyNode* operator ->() const {
75- auto * ptr = get_mutable ();
76- ICHECK (ptr != nullptr );
77- return static_cast <NameSupplyNode*>(ptr);
78- }
79-
80- TVM_DEFINE_OBJECT_REF_COW_METHOD (NameSupplyNode);
118+ TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS (NameSupply, ObjectRef, NameSupplyNode);
81119};
82120
83121} // namespace tvm
0 commit comments