diff --git a/src/wasm-stack.h b/src/wasm-stack.h index 2c9fdd616fa..05898623187 100644 --- a/src/wasm-stack.h +++ b/src/wasm-stack.h @@ -150,6 +150,7 @@ class BinaryInstWriter : public OverriddenVisitor { std::unordered_map numLocalsByType; void noteLocalType(Type type, Index count = 1); + Index getNumLocalsForType(Type type); // Keeps track of the binary index of the scratch locals used to lower // tuple.extract. If there are multiple scratch locals of the same type, they diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 496d15ca878..ea932dc175c 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -3245,7 +3245,7 @@ void BinaryInstWriter::mapLocalsAndEmitHeader() { Index baseIndex = func->getVarIndexBase(); for (auto& type : localTypes) { nextFreeIndex[type] = baseIndex; - baseIndex += numLocalsByType[type]; + baseIndex += getNumLocalsForType(type); } // Map the IR index pairs to indices. @@ -3261,14 +3261,20 @@ void BinaryInstWriter::mapLocalsAndEmitHeader() { scratchLocals[type] = nextFreeIndex[type]; } - o << U32LEB(numLocalsByType.size()); + o << U32LEB(localTypes.size()); for (auto& localType : localTypes) { - o << U32LEB(numLocalsByType.at(localType)); + o << U32LEB(getNumLocalsForType(localType)); parent.writeType(localType); } } void BinaryInstWriter::noteLocalType(Type type, Index count) { + // Group locals by the type they will eventually be written out as. For + // example, we do not need to differentiate exact and inexact versions of the + // same reference type if custom descriptors is not enabled and the type will + // be written as inexact either way. + auto feats = parent.getModule()->features; + type = type.asWrittenGivenFeatures(feats); auto& num = numLocalsByType[type]; if (num == 0) { localTypes.push_back(type); @@ -3276,6 +3282,15 @@ void BinaryInstWriter::noteLocalType(Type type, Index count) { num += count; } +Index BinaryInstWriter::getNumLocalsForType(Type type) { + auto feats = parent.getModule()->features; + type = type.asWrittenGivenFeatures(feats); + if (auto it = numLocalsByType.find(type); it != numLocalsByType.end()) { + return it->second; + } + return 0; +} + InsertOrderedMap BinaryInstWriter::countScratchLocals() { struct ScratchLocalFinder : PostWalker { BinaryInstWriter& parent;