NOTE: This section is auto-generated by
rivet init --agents. Do not edit between theBEGIN rivet-managed/END rivet-managedmarkers — edits there are overwritten on regeneration. Content outside the markers is preserved.
See AGENTS.md for project instructions.
Additional Claude Code settings:
- Use
rivet validateto verify changes to artifact YAML files - Use
rivet list --format jsonfor machine-readable artifact queries - Commit messages require artifact trailers (Implements/Fixes/Verifies/Satisfies/Refs)
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Standard build
cargo build --release
# Build with Z3 verification (requires Z3 installed)
export Z3_SYS_Z3_HEADER=/opt/homebrew/include/z3.h # macOS with Homebrew
LIBRARY_PATH=/opt/homebrew/lib cargo build --release
# Run all tests
cargo test --release
# Run a single test
cargo test --release test_name
# Run the optimizer
./target/release/loom optimize input.wasm -o output.wasm
# Validate output
wasm-tools validate output.wasmLOOM is a workspace with five crates:
- loom-shared: ISLE term definitions and WebAssembly IR (Module, Function, Instruction types)
- loom-isle: ISLE pattern matching rules for optimizations, auto-generated from
.islefiles - loom-core: Parser, encoder, 12-phase optimization pipeline, Z3 verification
- loom-cli: Command-line interface
- loom-testing: Differential testing framework
- Parse WASM →
Modulestruct withFunctions containingInstructionvectors - Convert instructions to ISLE
Valueterms viainstructions_to_terms() - Apply ISLE rewrite rules via
simplify_with_env() - Convert back to instructions via
terms_to_instructions() - Run optimization passes (DCE, code folding, branch simplification, etc.)
- Validate with stack checking and optionally Z3 verification
- Encode back to WASM binary
loom-core/src/lib.rs: Main optimizer with all passes, instruction definitions, parser, encoderloom-core/src/stack.rs: Stack validation and type checkingloom-core/src/verify.rs: Z3 SMT verificationloom-core/src/component_optimizer.rs: WebAssembly Component Model supportloom-shared/isle/wasm_terms.isle: ISLE term definitions and rewrite rules
When adding support for a new WebAssembly instruction, update ALL of these locations:
Instructionenum inlib.rs- Parser (wasmparser
Operator→Instruction) - Encoder (two locations:
encode_wasmfunction bodies) apply_instruction_to_stackinstack.rsinstruction_signatureinstack.rsinstructions_to_irinlib.rs(ISLE term conversion)instruction_stack_ioinlib.rs- Z3 verification in
verify.rs - Add to
has_unsupported_isle_instructionsif not ISLE-optimizable
LOOM's mission is to be a provably correct WebAssembly optimizer. This is not aspirational - it is the fundamental constraint that governs every decision.
NOTHING may be skipped, temporarily fixed, or worked around. There are no exceptions:
- No "temporary" fixes - If something doesn't work correctly, fix it properly or don't implement it at all. There is no "we'll fix it later."
- No silent failures - Every error condition must be handled explicitly with clear error messages
- No assumptions - If we cannot prove something is correct, we must not do it
- No "good enough" - Either an optimization is provably correct, or it is not included
- No shortcuts - Every optimization must have its corresponding proof before it is considered complete
Before every action, ask yourself:
- Does this maintain provable correctness?
- Can we formally verify this transformation preserves semantics?
- Am I cutting any corners?
- Would this pass rigorous formal verification?
Think not twice, but constantly - every line of code, every transformation, every decision must be evaluated against the standard of provable correctness.
Optimizations are added step by step, always providing the proof immediately afterwards:
- Design - Define the transformation formally
- Implement - Write the code
- Prove - Immediately provide Z3 verification or formal proof
- Validate - Run the proof, ensure it passes
- Only then - Consider the optimization complete
An optimization without its proof is not an optimization - it is a bug waiting to happen.
An optimization that works "most of the time" is worthless. We need optimizations that work all of the time, proven mathematically.
When in doubt:
- Skip the function rather than risk incorrect optimization
- Return an error rather than produce potentially wrong output
- Keep the original code rather than apply an unverified transformation
A correct optimizer that handles 50% of cases is infinitely better than a fast optimizer that corrupts 1% of cases.
Functions with unsupported instructions are skipped entirely - we do not optimize what we cannot prove. This is the correct behavior, not a limitation to be worked around.
Beyond formal verification, LOOM employs a comprehensive real-world testing strategy:
We maintain a collection of real-world WebAssembly files for validation:
- User-provided files: Actual production WASM from users
- Component Model files: Files using the Component Model specification
- Large-scale modules: Complex modules like
loom.wasmitself
Every optimization pass must work correctly on ALL test files. If an optimization fails on any real-world file, it is rejected.
The ultimate validation is dogfooding - LOOM optimizes itself:
- Build LOOM as WebAssembly Component: Compile LOOM to a .wasm component
- Optimize with LOOM: Run LOOM to optimize its own WebAssembly binary
- Execute the optimized LOOM: Run the optimized version
- Compare outputs: The optimized LOOM must produce identical optimization results as the original
If the optimized LOOM produces different results than the original, something is wrong. This is the most rigorous test possible - we are literally betting the correctness of our output on our own optimization.
tests/*.wasm: User-provided and generated test filesloom-testing/: Differential testing framework- Real components are tested via
loom optimize <file>.wasm
For any optimization to be considered complete:
- ✅ Unit tests pass
- ✅ Z3 verification passes (for supported instructions)
- ✅ All real-world test files optimize correctly
- ✅ Dogfooding produces identical results