Skip to content
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

Add support for automatically executing wasm-opt #625

Merged
merged 2 commits into from
Jul 18, 2019

Conversation

alexcrichton
Copy link
Contributor

@alexcrichton alexcrichton commented Apr 12, 2019

This commit adds support for automatically executing the wasm-opt
binary from the Binaryen project. By default wasm-pack
will now, in release and profiling modes, execute wasm-opt -O over the
final binary. The goal here is to enable optimizations that further
reduce binary size or improve runtime. In the long run it's expected
that wasm-opt's optimizations may mostly make their way into LLVM, but
it's empirically true today that wasm-opt plus LLVM is the best
combination for size and speed today.

A configuration section for wasm-opt has been added as previously
proposed
, namely:

[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-Os']

The wasm-opt binary is downloaded from Binaryen's own
releases
. They're
available for the same platforms that we download predownloaded binaries
for wasm-bindgen on. We'll also opportunistically use wasm-opt in
PATH if it's available. If we're untable to run wasm-opt, though, a
warning diagnostic is printed informing such.

Closes #159

@ashleygwilliams
Copy link
Member

i'd like to merge this with the refactor in #623, so gonna wait til that lands (which should be early next week, if not earlier)

# Configuration can be set to `false` if you want to disable it (as is the
# default for the dev profile), or it can be an array of strings which are
# explicit arguments to pass to `wasm-opt`. For example `['-Os']` would optimize
# for size while `['-O4']` would execute very expensive optimizations passes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing "size" and "speed" meta options that we planned in the original design:

# This is the dev profile, but could also be the profiling or release profiles.
[package.metadata.wasm-pack.profile.dev]
# The `wasm-opt` key may be absent, in which case we choose a default
#
# or we can explicitly configure that we *don't* want to run it
wasm-opt = false
# or use our default alias to optimize for size
wasm-opt = "size"
# or use our default alias to optimize for speed
wasm-opt = "speed"
# or give your own custom set of CLI flags
wasm-opt = ["--dce", "--duplicate-function-elimination", "--instrument-memory"]

Is the intention to add support for these in follow up PRs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! I forgot to mention that here, but I actually first implemented it as a string being a space-separated list of arguments (like wasm-opt = "-Os -some-other-pass"), and then I read the original issue and liked your idea better.

I figured though it may be best to do that as a separate PR as it should be pretty easy to add on top of this, but rather we'd just want to get the bare bones in here

src/lib.rs Outdated Show resolved Hide resolved
This commit adds support for automatically executing the `wasm-opt`
binary from the [Binaryen project][binaryen]. By default `wasm-pack`
will now, in release and profiling modes, execute `wasm-opt -O` over the
final binary. The goal here is to enable optimizations that further
reduce binary size or improve runtime. In the long run it's expected
that `wasm-opt`'s optimizations may mostly make their way into LLVM, but
it's empirically true today that `wasm-opt` plus LLVM is the best
combination for size and speed today.

A configuration section for `wasm-opt` has been added as [previously
proposed][fitzgen], namely:

```toml
[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-Os']
```

The `wasm-opt` binary is downloaded from Binaryen's [own
releases](https://github.com/webassembly/binaryen/releases). They're
available for the same platforms that we download predownloaded binaries
for `wasm-bindgen` on. We'll also opportunistically use `wasm-opt` in
`PATH` if it's available. If we're untable to run `wasm-opt`, though, a
warning diagnostic is printed informing such.

Closes rustwasm#159

[binaryen]: https://github.com/webassembly/binaryen
[fitzgen]: rustwasm#159 (comment)
@alexcrichton
Copy link
Contributor Author

Ping for review on this? I'm not sure if there's anything still blocking this?

@ashleygwilliams
Copy link
Member

i was blocking this on #623, but i think we can land this and i'll rebase #623 and add the updates that let this use the new more generic logic for binstall.

@ashleygwilliams
Copy link
Member

taking on the merge for this- gonna rewrite a bit to use the general logic in #623

@ashleygwilliams
Copy link
Member

gonna merge this and then refactor

@ashleygwilliams
Copy link
Member

this is failing on a cargo fmt, since i'm about to do #685 i think it's fine and gonna merge

@ashleygwilliams ashleygwilliams merged commit 3793ec2 into rustwasm:master Jul 18, 2019
@torch2424
Copy link

@alexcrichton @ashleygwilliams

So i was working on a PR, and I think this is breaking functionality for the current master? I get the following error:

Screenshot from 2019-07-29 07-36-12

Is there a way to skip the step? Or will this be fixed by #685 ?

Thanks! 😄

@alexcrichton alexcrichton deleted the wasm-opt branch July 29, 2019 15:07
@alexcrichton
Copy link
Contributor Author

@torch2424 you should be able to cofnigure:

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

to turn off wasm-opt support. Mind also opening an issue for that bug you're running into? It's an upstream issue with wasm-opt, but newer releases of wasm-opt may have fixed the issue.

@torch2424
Copy link

torch2424 commented Jul 29, 2019

@alexcrichton Awesome, will try that later tonight, and update here 😄

I'll open the issue then too, since a newer version may have fixed this like you said (and I assume we will consume this newer version 👍 )

EDIT: It worked! Thanks for the suggestion! 🎉

@torch2424
Copy link

torch2424 commented Jul 30, 2019

Oh after looking at: src/wasm_opt.rs I now realize the version we are using is hardcoded? Opening an issue now 😄

EDIT: opened #696

@jotyy
Copy link

jotyy commented Oct 9, 2021

@alexcrichton -O3 is not supported?

It works wrong with this configuration:

[profile.release]
opt-level = 3

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O3"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for running wasm-opt
5 participants