Skip to content

Commit 4595297

Browse files
authored
Use symbol tables more efficiently (#59)
* [mlir][debuginfo] Add flags to attribute creation where necessary * [MLIR][mlir-link] Cache symbol tables and use SymbolUserMap for replacement lookups * [MLIR][mlir-link] Use ModuleOp as key for symbolUserMap holding map
1 parent 66ee00d commit 4595297

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

mlir/include/mlir/Linker/LinkerInterface.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ enum LinkerFlags {
3737
class LinkState {
3838
public:
3939
LinkState(ModuleOp dst)
40-
: mapping(std::make_shared<IRMapping>()), builder(dst.getBodyRegion()) {}
40+
: mapping(std::make_shared<IRMapping>()), builder(dst.getBodyRegion()),
41+
symbolTableCollection(), moduleMaps() {}
4142

4243
Operation *clone(Operation *src);
4344
Operation *cloneWithoutRegions(Operation *src);
@@ -49,14 +50,21 @@ class LinkState {
4950
LinkState nest(ModuleOp submod) const;
5051

5152
IRMapping &getMapping();
53+
SymbolTableCollection &getSymbolTableCollection() {
54+
return symbolTableCollection;
55+
}
56+
SymbolUserMap &getSymbolUserMap(ModuleOp mod);
5257

5358
private:
5459
// Private constructor used by nest()
5560
LinkState(ModuleOp dst, std::shared_ptr<IRMapping> mapping)
56-
: mapping(std::move(mapping)), builder(dst.getBodyRegion()) {}
61+
: mapping(std::move(mapping)), builder(dst.getBodyRegion()),
62+
symbolTableCollection(), moduleMaps() {}
5763

5864
std::shared_ptr<IRMapping> mapping;
5965
OpBuilder builder;
66+
SymbolTableCollection symbolTableCollection;
67+
DenseMap<ModuleOp, SymbolUserMap> moduleMaps;
6068
};
6169

6270
struct Conflict {

mlir/lib/Linker/LinkerInterface.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ LinkState LinkState::nest(ModuleOp submod) const {
5353
return LinkState(submod, mapping);
5454
}
5555

56-
IRMapping &LinkState::getMapping() { return *mapping.get(); }
56+
IRMapping &LinkState::getMapping() { return *mapping; }
57+
SymbolUserMap &LinkState::getSymbolUserMap(ModuleOp mod) {
58+
return moduleMaps.try_emplace(mod, symbolTableCollection, mod).first->second;
59+
}
5760

5861
//===----------------------------------------------------------------------===//
5962
// SymbolAttrLinkerInterface
@@ -83,21 +86,17 @@ static void renameSymbolRefIn(Operation *op, StringAttr newName) {
8386
static LogicalResult renameRemappedUsersOf(Operation *op, StringAttr newName,
8487
LinkState &state) {
8588
ModuleOp module = op->getParentOfType<ModuleOp>();
89+
SymbolUserMap &userMap = state.getSymbolUserMap(module);
8690
// TODO: use something like SymbolTableAnalysis
87-
SymbolTable src(module);
88-
if (auto uses = src.getSymbolUses(op, module)) {
89-
for (SymbolTable::SymbolUse use : *uses) {
90-
// TODO: add test where user is not remapped
91-
Operation *dstUser = state.remapped(use.getUser());
92-
if (!dstUser)
93-
continue;
94-
renameSymbolRefIn(dstUser, newName);
95-
}
96-
97-
return success();
91+
auto users = userMap.getUsers(op);
92+
for (Operation *user : users) {
93+
// TODO: add test where user is not remapped
94+
Operation *dstUser = state.remapped(user);
95+
if (!dstUser)
96+
continue;
97+
renameSymbolRefIn(dstUser, newName);
9898
}
99-
100-
return op->emitError("failed to rename symbol to a unique name");
99+
return success();
101100
}
102101

103102
StringRef SymbolAttrLinkerInterface::getSymbol(Operation *op) const {
@@ -119,7 +118,8 @@ void SymbolAttrLinkerInterface::registerForLink(Operation *op) {
119118
}
120119

121120
LogicalResult SymbolAttrLinkerInterface::link(LinkState &state) const {
122-
SymbolTable st(state.getDestinationOp());
121+
SymbolTable &st =
122+
state.getSymbolTableCollection().getSymbolTable(state.getDestinationOp());
123123

124124
auto materializeError = [&](Operation *op) {
125125
return op->emitError("failed to materialize symbol for linking");
@@ -145,7 +145,7 @@ LogicalResult SymbolAttrLinkerInterface::link(LinkState &state) const {
145145
if (st.lookup(name)) {
146146
StringAttr newName = getUniqueNameIn(st, name);
147147
st.setSymbolName(materialized, newName);
148-
toRenameUsers.push_back({op, newName});
148+
toRenameUsers.emplace_back(op, newName);
149149
}
150150

151151
st.insert(materialized);

mlir/test/CAPI/llvm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ static void testDebugInfoAttributes(MlirContext ctx) {
303303
// CHECK: #llvm.di_derived_type<{{.*}}>
304304
// CHECK-NOT: dwarfAddressSpace
305305
mlirAttributeDump(mlirLLVMDIDerivedTypeAttrGet(
306-
ctx, 0, bar, di_type, 64, 8, 0, MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL,
306+
ctx, 0, bar, di_type, 0, 64, 8, 0, MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL,
307307
di_type));
308308

309309
// CHECK: #llvm.di_derived_type<{{.*}} dwarfAddressSpace = 3{{.*}}>
310310
mlirAttributeDump(
311-
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, 3, di_type));
311+
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 0, 64, 8, 0, 3, di_type));
312312

313313
MlirAttribute subroutine_type =
314314
mlirLLVMDISubroutineTypeAttrGet(ctx, 0x0, 1, &di_type);

0 commit comments

Comments
 (0)