Skip to content

Run fork-free leaves sequentially and distribute CPU ring work - #876

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:sequential-leaf-scheduling
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:sequential-leaf-scheduling

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

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 seq argument 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

bench SEQ-CPU PAR-CPU PAR-GPU (Metal)
bfs -0.1% ± 0.3 -0.2% ± 0.4 -0.2% ± 0.7
editdist -0.01% ± 0.04 +0.4% ± 0.9 +0.8% ± 2.0
gameoflife +0.0% ± 0.2 -0.1% ± 0.1 -0.7% ± 1.1
hashmap -0.3% ± 0.4 +0.0% ± 0.4 +0.0% ± 0.7
kmeans -0.3% ± 0.1 -0.7% ± 1.4 +0.1% ± 0.6
lexer +0.2% ± 1.2 +0.9% ± 1.1 +0.0% ± 0.2
mandelbrot +0.1% ± 0.4 +0.0% ± 0.5 -0.4% ± 0.7
merkle -0.1% ± 0.3 -0.1% ± 0.3 +0.1% ± 0.6
nbody -0.1% ± 0.1 -0.1% ± 0.2 +0.3% ± 1.0
queens -1.5% ± 0.2 -1.9% ± 0.8 -3.9% ± 1.2
raytrace -0.1% ± 0.2 -0.1% ± 1.0 -0.3% ± 0.9
symreg +1.3% ± 0.3 +0.4% ± 0.4 -24.0% ± 1.1
terrain +0.0% ± 0.1 -1.0% ± 1.4 -0.4% ± 0.4
tree-bitonic -0.6% ± 0.1 +2.6% ± 1.9 +0.3% ± 1.1
tree-matmul -0.4% ± 0.3 -1.6% ± 0.9 -0.5% ± 1.1
tree-radix +2.3% ± 0.3 +3.3% ± 1.5 -0.3% ± 0.3

Quoted Koru benchmark programs

bench SEQ-CPU PAR-CPU PAR-GPU (Metal)
binarytrees -5.3% ± 1.0 -37.1% ± 1.2 —
digitsum -2.4% ± 5.2 -53.2% ± 0.7 —

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 commit 5bf1525a. 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).

