# Basic optimization
loom optimize input.wat -o output.wasm
# With statistics
loom optimize input.wasm -o output.wasm --stats
# With verification
loom optimize input.wasm -o output.wasm --verify
# Output as WAT
loom optimize input.wasm -o output.wat --wat
# Everything
loom optimize input.wat -o output.wasm --stats --verifyLibrary optimize_module() (loom-core) runs the production pipeline.
CLI loom optimize --passes exposes selectable pass names. Library
order:
| Pass | Optimization | Example Transformation |
|---|---|---|
| Function Inlining | Inline small / single-call functions | |
| ISLE Constant Folding | 10 + 20 → 30 |
|
| Advanced Instructions | x * 8 → x << 3 (strength reduction) |
|
| Simplify Locals | Eliminate redundant local sets (RSE) | |
| Dead Code Elimination | Remove unreachable / unused code | |
| Code Folding | Flatten blocks, tail-merge common suffixes | |
| LICM | Hoist loop invariants | |
| Remove Unused Branches | Strip dead branches | |
| Optimize Added Constants | Fold (x + c1) + c2 → x + (c1+c2) |
|
| Dead Code Elimination (2nd) | Clean up after LICM/branch removal | |
| Coalesce Locals | Merge non-interfering locals (must run last) |
Plus a fused-component prelude (memory-import dedup, dead-function elim, type dedup) when input is a fused Component-Model module.
| Before | After | Speedup |
|---|---|---|
x * 2^n |
x << n |
~2-3x |
x / 2^n |
x >> n |
~2-3x |
x % 2^n |
x & (2^n - 1) |
~2-3x |
| `x | 0` | x |
x & -1 |
x |
- |
x ^ 0 |
x |
- |
cargo build --release
loom optimize input.wasm -o output.wasm --verify- Z3 SMT translation validation per pass per function
- Reverts a single function on counterexample; reports overall stats
- Z3 verification feature is on by default
cargo build --release --no-default-features
loom optimize input.wasm -o output.wasm --verify- Skips Z3 (verification feature off)
--verifyruns ISLE-rule property checks only
If installing Z3 from scratch:
brew install z3 # macOS
sudo apt install z3 # Linux
# Build with verification
cargo build --release --features verification
# Verify
./target/release/loom optimize input.wasm -o out.wasm --verify- Formal proof via SMT
- Translation validation
- Counterexample generation
- Slower (~100-500ms)
- Instruction reduction: 0-40% (varies by code)
- Binary size reduction: 80-95% (consistent)
- Optimization time: 10-30 µs (very fast)
- Parse time: ~7 µs
- Encode time: ~180 ns
- Math-heavy code: 20-40% instruction reduction
- Lots of constants: High constant folding
- Repeated computations: CSE shines
- Small functions: Inlining effective
- Already optimized code: 0% change
- Complex control flow: Limited LICM
- Dynamic computation: Few constant folds
| Input | Output | Command |
|---|---|---|
| WAT → WASM | Binary | loom optimize in.wat -o out.wasm |
| WASM → WASM | Binary | loom optimize in.wasm -o out.wasm |
| WAT → WAT | Text | loom optimize in.wat -o out.wat --wat |
| WASM → WAT | Text | loom optimize in.wasm -o out.wat --wat |
| Problem | Solution |
|---|---|
| "Failed to parse" | Validate with wasm-validate |
| No Z3 verification | Build with --features verification |
| Slow optimization | Normal for large modules (still <1ms usually) |
| Binary got bigger | Rare; file issue with test case |
| Verification failed | Possible optimizer bug; file issue |
optimize:
\t@for f in dist/*.wasm; do \\
\t\tloom optimize $$f -o $$f --stats; \\
\tdone{
"scripts": {
"optimize": "loom optimize build/app.wasm -o build/app.wasm --stats"
}
}use std::process::Command;
fn main() {
Command::new("loom")
.args(&["optimize", "target/wasm.wasm", "-o", "dist/optimized.wasm"])
.status()
.expect("LOOM optimization failed");
}Instructions: 24 → 20 (16.7% reduction)
- Fewer instructions = faster execution
- 10-20% is good, 20%+ is excellent
Binary size: 797 → 92 bytes (88.5% reduction)
- Smaller = faster download/parse
- 80%+ reduction is typical
Constant folds: 3
- Number of compile-time evaluations
- More is better (enables other optimizations)
Optimization time: 0 ms
- Total optimization duration
- Should be <1ms for most modules
# Not yet implemented
loom optimize input.wasm -o out.wasm --passes=cse,inline,dce# Not yet implemented
loom optimize input.wasm -o out.wasm --profile=usage.prof# Not yet implemented
loom optimize input.wasm -o out.wasm --level=3Current limitations:
- LICM only hoists constants and unmodified locals
- No inter-procedural analysis (yet)
- Limited floating-point optimizations
- No SIMD-specific optimizations
- Component model support is basic
Coming soon:
- More aggressive LICM
- Profile-guided optimization
- Custom optimization passes
- SIMD optimizations
- Enhanced component support
- Full Guide: usage.md
- Verification: formal-verification.md
- WASM Build: wasm-build.md
- Always use --stats to see what was optimized
- Use --verify for production to ensure correctness
- WAT format is slower to parse than binary
- Optimization is idempotent - safe to run multiple times
- Binary size reduction is more consistent than instruction count
- File issues with test cases if something seems wrong
- LOOM Version: 0.1.0
- ISLE Integration: Yes
- Z3 Support: Optional (feature flag)
- Component Model: Basic support
- Bazel Build: Yes
- wasm32-wasip2: Yes