From db14d0477f2715e3071687f42b77d8712477d83e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 19 Apr 2019 15:22:55 -0700 Subject: wasm2js2 import fixes (#2031) * Don't assume function types exist in legalize-js-interface. * Properly handle (ignore) imports in RemoveNonJSOps - do not try to recurse into them. * Run legalize-js-interface and remove-unused-module-elements in wasm2js, the first is necessary, the last is nice to have. --- src/passes/LegalizeJSInterface.cpp | 5 ++--- src/passes/RemoveNonJSOps.cpp | 5 ++++- src/wasm2js.h | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp index 63e4b1a6b..6002c7ad7 100644 --- a/src/passes/LegalizeJSInterface.cpp +++ b/src/passes/LegalizeJSInterface.cpp @@ -71,8 +71,7 @@ struct LegalizeJSInterface : public Pass { } // for each illegal import, we must call a legalized stub instead for (auto* im : originalFunctions) { - if (im->imported() && isIllegal(module->getFunctionType(im->type)) - && shouldBeLegalized(im)) { + if (im->imported() && isIllegal(im) && shouldBeLegalized(im)) { auto funcName = makeLegalStubForCalledImport(im, module); illegalImportsToLegal[im->name] = funcName; // we need to use the legalized version in the table, as the import from JS @@ -209,7 +208,7 @@ private: auto* call = module->allocator.alloc(); call->target = legal->name; - auto* imFunctionType = module->getFunctionType(im->type); + auto* imFunctionType = ensureFunctionType(getSig(im), module); for (auto param : imFunctionType->params) { if (param == i64) { diff --git a/src/passes/RemoveNonJSOps.cpp b/src/passes/RemoveNonJSOps.cpp index a98189584..906c34bc4 100644 --- a/src/passes/RemoveNonJSOps.cpp +++ b/src/passes/RemoveNonJSOps.cpp @@ -128,7 +128,10 @@ struct RemoveNonJSOpsPass : public WalkerPass> { auto function = m.getFunction(name); FindAll calls(function->body); for (auto* call : calls.list) { - this->addNeededFunctions(m, call->target, needed); + auto* called = m.getFunction(call->target); + if (!called->imported()) { + this->addNeededFunctions(m, call->target, needed); + } } } diff --git a/src/wasm2js.h b/src/wasm2js.h index 34471e213..faad6c3fa 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -290,6 +290,7 @@ private: Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { PassRunner runner(wasm); runner.add(); + runner.add("legalize-js-interface"); // First up remove as many non-JS operations we can, including things like // 64-bit integer multiplication/division, `f32.nearest` instructions, etc. // This may inject intrinsics which use i64 so it needs to be run before the @@ -303,6 +304,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { runner.add("simplify-locals-notee-nostructure"); runner.add("reorder-locals"); runner.add("vacuum"); + runner.add("remove-unused-module-elements"); runner.setDebug(flags.debug); runner.run(); @@ -311,7 +313,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { #ifndef NDEBUG if (!WasmValidator().validate(*wasm)) { WasmPrinter::printModule(wasm); - Fatal() << "error in validating input"; + Fatal() << "error in validating wasm2js output"; } #endif -- cgit v1.2.3