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 = mlir::dyn_cast<mlir::SymbolOpInterface>(&op))
52 nextIndex.insert(std::pair<StringRef, size_t>(symbol.getName(), 0));
58 for (
auto &&[attr, _] : symCache)
59 if (
auto strAttr = dyn_cast<StringAttr>(attr))
60 nextIndex.insert({strAttr.getValue(), 0});
63 void add(StringRef name) { nextIndex.insert({name, 0}); }
68 bool erase(llvm::StringRef symbol) {
69 assert(!locked &&
"Cannot erase names from a locked namespace");
70 return nextIndex.erase(symbol);
90 llvm::SmallString<64> tryName;
91 auto inserted = nextIndex.insert({name.toStringRef(tryName), 0});
93 return inserted.first->getKey();
97 name.toVector(tryName);
101 size_t &i = nextIndex[tryName];
102 tryName.push_back(
'_');
103 size_t baseLength = tryName.size();
105 tryName.resize(baseLength);
106 Twine(i++).toVector(tryName);
107 inserted = nextIndex.insert({tryName, 0});
108 }
while (!inserted.second);
110 return inserted.first->getKey();
120 StringRef
newName(
const Twine &name,
const Twine &suffix) {
124 llvm::SmallString<64> tryName;
125 auto inserted = nextIndex.insert(
126 {name.concat(
"_").concat(suffix).toStringRef(tryName), 0});
128 return inserted.first->getKey();
132 name.toVector(tryName);
133 tryName.push_back(
'_');
134 size_t baseLength = tryName.size();
139 tryName.push_back(
':');
140 suffix.toVector(tryName);
144 size_t &i = nextIndex[tryName];
146 tryName.resize(baseLength);
147 Twine(i++).toVector(tryName);
148 tryName.push_back(
'_');
149 suffix.toVector(tryName);
150 inserted = nextIndex.insert({tryName, 0});
151 }
while (!inserted.second);
153 return inserted.first->getKey();
assert(baseType &&"element must be base type")
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.
llvm::StringMap< size_t > nextIndex
void add(SymbolCache &symCache)
SymbolCache initializer; initialize from every key that is convertible to a StringAttr in the SymbolC...
Namespace & operator=(Namespace &&other)
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.