Linux command
wasm-opt 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Optimize WASM
wasm-opt -O [input.wasm] -o [output.wasm]
Maximum optimization
wasm-opt -O4 [input.wasm] -o [output.wasm]
Size optimization
wasm-opt -Os [input.wasm] -o [output.wasm]
Print stats
wasm-opt --print [input.wasm]
Validate only
wasm-opt --validate [input.wasm]
说明
wasm-opt is a WebAssembly optimizer from the Binaryen toolkit that applies various optimization passes to reduce module size and improve execution speed. It processes .wasm binary files and outputs optimized versions suitable for production deployment. Optimization levels range from -O0 (none) through -O4 (maximum), with each level enabling progressively more aggressive transformations. -O1 provides quick wins for iteration builds, -O2 covers most optimizations, -O3 is more aggressive, and -O4 additionally flattens the IR. The -Os and -Oz flags optimize for code size, which is important for web delivery. Available passes include dead code elimination, inlining, constant folding, and many WebAssembly-specific optimizations. The tool can also validate modules for specification conformance and print module contents in text format for inspection. It is commonly used as a post-processing step in WebAssembly build pipelines after compilation from source languages.
参数
- -O
- Execute default optimization passes.
- -O0
- No optimization passes.
- -O1
- Quick and useful optimizations for iteration builds.
- -O2
- Most optimizations, generally gets most performance.
- -O3
- Aggressive optimizations, may take significant time.
- -O4
- Also flatten the IR; can take a lot more time and memory.
- -Os
- Optimize for code size.
- -Oz
- Optimize aggressively for code size.
- -o _FILE_
- Output file.
- Print module in text format.
- --validate
- Validate the module without optimizing.
- --converge
- Keep iterating optimization passes until no further improvement.
- -g, --debuginfo
- Preserve debug info in output.
- -ism _FILE_
- Read input source map.
- -osm _FILE_
- Write output source map.
FAQ
What is the wasm-opt command used for?
wasm-opt is a WebAssembly optimizer from the Binaryen toolkit that applies various optimization passes to reduce module size and improve execution speed. It processes .wasm binary files and outputs optimized versions suitable for production deployment. Optimization levels range from -O0 (none) through -O4 (maximum), with each level enabling progressively more aggressive transformations. -O1 provides quick wins for iteration builds, -O2 covers most optimizations, -O3 is more aggressive, and -O4 additionally flattens the IR. The -Os and -Oz flags optimize for code size, which is important for web delivery. Available passes include dead code elimination, inlining, constant folding, and many WebAssembly-specific optimizations. The tool can also validate modules for specification conformance and print module contents in text format for inspection. It is commonly used as a post-processing step in WebAssembly build pipelines after compilation from source languages.
How do I run a basic wasm-opt example?
Run `wasm-opt -O [input.wasm] -o [output.wasm]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -O do in wasm-opt?
Execute default optimization passes.