14 #ifndef CIRCT_SUPPORT_NAMESPACE_H
15 #define CIRCT_SUPPORT_NAMESPACE_H
19 #include "mlir/IR/BuiltinOps.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/ADT/Twine.h"
48 void add(mlir::ModuleOp module) {
49 assert(module->getNumRegions() == 1);
50 for (
auto &op : module.getBody(0)->getOperations())
51 if (
auto symbol = op.getAttrOfType<mlir::StringAttr>(
52 SymbolTable::getSymbolAttrName()))
59 for (
auto &&[attr, _] : symCache)
60 if (
auto strAttr = dyn_cast<StringAttr>(attr))
61 nextIndex.insert({strAttr.getValue(), 0});
67 bool erase(llvm::StringRef symbol) {
68 assert(!
locked &&
"Cannot erase names from a locked namespace");
89 llvm::SmallString<64> tryName;
90 auto inserted =
nextIndex.insert({name.toStringRef(tryName), 0});
92 return inserted.first->getKey();
96 name.toVector(tryName);
101 tryName.push_back(
'_');
102 size_t baseLength = tryName.size();
104 tryName.resize(baseLength);
105 Twine(i++).toVector(tryName);
106 inserted =
nextIndex.insert({tryName, 0});
107 }
while (!inserted.second);
109 return inserted.first->getKey();
119 StringRef
newName(
const Twine &name,
const Twine &suffix) {
123 llvm::SmallString<64> tryName;
125 {name.concat(
"_").
concat(suffix).toStringRef(tryName), 0});
127 return inserted.first->getKey();
131 name.toVector(tryName);
132 tryName.push_back(
'_');
133 size_t baseLength = tryName.size();
138 tryName.push_back(
':');
139 suffix.toVector(tryName);
145 tryName.resize(baseLength);
146 Twine(i++).toVector(tryName);
147 tryName.push_back(
'_');
148 suffix.toVector(tryName);
149 inserted =
nextIndex.insert({tryName, 0});
150 }
while (!inserted.second);
152 return inserted.first->getKey();
assert(baseType &&"element must be base type")
static SmallVector< T > concat(const SmallVectorImpl< T > &a, const SmallVectorImpl< T > &b)
Returns a new vector containing the concatenation of vectors a and b.
A namespace that is used to store existing names and generate new names in some scope within the IR.
Namespace & operator=(const Namespace &other)=default
void clear()
Empty the namespace.
Namespace & operator=(Namespace &&other)
llvm::StringMap< size_t > nextIndex
void add(SymbolCache &symCache)
SymbolCache initializer; initialize from every key that is convertible to a StringAttr in the SymbolC...
void add(mlir::ModuleOp module)
bool erase(llvm::StringRef symbol)
Removes a symbol from the namespace.
StringRef newName(const Twine &name, const Twine &suffix)
Return a unique name, derived from the input name and ensure the returned name has the input suffix.
Namespace(Namespace &&other)
Namespace(const Namespace &other)=default
StringRef newName(const Twine &name)
Return a unique name, derived from the input name, and add the new name to the internal namespace.
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.