Skip to content

Commit

Permalink
[NFC] Eagerly create Functions in binary parser (#6957)
Browse files Browse the repository at this point in the history
In preparation for using IRBuilder in the binary parser, eagerly create
Functions when parsing the function section so that they are already
created once we parse the code section. IRBuilder will require the
functions to exist when parsing calls so it can figure out what type
each call should have, even when there is a call to a function whose
body has not been parsed yet.
  • Loading branch information
tlively authored Sep 20, 2024
1 parent b285448 commit 2711d4f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
for (auto type : func->vars) {
info.note(type);
}
if (!func->imported()) {
// Don't just use `func->imported()` here because we also might be
// printing an error message on a partially parsed module whose declared
// function bodies have not all been parsed yet.
if (func->body) {
CodeScanner(wasm, info, inclusion).walk(func->body);
}
});
Expand Down
3 changes: 3 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,9 @@ void PrintSExpression::visitDefinedGlobal(Global* curr) {
void PrintSExpression::visitFunction(Function* curr) {
if (curr->imported()) {
visitImportedFunction(curr);
} else if (curr->body == nullptr) {
// We are in the middle of parsing the module and have not parsed this
// function's code yet. Skip it.
} else {
visitDefinedFunction(curr);
}
Expand Down
5 changes: 5 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,11 @@ class WasmBinaryReader {
// reconstructing the HeapTypes from the Signatures is expensive.
std::vector<HeapType> functionTypes;

// Used to make sure the number of imported functions, signatures, and
// declared functions all match up.
Index numFuncImports = 0;
Index numFuncBodies = 0;

void readFunctionSignatures();
HeapType getTypeByIndex(Index index);
HeapType getTypeByFunctionIndex(Index index);
Expand Down
22 changes: 11 additions & 11 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,7 @@ void WasmBinaryReader::readImports() {
}
}
}
numFuncImports = wasm.functions.size();
}

Name WasmBinaryReader::getNextLabel() {
Expand All @@ -2595,9 +2596,12 @@ void WasmBinaryReader::readFunctionSignatures() {
size_t num = getU32LEB();
for (size_t i = 0; i < num; i++) {
auto index = getU32LEB();
functionTypes.push_back(getTypeByIndex(index));
HeapType type = getTypeByIndex(index);
functionTypes.push_back(type);
// Check that the type is a signature.
getSignatureByTypeIndex(index);
wasm.addFunction(
Builder(wasm).makeFunction(makeName("", i), type, {}, nullptr));
}
}

Expand Down Expand Up @@ -2633,22 +2637,19 @@ Signature WasmBinaryReader::getSignatureByFunctionIndex(Index index) {
}

void WasmBinaryReader::readFunctions() {
auto numImports = wasm.functions.size();
size_t total = getU32LEB();
if (total != functionTypes.size() - numImports) {
numFuncBodies = getU32LEB();
if (numFuncBodies + numFuncImports != wasm.functions.size()) {
throwError("invalid function section size, must equal types");
}
for (size_t i = 0; i < total; i++) {
for (size_t i = 0; i < numFuncBodies; i++) {
auto sizePos = pos;
size_t size = getU32LEB();
if (size == 0) {
throwError("empty function size");
}
endOfFunction = pos + size;

auto func = std::make_unique<Function>();
func->name = makeName("", i);
func->type = getTypeByFunctionIndex(numImports + i);
auto& func = wasm.functions[numFuncImports + i];
currFunction = func.get();

if (DWARF) {
Expand Down Expand Up @@ -2715,7 +2716,6 @@ void WasmBinaryReader::readFunctions() {
std::swap(func->epilogLocation, debugLocation);
currFunction = nullptr;
debugLocation.clear();
wasm.addFunction(std::move(func));
}
}

Expand Down Expand Up @@ -3192,8 +3192,8 @@ void WasmBinaryReader::validateBinary() {
throwError("Number of segments does not agree with DataCount section");
}

if (functionTypes.size() != wasm.functions.size()) {
throwError("function section without code section");
if (functionTypes.size() != numFuncImports + numFuncBodies) {
throwError("function and code sections have inconsistent lengths");
}
}

Expand Down

0 comments on commit 2711d4f

Please sign in to comment.