From 8e8284e6464d524bd9091f21a62982ed54df0093 Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Fri, 14 Jan 2022 11:50:52 -0800 Subject: Refactor ModuleUtils::collectHeapTypes (#4455) Update the API to make both the type indices and optimized sorting optional. It will become more important to avoid unnecessary sorting once isorecursive types have been implemented because they will make the sorting more complicated. --- src/wasm/wasm-binary.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 5ebc4d864..36cb0fd1a 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -34,7 +34,7 @@ namespace wasm { void WasmBinaryWriter::prepare() { // Collect function types and their frequencies. Collect information in each // function in parallel, then merge. - ModuleUtils::collectHeapTypes(*wasm, types, typeIndices); + indexedTypes = ModuleUtils::getOptimizedIndexedHeapTypes(*wasm); importInfo = wasm::make_unique(*wasm); } @@ -216,14 +216,14 @@ void WasmBinaryWriter::writeMemory() { } void WasmBinaryWriter::writeTypes() { - if (types.size() == 0) { + if (indexedTypes.types.size() == 0) { return; } BYN_TRACE("== writeTypes\n"); auto start = startSection(BinaryConsts::Section::Type); - o << U32LEB(types.size()); - for (Index i = 0; i < types.size(); ++i) { - auto type = types[i]; + o << U32LEB(indexedTypes.types.size()); + for (Index i = 0; i < indexedTypes.types.size(); ++i) { + auto type = indexedTypes.types[i]; bool nominal = type.isNominal() || getTypeSystem() == TypeSystem::Nominal; BYN_TRACE("write " << type << std::endl); if (type.isSignature()) { @@ -539,9 +539,9 @@ uint32_t WasmBinaryWriter::getTagIndex(Name name) const { } uint32_t WasmBinaryWriter::getTypeIndex(HeapType type) const { - auto it = typeIndices.find(type); + auto it = indexedTypes.indices.find(type); #ifndef NDEBUG - if (it == typeIndices.end()) { + if (it == indexedTypes.indices.end()) { std::cout << "Missing type: " << type << '\n'; assert(0); } @@ -774,7 +774,7 @@ void WasmBinaryWriter::writeNames() { // type names { std::vector namedTypes; - for (auto& [type, _] : typeIndices) { + for (auto& [type, _] : indexedTypes.indices) { if (wasm->typeNames.count(type) && wasm->typeNames[type].name.is()) { namedTypes.push_back(type); } @@ -784,7 +784,7 @@ void WasmBinaryWriter::writeNames() { startSubsection(BinaryConsts::UserSections::Subsection::NameType); o << U32LEB(namedTypes.size()); for (auto type : namedTypes) { - o << U32LEB(typeIndices[type]); + o << U32LEB(indexedTypes.indices[type]); writeEscapedName(wasm->typeNames[type].name.str); } finishSubsection(substart); @@ -909,7 +909,7 @@ void WasmBinaryWriter::writeNames() { // GC field names if (wasm->features.hasGC()) { std::vector relevantTypes; - for (auto& type : types) { + for (auto& type : indexedTypes.types) { if (type.isStruct() && wasm->typeNames.count(type) && !wasm->typeNames[type].fieldNames.empty()) { relevantTypes.push_back(type); @@ -921,7 +921,7 @@ void WasmBinaryWriter::writeNames() { o << U32LEB(relevantTypes.size()); for (Index i = 0; i < relevantTypes.size(); i++) { auto type = relevantTypes[i]; - o << U32LEB(typeIndices[type]); + o << U32LEB(indexedTypes.indices[type]); std::unordered_map& fieldNames = wasm->typeNames.at(type).fieldNames; o << U32LEB(fieldNames.size()); -- cgit v1.2.3