summaryrefslogtreecommitdiff
path: root/src/wasm-stack.h
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-03-05 03:25:09 +0000
committerGitHub <noreply@github.com>2021-03-04 19:25:09 -0800
commitdfafe354a0ebb6250851399a36b8263dd3d43de3 (patch)
treefe86da29d504d3fd4d1939fd56a89e6801888bb4 /src/wasm-stack.h
parentf71927c7318a25d2dd2b2a6fd0cafc210e568fd5 (diff)
downloadbinaryen-dfafe354a0ebb6250851399a36b8263dd3d43de3.tar.gz
binaryen-dfafe354a0ebb6250851399a36b8263dd3d43de3.tar.bz2
binaryen-dfafe354a0ebb6250851399a36b8263dd3d43de3.zip
Fix binary writing of local name indexes (#3649)
When writing a binary, we take the local indexes in the IR and turn them into the format in the binary, which clumps them by type. When writing the names section we should be aware of that ordering, but we never were, as noticed in #3499 This fixes that by saving the mapping of locals when we are emitting the name section, then using it when emitting the local names. This also fixes the order of the types themselves as part of the refactoring. We used to depend on the ordering of types to decide which to emit first, but that isn't good for at least two reasons. First, it hits #3648 - that order is not fully defined for recursive types. Also, it's not good for code size - we've ordered the locals in a way we think is best already (ReorderLocals pass). This PR makes us pick an order of types based on that, as much as possible, that is, when we see a type for the first time we append it to a list whose order we use. Test changes: Some are just because we use a different order than before, as in atomics64. But some are actual fixes, e.g. in heap-types where we now have (local $tv (ref null $vector)) which is indeed right - v there is for vector, and likewise m for matrix etc. - we just had wrong names before. Another example, we now have (local $local_externref externref) whereas before the name was funcref, and which was wrong... seems like the incorrectness was more common on reference types and GC types, which is why this was not noticed before. Fixes #3499 Makes part of #3648 moot.
Diffstat (limited to 'src/wasm-stack.h')
-rw-r--r--src/wasm-stack.h14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/wasm-stack.h b/src/wasm-stack.h
index 5af9adbce..38b64dff4 100644
--- a/src/wasm-stack.h
+++ b/src/wasm-stack.h
@@ -118,6 +118,8 @@ public:
void emitUnreachable();
void mapLocalsAndEmitHeader();
+ MappedLocals mappedLocals;
+
private:
void emitMemoryAccess(size_t alignment, size_t bytes, uint32_t offset);
int32_t getBreakIndex(Name name);
@@ -130,10 +132,12 @@ private:
std::vector<Name> breakStack;
+ // The types of locals in the compact form, in order.
+ std::vector<Type> localTypes;
// type => number of locals of that type in the compact form
- std::map<Type, size_t> numLocalsByType;
- // (local index, tuple index) => binary local index
- std::map<std::pair<Index, Index>, size_t> mappedLocals;
+ std::unordered_map<Type, size_t> numLocalsByType;
+
+ void noteLocalType(Type type);
// Keeps track of the binary index of the scratch locals used to lower
// tuple.extract.
@@ -401,6 +405,8 @@ public:
}
}
+ MappedLocals& getMappedLocals() { return writer.mappedLocals; }
+
private:
WasmBinaryWriter& parent;
BinaryInstWriter writer;
@@ -458,6 +464,8 @@ public:
void write();
+ MappedLocals& getMappedLocals() { return writer.mappedLocals; }
+
private:
BinaryInstWriter writer;
Function* func;