suite,bench,mode,version,round1_ms,round2_ms,round3_ms,round4_ms,round5_ms,round6_ms,round7_ms,round8_ms,round9_ms,round10_ms
bend,bfs,seq-cpu,baseline,3518.666834,3524.584583,3541.753666,3566.264875,3571.683459,3582.648833,3594.088792,3615.547666,3602.532542,3650.548791
bend,bfs,seq-cpu,patched,3514.303125,3527.274750,3541.271166,3557.055708,3576.205833,3591.472875,3594.646291,3601.289958,3614.661417,3611.449792
bend,bfs,par-cpu,baseline,595.073500,595.521208,593.105458,596.846750,596.551875,600.913041,595.219667,599.676500,595.233417,594.383791
bend,bfs,par-cpu,patched,593.308292,596.813000,594.607375,591.901834,593.528500,595.848625,598.318333,592.963750,599.141042,594.885250
bend,bfs,par-gpu,baseline,770.340750,766.100250,755.095708,765.740417,755.531292,759.762042,756.841958,759.913125,758.796000,758.426416
bend,bfs,par-gpu,patched,760.123666,766.024625,749.255416,758.682333,746.834000,758.728958,758.381459,773.529167,757.301083,762.578417
bend,editdist,seq-cpu,baseline,2098.619875,2098.856750,2098.851625,2098.306750,2097.111750,2097.398042,2097.084542,2096.861334,2096.911208,2096.572917
bend,editdist,seq-cpu,patched,2099.890500,2097.454750,2096.500791,2098.472542,2098.313250,2096.285500,2097.372958,2096.480291,2096.699334,2097.792333
bend,editdist,par-cpu,baseline,423.286666,422.777541,422.941709,426.849792,426.844792,422.940125,431.651833,424.511500,423.821709,428.867333
bend,editdist,par-cpu,patched,432.967125,423.577417,428.633542,426.985625,422.528500,428.475375,426.915541,428.363208,428.445666,423.612000
bend,editdist,par-gpu,baseline,468.094000,464.222459,467.680708,464.717583,481.894333,453.924041,456.185875,448.174917,472.739667,455.128084
bend,editdist,par-gpu,patched,462.799875,461.684291,471.528375,470.491292,472.899542,468.451041,456.575167,476.958833,456.798792,469.072500
bend,gameoflife,seq-cpu,baseline,7870.247667,7820.769459,7804.803709,7789.273292,7784.299750,7767.233666,7775.288209,7762.425042,7804.721041,7761.261625
bend,gameoflife,seq-cpu,patched,7853.710750,7835.535041,7802.515583,7797.918875,7784.341375,7779.412625,7773.582542,7770.005875,7763.256542,7757.658000
bend,gameoflife,par-cpu,baseline,1352.534584,1354.564458,1350.772042,1353.626084,1352.228042,1350.124959,1352.029000,1356.693792,1357.024917,1350.080500
bend,gameoflife,par-cpu,patched,1350.040542,1352.162000,1353.741875,1349.679416,1350.656209,1350.451416,1352.200958,1353.719417,1352.034042,1353.165291
bend,gameoflife,par-gpu,baseline,84.374459,84.141458,84.339375,84.459875,84.104834,84.260125,88.055958,83.869708,83.808875,84.026125
bend,gameoflife,par-gpu,patched,84.625750,83.939750,83.859750,83.185042,84.456000,83.961375,84.176042,83.384167,84.706292,83.381291
bend,hashmap,seq-cpu,baseline,2626.168375,2595.868625,2624.410917,2608.368708,2610.250625,2615.066083,2602.226209,2617.062167,2593.594042,2594.853792
bend,hashmap,seq-cpu,patched,2604.865666,2597.246458,2591.004083,2603.334750,2611.662417,2594.948084,2585.350541,2618.040334,2590.064417,2612.537667
bend,hashmap,par-cpu,baseline,460.574917,459.319167,458.157958,458.718833,458.918250,455.871667,458.119292,457.751167,458.648958,455.986125
bend,hashmap,par-cpu,patched,458.823375,460.195083,455.035041,456.232625,457.237834,457.380917,458.535083,458.518916,458.573333,461.407625
bend,hashmap,par-gpu,baseline,1650.674667,1651.906834,1651.012792,1643.976750,1642.620541,1656.814458,1643.778042,1650.057250,1642.153833,1655.421084
bend,hashmap,par-gpu,patched,1658.675666,1610.874208,1656.476917,1644.119875,1649.380917,1650.974666,1655.145209,1653.488459,1658.736875,1654.070041
bend,kmeans,seq-cpu,baseline,2073.346667,2069.920500,2075.580375,2074.071291,2073.371583,2077.231750,2072.103333,2071.281584,2072.570250,2070.697333
bend,kmeans,seq-cpu,patched,2062.984500,2070.136250,2067.122375,2064.975917,2064.961500,2067.589208,2069.913583,2067.904917,2062.348000,2062.218583
bend,kmeans,par-cpu,baseline,467.273541,463.907917,463.706625,462.178250,468.189458,458.615500,454.864792,463.946666,457.108000,466.529750
bend,kmeans,par-cpu,patched,467.489792,457.940125,466.580209,455.527417,449.936583,462.726792,449.769958,461.035042,469.021583,452.094417
bend,kmeans,par-gpu,baseline,661.242209,647.712083,634.855958,646.719333,645.461083,647.721750,647.874125,648.627375,648.306625,647.078750
bend,kmeans,par-gpu,patched,653.282541,650.529208,648.315833,646.880416,645.337375,647.729209,646.913666,647.824458,645.718958,647.806834
bend,lexer,seq-cpu,baseline,2252.155833,2357.676125,2290.086625,2281.740875,2263.013416,2309.474083,2244.740958,2357.938042,2308.692083,2259.956792
bend,lexer,seq-cpu,patched,2300.318083,2310.465000,2299.504667,2273.746542,2273.247209,2295.130166,2289.624458,2297.140334,2314.056708,2316.514000
bend,lexer,par-cpu,baseline,435.126166,433.442334,427.446792,428.173084,428.120333,427.463208,438.963166,433.815250,433.201958,433.184416
bend,lexer,par-cpu,patched,431.712791,433.398458,441.840917,435.764250,436.429458,437.144500,433.262291,432.776833,435.710875,437.753791
bend,lexer,par-gpu,baseline,2992.637750,2997.277792,3006.982500,3007.650709,3002.781917,2996.494292,2992.096583,2994.548167,2996.118417,2998.193333
bend,lexer,par-gpu,patched,2997.906417,3011.395791,3001.195583,2993.462875,3001.342792,2989.613916,2994.866375,2990.304833,2998.303459,2994.134792
bend,mandelbrot,seq-cpu,baseline,4901.174292,4848.373916,4807.564959,4779.433791,4757.836250,4729.285667,4727.168500,4705.658292,4703.760917,4693.524334
bend,mandelbrot,seq-cpu,patched,4873.076625,4834.039833,4798.984750,4811.163458,4763.676417,4779.890375,4716.495375,4725.857708,4700.104708,4698.126083
bend,mandelbrot,par-cpu,baseline,791.880500,797.122875,786.733709,789.249667,790.173292,790.043292,790.648250,795.289042,799.579250,788.534708
bend,mandelbrot,par-cpu,patched,789.952375,792.084875,790.499959,792.412542,793.654584,789.640875,793.130125,792.626417,787.112917,794.189875
bend,mandelbrot,par-gpu,baseline,67.312709,67.428541,67.074375,67.699875,68.210250,67.930875,67.268250,67.629666,66.959542,67.678875
bend,mandelbrot,par-gpu,patched,66.435833,66.800000,67.138834,66.939125,69.229791,67.031625,66.877708,66.986000,66.893959,68.066167
bend,merkle,seq-cpu,baseline,4476.591833,4447.712500,4429.613167,4416.476917,4417.457416,4400.517959,4441.537167,4390.693875,4391.053334,4383.823167
bend,merkle,seq-cpu,patched,4470.968542,4457.568250,4430.995834,4419.050791,4405.879084,4403.009166,4392.995083,4391.340000,4386.961625,4389.057875
bend,merkle,par-cpu,baseline,870.057708,869.790083,869.804042,867.721750,862.535084,870.600625,862.800458,863.171916,865.211875,864.360791
bend,merkle,par-cpu,patched,865.787750,864.221625,869.210583,866.616833,862.107292,868.729917,867.568833,865.346334,862.009250,864.998167
bend,merkle,par-gpu,baseline,80.210750,80.684542,81.379083,80.230458,80.805583,79.992792,80.453375,81.003667,81.271083,80.821792
bend,merkle,par-gpu,patched,80.677375,80.877791,81.303375,81.395500,80.118958,80.857125,80.972458,80.927458,80.433041,80.464208
bend,nbody,seq-cpu,baseline,5746.109833,5746.894209,5745.665334,5745.024917,5744.640834,5774.140583,5745.487459,5752.388209,5745.545417,5745.566541
bend,nbody,seq-cpu,patched,5743.990458,5745.296333,5746.653333,5748.043375,5744.034792,5745.410875,5745.070875,5744.168541,5744.150916,5746.235125
bend,nbody,par-cpu,baseline,980.424334,978.140250,977.276875,977.161000,981.015750,980.656375,977.722625,976.674375,980.777917,979.814959
bend,nbody,par-cpu,patched,978.497042,978.276833,977.847084,980.054083,975.020000,977.531750,978.419375,976.846916,977.560834,977.760750
bend,nbody,par-gpu,baseline,65.162833,65.168542,65.245416,64.887583,65.039458,64.884708,65.388459,65.373625,65.579583,65.089542
bend,nbody,par-gpu,patched,65.092125,65.525750,64.928708,65.268792,67.488709,65.580333,65.359208,64.677625,64.777334,65.368917
bend,queens,seq-cpu,baseline,5626.854834,5622.162875,5630.099458,5621.747084,5664.271667,5622.829959,5624.063708,5618.125541,5617.262958,5616.045417
bend,queens,seq-cpu,patched,5543.682166,5544.770917,5543.755958,5541.755000,5546.492500,5545.401083,5537.913084,5540.779583,5540.886292,5537.875625
bend,queens,par-cpu,baseline,953.558125,952.905208,967.223000,951.853084,953.454375,948.643667,952.525041,953.555084,947.596292,948.954708
bend,queens,par-cpu,patched,942.498625,932.816708,929.930708,929.827833,932.326917,931.697250,957.504500,930.599583,930.915834,933.660083
bend,queens,par-gpu,baseline,2773.705041,2774.324500,2736.561666,2804.109125,2803.096709,2769.143375,2831.511875,2751.521833,2696.942416,2813.383958
bend,queens,par-gpu,patched,2723.946042,2701.490625,2642.495333,2589.854083,2700.868125,2672.513125,2751.748666,2617.401458,2621.553042,2652.645625
bend,raytrace,seq-cpu,baseline,4614.813083,4601.197917,4596.433833,4588.177208,4584.940000,4583.913542,4620.087417,4578.082875,4576.758542,4574.459333
bend,raytrace,seq-cpu,patched,4613.151041,4604.153834,4591.241666,4589.124459,4588.883917,4582.442542,4578.639083,4582.086875,4575.172791,4574.035250
bend,raytrace,par-cpu,baseline,809.742875,819.607917,809.746541,813.233125,813.260458,814.245834,812.737291,814.774333,844.312292,812.848167
bend,raytrace,par-cpu,patched,819.907834,810.353375,820.931291,813.033041,816.870042,813.306167,815.277208,811.087500,815.220375,817.876750
bend,raytrace,par-gpu,baseline,415.049416,407.628167,410.000792,407.908208,409.850958,405.043541,403.776250,407.433208,405.501666,422.242958
bend,raytrace,par-gpu,patched,411.518792,399.717834,409.568583,406.513500,415.154791,408.935542,408.161167,409.802500,398.631125,413.736875
bend,symreg,seq-cpu,baseline,3073.263583,3074.055167,3073.375750,3071.235333,3069.969292,3063.839959,3061.831209,3056.655625,3052.622042,3046.681292
bend,symreg,seq-cpu,patched,3110.879084,3108.655042,3108.594959,3108.790792,3103.805875,3098.667834,3092.218000,3091.196167,3121.305875,3083.379167
bend,symreg,par-cpu,baseline,533.558833,535.418250,536.971250,540.558041,535.580875,538.750125,539.307417,537.518417,533.394667,541.974209
bend,symreg,par-cpu,patched,535.917791,539.995584,539.521458,540.250625,538.670833,538.646292,540.114333,544.709542,539.020250,540.129125
bend,symreg,par-gpu,baseline,890.774458,892.173958,891.391833,891.070708,891.007125,892.430125,892.231666,891.983334,890.436833,837.293584
bend,symreg,par-gpu,patched,673.686667,673.667917,673.377750,673.387208,673.262750,672.998042,673.091667,672.899292,673.114375,673.439709
bend,terrain,seq-cpu,baseline,2195.768166,2195.375209,2196.133667,2195.047375,2196.679708,2202.536875,2194.917500,2196.371167,2194.434709,2196.346042
bend,terrain,seq-cpu,patched,2197.106500,2194.715625,2195.906166,2196.799292,2195.191958,2196.062959,2196.301042,2194.660875,2195.815083,2194.322959
bend,terrain,par-cpu,baseline,386.922667,390.528834,410.657833,387.018833,386.190583,391.312667,385.871208,396.174750,396.244958,391.369583
bend,terrain,par-cpu,patched,386.504958,391.975625,387.041000,386.286416,386.174333,391.860250,388.155750,390.922541,386.713042,386.181833
bend,terrain,par-gpu,baseline,233.386833,232.239084,233.600166,231.077125,231.949750,233.886917,235.036125,235.188750,233.044000,231.730459
bend,terrain,par-gpu,patched,230.849834,232.376708,232.065208,230.652250,232.094291,232.625000,232.298708,233.886375,232.646958,232.968500
bend,tree-bitonic,seq-cpu,baseline,5964.730833,5959.396084,5971.333792,5959.611333,5966.627833,5957.284167,5967.131958,5958.880833,5975.254834,5972.847000
bend,tree-bitonic,seq-cpu,patched,5915.576542,5922.557833,5934.302583,5928.517583,5949.473333,5936.058833,5941.683000,5924.386416,5924.829375,5921.484792
bend,tree-bitonic,par-cpu,baseline,1346.995125,1388.797250,1361.494750,1346.922417,1352.328583,1368.152084,1416.363542,1371.432750,1384.447084,1377.035708
bend,tree-bitonic,par-cpu,patched,1369.857334,1398.144292,1351.580209,1422.327084,1353.596250,1420.012958,1511.640334,1413.672167,1383.613209,1452.180875
bend,tree-bitonic,par-gpu,baseline,1792.939458,1761.062875,1773.095250,1780.885417,1798.358000,1768.416333,1779.654584,1797.214000,1808.397500,1749.121917
bend,tree-bitonic,par-gpu,patched,1775.933000,1758.417709,1782.399500,1770.731958,1816.460125,1824.138459,1797.872667,1772.217125,1781.153083,1776.806542
bend,tree-matmul,seq-cpu,baseline,2018.929792,2010.101708,2012.424375,2015.688750,2007.277625,2015.756250,2011.016208,2013.658291,2042.901125,2010.036917
bend,tree-matmul,seq-cpu,patched,2003.336792,2005.689375,2005.142917,2003.921542,2007.944750,2006.433042,2014.313292,2007.673792,2013.290000,2004.202708
bend,tree-matmul,par-cpu,baseline,506.261208,504.054625,519.957042,504.848375,510.265042,505.542083,507.975792,516.986958,512.083166,510.272042
bend,tree-matmul,par-cpu,patched,499.858958,493.342000,502.154250,498.608334,500.995875,499.789250,498.660625,503.934125,503.017084,516.951459
bend,tree-matmul,par-gpu,baseline,626.792333,633.173583,645.599833,622.462459,629.408125,640.795417,632.025625,623.085209,636.507292,622.323084
bend,tree-matmul,par-gpu,patched,632.822750,621.724125,636.598042,635.873458,624.853000,623.162750,633.189875,624.644583,624.123875,623.673417
bend,tree-radix,seq-cpu,baseline,3921.728667,3905.608375,3931.471000,3876.806917,3913.207000,3898.748167,3879.439250,3877.037500,3881.190333,3895.715334
bend,tree-radix,seq-cpu,patched,4003.868542,4017.021958,3991.835417,3990.771916,3973.553708,3986.550958,3974.729791,3975.040458,3980.491875,3988.803583
bend,tree-radix,par-cpu,baseline,838.007292,795.452542,769.337625,788.489416,823.031958,807.142250,792.702708,805.604375,778.693125,786.040958
bend,tree-radix,par-cpu,patched,829.331959,838.965792,791.349875,826.735875,864.567208,823.648792,819.263333,834.088459,818.930584,798.264541
bend,tree-radix,par-gpu,baseline,1165.430500,1163.019250,1167.636500,1166.225166,1172.126375,1165.492375,1164.422417,1165.568917,1165.873917,1164.604667
bend,tree-radix,par-gpu,patched,1164.287375,1163.322250,1163.272458,1163.357375,1163.808458,1166.146125,1149.273458,1167.011750,1164.248458,1163.078000
koru,digitsum,seq-cpu,baseline,27.709875,20.874792,20.813583,20.735042,20.894958,20.677750,20.695875,20.551833,20.648417,20.785083
koru,digitsum,seq-cpu,patched,21.393792,20.656625,20.623209,20.838166,20.676542,20.673916,20.863792,20.504750,20.659959,20.975459
koru,digitsum,par-cpu,baseline,23.969750,23.846042,23.984958,23.793375,24.374375,24.144125,23.970292,24.037667,23.850792,24.044583
koru,digitsum,par-cpu,patched,10.885375,11.358750,11.259250,11.488459,11.301250,11.233375,11.198709,11.329833,11.308583,10.925833
koru,binarytrees,seq-cpu,baseline,80.557959,81.668333,80.584166,78.388333,80.222500,79.906917,79.928458,77.811125,78.341667,77.717792
koru,binarytrees,seq-cpu,patched,76.631666,75.433958,75.233750,75.035625,74.887958,76.542375,74.880208,74.049792,75.092042,75.082584
koru,binarytrees,par-cpu,baseline,311.001417,307.424666,308.537292,303.023541,307.728459,302.533417,278.400000,275.160416,301.551583,309.270458
koru,binarytrees,par-cpu,patched,190.643750,193.429792,191.705000,191.222000,193.843375,198.129667,178.301000,165.409417,195.676292,191.031583

