Skip to content

feat(opt): #70 six-pass chain composition + task.return shim peephole (v1.0.5 Track 2)#131

Merged
avrabe merged 1 commit into
mainfrom
release/v1.0.5-pr-70-chain
May 19, 2026
Merged

feat(opt): #70 six-pass chain composition + task.return shim peephole (v1.0.5 Track 2)#131
avrabe merged 1 commit into
mainfrom
release/v1.0.5-pr-70-chain

Conversation

@avrabe
Copy link
Copy Markdown
Contributor

@avrabe avrabe commented May 19, 2026

Composes the v1.0.4 async-adapter detector with inline + directize + const-fold + DCE + new forward_global_shim peephole + dead-stores. ~600 LOC across component_optimizer.rs + lib.rs. 8 new tests. Strict-shrink gate on the hand-built P3 adapter fixture.

🤖 Generated with Claude Code

… (v1.0.5 Track 2)

Composes the remaining five steps of the v1.0.3 #70 roadmap on the
post-detection IR produced by v1.0.4 (Track A, #125).

## Part A: orchestrator (component_optimizer.rs)

New `run_async_chain_passes(module)` runs, in order:

  2. crate::optimize::inline_functions       (force-inline async-lift thunks)
  3. crate::optimize::directize              (const-slot call_indirect → direct)
  4. crate::optimize::constant_folding       (propagate EXIT_OK discriminant)
  5. crate::optimize::eliminate_dead_code    (drop unreachable slow-path arms)
  5.5 crate::optimize::forward_global_shim   (NEW — task.return forward, lib.rs)
  6. crate::optimize::eliminate_dead_stores  (kill start_task waitable init)

Each constituent pass is no-op on functions that don't need it, so
over-applying is safe. Every pass already carries its own Z3
`verify_or_revert` gate, so the orchestrator does NOT bypass
verification — it just composes existing proven passes in the right
order for the post-detection IR.

Wiring in `optimize_core_module` mirrors the Phase 3 / Phase 4
save-and-revert pattern: encode + wasm-tools-validate after the
chain; revert on mismatch.

## Part B: forward_global_shim peephole (lib.rs)

New `optimize::forward_global_shim(module)` recognises

    global.set $g
    global.get $g     ;; immediately follows

and erases both. Soundness:

  - the global has EXACTLY ONE writer module-wide (verified via a
    pre-scan that recurses into Block/Loop/If bodies),
  - `global.get` immediately follows the matching `global.set`
    (any intervening instruction blocks the fold),
  - any function containing Instruction::Unknown disqualifies the
    module-wide pass.

The fold is byte-for-byte equivalent to keeping the value on the
stack when the global has a single writer — no observer can race
the round-trip.

## Tests (8 new, all pass)

forward_global_shim:
  - test_forward_global_shim_folds_simple_pair
  - test_forward_global_shim_skips_multiple_writers
  - test_forward_global_shim_skips_intervening_op
  - test_forward_global_shim_skips_mismatched_indices
  - test_forward_global_shim_no_op_on_plain_function
  - test_forward_global_shim_skips_unknown_instructions

chain composition:
  - test_chain_compose_eliminates_full_adapter
  - test_chain_no_op_when_pattern_absent

Targeted run (async_adapter forward_global chain_compose):
  12 passed; 0 failed; 0 ignored.

## Scope

The chain re-uses existing passes — no duplication, no Z3 bypass.
Token cost: +601 LOC (orchestrator + peephole + tests + comments).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@avrabe avrabe merged commit bd1a0c8 into main May 19, 2026
@avrabe avrabe deleted the release/v1.0.5-pr-70-chain branch May 19, 2026 05:28
avrabe added a commit that referenced this pull request May 19, 2026
…#68 fused + Rocq prep) (#133)

Four v1.0.4 infrastructure pieces grew real consumers:

  #130  Track 1+4  ægraph pipeline integration + Rocq Roundtrip prep doc
  #131  Track 2    #70 six-pass chain composition + task.return shim peephole
  #132  Track 3    #68 Tier-1.1 scalar adapter inlining + Tier-2.2 body dedup

+18 tests, 400+ total. Code-section bytes unchanged on the current
corpus — Track 3 agent flagged 4 pre-existing observations worth
tracking for v1.0.6+.
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