ReviewOS

zig-utils/zig-js

JIT: lower general reducible loop CFGs with path-exact safepoints

#438
Closed chris opened this 9 days ago ·
9 days ago

Parent: #432 Follow-up to: #435 Blocks: #429 Related: #431, #434

Generalize verified loop OSR beyond a straight-line body or one bounded inner branch. Unsupported graphs must fail closed until every selected path has exact recovery and step semantics.

Acceptance

  • Discover and lower general reducible numeric loop regions with multiple internal branches, merges, latches, and backedges.
  • Account unequal path costs dynamically and side-exit the exact edge before budget, 1,024-step checkpoint, termination, or invalidation work is crossed.
  • Preserve parallel block arguments across continue, conditional break, nested loops, and multi-latch control.
  • Publish immutable recovery records and precise stack maps for every internal guard and safepoint.
  • Prove normal/TSan differential convergence for both directions of unequal diamonds, deeper nesting, multiple exits, and unsupported irreducible graphs.
  • Update optimizer documentation and any benchmark claims only from complete reruns.

Current checkpoint

561a516 supports one inner diamond with unequal arm costs. The header reserves the larger safe quantum while the selected arm updates exact steps, budget, and checkpoint distance by its own cost.

40d85b3 adds a second reducible shape: both inner arms may now be independent latches that backedge directly to the header, preserving separate parallel-copy states and path costs. Source-level repeated execution and exact recovered locals converge. The same batch fixes equal-cost terminal branches to emit only the selected branch-local SSA operations instead of reading uninitialized scratch.

a95fa40 adds a conditional loop-exit shape: one inner arm may backedge while the other exits directly or through a tiny arm block. The exit accounts only the selected bytecode path, reconstructs the exact arm-to-outer-exit edge, and resumes the target without double execution. Source-level convergence and exact recovered locals are covered.

80055d8 replaces that single-exit encoding with an owned, region-scoped guard chain. Any number of sequential conditional exits may resume the exact outer edge; unrelated control after the loop is ignored by discovery. Both exits in a two-guard witness recover exact locals and cumulative steps. ad0b211 adds source-level convergence and keeps debugger, host, and profiler statement hooks on bytecode across entry, OSR, and direct calls.

c99dafa composes the exit-guard prefix with the existing shared or independent-latch branch tail. dfb728e proves both diamond arms and the later exact break converge from source, while an explicit two-entry irreducible witness rejects before publication.

149e065 replaces the single branch tail with owned sequential segments. Shared joins can feed another branch directly or through a linear merge; each selected arm updates exact steps and checkpoint/budget distance before the final backedge. f8a7343 exercises an exit prefix followed by two diamonds and takes both outcomes of each across optimized iterations.

50674c6 adds an owned chain of conditional continue/backedge guards before the shared tail. Every early latch owns its edge copies, exact cumulative bytecode charge, checkpoint/budget decrement, moving safepoint, observer notification, and native backedge. f7417cc proves three distinct latches converge from source with exact repeated step accounting and real optimizer OSR publication. b8f7bd5 updates the concise README boundary and detailed optimizer contract.

0bf1d92 publishes only the exact last bytecode loop header, allowing an innermost structured loop to enter native OSR while unmatched outer backedges remain in bytecode. e1a2032 composes that selection with the three-latch region and proves repeated nested execution, exact total steps, and one-entry metadata. ff0b77f documents the boundary.

a42aaf2 adds a general single-header fallback: removing header backedges must leave a single-entry DAG, every CFG edge owns its parallel copies, blocks charge steps incrementally, internal exits own exact edge recovery, and every independent latch publishes a moving safepoint. 014d910 proves nested diamonds, four nested independent latches, and a break from a nested arm all converge with exact repeated steps. a148456 narrows the documented boundary to cyclic/fused nested regions.

48bfe60 selects an exact outer OSR entry only when the region contains inner cycles, validates every removed backedge by dominance, defers loop-carried copies until their sources exist, and emits exact internal block checks plus per-header moving safepoints. b80e81f proves two- and three-level fusion, outer-entry selection, exact internal checkpoint recovery at step 1,024, and an off-checkpoint precise moving callback under TSan. 3d78eaf removes the completed CFG limitation from README and documents the final contract.

The fused source gate passes 4/4 normally and 4/4 under TSan. The combined optimizer TSan gate passes 42/42 lowering and 80/80 runtime/context tests with zero failures or leaks; the later 32-backedge moving-request cadence is covered by the focused TSan gate. General reducible numeric loop regions are complete, while irreducible multi-entry cycles retain explicit fail-closed coverage. Benchmark scores are unchanged because published kernels do not use these new CFG shapes.

9 days ago

Batch 561a516 is on main, documented by aef987d. One inner reducible diamond may now have unequal arm costs: the header checks the larger safe quantum, each selected arm accounts and decrements its own exact bytecode cost, then applies the merge/backedge state. Synthetic coverage runs both longer-arm directions through complete loops and checkpoint-adjacent exits; VM coverage proves both hot functions converge with exact repeat-run steps. Combined batch gate: lowering 2/2 normal and 2/2 TSan; VM 3/3 normal and 3/3 TSan. The README remains concise and now names the one path-accounted inner branch. Deeper nesting, multiple latches/exits, and irreducible graphs remain open.

9 days ago

Landed and pushed to main in three validated chunks: 50674c6 lowers owned conditional backedge chains, f7417cc proves exact source-level convergence across three latches, and b8f7bd5 refreshes README/docs. Focused normal: 9/9 lowering + 3/3 source. Combined TSan: 42/42 lowering + 75/75 runtime/context, zero failures/leaks. The issue body now removes the completed more-than-two-latches limitation and retains nested loops, arbitrary reconvergence, and fully general reducible regions.

9 days ago

Nested-region batch landed on main: 0bf1d92 selects and publishes one exact innermost loop entry, e1a2032 proves nested three-latch convergence with exact repeated steps, and ff0b77f updates README/docs. One combined TSan run passed 42/42 lowering and 76/76 runtime/context tests. Remaining scope is fused nested-loop regions, arbitrary reconvergence, and fully general reducible control.

9 days ago

Landed on main: a42aaf2 generalizes lowering to a single-entry/single-header DAG region, 014d910 proves nested diamonds, four independent nested latches, and a nested-arm break, and a148456 updates README/docs. Focused 4/4 and combined TSan 42/42 + 79/79 pass with zero failures/leaks. Dynamic path accounting, recovery/safepoint publication, and reconvergent differential acceptance boxes are now complete; cyclic/fused nested-loop regions remain.

9 days ago

Completed on main through 48bfe60/b80e81f/3d78eaf. All acceptance boxes are satisfied: general reducible numeric regions now include arbitrary reconvergence, multiple exits/latches, and fused two- and three-level loops with edge-owned parallel copies, exact internal deopts/checkpoints, and per-header moving safepoints. Focused fused evidence passes 4/4 normally and under TSan; the broad optimizer TSan gate passes 42/42 + 80/80. README and detailed docs no longer list reducible CFGs as missing.

Sign in to comment on this issue.