Repository navigation
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
Closed
nicolas-abril wants to merge 1 commit into
nicolas-abril wants to merge 1 commit into
Conversation
…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]>
Collaborator
Author
|
Moved to nicolas-abril#13, which keeps it saved off the main queue until it can be accepted. |
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.
comp.tsis 66,162 ttok, 162 over the 66,000 cap (main011b54d3is at 65,962), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. Independent of #2135 and #2137.What it does
clang inlines a
staticfunction 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.gameoflifeis the case in the suite:runloops overstep(16step_cells once clang has folded them in),classifycallssteptwice more, andstepis left out of line. Its C twin marksboard_stepalways_inlineby hand.emit_spinnow marks such a call for clang to inline at that one site:loop_of), and the callee is a flat def that does not loop itself, has more than one call site (FL.sites) and is overHOT_SIZE(1024) terms, counting the flat defs it calls. The other call sites of the callee are left to clang.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.bfsran 1.40x the instructions (pop_cellfell out of line) andgameoflifeitself 1.25x the cycles on 0.75x the instructions (runfell out of line, and clang vectorised thestepinside 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, andstepis 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.gameoflifeThree 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/runtimeprograms emit the same C but for the unusedHOTdefinition in the runtime, and compile to byte-identical object code (otool -tofcc -O3 -c), so they were not timed.demos/app_slash_boss_3dis 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__TEXTgrows 0.45%. The other 14 demos that emit C differ only in theHOTdefinition.Checks
All local, against main
011b54d3:HOTdefinition and theHOTmarks are taken out, and the JS is byte-identical. None of the 882 has a marked call; the new test has two.gameoflifebuilt with-DBEND_POISONand a count of live heap words at exit: the same output and the same count as main on every one.tests/run/loop_call_inline.bendhas 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 andbend file.bend; here its C has the two marked calls.HOTis empty, so the build is main's.tsc5.8.3 (the machine's, not the repo's pin) reports the same 6 errors as on main (3 inbend.ts, 3 incomp.ts), none new.repo.ts: 55 / 56, failing only on the ttok cap.Not covered
gameoflifewas 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.queens'solve,symreg'seval) are not loops forloop_of, so their calls are not marked.FARruntime helper included. The arguments are locals in every marked call of the benches and demos.HOT_SIZEis 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