CIRCT  19.0.0git
Namespace.h
Go to the documentation of this file.
1 //===- Namespace.h - Utilities for generating names -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides utilities for generating new names that do not conflict
10 // with existing names.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef CIRCT_SUPPORT_NAMESPACE_H
15 #define CIRCT_SUPPORT_NAMESPACE_H
16 
17 #include "circt/Support/LLVM.h"
18 #include "circt/Support/SymCache.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"
23 
24 namespace circt {
25 
26 /// A namespace that is used to store existing names and generate new names in
27 /// some scope within the IR. This exists to work around limitations of
28 /// SymbolTables. This acts as a base class providing facilities common to all
29 /// namespaces implementations.
30 class Namespace {
31 public:
33  // This fills an entry for an empty string beforehand so that `newName`
34  // doesn't return an empty string.
35  nextIndex.insert({"", 0});
36  }
37  Namespace(const Namespace &other) = default;
38  Namespace(Namespace &&other) : nextIndex(std::move(other.nextIndex)) {}
39 
40  Namespace &operator=(const Namespace &other) = default;
42  nextIndex = std::move(other.nextIndex);
43  return *this;
44  }
45 
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()))
51  nextIndex.insert({symbol.getValue(), 0});
52  }
53 
54  /// SymbolCache initializer; initialize from every key that is convertible to
55  /// a StringAttr in the SymbolCache.
56  void add(SymbolCache &symCache) {
57  for (auto &&[attr, _] : symCache)
58  if (auto strAttr = dyn_cast<StringAttr>(attr))
59  nextIndex.insert({strAttr.getValue(), 0});
60  }
61 
62  /// Empty the namespace.
63  void clear() { nextIndex.clear(); }
64 
65  /// Return a unique name, derived from the input `name`, and add the new name
66  /// to the internal namespace. There are two possible outcomes for the
67  /// returned name:
68  ///
69  /// 1. The original name is returned.
70  /// 2. The name is given a `_<n>` suffix where `<n>` is a number starting from
71  /// `0` and incrementing by one each time (`_0`, ...).
72  StringRef newName(const Twine &name) {
73  // Special case the situation where there is no name collision to avoid
74  // messing with the SmallString allocation below.
75  llvm::SmallString<64> tryName;
76  auto inserted = nextIndex.insert({name.toStringRef(tryName), 0});
77  if (inserted.second)
78  return inserted.first->getKey();
79 
80  // Try different suffixes until we get a collision-free one.
81  if (tryName.empty())
82  name.toVector(tryName); // toStringRef may leave tryName unfilled
83 
84  // Indexes less than nextIndex[tryName] are lready used, so skip them.
85  // Indexes larger than nextIndex[tryName] may be used in another name.
86  size_t &i = nextIndex[tryName];
87  tryName.push_back('_');
88  size_t baseLength = tryName.size();
89  do {
90  tryName.resize(baseLength);
91  Twine(i++).toVector(tryName); // append integer to tryName
92  inserted = nextIndex.insert({tryName, 0});
93  } while (!inserted.second);
94 
95  return inserted.first->getKey();
96  }
97 
98  /// Return a unique name, derived from the input `name` and ensure the
99  /// returned name has the input `suffix`. Also add the new name to the
100  /// internal namespace.
101  /// There are two possible outcomes for the returned name:
102  /// 1. The original name + `_<suffix>` is returned.
103  /// 2. The name is given a suffix `_<n>_<suffix>` where `<n>` is a number
104  /// starting from `0` and incrementing by one each time.
105  StringRef newName(const Twine &name, const Twine &suffix) {
106  // Special case the situation where there is no name collision to avoid
107  // messing with the SmallString allocation below.
108  llvm::SmallString<64> tryName;
109  auto inserted = nextIndex.insert(
110  {name.concat("_").concat(suffix).toStringRef(tryName), 0});
111  if (inserted.second)
112  return inserted.first->getKey();
113 
114  // Try different suffixes until we get a collision-free one.
115  tryName.clear();
116  name.toVector(tryName); // toStringRef may leave tryName unfilled
117  tryName.push_back('_');
118  size_t baseLength = tryName.size();
119 
120  // Get the initial number to start from. Since `:` is not a valid character
121  // in a verilog identifier, we use it separate the name and suffix.
122  // Next number for name+suffix is stored with key `name_:suffix`.
123  tryName.push_back(':');
124  suffix.toVector(tryName);
125 
126  // Indexes less than nextIndex[tryName] are already used, so skip them.
127  // Indexes larger than nextIndex[tryName] may be used in another name.
128  size_t &i = nextIndex[tryName];
129  do {
130  tryName.resize(baseLength);
131  Twine(i++).toVector(tryName); // append integer to tryName
132  tryName.push_back('_');
133  suffix.toVector(tryName);
134  inserted = nextIndex.insert({tryName, 0});
135  } while (!inserted.second);
136 
137  return inserted.first->getKey();
138  }
139 
140 protected:
141  // The "next index" that will be tried when trying to unique a string within a
142  // namespace. It follows that all values less than the "next index" value are
143  // already used.
144  llvm::StringMap<size_t> nextIndex;
145 };
146 
147 } // namespace circt
148 
149 #endif // CIRCT_SUPPORT_NAMESPACE_H
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.
Definition: CalyxOps.cpp:539
A namespace that is used to store existing names and generate new names in some scope within the IR.
Definition: Namespace.h:30
Namespace & operator=(const Namespace &other)=default
void clear()
Empty the namespace.
Definition: Namespace.h:63
Namespace & operator=(Namespace &&other)
Definition: Namespace.h:41
llvm::StringMap< size_t > nextIndex
Definition: Namespace.h:144
void add(SymbolCache &symCache)
SymbolCache initializer; initialize from every key that is convertible to a StringAttr in the SymbolC...
Definition: Namespace.h:56
void add(mlir::ModuleOp module)
Definition: Namespace.h:46
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.
Definition: Namespace.h:105
Namespace(Namespace &&other)
Definition: Namespace.h:38
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.
Definition: Namespace.h:72
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition: SymCache.h:85
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21