diff options
author | Thomas Lively <7121787+tlively@users.noreply.github.com> | 2022-01-14 11:50:52 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-14 11:50:52 -0800 |
commit | 8e8284e6464d524bd9091f21a62982ed54df0093 (patch) | |
tree | 13b88033d4e5d2f8d53289807efdd6b88055e0be /src/ir/module-utils.cpp | |
parent | 80329023c30ca108b0a8ce1b3939f5e9a96250bb (diff) | |
download | binaryen-8e8284e6464d524bd9091f21a62982ed54df0093.tar.gz binaryen-8e8284e6464d524bd9091f21a62982ed54df0093.tar.bz2 binaryen-8e8284e6464d524bd9091f21a62982ed54df0093.zip |
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.
Diffstat (limited to 'src/ir/module-utils.cpp')
-rw-r--r-- | src/ir/module-utils.cpp | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/src/ir/module-utils.cpp b/src/ir/module-utils.cpp index c738c6143..9b2293984 100644 --- a/src/ir/module-utils.cpp +++ b/src/ir/module-utils.cpp @@ -19,22 +19,23 @@ namespace wasm::ModuleUtils { -void collectHeapTypes(Module& wasm, - std::vector<HeapType>& types, - std::unordered_map<HeapType, Index>& typeIndices) { - struct Counts : public InsertOrderedMap<HeapType, size_t> { - void note(HeapType type) { - if (!type.isBasic()) { - (*this)[type]++; - } +namespace { + +// Helper for collecting HeapTypes and their frequencies. +struct Counts : public InsertOrderedMap<HeapType, size_t> { + void note(HeapType type) { + if (!type.isBasic()) { + (*this)[type]++; } - void note(Type type) { - for (HeapType ht : type.getHeapTypeChildren()) { - note(ht); - } + } + void note(Type type) { + for (HeapType ht : type.getHeapTypeChildren()) { + note(ht); } - }; + } +}; +Counts getHeapTypeCounts(Module& wasm) { struct CodeScanner : PostWalker<CodeScanner, UnifiedExpressionVisitor<CodeScanner>> { Counts& counts; @@ -160,15 +161,49 @@ void collectHeapTypes(Module& wasm, } } + return counts; +} + +} // anonymous namespace + +std::vector<HeapType> collectHeapTypes(Module& wasm) { + Counts counts = getHeapTypeCounts(wasm); + std::vector<HeapType> types; + types.reserve(counts.size()); + for (auto& [type, _] : counts) { + types.push_back(type); + } + return types; +} + +IndexedHeapTypes getIndexedHeapTypes(Module& wasm) { + Counts counts = getHeapTypeCounts(wasm); + IndexedHeapTypes indexedTypes; + Index i = 0; + for (auto& [type, _] : counts) { + indexedTypes.types.push_back(type); + indexedTypes.indices[type] = i++; + } + return indexedTypes; +} + +IndexedHeapTypes getOptimizedIndexedHeapTypes(Module& wasm) { + Counts counts = getHeapTypeCounts(wasm); + // Sort by frequency and then original insertion order. std::vector<std::pair<HeapType, size_t>> sorted(counts.begin(), counts.end()); std::stable_sort(sorted.begin(), sorted.end(), [&](auto a, auto b) { return a.second > b.second; }); + + // Collect the results. + IndexedHeapTypes indexedTypes; for (Index i = 0; i < sorted.size(); ++i) { - typeIndices[sorted[i].first] = i; - types.push_back(sorted[i].first); + HeapType type = sorted[i].first; + indexedTypes.types.push_back(type); + indexedTypes.indices[type] = i; } + return indexedTypes; } } // namespace wasm::ModuleUtils |