Validation

  • All 1,040 measured executions and their warmups matched the existing output oracles. Benchmark sources and hardware pins are unchanged.
  • Both new regression tests passed on the public-based branch with 1, 2, and 8 CPU workers and JS; the GPU regression passed on Metal. The repository gate passed 46 / 46, and the diff passed whitespace checks.
  • The final implementation previously passed 189 local CPU/JS checks across 48 cases, ten Metal tests, and instrumented checks that later forks and device requests still occur. The compiler, language, prelude, and CLI on this public base match that investigation's baseline.
  • Full cluster correctness/performance gates remain unverified because worker SSH authentication failed. CUDA was not tested locally.

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 55000002 and 19659600, 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.

# Bend 2 fork/join variant of the Osprey compute-kernel reference.
# 8-way range split via recursive psum; leaves run the sequential fold.
# caveat: @unsafe — same non-structural recursion as the bend/ port.

import Base

@unsafe
def digsum(+n: Nat, small: Bool) -> Nat:
  match small:
    case True{}:
      n
    case False{}:
      Nat.add(Nat.mod(n, 10n), digsum(Nat.div(n, 10n), Nat.is_lt(Nat.div(n, 10n), 10n)))

def go(+n: Nat, +i: Nat, acc: Nat) -> Nat:
  match n:
    case 0n:
      acc
    case 1n+p:
      go(p, Nat.sub(i, 1n), Nat.add(acc, digsum(i, Nat.is_lt(i, 10n))))

