Skip to content

A spin is a call on the device when its C is long, by characters not lines, and inline on the host - #1413

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
nicolas-abril:device-loop-spins
Oct 8, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
nicolas-abril:device-loop-spins

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

emit_native chose a spin's qualifier by its line count: under SPIN_FAR (256) lines it was INLINE, else FAR, on the host and the device alike. Lines are a fragile measure: #834 changed how template keys print, raytrace's main loop (spin_18) folded into fewer, longer lines (358 → 259, 217 on main) with about the same code, crossed under 256 and became INLINE. Metal inlines everything that is not noinline into the one bend_dev kernel, so its back end (library → binary archive) went from ~120 to ~220 ms, and raytrace's COMPILER cell sat at 1.15–1.16x its pin, failing the perf gate on most runs.

The rule is now:

  • Device: a spin whose C is SPIN_FAR (8192) characters or more is a call (DFAR, FAR on the device); a shorter one is inlined. Characters do not change when the emitter folds code onto fewer lines.
  • Host: every spin is INLINE, so clang decides. A forced call there never saved clang time and cost speed (raytrace lost 31% on PAR-CPU at a threshold of 128 lines).

Where 8192 comes from

Each constraint below was measured on the minis (macOS 26.5.2) or locally, and every one sits on its side of 8 KB:

spin size must be measured
lexer's loop 0.9 KB inline a call: +3% on the GPU (minis)
histogram's two straight-line spins 5.2 KB inline calls: +23% on the GPU (minis)
big loops in raytrace, merkle, nbody, kmeans 9–15 KB call raytrace: −0.09 s build, −11% GPU; the rest unchanged
a 331-line straight-line native at 8 call sites under a ! 14 KB call inlined: Metal back end 56 → 369 ms
hvm5's natives under a ! (the case SPIN_FAR was added for, in 386b7fc) 459–608 lines call inlined: 32 s of Metal compile, against 2.6 s

Two alternatives that were measured and rejected:

  • Every looping spin a device call (this PR's first commit): fixes raytrace, but lexer's small loop costs 3% on the minis, and a long straight-line native under a ! is inlined again (the hvm5 case).
  • A 4 KB threshold: histogram loses 23% on the GPU.

No straight-line spin in bench/, demos/ or tests/ that the device reaches is 8 KB or more today; the largest is kmeans' at 7.4 KB.

Same device calls elsewhere

  • In every GPU demo and test (21 programs with a !), the device makes exactly the calls it made on main, including slash_boss_3d's two long loops.
  • The only new device calls are the long loops of raytrace, merkle and nbody (kmeans' was already FAR).
  • On the host, all 10 files with a FAR spin on main (slash_boss_3d's 3 MB of C, hvm5_mini's 1 MB among them) take the same clang time with it INLINE.

Perf gate (cluster)

main (3 runs) this PR (3 runs)
verdict 121, 124, 123 / 124 123, 124, 124 / 124
raytrace COMPILER 0.749–0.755 s (1.15–1.16x) 0.666 s median (1.02x)
raytrace PAR-GPU 0.326 s median 0.284 s median
geomean COMPILER / SEQ / PAR / GPU (medians) 0.99 / 1.00 / 1.00 / 0.99 of main

The one miss on this PR is mandelbrot PAR-GPU at 1.17x, which main misses too (1.20x). Every other cell is within ±4% of main, which is the spread of cells whose code did not change. Expect a COMPILER cell or two to read slow on the first runs after the merge: the minis' Metal shader cache is cold for new C (tree-radix read 0.67 s for three runs and 0.50 s on the fourth, during this work).

Checks

  • Gates on the branch: repo 50 / 50, test 1587 / 1587, ping 46 / 46.
  • Every variant printed the same output as main on every bench.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits October 8, 2026 08:48
…place of SPIN_FAR's line count

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…and inline on the host; a short loop stays inline

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril nicolas-abril changed the title A spin that loops is a call on the device and inline on the host, in place of SPIN_FAR's line count A spin is a call on the device when its C is long, by characters not lines, and inline on the host Oct 8, 2026
@nicolas-abril
nicolas-abril marked this pull request as ready for review October 8, 2026 08:09
@nicolas-abril
nicolas-abril merged commit 323f249 into bendlang:main Oct 8, 2026
1 check passed
@wakamex

wakamex commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Nice to see DFAR and the character-based SPIN_FAR land. I re-ran bend-bench paired on the RTX 3090 against current main: #1413 captures 1.53x of #1282's 1.87x CUDA speedup on raytrace by putting spin_18 (11.9 KB) back over SPIN_FAR, but still leaves a 1.22x speedup on raytrace (95% interval 1.20x to 1.24x) and 1.12x on editdist (1.12x to 1.14x) from the looping spins under 8 KB (spin_13 at 3.0 KB, spin_16 at 3.4 KB, and editdist's 0.7 to 0.9 KB loops).

Even if M4 Metal barely feels that divergence, most GPU compute runs on CUDA, where inlining multiple copies of a variable-trip loop serializes the warp. With DFAR already in main and the quadratic map bug fixed, the zero-magic-number rule from #1282 (sharing a spin that has a loop calling spins when one caller runs it twice) drops from +325 to +174 ttok (63,495 -> 63,669, well under the 64k cap) and changes no other benchmark's code.

If you prefer a size threshold like SPIN_FAR, seg.spin ? 2950 : SPIN_FAR (+13 ttok) gets the 1.22x on raytrace, but a size cutoff can't also get the 1.12x on editdist: lowering it to 0.7 KB to catch editdist would also turn mandelbrot's 2.6 KB loop and lexer's 0.9 KB loop into calls, slowing both down. This shows the downside of using magic numbers: they can't optimize all use-cases. Happy to send a PR with either one if you want the CUDA speedup.

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