Skip to content

A native is a call on the device when it loops and is long - #1102

Closed
wakamex wants to merge 1 commit into
bendlang:mainfrom
wakamex:spin-loop-rule-2.0.29
Closed

wakamex wants to merge 1 commit into
bendlang:mainfrom
wakamex:spin-loop-rule-2.0.29

Conversation

@wakamex

@wakamex wakamex commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

I found this with bend-bench, comparing 2.0.3 against 2.0.26 in alternating runs: ray tracing on the GPU got 38% slower.

The cause was the inline rule. Natives were FAR (noinline) when their body reached SPIN_FAR (256) lines, else INLINE. 6e16f4e folded a let into constants and raytrace's bounce loop fell from 357 to 258 lines, under the bar. It became INLINE, and since its only call site sits in the per-sample shader, which is itself inlined at four sites, the device program carried four copies of the loop. Raytrace on CUDA went from 0.94 s to 1.33 s.

Now a native is FAR only when it loops (seg.spin, which emit_jump already sets) and has SPIN_FAR lines, with SPIN_FAR 200. A loop-free native inlines at any size. FAR is noinline on the devices and plain static on the host, where clang decides (the old noinline on raytrace's per-cell loops cost 23-31% on the CPU, which the header note recorded).

I tried making every loop FAR first, which is shorter and gets raytrace to 1.9x, but a noinline call raises the kernel's register count (editdist's bend_dev 62 -> 80, with 96 bytes of stack) and cuts occupancy: editdist lost 9% and merkle 5%. Capping registers at 64 fixed both but cost queens 30%. Keeping short loops inline avoids all of that.

comp.ts 62584 -> 62579 ttok, +8 -7 lines, on 2.0.29.

Measured with bend-bench gpu fast (profile fast-gpu-v2) on an RTX 3090, CUDA 13.1, 4GB heap, 3 measured runs per workload after a correctness check and warmup, 574b6d3 and this branch alternating three times:

Round 1 Round 2 Round 3
Geomean over 21 workloads 0.994x 1.027x 1.018x
Ray tracing 1343 -> 977 ms 1363 -> 957 ms 1340 -> 964 ms

Device code only changes for raytrace (the bounce loop is a call again) and game-search (an 8199-line loop-free native now inlines). Game-search alone over 11 alternating runs: 378 -> 376 ms, cold NVRTC build 1.94 -> 2.00 s. Every other workload's generated C only differs in where FAR is defined, so their differences are noise. Compact UTS fails on both (the known ERR_DEEP overflow).

Local C lane of tests/ (1478 tests with a #| expectation) matches 574b6d3 exactly: 650 pass, 11 wrong, 817 build-fail in this setup. Metal is unmeasured, I don't have Apple hardware.

The inline rule was a line count on every native: SPIN_FAR (256) body lines
or more was FAR (noinline), else INLINE. 6e16f4e folded a let into
constants and raytrace's bounce loop fell from 357 to 258 lines, under the
bar. It became INLINE, and since its one call site sits in the per-sample
shader, itself inlined at four sites, the device program carried four copies
of the loop and raytrace on CUDA ran 0.94 -> 1.33 s.

Now a native is FAR only when its segment spins (seg.spin, which emit_jump
already sets) and has SPIN_FAR lines, with SPIN_FAR 200. A loop-free native
inlines at any size. FAR is noinline on Metal and CUDA and plain static on
the host, where clang decides (on the host the old noinline on raytrace's
per-cell loops cost 23-31%, which the header note recorded).

200 rather than every loop: a noinline call raises the device kernel's
register count (editdist's bend_dev 62 -> 80, 96 bytes of stack), which
cuts occupancy, so making the short loops of editdist and merkle calls cost
them 9% and 5%. Capping registers at 64 recovered both but cost queens 30%.

comp.ts 62584 -> 62579 ttok, +8 -7 lines.

Measured with bend-bench gpu fast, profile fast-gpu-v2 (RTX 3090, CUDA 13.1,
clang, 4GB heap, 3 measured runs per workload after a correctness check and
warmup), base 574b6d3 and this commit alternating three times:
  geomean 0.994x, 1.027x and 1.018x over 21 matched workloads;
  raytrace 1343 -> 977 ms, 1363 -> 957 ms, 1340 -> 964 ms (1.39x).
Device code changes only for raytrace (its 258-line bounce loop is a call
again) and game-search (an 8199-line loop-free native now inlines): over 11
alternating runs game-search took 378 -> 376 ms, and its cold NVRTC build
1.94 -> 2.00 s. Every other workload's generated C differs only in where FAR
is defined. Compact UTS fails on both (the known ERR_DEEP overflow).
Local C lane of tests/ (1478 tests with a #| expectation): identical status
to 574b6d3 (650 pass, 11 wrong, 817 build-fail). Metal is unmeasured: no
Apple hardware here.
@VictorTaelin

Copy link
Copy Markdown
Contributor

Thank you for the measurements. We are not taking this rule: it tunes a line-count constant on one CUDA bench and also drops noinline from term_drop on the host. The cause is copies times size: a native inlined into a shader that is itself inlined at several sites. The rule we want counts inline copies. Closing for now.

@wakamex

wakamex commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look! I missed that FAR also covers term_drop. Copies times size sounds like the principled way to go. Interested in a PR that counts inline copies even if it grows comp.ts a bit? I'll start benchmarking.

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