def psum(+k: Nat, +n: Nat, +i: Nat) -> Nat:
  match k:
    case 0n:
      go(n, i, 0n)
    case 1n+kp:
      a b = psum(kp, Nat.sub(n, Nat.div(n, 2n)), i)  psum(kp, Nat.div(n, 2n), Nat.sub(i, Nat.sub(n, Nat.div(n, 2n))))
      Nat.add(a, b)

def main() -> IO(Unit):
  r = psum(3n, Nat.mul(2000n, 1000n), Nat.mul(2000n, 1000n))
  IO.print(Nat.show(r))

binarytrees

Source: suites/osprey-compute-kernels/bend-par/binarytrees/binarytrees.bend.

# binarytrees — bend-par variant: `makep` forks the two subtree builds
# only above a depth cutoff (`cut`), falling back to sequential `make`
# below it — naive full-fork measured ~4x SLOWER (Tree values crossing
# the fork boundary). cut=4.

import Base

type Tree is Data:
  Leaf{}
  Node{l: Tree, r: Tree}

def make(+d: Nat) -> Tree:
  match d:
    case 0n:
      Node{Leaf{}, Leaf{}}
    case 1n+p:
      Node{make(p), make(p)}

def makep(+cut: Nat, +d: Nat) -> Tree:
  match cut:
    case 0n:
      make(d)
    case 1n+cp:
      match d:
        case 0n:
          Node{Leaf{}, Leaf{}}
        case 1n+p:
          l r = makep(cp, p) makep(cp, p)
          Node{l, r}

def check(t: Tree) -> Nat:
  match t:
    case Leaf{}:
      0n
    case Node{l, r}:
      Nat.add(1n, Nat.add(check(l), check(r)))

def go(+i: Nat, acc: Nat) -> Nat:
  match i:
    case 0n:
      acc
    case 1n+p:
      go(p, Nat.add(acc, check(makep(4n, 13n))))

def main() -> IO(Unit):
  IO.print(Nat.show(go(1200n, 0n)))

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
MIT License

Copyright (c) 2026 korulang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Note: the vendored reference corpus under
suites/*/reference/ retains its own upstream license; see the adjacent
LICENSE.osprey and PROVENANCE.md.
Osprey MIT notice
# Released under MIT License

Copyright (c) 2025 Christian Findlay (ChristianFindlay.com).

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

VictorTaelin added a commit that referenced this pull request Sep 20, 2026
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)
VictorTaelin added a commit that referenced this pull request Sep 20, 2026
…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)
VictorTaelin added a commit that referenced this pull request Sep 20, 2026
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)
@VictorTaelin

Copy link
Copy Markdown
Contributor

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.

VictorTaelin added a commit that referenced this pull request Sep 21, 2026
…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).
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
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)
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…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)
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
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)
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…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).
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.

2 participants