Skip to content

Commit abe59c4

Browse files
[mlir] Make overloads of SymbolTable::replaceAllSymbolUses consistent.
This function has several overloads that allow to specify the symbol that should be renamed and the scope for that renaming in different ways. The overloads were inconsistent in the following way (quoted strings are `StringAttr`s, other variables are `Operation *`): * `replaceAllSymbolUses(symbolOp, "new_symbol", scopeOp)` would traverse into the symbol table of `scopeOp`. * `replaceAllSymbolUses("symbol", "new_symbol", scopeOp)` would *not* traverse into the symbol table of `scopeOp`. The underlying behavior was spread over different places and is somewhat hard to understand. The two overloads above mainly differed by what `collectSymbolScopes` computed, which is itself overloaded. If `scopeOp` is a top-level module, then the overload on `(Operation *, Operation *)`, which is used in the first of the above cases, computes a scope where the body region of the module is the `limit`; however, the overload on `(StringAttr, Operation *)` computed the module op itself as the `limit`. Later, `walkSymbolTable` would walk the body of the module if it was given as a region but it would *not* enter the regions of the module op because that op has a symbol table (which was assumed to be a *different* scope). The fix in this commit is change the behavior of `collectSymbolScopes` such that the `(StringAttr, Operation *)` overload returns a scope for each region in the `limit` argument.
1 parent 4ccd57d commit abe59c4

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

mlir/lib/IR/SymbolTable.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,20 @@ static SmallVector<SymbolScope, 2> collectSymbolScopes(Operation *symbol,
655655
scopes.back().limit = limit;
656656
return scopes;
657657
}
658-
template <typename IRUnit>
659658
static SmallVector<SymbolScope, 1> collectSymbolScopes(StringAttr symbol,
660-
IRUnit *limit) {
659+
Region *limit) {
661660
return {{SymbolRefAttr::get(symbol), limit}};
662661
}
663662

663+
static SmallVector<SymbolScope, 1> collectSymbolScopes(StringAttr symbol,
664+
Operation *limit) {
665+
SmallVector<SymbolScope, 1> scopes;
666+
auto symbolRef = SymbolRefAttr::get(symbol);
667+
for (auto &region : limit->getRegions())
668+
scopes.push_back({symbolRef, &region});
669+
return scopes;
670+
}
671+
664672
/// Returns true if the given reference 'SubRef' is a sub reference of the
665673
/// reference 'ref', i.e. 'ref' is a further qualified reference.
666674
static bool isReferencePrefixOf(SymbolRefAttr subRef, SymbolRefAttr ref) {

mlir/test/python/ir/symbol_table.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ def testSymbolTableRAUW():
106106
"""
107107
)
108108
foo, bar = list(m.operation.regions[0].blocks[0].operations)[0:2]
109+
110+
# Do renaming just within `foo`.
109111
SymbolTable.set_symbol_name(bar, "bam")
110-
# Note that module.operation counts as a "nested symbol table" which won't
111-
# be traversed into, so it is necessary to traverse its children.
112112
SymbolTable.replace_all_symbol_uses("bar", "bam", foo)
113113
# CHECK: call @bam()
114114
# CHECK: func private @bam
@@ -118,6 +118,17 @@ def testSymbolTableRAUW():
118118
print(f"Foo symbol: {repr(SymbolTable.get_symbol_name(foo))}")
119119
print(f"Bar symbol: {repr(SymbolTable.get_symbol_name(bar))}")
120120

121+
# Do renaming within the module.
122+
SymbolTable.set_symbol_name(bar, "baz")
123+
SymbolTable.replace_all_symbol_uses("bam", "baz", m.operation)
124+
# CHECK: call @baz()
125+
# CHECK: func private @baz
126+
print(m)
127+
# CHECK: Foo symbol: StringAttr("foo")
128+
# CHECK: Bar symbol: StringAttr("baz")
129+
print(f"Foo symbol: {repr(SymbolTable.get_symbol_name(foo))}")
130+
print(f"Bar symbol: {repr(SymbolTable.get_symbol_name(bar))}")
131+
121132

122133
# CHECK-LABEL: testSymbolTableVisibility
123134
@run

0 commit comments

Comments
 (0)