Skip to content

Commit

Permalink
Avoid duplicate type names (#6633)
Browse files Browse the repository at this point in the history
If we replace a type with another, use the original name for the new type,
and give the old a unique name (for the rare cases in which it has uses).
  • Loading branch information
kripken authored May 30, 2024
1 parent 0148db0 commit 18bb1f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "find_all.h"
#include "ir/local-structural-dominance.h"
#include "ir/module-utils.h"
#include "ir/names.h"
#include "ir/utils.h"
#include "support/topological_sort.h"
#include "wasm-type-ordering.h"
Expand Down Expand Up @@ -152,11 +153,27 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes(
oldToNewTypes[type] = newTypes[index];
}

// Update type names (doing it before mapTypes can help debugging there, but
// has no other effect; mapTypes does not look at type names).
// Update type names to avoid duplicates.
std::unordered_set<Name> typeNames;
for (auto& [type, info] : wasm.typeNames) {
typeNames.insert(info.name);
}
for (auto& [old, new_] : oldToNewTypes) {
if (old == new_) {
// The type is being mapped to itself; no need to rename anything.
continue;
}

if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) {
wasm.typeNames[new_] = it->second;
wasm.typeNames[new_] = wasm.typeNames[old];
// Use the existing name in the new type, as usually it completely
// replaces the old. Rename the old name in a unique way to avoid
// confusion in the case that it remains used.
auto deduped =
Names::getValidName(wasm.typeNames[old].name,
[&](Name test) { return !typeNames.count(test); });
wasm.typeNames[old].name = deduped;
typeNames.insert(deduped);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/passes/RemoveUnusedTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct RemoveUnusedTypes : Pass {
}

// We're not changing the contents of any of the types, so we just round
// trip them throgh GlobalTypeRewriter, which will put all the private types
// trip them through GlobalTypeRewriter which will put all the private types
// in a single new rec group and leave out all the unused types.
GlobalTypeRewriter(*module).update();
}
Expand Down
3 changes: 2 additions & 1 deletion test/lit/passes/signature-pruning.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1168,10 +1168,11 @@
;; CHECK: (rec
;; CHECK-NEXT: (type $none (func))
(type $none (func))
;; CHECK: (type $much (func (param i32)))
(type $much (func (param i32)))
)

;; CHECK: (type $much_0 (func (param i32)))

;; CHECK: (export "exported" (func $exported))

;; CHECK: (func $exported (type $none)
Expand Down

0 comments on commit 18bb1f5

Please sign in to comment.