Skip to content

Keep the loops' helpers within the inliner's budget for a big function - #57

Closed
folbricht wants to merge 2 commits into
mainfrom
helper-size
Closed

folbricht wants to merge 2 commits into
mainfrom
helper-size

Conversation

@folbricht

@folbricht folbricht commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

fast and checkedLoop are just under the compiler's size for a "big" function (5000 IR nodes). Past it, the inliner takes only callees costing up to 20, and ten helpers the loops use cost more. Growing the loop past it would turn them into calls and spill the loop's state on every instruction. This PR brings every helper under the budget, and makes asmgate enforce it.

Helpers

Helper Cost before Cost after How
setInt, setBool 25 17 address the tag directly instead of calling tInt()
setFloat 30 18 the float tag at offset 0 of the tag struct, which the inliner charges nothing for
isNum 22 12 tags addressed directly
toFloat 24 20 tags addressed directly
strEqBytes 28 19 takes the two pointers, ranges over n
wholeIndex 38 18 the caller converts the float; drops the lower-bound test, which can never matter
checkedCaller 29 19 tests only the sentinels; a nil pointer with a function's n still panics
strWords, hashShort 61, 77 written out in GETTABLE's probe two multiply-mixes can't fit in 20
  • The type tags become fields of one struct, struct{ float, boolean, integer, unused byte }, so isScalar stays one range check.
  • hashValue mixes in each tag's offset. Float keys now get 0 instead of 3; integer keys keep 2, which the loop's inline hash of integer keys hard-codes.
  • TestShortKeysHashInline covers the written-out hash. It looks up keys of every length up to 17, each built behind a pointer of its own. A copy that hashes differently probes the wrong nodes and reads nil: I mutated one constant in the copy to check, and the test failed.
  • wholeIndex keeps its early-return shape. Returning the index together with the result of the test, or testing in the caller, costs GETTABLE one extra stack reload on amd64.

asmgate check

asmgate now builds the package with -m=2 twice: once as it is, and once through an -overlay that pads both loops past the limit. The padding goes on the line of each opening brace, so no line numbers move. It fails on any call the loops inline only in the first build. Against main it lists all of the helpers above.

Results

  • With both loops padded past the limit, they inline exactly what they inline now and make no calls beyond the allowed ones.
  • asmgate's stack-access counts match the baseline for every opcode on amd64-v1, amd64-v3 and arm64.
  • Instruction counts with the GC off, against main: sieve +1.1% on the fast loop and +1.2% on the checked loop; fannkuch -0.1% and +1.2%; fib and fieldread 0.0% on the fast loop. Building each change on its own, for sieve and fannkuch, shows that the new wholeIndex accounts for all of it; the tags, the written-out hash and strEqBytes cost 0.04% or less.

Not needed now

  • Neither loop is over the limit, and option A (Keep array parts that hold only numbers and booleans out of the GC's sight #58) does not need this PR: it passes asmgate on main.
  • It uses up headroom. The number of filler statements that fit before the loop becomes big falls from 47 to 21 in fast, and from 77 to 52 in checkedLoop. How much of that each change costs was not measured.
  • It is worth bringing back if a change with a measured gain takes a loop over the limit, without the wholeIndex change or with one that costs nothing.

Corrections

  • An earlier version of this description put sieve's 1% down to code layout. That was never tested and is wrong: it is wholeIndex.
  • The first commit's message says the generated code is unchanged and that instruction counts are within noise. Neither is right: only asmgate's counts are unchanged.
  • Earlier versions counted nine helpers over the budget and gave checkedCaller a cost of 27. There are ten, and it costs 29. They also said most of the lost headroom comes from the written-out hash, which was not measured.

fast and checkedLoop are just under the compiler's size for a big function,
5000 nodes. Past it, only callees costing up to 20 are inlined, and nine of
the helpers they use cost more: setInt, setBool, setFloat, isNum, toFloat,
wholeIndex, strEqBytes, strWords and hashShort, plus checkedCaller in the
checked loop. A few more cases in the loop would have turned them into calls
and spilled the loop's state on every instruction.

The type tags become fields of one struct, which the helpers address
directly: &tags.integer costs the inliner 3 where a call to tInt costs 5,
and the float tag sits at offset zero, which costs nothing. The setters cost
17 and 18, isNum 12, toFloat 20. strEqBytes takes the two pointers and
ranges over n (19); wholeIndex takes the float and its conversion from the
caller (18), keeping the early return, as other shapes cost GETTABLE a stack
reload on amd64. checkedCaller tests only the sentinels (19): a nil pointer
with a function's n breaks invariant 1 and still panics at the first read.

strWords and hashShort cannot get under 20, so GETTABLE's probe writes them
out; TestShortKeysHashInline looks up a key of every length up to 17 built
behind a pointer of its own, which a copy that hashed differently would miss.

With both loops padded past the limit they inline the same calls as now, and
make no calls. The generated code and asmgate's baseline are unchanged, and
instruction counts across the suite are within noise.
The gate builds the package with -m=2 as it is and once more through an
overlay that pads fast and checkedLoop past the size of a big function, on
the line of each opening brace so that no line moves, and fails on any call
the loops inline only in the first build. A helper over the budget of 20
then fails CI the day it is written, instead of spilling the loop the day
the loop grows.
@folbricht

Copy link
Copy Markdown
Owner Author

Not really needed, closing

@folbricht folbricht closed this Nov 24, 2026
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