ReviewOS

zig-utils/zig-js

Optimizer: add global value, loop, range, and effect-aware optimization

#469
Open chris opened this 9 days ago ·
9 days ago

Parent: #459 Depends on: #467, #461

Goal

Implement backend-neutral global optimizations over the optimizer IR: redundant work elimination, loop optimization, range reasoning, and guard/bounds-check reduction.

Scope

  • Global value numbering/CSE, copy propagation, sparse conditional constant propagation, dead-code elimination, and effect-aware load/store forwarding.
  • Dominator-based loop-invariant code motion, induction-variable recognition, strength reduction, loop rotation/unswitching where profitable, and invariant guard hoisting.
  • Integer/range/length analysis for overflow-aware arithmetic, packed arrays, strings, typed arrays, and Wasm memory.
  • Redundant shape/prototype/tag/bounds/barrier elimination only when invalidation and memory-order proofs remain valid.
  • Block layout, branch probability, register-pressure, and code-size cost models.
  • Preserve exact bytecode-step/watchdog accounting even when emitted machine operations are removed or moved.

Effect model requirements

Calls, coercions, getters/setters, proxies, iterators, allocation/OOM, GC safepoints, debugger hooks, atomics, shared-memory accesses, and no-GIL mutations must be explicit barriers or modeled effects. Unknown does not mean pure.

Acceptance

  • An IR verifier checks dominance, SSA, effect ordering, recovery states, and map liveness after every pass in test builds.
  • Each pass has focused positive/negative tests and seeded forced-off/on semantic differentials.
  • Moving GC, exceptions, invalidation races, termination, exact steps, atomics, and host effects remain exact.
  • #461 reports pass-level compile time, code bytes, eliminated operations/guards, and deopt changes.
  • Retained passes improve at least one predeclared #460 row without a noise-qualified regression elsewhere.

Foundation

Optimizer CFG/SSA and reducible regions landed under #431 and #438, including a42aaf26.

9 days ago

Closed SSA/recovery-verification child #1088 in bbeab18f. Test builds validate full value graphs after compaction and selected-loop graphs before lowering, covering identities/ranges, SSA dominance/order, effect ordering, edge and handler shape, and pre-operation recovery value availability. Invalid generated graphs fail loudly; production compilation does not invoke the hooks.

All 181 optimizer cases pass Debug, ReleaseSafe, and unsuppressed TSan on combined parent e9400ed6; mutation/OOM/deep-CFG tests pass. The broader JIT/VM/concurrency gates, 700 seeded fuzz programs, and exact 980-case parent/current corpus comparison pass. Hosted integration is queued, and #933's local memory cap remains unchanged.

The new checker validates the SSA recovery contract; emitted native-map validation and the remaining global optimization/performance criteria are not claimed complete. The parent remains open.

9 days ago

The verifier acceptance item is implemented by #1088 / bbeab18f and #1089 / bc83af30.

Test-build hooks check the graph after value-graph compaction and selected-loop compaction, then independently check lowered native recovery/root maps, runtime descriptors, OSR, and control/safepoint metadata after lowering and before emission. Both verifiers remain callable independently. Mutation/OOM tests and 186 optimizer tests pass in Debug, ReleaseSafe, and unsuppressed TSan, including moving-GC and no-GIL witnesses. Exact-parent outcomes match for 1,037 corpus cases, with no new default-tier failures or tree/VM differences. Full scoped evidence is on #1089; hosted CI remains pending.

Only the verifier item is checked here. The remaining optimization passes, pass-level attribution, predeclared performance acceptance, and broader roadmap stay open.

Sign in to comment on this issue.