From 9ce4b8a47b15c324b170f7572829532b7e5bfe47 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 13 Aug 2024 00:34:19 -0400 Subject: [NFC] Separate out GlobalTypeRewriter::mapTypeNames (#6829) 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`. --- src/ir/type-updating.cpp | 52 +++++++++++++++++++++++++----------------------- src/ir/type-updating.h | 5 +++++ 2 files changed, 32 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/ir/type-updating.cpp b/src/ir/type-updating.cpp index 92ec97980..123b13119 100644 --- a/src/ir/type-updating.cpp +++ b/src/ir/type-updating.cpp @@ -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 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; } @@ -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 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; diff --git a/src/ir/type-updating.h b/src/ir/type-updating.h index 60b92e585..a8e071fb6 100644 --- a/src/ir/type-updating.h +++ b/src/ir/type-updating.h @@ -369,6 +369,11 @@ public: // 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 -- cgit v1.2.3