-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Use Binaryen StackIR at the end of the pipeline #22218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Note that we'd see even more code size improvements here, but the benefits of this are missed in the metadce tests. Those tests run |
So calling wasm-opt repeatedly somehow means we loose these benefits forever? |
|
Yes, StackIR opts generate "stacky" wasm forms, that do not fit perfectly in BinaryenIR. Binaryen may need to add a local or a block or such to handle them. We can gain the benefits again if we re-optimize (to remove the locals, and to regenerate StackIR). But it is simplest to just run StackIR at the very end, because that is when it actually matters. StackIR opts are just minor binary format tweaks that do not unlock any other benefits. |
|
Looks like this needs a rebase. |
|
Updated after the metadce test change. Test results here now show more benefits to code size on those metadce tests. |
tools/link.py
Outdated
| if settings.WASM_EXNREF: | ||
| passes += ['--emit-exnref'] | ||
|
|
||
| # If we are not going to run metadce then we will invoke wasm-opt later for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this say "If we are going to run.."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed.
|
The last commit adds a fix that is needed to avoid a test breaking here. The test checked that we don't alter wasm size if we use |
Before this PR, if we did -O3 then we ended up doing this: * wasm-opt -O3 * wasm-metadce * wasm-opt --minify-imports-and-exports The first invocation uses StackIR (which is enabled by default in -O3), but then the later invocations "lose" those optimizations, since they read the wasm binary and generate structured BinaryenIR once more. We would need to do StackIR opts again, but even more, we'd need to do work to undo the effects of stackiness, which can lead to more locals and such, see emscripten-core#22196 After this PR, we do this: * wasm-opt -O3 --no-stack-ir * wasm-metadce * wasm-opt --minify-imports-and-exports --optimize-stack-ir The first command is now told to not use StackIR. The second remains unchanged. The last then does StackIR optimizations. This is the same amount of work (we just shuffle around the StackIR opts), so compile times are unaffected, but we gain some code improvements. To see them, see the commit before last which applies the size changes on top of a recent rebase. Many tests improve by small but noticeable amounts, though two wasm2js tests somehow lose by a few bytes, which I think is noise. There is a slight code complexity downside here, but given this avoids corner cases that may be quite annoying (emscripten-core#22196) it seems worthwhile. Also improve our handling of --emit-symbol-map to use llvm-objcopy, which avoids wasm-opt roundtrip issues (this is necessary in this commit as otherwise the improvements of this PR are undone by that roundtrip, which was caught by a test).
Before this PR, if we did
-O3then we ended up doing this:wasm-opt -O3wasm-metadcewasm-opt --minify-imports-and-exportsThe first invocation uses StackIR (which is enabled by default in
-O3), but then thelater invocations "lose" those optimizations, since they read the wasm binary and
generate structured BinaryenIR once more. We would need to do StackIR opts again,
but even more, we'd need to do work to undo the effects of stackiness, which can
lead to more locals and such, see #22196
After this PR, we do this:
wasm-opt -O3 --no-stack-irwasm-metadcewasm-opt --minify-imports-and-exports --optimize-stack-irThe first command is now told to not use StackIR. The second remains unchanged.
The last then does StackIR optimizations.
This is the same amount of work (we just shuffle around the StackIR opts), so
compile times are unaffected, but we gain some code improvements. To see them,
see the commit before last which applies the size changes on top of a recent rebase.
Many tests improve by small but noticeable amounts, though two wasm2js tests
somehow lose by a few bytes, which I think is noise.
There is a slight code complexity downside here, but given this avoids corner
cases that may be quite annoying (#22196) it seems worthwhile.
Depends on WebAssembly/binaryen#6725 to land first.