Skip to content

Pick host-FAR loops by the F32 math in their body (follow-up to #1814) - #1950

Merged
nicolas-abril merged 1 commit into
mainfrom
slp-float-body
Oct 29, 2026
Merged

nicolas-abril merged 1 commit into
mainfrom
slp-float-body

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Follow-up to #1814, which merged before this review point was addressed.

The change

#1814 made a long loop (SPIN_FAR chars, seg.spin) a FAR call on the host when one of its live parameters has top-level type F32 (ty_adt(A)?.k === "F32"). The reason is clang's SLP vectorizer. That type test misses:

  • F32 inside a record or tuple (pos: Vec3), which still reaches the loop as separate 32-bit words;
  • a generic parameter (x: T) used at F32, since ty_adt of a type variable is null.

SLP packs float arithmetic, not parameter types. This PR checks the loop's emitted C body for F32 ops instead (f32_unbox( / f32_rewrap(, which every F32 op emits). That catches both cases above, because the body is fully concrete, and it skips a loop that only passes an F32 along without doing math on it. I also updated the comment on SPIN_FAR and the runtime comment above DFAR, which still said "The host lets clang decide".

Checks

  • Emitted C: all 1,867 .bend files under bench/runtime, demos and tests, compiled with -o x.c on main and on this branch. Exit codes match (916 build, 951 fail, as on main). Every emitted file is byte-identical except the runtime comment line. The host-FAR loops are still nbody's, raytrace's and one in app_slash_boss_3d, so A long loop over F32 is a call on the host too: nbody 1.19x SEQ, 1.15x PAR (#1240) #1814's numbers still hold.

  • Gates: repo 54/54, test 1783/1783. Ping is 80/81, and main fails the same case (a BendHub publish of lic_bom--name.bend, which the local hub refuses). Perf is 123/124, and main shows the same: merkle's COMPILER cell sits at 1.14–1.16x on both, and main also went 123/124 in one run.

  • Perf, alternating main and this branch (2 runs each):

    cell main this PR
    nbody SEQ-CPU 4.819 / 4.817 s 4.818 / 4.822 s
    nbody PAR-CPU 0.712 / 0.710 s 0.711 / 0.711 s
    raytrace SEQ-CPU 4.160 / 4.162 s 4.160 / 4.163 s
    raytrace PAR-CPU 0.700 / 0.703 s 0.701 / 0.700 s

    No cell moves beyond run-to-run noise.

  • Size: comp.ts is 65,917 ttok (main 65,907, cap 66,000).

🤖 Generated with Claude Code

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 29, 2026
#1814 made a long loop FAR on the host when a live parameter's top-level
type is F32, which missed F32 held in a record or tuple and a generic
parameter used at F32. SLP packs float arithmetic, so test the loop's
emitted body for F32 ops instead: it sees every such case, at the same
cost. The emitted C of all 1,867 benches, demos and tests is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit 9d514f3 into main Oct 29, 2026
1 check passed
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Merged onto main 5c4d23e: repo 54/54, test 1783/1783, perf 124/124, ping 81/81, safe 0 false reject. Emitted C for the existing corpus matches main, as you said. The regex disagrees with main's type test in two directions, though, and one of them undoes part of #1814:

  1. Float math in a nested loop is missed. The regex only reads the long spin's own text. If the F32 work sits in a short recursive def that the long loop calls, the long body contains spin_step(e, ...) and no f32_unbox(. Repro: outer(s: Nat, +k: Nat, +a: F32, +b: F32, +h: U32) runs 300 prng steps (to pass SPIN_FAR), then recurses with step(k, a, b), where step loops F32.add(F32.mul(a, b), b). spin_outer is FAR on main and DFAR on this branch, so clang can inline the float loop into the long one again (SPIN_FAR inlines a spin by its line count alone: one spin made FAR runs nbody 1.18x faster on the CPU and raytrace 1.23x faster on Metal #1240's setup).

  2. Conversions and compares count as math. U32.to_f32 emits f32_rewrap((f32)...) and F32 compares emit f32_unbox. A long U32-only loop whose only float use is F32.to_u32(U32.to_f32(h)) goes from DFAR on main to FAR here: noinline with no SLP reason.

  3. The check is tied to the spelling of the INTR templates (~171-193). If they are renamed (as in refactor(comp): name clz and popcount after Metal's builtins #1906), or a new F32 op skips f32_unbox/f32_rewrap (a32_fadd already does: it's a call, and the math is in the A32_LOOP macro), nbody's loop silently drops back to DFAR, and no gate or pin notices.

Suggestion: mark the segment when emit_intr emits an F32 arithmetic op (not conversions or compares), and OR that mark up from nested spins. This fixes 1 and 2 and drops the dependence on the text. A small test or pin that keeps nbody's loop FAR would guard #1814's gain.

Nits: the header comment ("if it loops doing F32 math") should match whatever the check ends up doing, and "The host lets clang decide, but for a long F32 loop" reads better as "except for".

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