Skip to content

Commit

Permalink
[NFC] Separate out GlobalTypeRewriter::mapTypeNames
Browse files Browse the repository at this point in the history
Previously a module's type names were updated in
`GlobalTypeRewriter::rebuildTypes`, which builds new versions of the
existing types, rather than `GlobalTypeRewriter::mapTypes`, which
otherwise handles replacing old types with new types everywhere in a
module, but should not necessarily replace names. So that users of
`mapTypes` who are building their own versions of existing types can
also easily update type names, split type name mapping logic out into a
new method `GlobalTypeRewriter::mapTypeNames`.
  • Loading branch information
tlively committed Aug 9, 2024
1 parent d7e09b4 commit a46dbab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
52 changes: 27 additions & 25 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,7 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes(
for (auto [type, index] : typeIndices) {
oldToNewTypes[type] = newTypes[index];
}

// 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_] = 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);
}
}

mapTypeNames(oldToNewTypes);
return oldToNewTypes;
}

Expand Down Expand Up @@ -293,6 +269,32 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) {
}
}

void GlobalTypeRewriter::mapTypeNames(const TypeMap& oldToNewTypes) {
// 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_] = 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);
}
}
}

Type GlobalTypeRewriter::getTempType(Type type) {
if (type.isBasic()) {
return type;
Expand Down
5 changes: 5 additions & 0 deletions src/ir/type-updating.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ class GlobalTypeRewriter {
// not appear, it is mapped to itself.
void mapTypes(const TypeMap& oldToNewTypes);

// Users of `mapTypes` may want to update the type names according to their
// mapping. This is not done automatically in `mapTypes` because other users
// may want the names to reflect that types have been replaced.
void mapTypeNames(const TypeMap& oldToNewTypes);

// Subclasses can implement these methods to modify the new set of types that
// we map to. By default, we simply copy over the types, and these functions
// are the hooks to apply changes through. The methods receive as input the
Expand Down

0 comments on commit a46dbab

Please sign in to comment.