From 8d4bcd6e02bb0df3ac87e4850896ba733525b055 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 30 Apr 2019 20:30:03 -0700 Subject: wasm2js: run full optimizations during the pipeline (#2071) We flatten for the i64 lowering etc. passes, and it is worth optimizing afterwards, to clean up stuff they created. That is run if the user ran wasm2js with an optimization level (like wasm2js -O3). Split the test files to check both optimized and unoptimized code. --- src/wasm2js.h | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) (limited to 'src/wasm2js.h') diff --git a/src/wasm2js.h b/src/wasm2js.h index c05c16f2b..18175d443 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -267,29 +267,40 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { // If later on they aren't needed, we'll clean them up. ABI::wasm2js::ensureScratchMemoryHelpers(wasm); - 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 - // i64-to-i32 lowering pass. - runner.add("remove-non-js-ops"); - // Currently the i64-to-32 lowering pass requires that `flatten` be run before - // it to produce correct code. For some more details about this see #1480 - runner.add("flatten"); - runner.add("i64-to-i32-lowering"); - runner.add("flatten"); - runner.add("simplify-locals-notee-nostructure"); - runner.add("reorder-locals"); - runner.add("remove-unused-names"); - runner.add("vacuum"); - runner.add("remove-unused-module-elements"); - runner.setDebug(flags.debug); - runner.run(); - - // Make sure we didn't corrupt anything if we're in --allow-asserts mode (aka - // tests) + // Process the code, and optimize if relevant. + // First, do the lowering to a JS-friendly subset. + { + PassRunner runner(wasm, options); + 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 + // i64-to-i32 lowering pass. + runner.add("remove-non-js-ops"); + // Currently the i64-to-32 lowering pass requires that `flatten` be run + // before it to produce correct code. For some more details about this see + // #1480 + runner.add("flatten"); + runner.add("i64-to-i32-lowering"); + // Next, optimize that as best we can. This should not generate + // non-JS-friendly things. + if (options.optimizeLevel > 0) { + runner.addDefaultOptimizationPasses(); + } + // Finally, get the code into the flat form we need for wasm2js itself, and + // optimize that a little in a way that keeps flat property. + runner.add("flatten"); + runner.add("simplify-locals-notee-nostructure"); + // TODO: coalesce-locals? + runner.add("reorder-locals"); + runner.add("remove-unused-names"); + runner.add("vacuum"); + runner.add("remove-unused-module-elements"); + runner.setDebug(flags.debug); + runner.run(); + } + #ifndef NDEBUG if (!WasmValidator().validate(*wasm)) { WasmPrinter::printModule(wasm); -- cgit v1.2.3