LinuxCommandLibrary

wasm-opt

Optimize WebAssembly modules

TLDR

Apply default optimizations and write to a given file

$ wasm-opt -O [input.wasm] [[-o|--output]] [output.wasm]
copy

Apply all optimizations and write to a given file (takes more time, but generates optimal code)
$ wasm-opt -O4 [input.wasm] [[-o|--output]] [output.wasm]
copy

Optimize a file for size
$ wasm-opt -Oz [input.wasm] [[-o|--output]] [output.wasm]
copy

Print the textual representation of the binary to console
$ wasm-opt [input.wasm] --print
copy

SYNOPSIS

wasm-opt [OPTIONS] INFILE [-o OUTFILE]

PARAMETERS

-O, -O1, -O2, -O3, -O4
    Specify optimization levels. -O0 is no-op. Higher levels enable progressively more optimizations. -Os and -Oz are available for size optimization.

--strip
    Strips all non-essential data from the module, including debug information, producers, and name sections.

--pass <name>
    Runs a specific optimization pass by its name. Useful for targeted optimizations or debugging.

--all-passes
    Runs all default optimization passes. This is usually implied by optimization levels like -O1 and above.

--add-pass <name>
    Runs a specific pass in addition to the default optimization passes set by -O levels.

-o, --output <file>
    Specifies the output file path. If omitted, the optimized WebAssembly module is written to standard output.

--emit-text
    Outputs the optimized module in WebAssembly Text Format (WAT) instead of the default binary format.

--emit-binary
    Outputs the optimized module in WebAssembly Binary Format (WASM). This is the default behavior.

-v, --validate
    Validates the module after optimization to ensure correctness and adherence to the WebAssembly specification.

--enable-<feature>
    Enables specific experimental or proposed WebAssembly features (e.g., --enable-simd, --enable-threads, --enable-gc) for optimization.

-h, --help
    Displays a comprehensive help message with all available options and optimization passes.

DESCRIPTION

wasm-opt is a powerful command-line tool from the Binaryen toolkit, designed to optimize WebAssembly (.wasm) binary modules. It can significantly reduce the size of .wasm files and improve their execution speed by applying various passes such as dead code elimination, local variable coalescing, instruction optimization, and more. It supports multiple optimization levels, allowing users to balance between optimization aggressiveness and compilation time. wasm-opt is a crucial tool for developers deploying WebAssembly to production environments, ensuring their modules are as efficient as possible. By default, it applies a set of common and safe optimizations, but users can also enable experimental features or run specific passes for fine-grained control. Its versatility makes it indispensable for achieving high-performance and compact WebAssembly applications.

CAVEATS

Optimizations, especially aggressive ones like -O3 or -O4, can significantly increase compilation time.
Using specific --pass options without understanding their effects may lead to unintended behavior or break module functionality.
While wasm-opt aims for correctness, it's always recommended to validate and test optimized modules thoroughly in their target environment.
Some experimental WebAssembly features require explicit --enable- flags to be recognized and optimized.

OPTIMIZATION PASSES

wasm-opt includes a vast array of fine-grained optimization passes (e.g., dce for dead code elimination, coalesce-locals for merging locals, precompute for constant folding, inlining for function call inlining). These passes can be invoked individually using --pass <name>, allowing for highly specific optimization needs, research, or custom optimization pipelines beyond the standard -O levels.

TARGET ENVIRONMENTS

The --target-env <name> option allows specifying the target WebAssembly environment (e.g., web, node, wasm). This can influence certain optimizations or validation rules based on the capabilities and requirements of the intended execution environment.

HISTORY

wasm-opt is a core component of Binaryen, an optimizing compiler and toolkit for WebAssembly. Binaryen was initially developed by Google and has been a foundational tool since the early days of the WebAssembly specification. Its development focused on providing a fast, efficient, and flexible toolchain for WebAssembly, used widely for tasks such as post-compilation optimization (e.g., by Emscripten) and direct WebAssembly code manipulation.

SEE ALSO

wat2wasm(1), wasm2wat(1), wasm-dis(1), wasm-objdump(1), llvm-objcopy(1)

Copied to clipboard