From 12a5d972ec7c5144cd54afe7bdee7351219aaca0 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 22 Mar 2016 10:06:16 -0700 Subject: add an option to not optimize in asm2wasm --- src/asm2wasm-main.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/asm2wasm-main.cpp b/src/asm2wasm-main.cpp index cbcb3b547..56e8a417f 100644 --- a/src/asm2wasm-main.cpp +++ b/src/asm2wasm-main.cpp @@ -29,6 +29,7 @@ using namespace cashew; using namespace wasm; int main(int argc, const char *argv[]) { + bool opts = true; bool imprecise = false; Options options("asm2wasm", "Translate asm.js files to .wast files"); @@ -47,6 +48,10 @@ int main(int argc, const char *argv[]) { [](Options *o, const std::string &argument) { o->extra["total memory"] = argument; }) + .add("--no-opts", "-n", "Disable optimization passes", Options::Arguments::Zero, + [&opts](Options *o, const std::string &) { + opts = false; + }) .add("--imprecise", "-i", "Imprecise optimizations", Options::Arguments::Zero, [&imprecise](Options *o, const std::string &) { imprecise = true; @@ -90,8 +95,10 @@ int main(int argc, const char *argv[]) { Asm2WasmBuilder asm2wasm(wasm, pre.memoryGrowth, options.debug, imprecise); asm2wasm.processAsm(asmjs); - if (options.debug) std::cerr << "optimizing..." << std::endl; - asm2wasm.optimize(); + if (opts) { + if (options.debug) std::cerr << "optimizing..." << std::endl; + asm2wasm.optimize(); + } if (options.debug) std::cerr << "printing..." << std::endl; Output output(options.extra["output"], options.debug); -- cgit v1.2.3 From 996416ccefc4f079b14495bcb4ac66e7aa705f4f Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 22 Mar 2016 10:13:08 -0700 Subject: only reuse nameless blocks in blockify in asm2wasm - if they are named, they can be broken out of, which is bad for our loop constructs --- src/asm2wasm.h | 3 ++- test/unit.asm.js | 27 +++++++++++++++++++++++++++ test/unit.fromasm | 37 +++++++++++++++++++++++++++++++++++++ test/unit.fromasm.imprecise | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/asm2wasm.h b/src/asm2wasm.h index c994d0ef2..4c37b7705 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -419,8 +419,9 @@ private: return nullptr; } + // ensure a nameless block Block* blockify(Expression* expression) { - if (expression->is()) return expression->dyn_cast(); + if (expression->is() && !expression->cast()->name.is()) return expression->dyn_cast(); auto ret = allocator.alloc(); ret->list.push_back(expression); ret->finalize(); diff --git a/test/unit.asm.js b/test/unit.asm.js index c600f714d..5bb84bdcd 100644 --- a/test/unit.asm.js +++ b/test/unit.asm.js @@ -55,6 +55,7 @@ function asm() { } function switcher(x) { x = x | 0; + var waka = 0; switch (x | 0) { case 1: return 1; case 2: return 2; @@ -79,6 +80,32 @@ function asm() { break; } } + + L1 : while (1) { + L3 : while (1) switch (x) { + case -1: + { + break L1; + break; + } + case 116: + { + waka = 1; + break; + } + case 110: + { + break L3; + break; + } + default: + { + break L1; + } + } + h(120); + } + return 0; } function blocker() { diff --git a/test/unit.fromasm b/test/unit.fromasm index e00c6b43b..3db25c78b 100644 --- a/test/unit.fromasm +++ b/test/unit.fromasm @@ -177,6 +177,7 @@ ) ) (func $switcher (param $x i32) (result i32) + (local $waka i32) (block $switch-default$3 (block $switch-case$2 (block $switch-case$1 @@ -243,6 +244,42 @@ (br $label$break$Lout) ) ) + (loop $label$break$L1 $label$continue$L1 + (loop $label$break$L3 $label$continue$L3 + (block $switch$17 + (block $switch-default$21 + (block $switch-default$21 + (block $switch-case$20 + (block $switch-case$19 + (block $switch-case$18 + (br_table $switch-case$18 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-case$20 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-case$19 $switch-default$21 + (i32.sub + (get_local $x) + (i32.const -1) + ) + ) + ) + (br $label$break$L1) + (br $switch$17) + ) + (set_local $waka + (i32.const 1) + ) + (br $switch$17) + ) + (br $label$break$L3) + (br $switch$17) + ) + (br $label$break$L1) + ) + ) + (br $label$continue$L3) + ) + (call_import $h + (i32.const 120) + ) + (br $label$continue$L1) + ) (return (i32.const 0) ) diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise index 9bfa1789c..ef9ccaa5d 100644 --- a/test/unit.fromasm.imprecise +++ b/test/unit.fromasm.imprecise @@ -173,6 +173,7 @@ ) ) (func $switcher (param $x i32) (result i32) + (local $waka i32) (block $switch-default$3 (block $switch-case$2 (block $switch-case$1 @@ -239,6 +240,42 @@ (br $label$break$Lout) ) ) + (loop $label$break$L1 $label$continue$L1 + (loop $label$break$L3 $label$continue$L3 + (block $switch$17 + (block $switch-default$21 + (block $switch-default$21 + (block $switch-case$20 + (block $switch-case$19 + (block $switch-case$18 + (br_table $switch-case$18 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-case$20 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-default$21 $switch-case$19 $switch-default$21 + (i32.sub + (get_local $x) + (i32.const -1) + ) + ) + ) + (br $label$break$L1) + (br $switch$17) + ) + (set_local $waka + (i32.const 1) + ) + (br $switch$17) + ) + (br $label$break$L3) + (br $switch$17) + ) + (br $label$break$L1) + ) + ) + (br $label$continue$L3) + ) + (call_import $h + (i32.const 120) + ) + (br $label$continue$L1) + ) (return (i32.const 0) ) -- cgit v1.2.3