File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,15 @@ class SymbolTable {
63
63
LogicalResult rename (StringAttr from, StringRef to);
64
64
LogicalResult rename (Operation *op, StringRef to);
65
65
66
+ // / Renames the given op or the op refered to by the given name to the a name
67
+ // / that is unique within this and the provided other symbol tables and
68
+ // / updates the symbol table and all usages of the symbol accordingly. Returns
69
+ // / the new name or failure if the renaming fails.
70
+ FailureOr<StringAttr> renameToUnique (StringAttr from,
71
+ ArrayRef<SymbolTable *> others);
72
+ FailureOr<StringAttr> renameToUnique (Operation *op,
73
+ ArrayRef<SymbolTable *> others);
74
+
66
75
// / Return the name of the attribute used for symbol names.
67
76
static StringRef getSymbolAttrName () { return " sym_name" ; }
68
77
Original file line number Diff line number Diff line change @@ -258,6 +258,39 @@ LogicalResult SymbolTable::rename(Operation *op, StringRef to) {
258
258
return rename (op, toAttr);
259
259
}
260
260
261
+ FailureOr<StringAttr>
262
+ SymbolTable::renameToUnique (StringAttr oldName,
263
+ ArrayRef<SymbolTable *> others) {
264
+
265
+ // Determine new name that is unique in all symbol tables.
266
+ StringAttr newName;
267
+ {
268
+ MLIRContext *context = oldName.getContext ();
269
+ SmallString<64 > prefix = oldName.getValue ();
270
+ int uniqueId = 0 ;
271
+ prefix.push_back (' _' );
272
+ while (true ) {
273
+ newName = StringAttr::get (context, prefix + Twine (uniqueId++));
274
+ auto lookupNewName = [&](SymbolTable *st) { return st->lookup (newName); };
275
+ if (!lookupNewName (this ) && llvm::none_of (others, lookupNewName)) {
276
+ break ;
277
+ }
278
+ }
279
+ }
280
+
281
+ // Apply renaming.
282
+ if (failed (rename (oldName, newName)))
283
+ return failure ();
284
+ return newName;
285
+ }
286
+
287
+ FailureOr<StringAttr>
288
+ SymbolTable::renameToUnique (Operation *op, ArrayRef<SymbolTable *> others) {
289
+ StringAttr from = getNameIfSymbol (op);
290
+ assert (from && " expected valid 'name' attribute" );
291
+ return renameToUnique (from, others);
292
+ }
293
+
261
294
// / Returns the name of the given symbol operation.
262
295
StringAttr SymbolTable::getSymbolName (Operation *symbol) {
263
296
StringAttr name = getNameIfSymbol (symbol);
You can’t perform that action at this time.
0 commit comments