@@ -218,6 +218,46 @@ StringAttr SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
218
218
return getSymbolName (symbol);
219
219
}
220
220
221
+ LogicalResult SymbolTable::rename (StringAttr from, StringAttr to) {
222
+ Operation *op = lookup (from);
223
+ return rename (op, to);
224
+ }
225
+
226
+ LogicalResult SymbolTable::rename (Operation *op, StringAttr to) {
227
+ StringAttr from = getNameIfSymbol (op);
228
+
229
+ assert (from && " expected valid 'name' attribute" );
230
+ assert (op->getParentOp () == symbolTableOp &&
231
+ " expected this operation to be inside of the operation with this "
232
+ " SymbolTable" );
233
+ assert (lookup (from) == op && " current name does not resolve to op" );
234
+ assert (lookup (to) == nullptr && " new name already exists" );
235
+
236
+ if (failed (SymbolTable::replaceAllSymbolUses (op, to, getOp ())))
237
+ return failure ();
238
+
239
+ // Remove op with old name, change name, add with new name. The order is
240
+ // important here due to how `remove` and `insert` rely on the op name.
241
+ remove (op);
242
+ setSymbolName (op, to);
243
+ insert (op);
244
+
245
+ assert (lookup (to) == op && " new name does not resolve to renamed op" );
246
+ assert (lookup (from) == nullptr && " old name still exists" );
247
+
248
+ return success ();
249
+ }
250
+
251
+ LogicalResult SymbolTable::rename (StringAttr from, StringRef to) {
252
+ auto toAttr = StringAttr::get (getOp ()->getContext (), to);
253
+ return rename (from, toAttr);
254
+ }
255
+
256
+ LogicalResult SymbolTable::rename (Operation *op, StringRef to) {
257
+ auto toAttr = StringAttr::get (getOp ()->getContext (), to);
258
+ return rename (op, toAttr);
259
+ }
260
+
221
261
// / Returns the name of the given symbol operation.
222
262
StringAttr SymbolTable::getSymbolName (Operation *symbol) {
223
263
StringAttr name = getNameIfSymbol (symbol);
0 commit comments