Skip to content

A loop's call to a large def with several call sites is inlined into the loop - #2152

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/hot-loop-calls
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/hot-loop-calls

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

comp.ts is 66,162 ttok, 162 over the 66,000 cap (main 011b54d3 is at 65,962), so repo-gate fails until that is settled. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run. Independent of #2135 and #2137.

What it does

clang inlines a static function that has one caller whatever its size, but one with several callers only while it is under clang's size threshold. So a large helper that a hot loop calls, and that something else calls too, stays a real call on every iteration. gameoflife is the case in the suite: run loops over step (16 step_cells once clang has folded them in), classify calls step twice more, and step is left out of line. Its C twin marks board_step always_inline by hand.

emit_spin now marks such a call for clang to inline at that one site:

  • Where: the caller is in a tail-call loop (loop_of), and the callee is a flat def that does not loop itself, has more than one call site (FL.sites) and is over HOT_SIZE (1024) terms, counting the flat defs it calls. The other call sites of the callee are left to clang.
  • How: the call statement gets HOT, which is [[clang::always_inline]] on the host under clang 17 or newer (the first to read [[...]] attributes in C11) and nothing on the device or under an older clang.
  • Why a size floor: forcing every call in a loop was measured and rejected. The forced calls are inlined before clang's own inliner runs, which changes what it does next: bfs ran 1.40x the instructions (pop_cell fell out of line) and gameoflife itself 1.25x the cycles on 0.75x the instructions (run fell out of line, and clang vectorised the step inside it). clang inlines small and single-site callees on its own, so the mark is kept for the ones it would not. Across the suite every multi-site callee in a loop is at most 544 terms and inlined by clang already, and step is 13,630.

Results

One thread, GPU off, one M4, cc -std=c11 -O3. The machine was shared with other benchmark runs, so the table gives instructions retired (stable) and the least cycles and wall time of 3 interleaved runs, from the calmest batch.

gameoflife instructions cycles wall
main 172.35G 31.81G 8.08 s
this 145.63G 27.85G 7.07 s 1.14x
C twin 158.61G 27.70G 7.03 s

Three batches under heavier load gave 1.12x, 1.14x and 1.16x in cycles (35.14G to 31.50G, 33.50G to 29.48G, 36.01G to 31.03G); the instruction counts do not move between batches. On 8 threads the least of 5 runs went from 37.41G to 31.67G cycles (1.18x) and from 2.60 s to 2.21 s of wall time.

The other 16 bench/runtime programs emit the same C but for the unused HOT definition in the runtime, and compile to byte-identical object code (otool -t of cc -O3 -c), so they were not timed.

demos/app_slash_boss_3d is the one demo with marked calls (18 sites, 7 callees): its build takes the same time (13.7 s and 13.9 s of CPU on main, 14.3 s and 13.9 s here) and its __TEXT grows 0.45%. The other 14 demos that emit C differ only in the HOT definition.

Checks

All local, against main 011b54d3:

  • Emitted code, every test: for each of the 882 tests that build and pass on main, and the new one, the C emitted here equals main's once the HOT definition and the HOT marks are taken out, and the JS is byte-identical. None of the 882 has a marked call; the new test has two.
  • Native lane: all 1,869 test files end as they do on main (882 pass, the GPU off).
  • Memory: each of the 882, the new test and gameoflife built with -DBEND_POISON and a count of live heap words at exit: the same output and the same count as main on every one.
  • New test: tests/run/loop_call_inline.bend has a loop over a large def that two other sites call. It prints the same number on the C lane (main and here), the JS lane and bend file.bend; here its C has the two marked calls.
  • Older clang: not run. Under clang 14 to 16 HOT is empty, so the build is main's.
  • tsc 5.8.3 (the machine's, not the repo's pin) reports the same 6 errors as on main (3 in bend.ts, 3 in comp.ts), none new.
  • repo.ts: 55 / 56, failing only on the ttok cap.

Not covered

  • Cluster gates, CUDA have not run. Metal: gameoflife was built through the CLI (the host as Objective-C, the device program by --gpu-build) and prints its checksum on the GPU; nothing else was run there.
  • Callers that recurse without a tail call (queens' solve, symreg's eval) are not loops for loop_of, so their calls are not marked.
  • A call two defs below the loop (the loop calls a small def that calls the large one) is not marked.
  • Anything else called inside the marked statement is forced inline too, a FAR runtime helper included. The arguments are locals in every marked call of the benches and demos.
  • HOT_SIZE is one number set from this suite: twice the largest callee clang already inlines (544), well under the one it does not (13,630). No program between the two was measured.

Cost

+200 ttok (two conditions, the size walk, the macro and its comments). Getting under the cap needs that many tokens cut elsewhere in comp.ts, or a raise.

🤖 Generated with Claude Code

…nlined into the loop

clang inlines a static function with one caller whatever its size, but
one with several callers only under its size threshold, so a large
helper called from a hot loop and from elsewhere stays a call per
iteration. emit_spin now marks such a call with a statement attribute
(HOT, [[clang::always_inline]] on the host with clang 17+, nothing on
the device): the caller is in a tail-call loop, the callee is flat,
does not loop, has more than one call site and is over HOT_SIZE (1024)
terms with its flat callees included.

gameoflife, one thread: 172.4G to 145.7G instructions retired. The other
16 benches compile to the same object code.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Nov 22, 2026
…the node, inline take and wrap, recursive drop, lent flat defs

With e34e9ce (the use-after-free fix for an erased field). Conflicts, all
in comp.ts:
- emit_args (three hunks; bendlang#2207 against bendlang#2135): bendlang#2135 added `inl` (an
  inlined callee roots its borrowed parameters) and `slot` (an Array slot
  passed as a variable is owned); bendlang#2207 added `lend` (a flat def that does
  not loop is lent a box its caller still uses) and makes an owned
  argument's words the ones val_own answers (term_keep of a lent word).
  Both kept: the parameters are (jump, fork, inl, lend); an owned argument
  takes val_own's words except when inlined, where bendlang#2135 leaves it to the
  callee's body, as before. lend[i] is only computed for an argument that
  is not borrowed, so it never meets a slot or a borrowed root.
- emit_spin (bendlang#2207 against bendlang#2149, bendlang#2152, bendlang#2154, bendlang#2135): the in-place
  helper (bendlang#2149's ix_inl) passes no lend array, since it has no lent copy;
  the out-of-line spin computes lend and calls its lent copy, and keeps
  bendlang#2152's HOT prefix, bendlang#2154's lanes (which refuse a def with a box
  parameter, so never meet a lent copy) and bendlang#2135's ret_root.
- emit_open / emit_native (bendlang#2207 against bendlang#2135 and bendlang#2149, bendlang#2154): bendlang#2135's
  def_vals now takes lend (a lent parameter's rent is "+", before the
  borrowed key); emit_native is (sc, k, to?, lend), its unit key and name
  carry the lent positions as bendlang#2207 has them, and every one of bendlang#2149's
  passes opens the lent copy the same way (74e27d5's clear stays first).
- the C macros after FAR: bendlang#2152's HOT and bendlang#2207's NEAR, both kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#13, which keeps it saved off the main queue until it can be accepted.

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.

1 participant