14 #ifndef MLIR_SUPPORT_NAMESPACE_H
15 #define MLIR_SUPPORT_NAMESPACE_H
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/ADT/Twine.h"
47 void add(mlir::ModuleOp module) {
48 assert(module->getNumRegions() == 1);
49 for (
auto &op : module.getBody(0)->getOperations())
50 if (
auto symbol = op.getAttrOfType<mlir::StringAttr>(
58 for (
auto &&[attr, _] : symCache)
59 if (
auto strAttr = dyn_cast<mlir::StringAttr>(attr))
60 nextIndex.insert({strAttr.getValue(), 0});
68 bool erase(llvm::StringRef symbol) {
69 assert(!
locked &&
"Cannot erase names from a locked namespace");
86 llvm::StringRef
newName(
const llvm::Twine &name) {
91 auto inserted =
nextIndex.insert({name.toStringRef(tryName), 0});
93 return inserted.first->getKey();
97 name.toVector(tryName);
102 tryName.push_back(
'_');
103 size_t baseLength = tryName.size();
105 tryName.resize(baseLength);
106 llvm::Twine(i++).toVector(tryName);
107 inserted =
nextIndex.insert({tryName, 0});
108 }
while (!inserted.second);
110 return inserted.first->getKey();
120 llvm::StringRef
newName(
const llvm::Twine &name,
const llvm::Twine &suffix) {
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);
146 tryName.resize(baseLength);
147 llvm::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();
A namespace that is used to store existing names and generate new names in some scope within the IR.
llvm::StringMap< size_t > nextIndex
void add(SymbolCache &symCache)
SymbolCache initializer; initialize from every key that is convertible to a StringAttr in the SymbolC...
llvm::StringRef newName(const llvm::Twine &name, const llvm::Twine &suffix)
Return a unique name, derived from the input name and ensure the returned name has the input suffix.
Namespace(Namespace &&other)
bool erase(llvm::StringRef symbol)
Removes a symbol from the namespace.
void add(mlir::ModuleOp module)
void add(llvm::StringRef name)
Namespace & operator=(const Namespace &other)=default
void clear()
Empty the namespace.
llvm::StringRef newName(const llvm::Twine &name)
Return a unique name, derived from the input name, and add the new name to the internal namespace.
Namespace(const Namespace &other)=default
Namespace & operator=(Namespace &&other)
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
SmallVector< AffineExpr, 4 > concat(ArrayRef< AffineExpr > a, ArrayRef< AffineExpr > b)
Return the vector that is the concatenation of a and b.
Include the generated interface declarations.