Repository navigation
Run fork-free leaves sequentially and distribute CPU ring work - #876
nicolas-abril wants to merge 1 commit into
Conversation
The fork-free table made every bang target forky, so a leaf called with a ! inside a parallel region ran inline on the grow lane with heap continuations instead of being pushed to a ring as a leaf (3x slower on a bang-kid probe). The device handoff is a property of the call site: emit_jump now takes the bang, marks the segment forky, and emits the bang's `if (!seq) return task` in the same block as the leaf handoff, so the two bang sites in emit_body and emit_fork are gone. FID_EXIT sets seq in one statement; the work pass walks a unit's rings in one for. The GPU leaf test folds into fork_leaf_handoff (pipeline, pipeline!, pipeline). comp.ts 64340 -> 64251 ttok. (#876)
…e callee emit_jump took its ref at the top, so a spin body's self-jump (emitted through emit_native under the spin's fid) added the def's own segment to its refs; reach() then kept that segment live and the C lane emitted it dead (raytrace: 3 segments, 144145 -> 123880 bytes, clang 0.33 -> 0.29 s; 40 tests and benches shed one or more). The ref now rides WL_JMP. The leaf handoff drops its caller test: under !seq a jump into a fork-free def is a task whoever jumps, so a leaf reached through a closure body (FID_CLO_APPLY) is handed off like a direct one instead of running inline on heap continuations. fork_leaf_handoff pins a closure-applied fork and a closure-applied leaf under a bang. comp.ts 64951 -> 64938 ttok. (#876)
WL_ROOM bounded a frame by HEAP_OFF + CUBE: the end of the lane stacks when the heap followed them (368270a). c4068a7 put the static image between the two (STAT_OFF, PAGE_UP(STAT_LEN) words) and kept the bound, so a lane under that page pushed its 2049th word over the image's word at its own index and ran on: on Metal, main and next, deep!(2046n) then U32.show(0) prints \002 (the fid of the pushed frame) in place of "0". The bound is STAT_OFF + CUBE, exact for every lane: 2048 words run and the 2049th posts ERR_DEEP. The host is untouched: its WL_ROOM is dead code and it runs a handed-off leaf on its 2 GiB pool stack at any depth (the 2048-word deaths seen at --threads 1 and 8 were Metal, which --gpu turns on by default on a Mac). fork_leaf_budget pins a leaf handed off from a forky segment under ! at the full budget, 2045 levels, on every lane. comp.ts 64840 -> 64840 ttok. (#876)
|
Merged and shipped in 2.0.22, thank you. It landed as commit cea5097 with one change on top: a bang marks its caller forky rather than its callee, so a fork-free bang target stays a leaf (your seeding of the forky table by bang targets kept some leaves inline on the grow lane), and a jump refs its target only when it jumps. Your measurements reproduced here: binarytrees 0.31 → 0.20 s and symreg on the GPU 0.56 → 0.32 s on an M4 Max. One consequence we accepted and noted in the changelog: on the GPU a fork-free leaf reached from a forking def now runs on its lane's 2048-word stack, as a fork kid does. Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself. |
…ontinuations again (#930) A fork-free leaf reached from a forking def inside a bang was handed off as its own task since 2.0.22 (PR #876), on the host and on the device. On the device that task runs on its lane's 2048-word stack, so a leaf that keeps frames per turn, as a do-Result loop does (the generic bind returns a boxed value, the loop a flat record: two frames a turn), died past ~400 turns where 2.0.21 ran it on heap continuations. The handoff now emits under !DEVICE: the host keeps its 2 GiB stack and PR #876's gains, the device runs the leaf as 2.0.21 did. symreg and queens on Metal measure the same or faster without it (0.34 → 0.32 s, 1.0 → 0.95 s on an M4 Max).
The fork-free table made every bang target forky, so a leaf called with a ! inside a parallel region ran inline on the grow lane with heap continuations instead of being pushed to a ring as a leaf (3x slower on a bang-kid probe). The device handoff is a property of the call site: emit_jump now takes the bang, marks the segment forky, and emits the bang's `if (!seq) return task` in the same block as the leaf handoff, so the two bang sites in emit_body and emit_fork are gone. FID_EXIT sets seq in one statement; the work pass walks a unit's rings in one for. The GPU leaf test folds into fork_leaf_handoff (pipeline, pipeline!, pipeline). comp.ts 64340 -> 64251 ttok. (bendlang#876)
…e callee emit_jump took its ref at the top, so a spin body's self-jump (emitted through emit_native under the spin's fid) added the def's own segment to its refs; reach() then kept that segment live and the C lane emitted it dead (raytrace: 3 segments, 144145 -> 123880 bytes, clang 0.33 -> 0.29 s; 40 tests and benches shed one or more). The ref now rides WL_JMP. The leaf handoff drops its caller test: under !seq a jump into a fork-free def is a task whoever jumps, so a leaf reached through a closure body (FID_CLO_APPLY) is handed off like a direct one instead of running inline on heap continuations. fork_leaf_handoff pins a closure-applied fork and a closure-applied leaf under a bang. comp.ts 64951 -> 64938 ttok. (bendlang#876)
WL_ROOM bounded a frame by HEAP_OFF + CUBE: the end of the lane stacks when the heap followed them (baaddc4). 402fea9 put the static image between the two (STAT_OFF, PAGE_UP(STAT_LEN) words) and kept the bound, so a lane under that page pushed its 2049th word over the image's word at its own index and ran on: on Metal, main and next, deep!(2046n) then U32.show(0) prints \002 (the fid of the pushed frame) in place of "0". The bound is STAT_OFF + CUBE, exact for every lane: 2048 words run and the 2049th posts ERR_DEEP. The host is untouched: its WL_ROOM is dead code and it runs a handed-off leaf on its 2 GiB pool stack at any depth (the 2048-word deaths seen at --threads 1 and 8 were Metal, which --gpu turns on by default on a Mac). fork_leaf_budget pins a leaf handed off from a forky segment under ! at the full budget, 2045 levels, on every lane. comp.ts 64840 -> 64840 ttok. (bendlang#876)
…ontinuations again (bendlang#930) A fork-free leaf reached from a forking def inside a bang was handed off as its own task since 2.0.22 (PR bendlang#876), on the host and on the device. On the device that task runs on its lane's 2048-word stack, so a leaf that keeps frames per turn, as a do-Result loop does (the generic bind returns a boxed value, the loop a flat record: two frames a turn), died past ~400 turns where 2.0.21 ran it on heap continuations. The handoff now emits under !DEVICE: the host keeps its 2 GiB stack and PR bendlang#876's gains, the device runs the leaf as 2.0.21 did. symreg and queens on Metal measure the same or faster without it (0.34 → 0.32 s, 1.0 → 0.95 s on an M4 Max).
This PR improves parallel execution of programs with expensive sequential leaves. It replaces heap continuations inside eligible leaf computations with stack continuations and distributes adjacent ready leaves across CPU worker groups, reducing allocation and allowing more workers to execute useful work.
Temporary sequential mode uses a bit in the existing
seqargument and is cleared before an outer continuation resumes. The fork-free analysis includes GPU bang reachability, preserving later device requests. Task/mode changes apply to CPU and GPU; ring ownership changes only on CPU. The caller eligibility check stays because removing it regressed local Metal queens and symreg timings.The compiler change is 16 inserted / 5 removed lines, +141 ttok against this base. Two regression tests exercise sequential leaves followed by further forks, captured/shared values, tuple results, and execution before, during, and after a GPU call.
Performance measurements
Existing Bend runtime benchmarks
Quoted Koru benchmark programs
Negative is faster. Each cell is the mean of ten paired percentage changes
100 × (PR / base − 1), with a 95% Student-t confidence interval (the±value is its half-width in percentage points). Bold marks a two-sided paired test with Holm-adjusted p < 0.05 across all 52 cells. The displayed intervals are per-cell, not simultaneous intervals. Unlike #855’s two-sigma/2% heuristic, this marks statistical significance without imposing a minimum effect size; a tiny bold change can still be practically negligible.Measured on Apple M4 against base
15ae0c86, PR commit5bf1525a. Ten measured pairs after one excluded warmup per executable and mode, alternating AB/BA order. CPU uses 1 or 8 workers; Metal uses 8 host workers and the gate’s per-benchmark GPU memory budgets. Each timed Metal launch has an immediate same-executable warmup to avoid the binary-switch cache bias seen earlier. Whole-process wall time includes/usr/bin/time -l; compilation and warmups are excluded. Builds and runs were serialized under one lock, every output matched its oracle, and no measured samples were discarded. These are local measurements, not cluster pins.The Koru rows run the verbatim quoted parallel source with either one or eight CPU workers. They make no GPU request, so the Metal cells are unmeasured rather than CPU fallbacks presented as GPU results.
Raw paired timing samples (milliseconds)
Rounds pair the baseline and patched entries in the same column. Even-indexed rounds run baseline then patched; odd-indexed rounds reverse that order (round 1 is index 0).
Validation
46 / 46, and the diff passed whitespace checks.Suggested additional benchmarks
I suggest adding the following digitsum and binarytrees programs directly from korulang/koru-benchmarks as additional Bend benchmarks. These are verbatim copies of its parallel Bend ports, not the modified batching variants used elsewhere in the investigation. Preserve their workloads and outputs; integrate them with Bend's benchmark harness and language twins separately.
Digitsum exercises a shallow eight-way split into expensive sequential leaves. Binarytrees exercises allocation and tree values crossing fork boundaries; it still exposes fine-grained parallel overhead even with this patch. Their expected outputs are
55000002and19659600, respectively. The suggestions are included here for review; this commit adds the compiler regression tests.digitsum
Source: suites/osprey-compute-kernels/bend-par/digitsum/digitsum.bend.
binarytrees
Source: suites/osprey-compute-kernels/bend-par/binarytrees/binarytrees.bend.
Attribution and license for the quoted benchmarks
The Bend ports are from korulang/koru-benchmarks, copyright (c) 2026 korulang, under its MIT license. They derive from the Osprey benchmark corpus by Christian Findlay / Nimblesite, copyright (c) 2025 Christian Findlay, also MIT; the upstream provenance identifies Osprey commit
79989f21317785b7e7870968a1facaf0a50bea13. Any copies added to Bend should retain the applicable copyright and permission notices below, alongside attribution to both sources.Koru benchmark license, Osprey provenance, Osprey license.
Koru benchmarks MIT notice
Osprey MIT notice