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"
46 void add(mlir::ModuleOp module) {
47 assert(module->getNumRegions() == 1);
48 for (
auto &op : module.getBody(0)->getOperations())
49 if (
auto symbol = op.getAttrOfType<mlir::StringAttr>(
50 SymbolTable::getSymbolAttrName()))
57 for (
auto &&[attr, _] : symCache)
58 if (
auto strAttr = dyn_cast<StringAttr>(attr))
59 nextIndex.insert({strAttr.getValue(), 0});
75 llvm::SmallString<64> tryName;
76 auto inserted =
nextIndex.insert({name.toStringRef(tryName), 0});
78 return inserted.first->getKey();
82 name.toVector(tryName);
87 tryName.push_back(
'_');
88 size_t baseLength = tryName.size();
90 tryName.resize(baseLength);
91 Twine(i++).toVector(tryName);
92 inserted =
nextIndex.insert({tryName, 0});
93 }
while (!inserted.second);
95 return inserted.first->getKey();
105 StringRef
newName(
const Twine &name,
const Twine &suffix) {
108 llvm::SmallString<64> tryName;
110 {name.concat(
"_").
concat(suffix).toStringRef(tryName), 0});
112 return inserted.first->getKey();
116 name.toVector(tryName);
117 tryName.push_back(
'_');
118 size_t baseLength = tryName.size();
123 tryName.push_back(
':');
124 suffix.toVector(tryName);
130 tryName.resize(baseLength);
131 Twine(i++).toVector(tryName);
132 tryName.push_back(
'_');
133 suffix.toVector(tryName);
134 inserted =
nextIndex.insert({tryName, 0});
135 }
while (!inserted.second);
137 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)
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.