24 #ifndef TVM_IR_NAME_SUPPLY_H_
25 #define TVM_IR_NAME_SUPPLY_H_
27 #include <tvm/ffi/reflection/registry.h>
33 #include <unordered_map>
53 NameSupplyNode(
const String& prefix, std::unordered_map<std::string, int> name_map)
54 :
prefix_(prefix), name_map(std::move(name_map)) {}
64 String
FreshName(
const String& name,
bool add_prefix =
true,
bool add_underscore =
true);
73 String
ReserveName(
const String& name,
bool add_prefix =
true);
87 static constexpr
const char*
_type_key =
"ir.NameSupply";
92 String add_prefix_to_name(
const String& name);
100 std::string GetUniqueName(std::string name,
bool add_underscore =
true);
103 std::unordered_map<std::string, int> name_map;
118 std::unordered_map<std::string, int> name_map = {});
126 template <
typename Iter,
typename Lambda>
128 :
NameSupply(
"", GetNameMap(begin, end, f)) {}
133 template <
typename Iter,
typename Lambda>
134 static std::unordered_map<std::string, int> GetNameMap(Iter begin, Iter end, Lambda f) {
136 static_assert(std::is_convertible<decltype(f(*begin)), std::string>::value,
137 "Lambda f must has a signature of [?](*it) -> string {}");
138 std::unordered_map<std::string, int> name_map;
139 for (
auto it = begin; it != end; ++it) {
140 const std::string& name = f(*it);
141 const size_t idx_last_first_num = std::distance(
142 std::find_if(name.rbegin(), name.rend(), [](
char c) { return !std::isdigit(c); }),
146 std::string prefix = name.substr(0, idx_last_first_num);
147 ICHECK(prefix.size() > 0 && std::isalpha(prefix[0])) <<
"Invalid variable name: " << name;
148 if (0 == name_map.count(prefix)) name_map[prefix] = 0;
149 if (idx_last_first_num < name.size()) {
152 name_map[prefix] =
std::max(name_map[prefix], std::stoi(name.substr(idx_last_first_num)));
NameSupply can be used to generate unique names.
Definition: name_supply.h:41
NameSupplyNode(const String &prefix, std::unordered_map< std::string, int > name_map)
Constructor.
Definition: name_supply.h:53
String FreshName(const String &name, bool add_prefix=true, bool add_underscore=true)
Generates a unique name from this NameSupply.
TVM_DECLARE_FINAL_OBJECT_INFO(NameSupplyNode, Object)
static constexpr const char * _type_key
Definition: name_supply.h:87
String ReserveName(const String &name, bool add_prefix=true)
Reserves an existing name with this NameSupply.
std::string prefix_
Definition: name_supply.h:85
NameSupplyNode()=default
Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro.
bool ContainsName(const String &name, bool add_prefix=true)
Checks if this NameSupply already generated a name.
Managed reference class to NameSupplyNode.
Definition: name_supply.h:110
NameSupply(Iter begin, Iter end, Lambda f)
Construct NameSupply with a name map created from the given iterator range and the functor.
Definition: name_supply.h:127
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(NameSupply, ObjectRef, NameSupplyNode)
NameSupply(const String &prefix="", std::unordered_map< std::string, int > name_map={})
Constructor.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values