Skip to content

Phase 7: Additional Optimization Rules #10

@avrabe

Description

@avrabe

Phase 7: Additional Optimization Rules

Goals

Extend LOOM's optimization capabilities beyond i32.add to support a comprehensive set of constant folding and algebraic simplification rules.

Planned Optimizations

1. Additional i32 Operations

  • i32.sub - Constant folding for subtraction
  • i32.mul - Constant folding for multiplication
  • i32.div_s - Signed division (with overflow checks)
  • i32.div_u - Unsigned division
  • i32.rem_s - Signed remainder
  • i32.rem_u - Unsigned remainder

2. Algebraic Simplifications

  • Identity laws:
    • x + 0 = x
    • x - 0 = x
    • x * 1 = x
    • x / 1 = x
  • Zero laws:
    • x * 0 = 0
    • 0 / x = 0
  • Subtraction laws:
    • x - x = 0

3. i64 Operations

  • i64.add - Constant folding for 64-bit addition
  • i64.sub - 64-bit subtraction
  • i64.mul - 64-bit multiplication
  • Apply same algebraic simplifications as i32

4. Comparison Operations

  • i32.eq - Constant folding for equality
  • i32.ne - Not equal
  • i32.lt_s / i32.lt_u - Less than (signed/unsigned)
  • i32.gt_s / i32.gt_u - Greater than
  • Similar for i64

Implementation Tasks

  • Extend ISLE definitions in wasm_terms.isle
  • Add new Value variants (I32Sub, I32Mul, etc.)
  • Implement helper functions (imm32_sub, imm32_mul, etc.)
  • Add optimization rules to simplify()
  • Extend instruction parsing/encoding
  • Add comprehensive tests (50+ new tests)
  • Extend property-based verification
  • Update CLI to detect new optimization types
  • Documentation in PHASE7_COMPLETE.md

Example Transformations

;; Before
i32.const 100
i32.const 50
i32.sub
;; After
i32.const 50

;; Before
i32.const 6
i32.const 7
i32.mul
;; After
i32.const 42

;; Before (algebraic)
i32.const 42
i32.const 0
i32.add
;; After
i32.const 42

;; Before (identity)
i32.const 100
i32.const 1
i32.mul
;; After
i32.const 100

Success Criteria

  • All new operations have constant folding
  • Algebraic simplifications work correctly
  • Property-based verification covers new rules
  • 50+ total tests passing
  • Statistics show detection of new optimization types
  • Documentation complete

Estimated Time

1-2 weeks

Dependencies

No new dependencies required - all using existing wasmparser/wasm-encoder infrastructure.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions