summaryrefslogtreecommitdiff
path: root/src/ir/module-utils.h
diff options
context:
space:
mode:
authorThomas Lively <7121787+tlively@users.noreply.github.com>2019-12-11 17:12:37 -0800
committerAlon Zakai <azakai@google.com>2019-12-11 17:12:37 -0800
commit759c485a9f35bd859d43b86b02e1397a669fa469 (patch)
treea5c7475002b406e35c6d1e5c2d843000947ef192 /src/ir/module-utils.h
parentacd786dbd1e59f9d105c4ec8603c2ff46f233649 (diff)
downloadbinaryen-759c485a9f35bd859d43b86b02e1397a669fa469.tar.gz
binaryen-759c485a9f35bd859d43b86b02e1397a669fa469.tar.bz2
binaryen-759c485a9f35bd859d43b86b02e1397a669fa469.zip
Remove FunctionType (#2510)
Function signatures were previously redundantly stored on Function objects as well as on FunctionType objects. These two signature representations had to always be kept in sync, which was error-prone and needlessly complex. This PR takes advantage of the new ability of Type to represent multiple value types by consolidating function signatures as a pair of Types (params and results) stored on the Function object. Since there are no longer module-global named function types, significant changes had to be made to the printing and emitting of function types, as well as their parsing and manipulation in various passes. The C and JS APIs and their tests also had to be updated to remove named function types.
Diffstat (limited to 'src/ir/module-utils.h')
-rw-r--r--src/ir/module-utils.h67
1 files changed, 59 insertions, 8 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h
index e212ae8dc..d84648dfd 100644
--- a/src/ir/module-utils.h
+++ b/src/ir/module-utils.h
@@ -63,11 +63,8 @@ struct BinaryIndexes {
inline Function* copyFunction(Function* func, Module& out) {
auto* ret = new Function();
ret->name = func->name;
- ret->result = func->result;
- ret->params = func->params;
+ ret->sig = func->sig;
ret->vars = func->vars;
- // start with no named type; the names in the other module may differ
- ret->type = Name();
ret->localNames = func->localNames;
ret->localIndices = func->localIndices;
ret->debugLocations = func->debugLocations;
@@ -108,9 +105,6 @@ inline Event* copyEvent(Event* event, Module& out) {
inline void copyModule(const Module& in, Module& out) {
// we use names throughout, not raw pointers, so simple copying is fine
// for everything *but* expressions
- for (auto& curr : in.functionTypes) {
- out.addFunctionType(make_unique<FunctionType>(*curr));
- }
for (auto& curr : in.exports) {
out.addExport(new Export(*curr));
}
@@ -137,7 +131,6 @@ inline void copyModule(const Module& in, Module& out) {
}
inline void clearModule(Module& wasm) {
- wasm.functionTypes.clear();
wasm.exports.clear();
wasm.functions.clear();
wasm.globals.clear();
@@ -413,6 +406,64 @@ template<typename T> struct CallGraphPropertyAnalysis {
}
};
+// Helper function for collecting the type signature used in a module
+//
+// Used when emitting or printing a module to give signatures canonical
+// indices. Signatures are sorted in order of decreasing frequency to minize the
+// size of their collective encoding. Both a vector mapping indices to
+// signatures and a map mapping signatures to indices are produced.
+inline void
+collectSignatures(Module& wasm,
+ std::vector<Signature>& signatures,
+ std::unordered_map<Signature, Index>& sigIndices) {
+ using Counts = std::unordered_map<Signature, size_t>;
+
+ // Collect the signature use counts for a single function
+ auto updateCounts = [&](Function* func, Counts& counts) {
+ if (func->imported()) {
+ return;
+ }
+ struct TypeCounter : PostWalker<TypeCounter> {
+ Counts& counts;
+
+ TypeCounter(Counts& counts) : counts(counts) {}
+
+ void visitCallIndirect(CallIndirect* curr) { counts[curr->sig]++; }
+ };
+ TypeCounter(counts).walk(func->body);
+ };
+
+ ModuleUtils::ParallelFunctionAnalysis<Counts> analysis(wasm, updateCounts);
+
+ // Collect all the counts.
+ Counts counts;
+ for (auto& curr : wasm.functions) {
+ counts[curr->sig]++;
+ }
+ for (auto& curr : wasm.events) {
+ counts[curr->sig]++;
+ }
+ for (auto& pair : analysis.map) {
+ Counts& functionCounts = pair.second;
+ for (auto& innerPair : functionCounts) {
+ counts[innerPair.first] += innerPair.second;
+ }
+ }
+ std::vector<std::pair<Signature, size_t>> sorted(counts.begin(),
+ counts.end());
+ std::sort(sorted.begin(), sorted.end(), [&](auto a, auto b) {
+ // order by frequency then simplicity
+ if (a.second != b.second) {
+ return a.second > b.second;
+ }
+ return a.first < b.first;
+ });
+ for (Index i = 0; i < sorted.size(); ++i) {
+ sigIndices[sorted[i].first] = i;
+ signatures.push_back(sorted[i].first);
+ }
+}
+
} // namespace ModuleUtils
} // namespace wasm