summaryrefslogtreecommitdiff
path: root/src/wasm
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-07-07 12:46:43 -0700
committerGitHub <noreply@github.com>2022-07-07 12:46:43 -0700
commit9831b36d339b1cea61d5313f7dfa256fc1ee9bcc (patch)
tree70178cf4d01b7d5b9319be6fdd59c4d05cc2a0a1 /src/wasm
parenta82e2dbfc3a554377b93cc1f5fca731ff688f925 (diff)
downloadbinaryen-9831b36d339b1cea61d5313f7dfa256fc1ee9bcc.tar.gz
binaryen-9831b36d339b1cea61d5313f7dfa256fc1ee9bcc.tar.bz2
binaryen-9831b36d339b1cea61d5313f7dfa256fc1ee9bcc.zip
Group reference types in binary format. (#4774)
Grouping all references together makes it easier for baseline compilers to zero out memory (as the zeroing out may be different for MVP types vs. references). This puts all references together, either at the start or the end. As a heuristic for that we see if the first local is a reference. As the optimizer will sort locals by frequency, this ensures that the most-frequent local stays in index 0. Fixes #4773. See more details there
Diffstat (limited to 'src/wasm')
-rw-r--r--src/wasm/wasm-stack.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp
index e3740ecee..9c086bb47 100644
--- a/src/wasm/wasm-stack.cpp
+++ b/src/wasm/wasm-stack.cpp
@@ -2327,6 +2327,29 @@ void BinaryInstWriter::mapLocalsAndEmitHeader() {
}
}
countScratchLocals();
+
+ if (parent.getModule()->features.hasReferenceTypes()) {
+ // Sort local types in a way that keeps all MVP types together and all
+ // reference types together. E.g. it is helpful to avoid a block of i32s in
+ // between blocks of different reference types, since clearing out reference
+ // types may require different work.
+ //
+ // See https://github.com/WebAssembly/binaryen/issues/4773
+ //
+ // In order to decide whether to put MVP types or reference types first,
+ // look at the type of the first local. In an optimized binary we will have
+ // sorted the locals by frequency of uses, so this way we'll keep the most
+ // commonly-used local at the top, which should work well in many cases.
+ bool refsFirst = !localTypes.empty() && localTypes[0].isRef();
+ std::stable_sort(localTypes.begin(), localTypes.end(), [&](Type a, Type b) {
+ if (refsFirst) {
+ return a.isRef() && !b.isRef();
+ } else {
+ return !a.isRef() && b.isRef();
+ }
+ });
+ }
+
std::unordered_map<Type, size_t> currLocalsByType;
for (Index i = func->getVarIndexBase(); i < func->getNumLocals(); i++) {
Index j = 0;