Skip to content

Commit feec2d9

Browse files
committed
[mlir] return the updated symbol table after inserting into SymbolTable
Inserting a symbol into a SymbolTable may lead to the name of the symbol being changed in order to ensure uniqueness of symbol names in the table. Return this new name to spare the caller the need to extract it from the symbol operation. Depends On D112700 Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D112886
1 parent a39eadc commit feec2d9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

mlir/include/mlir/IR/SymbolTable.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class SymbolTable {
4949
/// Insert a new symbol into the table, and rename it as necessary to avoid
5050
/// collisions. Also insert at the specified location in the body of the
5151
/// associated operation if it is not already there. It is asserted that the
52-
/// symbol is not inside another operation.
53-
void insert(Operation *symbol, Block::iterator insertPt = {});
52+
/// symbol is not inside another operation. Return the name of the symbol
53+
/// after insertion as attribute.
54+
StringAttr insert(Operation *symbol, Block::iterator insertPt = {});
5455

5556
/// Return the name of the attribute used for symbol names.
5657
static StringRef getSymbolAttrName() { return "sym_name"; }

mlir/lib/IR/SymbolTable.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ void SymbolTable::erase(Operation *symbol) {
151151

152152
// TODO: Consider if this should be renamed to something like insertOrUpdate
153153
/// Insert a new symbol into the table and associated operation if not already
154-
/// there and rename it as necessary to avoid collisions.
155-
void SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
154+
/// there and rename it as necessary to avoid collisions. Return the name of
155+
/// the symbol after insertion as attribute.
156+
StringAttr SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
156157
// The symbol cannot be the child of another op and must be the child of the
157158
// symbolTableOp after this.
158159
//
@@ -180,10 +181,10 @@ void SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
180181
// detected.
181182
StringAttr name = getSymbolName(symbol);
182183
if (symbolTable.insert({name, symbol}).second)
183-
return;
184+
return name;
184185
// If the symbol was already in the table, also return.
185186
if (symbolTable.lookup(name) == symbol)
186-
return;
187+
return name;
187188
// If a conflict was detected, then the symbol will not have been added to
188189
// the symbol table. Try suffixes until we get to a unique name that works.
189190
SmallString<128> nameBuffer(name.getValue());
@@ -199,6 +200,7 @@ void SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
199200
} while (!symbolTable.insert({StringAttr::get(context, nameBuffer), symbol})
200201
.second);
201202
setSymbolName(symbol, nameBuffer);
203+
return getSymbolName(symbol);
202204
}
203205

204206
/// Returns the name of the given symbol operation.

0 commit comments

Comments
 (0)