diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-06-08 17:51:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-08 17:51:22 -0700 |
commit | c7b78eadec731675ef3274126d0d19df5e4263c5 (patch) | |
tree | 2f373af3e8b8bad6adc3af9e52c3e5ca4a95ff37 /src | |
parent | e3d201158d9136d6ffb655f70904dae5f9079317 (diff) | |
download | binaryen-c7b78eadec731675ef3274126d0d19df5e4263c5.tar.gz binaryen-c7b78eadec731675ef3274126d0d19df5e4263c5.tar.bz2 binaryen-c7b78eadec731675ef3274126d0d19df5e4263c5.zip |
-O4: When -O3 isn't enough (#1596)
This defines a new -O4 optimization mode, as flatten + flat-only opts (currently local-cse) + -O3.
In practice, flattening is not needed for LLVM output, which is pretty flat already (no block or if values, etc., even if it does use tees and does nest expressions; and LLVM has already done gvn etc. anyhow). In general, though, wasm generated by a non-LLVM compiler may naturally be nested because wasm allows that. See for example #1593 where an AssemblyScript testcase requires flattening to be fully optimized. So -O4 can help there.
-O4 takes 3x longer to run than -O3 in my testing, basically because flat IR is much bigger. But when it's useful it may be worth it. It does handle that AssemblyScript testcase and others like it. There's not much big real-world code that isn't LLVM yet, but running the fuzzer - which happily creates nested stuff all the time - I see -O4 consistently shrink the size by around 20% over -O3.
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/pass.cpp | 6 | ||||
-rw-r--r-- | src/tools/optimization-options.h | 13 | ||||
-rw-r--r-- | src/tools/wasm-reduce.cpp | 1 |
3 files changed, 17 insertions, 3 deletions
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index 2f917d779..c0354524d 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -132,6 +132,12 @@ void PassRunner::addDefaultOptimizationPasses() { } void PassRunner::addDefaultFunctionOptimizationPasses() { + // if we are willing to work very very hard, flatten the IR and do opts + // that depend on flat IR + if (options.optimizeLevel >= 4) { + add("flatten"); + add("local-cse"); + } if (!options.debugInfo) { // debug info must be preserved, do not dce it add("dce"); } diff --git a/src/tools/optimization-options.h b/src/tools/optimization-options.h index 9fbba88cb..abe3bcd6d 100644 --- a/src/tools/optimization-options.h +++ b/src/tools/optimization-options.h @@ -42,27 +42,34 @@ struct OptimizationOptions : public Options { passOptions.optimizeLevel = 0; passOptions.shrinkLevel = 0; }) - .add("", "-O1", "execute -O1 optimization passes", + .add("", "-O1", "execute -O1 optimization passes (quick&useful opts, useful for iteration builds)", Options::Arguments::Zero, [this](Options*, const std::string&) { passOptions.optimizeLevel = 1; passOptions.shrinkLevel = 0; passes.push_back(DEFAULT_OPT_PASSES); }) - .add("", "-O2", "execute -O2 optimization passes", + .add("", "-O2", "execute -O2 optimization passes (most opts, generally gets most perf)", Options::Arguments::Zero, [this](Options*, const std::string&) { passOptions.optimizeLevel = 2; passOptions.shrinkLevel = 0; passes.push_back(DEFAULT_OPT_PASSES); }) - .add("", "-O3", "execute -O3 optimization passes", + .add("", "-O3", "execute -O3 optimization passes (spends potentially a lot of time optimizing)", Options::Arguments::Zero, [this](Options*, const std::string&) { passOptions.optimizeLevel = 3; passOptions.shrinkLevel = 0; passes.push_back(DEFAULT_OPT_PASSES); }) + .add("", "-O4", "execute -O4 optimization passes (also flatten the IR, which can take a lot more time and memory, but is useful on more nested / complex / less-optimized input)", + Options::Arguments::Zero, + [this](Options*, const std::string&) { + passOptions.optimizeLevel = 4; + passOptions.shrinkLevel = 0; + passes.push_back(DEFAULT_OPT_PASSES); + }) .add("", "-Os", "execute default optimization passes, focusing on code size", Options::Arguments::Zero, [this](Options*, const std::string&) { diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 6b19e6216..f31c11edd 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -236,6 +236,7 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor< "-O1", "-O2", "-O3", + "-O4", "--flatten -Os", "--flatten -O3", "--flatten --local-cse -Os", |