Skip to content

feat: add value optimization passes (const_prop, algebraic, copy_prop)#30

Open
arnoox wants to merge 7 commits intomainfrom
pr-e/value-optimizations
Open

feat: add value optimization passes (const_prop, algebraic, copy_prop)#30
arnoox wants to merge 7 commits intomainfrom
pr-e/value-optimizations

Conversation

@arnoox
Copy link
Owner

@arnoox arnoox commented Mar 23, 2026

Summary

Adds three value-based optimization passes that analyze and simplify IR value operations:

Pre-lowering passes (on SSA IR with phi nodes):

  • const_prop: Constant folding and propagation. Tracks which variables hold known constants and evaluates expressions using Wasm semantics (via herkos-runtime)
  • algebraic: Algebraic simplifications. Rewrites x * 1 → x, x & x → x, etc.
  • copy_prop: Backward coalescing and forward substitution. Eliminates redundant copies.

Post-lowering passes (on phi-free IR):

  • copy_prop (2nd pass): Forwards the assignments that lower_phis inserted into predecessor blocks, reducing further redundancy.

Key improvements:

  • Constant folding: Operations with compile-time-known operands are evaluated and replaced with constants
  • Dead code exposure: Const prop may eliminate all uses of a variable, exposing dead instructions
  • Phi simplification: phi(5, 5) → const(5) is folded before SSA lowering
  • Assignment forwarding: Post-lowering copy_prop eliminates intermediate copies created by lower_phis

Implementation notes:

  • const_prop uses herkos-runtime functions (wasm_min_f32, i32_div_s, etc.) for spec-compliant evaluation
  • IrValue now derives PartialEq to support test assertions
  • herkos-core now depends on herkos-runtime for float operation evaluation
  • Passes integrate into the existing optimize_ir and optimize_lowered_ir pipeline

Dependencies:

🤖 Generated with Claude Code

bench and others added 2 commits March 23, 2026 09:04
Add three value optimization passes to the pre-lowering and post-lowering pipeline:

**Pre-lowering passes** (run on SSA IR with phi nodes):
- const_prop: Constant folding and propagation.  Tracks which VarIds hold
  known constant values and evaluates operations on constants using Wasm
  arithmetic semantics (via herkos-runtime functions).
- algebraic: Algebraic simplifications. Rewrites BinOp instructions when one
  operand is a known constant and an identity/annihilator rule applies.
  (e.g., x * 1 = x, x + 0 = x, x & x = x)
- copy_prop (pre-lowering): Backward coalescing and forward substitution.
  Eliminates intermediate copies before phi lowering.

**Post-lowering passes** (run on phi-free IR):
- copy_prop (post-lowering): Forward substitution of assignments created by
  lower_phis, reducing redundant variable copies.

## Value optimizations improve:
- Constant folding: Wasm instr with const operands evaluated at compile time
- Dead code elimination: Const prop may make more instructions side-effect-free
- phi simplification: Pre-lowering const prop folds phi(const, const)
- Assignment forwarding: Post-lowering copy_prop forwards lower_phis assignments

## Integration:
- Both const_prop and algebraic run in optimize_ir (pre)
- copy_prop runs in both optimize_ir (pre) and optimize_lowered_ir (post)
- Added PartialEq to IrValue to support test assertions
- herkos-core now depends on herkos-runtime for float op evaluation

## Test coverage:
All unit tests pass for instruction folding, algebraic rules, copy propagation.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant