Skip to content

Compile once, reuse everywhere: shared generic bodies, cached front-end work, outlined by-name dispatch - #645

Merged
blacktrs merged 42 commits into
mainfrom
shared-generics
Nov 11, 2026
Merged

blacktrs merged 42 commits into
mainfrom
shared-generics

Conversation

@blacktrs

@blacktrs blacktrs commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Why

A compile spends most of its time redoing work that does not depend on the program: clang re-optimizes the same large prelude bodies for every program, the analyzer re-parses the prelude, link flags are re-probed, and every by-name call site inlines the whole program's call table. This PR removes that repeated work (roadmap E1 + E2 in docs/design/compile-speed-roadmap.md) and fixes the dynamic-call bugs found on the way.

What changes for a user

How

  • Shared generics (E1): large linkonce_odr prelude/runtime bodies are cut into their own parts (ShareSplit), keyed by their exact IR text in the object cache, so a body compiled for one program is reused by every program that emits the same text. Made possible by removing program-specific names from those bodies (app module token, per-file prelude parse, stable closure and dynf ids). Gate: tests/generic/run.sh (identity and variant-hit floors), tests/generic/cache.sh.
  • Front-end diet (E2): the analyzer's prelude index, each prelude file's defined functions and link-flag probes are cached beside the object cache, keyed by the compiler binary and content; stale entries are pruned. By-name call dispatch is outlined into per-shape __mir_dyncall_* dispatchers, so prelude callback bodies (usort, uksort, uasort) no longer embed the program's table and become shareable.
  • Typed spread packs reach the cell dynamic-call thunks as they are (the spread fill decodes each element by the buffer hint) instead of a thunk per function per element type; the spread thunk calls the function with the pack as a spread. Spread call sites are outlined into the dispatchers too.
  • tests/ir_budget ceilings re-blessed (the old ones were exceeded on every case) and two cases added.

Bootstrap

No new syntax in src/. array_keys keeps its one-parameter declaration until BOOTSTRAP_VERSION includes this PR's lowering fix (an older compiler fills defaults into that inline builtin call); noted on #643.

Testing

  • macOS: full AOT suite 2811/2813 on b451934 (2 SKIP, no expected output), xfail.sh no XPASS, tests/cli, tests/analyze, tests/generic/*, tests/ir_budget 13/13; compiler bit-identical across generations (checked before the issue fixes).
  • Linux arm64/amd64: CI. The CI object-cache steps in linux-target.yml / ci.yml run for the first time here.
  • Not run: tools/difftest.sh.

Fixes #575
Fixes #612
Fixes #636
Fixes #637
Fixes #638
Fixes #639
Fixes #640
Fixes #641
Fixes #643
Fixes #644
Fixes #647

…ass's property no longer changes another class's private slot
…ength, so prelude bodies do not inline the program's function count
… variant is compiled once and reused from the object cache
…mutable state, copy runtime helpers up to 2 KB
…art in prog/ so only the shared objects travel
…(per-writer temp names, size-checked puts, one clang --version per process)
…ellified copy instead of a thunk per function per element type

Fixes #638
…allback bodies stop carrying the program's call table

A site whose arguments are plain locals passes their slots (a by-reference
callee binds the caller's variable as before); a prelude site with element or
property arguments passes values, as php calls an internal function's callback,
and a by-reference callee there throws php's 'must be passed by reference,
value given'. usort/uksort drop from 205 KB of IR each to a shared body.
…llback dispatch

uasort's comparator wrapper bound the elements by reference; a by-reference
comparator then corrupted the array instead of getting php's 'must be passed by
reference, value given'.
…lude and runtime and were exceeded on every case
…ags on build, refuse only passed by-ref params, give dispatchers the call site

- analyze/link/demand entries unwritten for 14 days are pruned when a new one is stored
- build probes its link flags (a failed link there is usually the program's own; a retry reran ThinLTO)
- a by-value callback dispatch refuses a by-reference parameter only when it is passed
- by-name dispatchers take the call site's line and file, so an error raised in them points at the caller
- plain-local dispatch excludes the same reference-backed locals the erased-invoke helper does
The spread thunk read element k into parameter numFixed+k: a variadic tail got
a scalar (max/min crashed, max over a string pack answered the first element),
an omitted parameter got 0 instead of its default, and string keys bound by
position. It now calls the function with the pack as a spread, which takes
defaults, variadics and named keys like a direct call; a codegen builtin is
sent to its same-named PHP body, since builtins index arguments positionally.

Fixes #636
Fixes #637
Fixes #639
Fixes #640
…mentCountError for surplus arguments

- 17 stdlib functions gain php's missing parameters (context, use_include_path,
  options) and php's parameter names; strrchr before_needle, strspn length,
  str_word_count characters and file_get_contents offset/length work
- 6 prelude functions gain theirs; openssl padding/digest_algo/serial_hex throw
  when given a value they cannot honour, pcntl_waitpid leaves resource_usage empty
- an internal function called with more arguments than it declares throws
  php's ArgumentCountError; names the emitter answers by argument count
  (array_keys) are exempt, their declarations stay short on purpose
- lowering no longer fills defaults into a call the emitter answers inline,
  so such a body can declare php's optional parameters once the pin has this

Fixes #643
Fiber::__mcRun, __mc_array_map_zip and prelude closures spreading into a
callable by name carried the program's dispatch inline; a spread never binds a
reference, so its fixed values and pack go to a by-value dispatcher from any
frame. ws_tls program part 7.74 -> 7.68 MB, inline arms outside dispatchers 0.
A cache entry that could not be removed (read-only cache) was read back by the
retry, which then linked with the same stale flags.
…lified copy

The copy was released after the call and leaked whenever the callee threw (2 KB
per throw for a 64-int pack). The thunk's spread fill decodes each element by the
buffer's own hint, so the raw pack reads right; a position past its end now stays
0 for the default instead of decoding a stray word into null (explode's limit).
The emitter releases some values only after a call returns: a variadic pack
rebuilt into the callee's typed vec, a co-owning typed copy of a cell array. An
unwind skipped those releases, so a spread into a variadic function, method,
static method or constructor that throws leaked the pack (132 MB per 200k
throws of a 64-int pack).

A frame that may own such temps keeps a short list of (value, releaser): pushed
before the call, popped after its release, drained first by every landing pad
of the frame (cleanup, raising and catch). @main keeps it for its catch pads,
and the by-name thunks and dispatchers get a pad of their own. The spread fill
now reports the variadic tail it rebuilds, so the by-name spread path also stops
leaking it on a normal return.

Fixes #647
@blacktrs
blacktrs merged commit c662631 into main Nov 11, 2026
69 checks passed
@blacktrs
blacktrs deleted the shared-generics branch November 11, 2026 10:40
blacktrs added a commit that referenced this pull request Nov 11, 2026
Keeps this branch's diagnostics sink beside main's prelude-demand stats step (#645).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment