Repository navigation
A long loop over F32 is a call on the host too: nbody 1.19x SEQ, 1.15x PAR (#1240) - #1814
Open
nicolas-abril wants to merge 2 commits into
Open
nicolas-abril wants to merge 2 commits into
nicolas-abril wants to merge 2 commits into
Conversation
Lorenzobattistela
approved these changes
Oct 21, 2026
…x PAR (#1240) A spin of SPIN_FAR characters or more was a call on the device (DFAR) and inline on the host. Inlined, clang's SLP vectorizer packs a float loop's state as its caller's code suits: nbody's integration loop (spin_4, 21 F32 words) took its 4-lane PRNG setup's vectors into the loop and spent each step shuffling lanes. As a call, the state arrives as scalars and SLP judges the loop by its own body (here, it leaves it scalar). So a long spin that loops with an F32 among its live parameters is now FAR on both lanes; on the device FAR is what DFAR already was. Four spins change, in nbody, raytrace (spin_18) and app_slash_boss_3d (spin_39, spin_53); every other program's C is byte-identical to main. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
nicolas-abril
force-pushed
the
slp-float-loops
branch
from
October 22, 2026 14:54
f37326e to
afb3b05
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1240. #1413 already fixed that issue's GPU half: raytrace's and nbody's long loops are calls on the device. This PR fixes the half that remained, nbody on the CPU.
The bug
On the host every spin is
INLINE. nbody's integration loop (spin_4: 15 KB, 21 F32 loop-carried words, one call site) is inlined intoWL_FID_batch, whose setup computes four PRNG streams in 4-lane vectors. clang's SLP vectorizer carries those vectors into the loop: in the inlined build, 233 of the function's 571 instructions are vector or lane-shuffle ops, and two of the three pair kernels run asfsqrt.2s/fdiv.2s. As a separate function, the state arrives as 22 scalar arguments, SLP leaves the loop scalar (0 vector instructions in 313), and it runs 18% faster.SLP is the whole difference. With
-fno-slp-vectorize, the inlined build runs as fast as the call: 4.880 s against 4.840 s, and 5.746 s with SLP on. Register pressure, the guess in #1240, isn't the cause.The fix
A spin of
SPIN_FARcharacters or more that loops (seg.spin) with an F32 among its live parameters isFAR, a call on both lanes. On the deviceFARis whatDFARalready was, so device programs don't change.Across the 1,729 benches, demos and tests, 4 spins change, in nbody (
spin_4), raytrace (spin_18) andapp_slash_boss_3d(spin_39,spin_53). The emitted C of every other program is byte-identical to main, and every program builds or fails as it does on main.Numbers
Apple M4, built with
gates/perf.ts's cc lines, each cell the median of 5 interleaved runs, outputs identical:gates/perf.ts, alternating with main (4 runs each):err(exit 127or an empty build error) on both main and this branch, in the same bench/mode pairs, nbody SEQ-CPU among them every time. That looks like a cluster problem, not this change.bend -otime doesn't change:app_slash_boss_3dtakes 9.59–9.84 s on main and 9.59–9.60 s here.Alternatives measured and rejected
All 17 benches, SEQ-CPU and PAR-CPU (16):
-fno-slp-vectorizeFARfor every loop with F32 stateFARfor every loopFARrules and the barrier: raytrace'sspin_13andspin_16are 8-iteration F32 loops called once per ray. Inlined, clang unrolls and specializes them at each call site. Behind a call, or behind a barrier, it can't, and raytrace loses 25%. RequiringSPIN_FARkeeps them inline; only long loops become calls.-fno-slp-vectorize: matches this PR on nbody, but turns SLP off for every program. On synthetic loops with independent float lanes, SLP gains up to 1.48x as a call (8 lanes) and 1.18x inlined (4 lanes, as complex pairs). This PR keeps SLP on, and lets a long loop's vectorization depend on the loop body alone.Checks
comp.tsis 65,690 ttok (main 65,612, cap 66,000).Not run: Linux/x86, CUDA hosts.
app_slash_boss_3dbuilds but is interactive, so its two changed spins aren't timed.🤖 Generated with Claude Code