Skip to content

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

Closed

Description

@nicolas-abril

What

emit_native marks a spin INLINE when its segment has fewer than SPIN_FAR (256) lines, and FAR (static __attribute__((noinline))) otherwise:

fl.spins.push({ ...seg, lines: [`${seg.lines.length < SPIN_FAR
  ? "INLINE" : "FAR"} Term ${name}(...

The constant's comment says that at 128, raytrace lost 31% on PAR-CPU. Two spins on the benches that are under 256 lines run faster as FAR:

  • nbody spin_4: 169 lines in the C, one call site, called with 24 arguments. Faster FAR on both CPU modes.
  • raytrace spin_18: 261 lines in the C, one call site. Faster FAR on Metal but a little slower on the CPU.

That raytrace split means no single threshold can be right for both: the C file is the same for every lane, and FAR means noinline on the host, on Metal and on CUDA.

Numbers

These are from an Apple M4 (4P + 6E), on main at 0070d08f, built with gates/perf.ts's cc lines. Each cell is the median of 5 runs, with INLINE and FAR interleaved, and every output matches.

bench mode INLINE FAR speedup
nbody SEQ-CPU 5.817 s 4.929 s 1.18x
nbody PAR-CPU (16) 0.878 s 0.763 s 1.15x
nbody PAR-GPU 0.167 s 0.165 s 1.01x
raytrace SEQ-CPU 4.588 s 4.662 s 0.98x
raytrace PAR-CPU (16) 0.813 s 0.828 s 0.98x
raytrace PAR-GPU 0.713 s 0.578 s 1.23x

The GPU times here are higher than the apple_m4 pins (raytrace's pin is 0.395 s), because GPU times on this machine moved between sessions. The ratios come from interleaved runs in one session.

Reproduce

cd bench/runtime/nbody
bend main.bend -o main.c
sed 's/^INLINE Term spin_4(/FAR Term spin_4(/' main.c > far.c
cc -std=c11 -O3 main.c -lpthread -o inline
cc -std=c11 -O3 far.c -lpthread -o far
./inline --threads 1 --gpu off; ./far --threads 1 --gpu off

For raytrace, use spin_18, and the Metal build line -DBEND_METAL=1 -x objective-c -fobjc-arc ... -framework Metal -framework Foundation with --gpu 512MB.

How this was found

While measuring alternatives for #1123 (see #1239), emitting a temporary for every primitive result added enough lines to push these spins, and merkle's, past 256. That variant ran nbody 10% faster and raytrace on Metal 18% faster. Forcing only the FAR flip in main's C gets the whole gain, so it comes from the inline decision, not from the temporaries.

Open questions

  • Why FAR wins here: not established. One guess is register pressure: a spin inlined into a caller that holds 24 live words shares its register allocation and spills in the loop. The disassembly would show it.
  • A better rule: possibly one that also weighs what the call site holds live, or a lane-specific decision (FAR only under DEVICE, or a separate threshold for the device), instead of a single line count. Any change needs gates/perf.ts on all 48 cells, since the 128 → 256 change was tuned on raytrace PAR-CPU.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions