tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

99 lignes · 5542 octetsBrut · Historique

ADR 0056: Split giant functions into tail-call chains

Status: Proposed (2026-10-22); needs engineering review (#44).

Context

  • The Core optimiser inlines aggressively (ADR 0043), and the C backend emits one C function per Core function. Some functions therefore grow huge: tests/run/msgpack.lse's main — derived ToMsgPack/FromMsgPack impls and std callees inlined into it — is about 13,000 lines of C (issue #281).
  • clang's debug-info passes are quadratic in function size. Compiling that file at -O1 -g takes 88 s and 6.5 GB peak RSS in "Live DEBUG_VALUE analysis" (4.3 s and 178 MB at -g1); the tsan workflow OOM'd on it. LOUISE_CFLAGS=-g is the documented way to keep variable information in release builds (issue #116), so every big enough program hits this, not just msgpack.
  • Shrinking the inliner would cost real optimizations everywhere to fix a C-compiler scaling problem. The C backend already has the opposite lever available: every generated function has the same signature, so any tail call is a musttail call (ADR 0046) and costs a jump.

Decision

A Core pass (crates/louise_core/src/opt/split.rs) cuts a function whose body exceeds a node budget into a chain of chunks connected by tail calls. The C backend runs it after the optimiser and before Perceus (louise_codegen_c::units); the interpreter never runs it.

  • What is cut: only the function's top-level let chain. A switch arm's or a Value::Block's body is never entered — a block's label cannot cross a C function boundary. A function whose size lives inside those (one item over the budget on its own) is left whole, as is one whose every in-budget cut has more live variables than a C signature can take with headroom (24; the signature passes 6 register arguments plus 26 through the context). Functions with an unboxed signature (ADR 0045) or a CPR worker's multi-value return (issue #200) are skipped: theirs is not the uniform signature the chain relies on.
  • The boundary: chunk k ends let r = f$split{k+1}(live…); r — the shape the backend compiles to TL_MUSTTAIL return …. The parameters are exactly the variables live at the cut, computed by one backward liveness walk over the chain; each chunk's variables are renumbered compactly so no chunk declares the whole function's locals. Perceus then inserts the reference counts and infers borrowed parameters as for any function; borrow inference treats a chain and its origin as one recursive group, so a chunk boundary stays a tail call (borrow.rs): the C stack stays constant along the chain.
  • The budget is Core nodes (lets, values, branches — the simplifier's size), default 500, overridden by LOUISE_SPLIT_NODES (0 disables). At 500 a chunk is under ~2,100 lines of C; clang -O1 -g on msgpack's unit drops from 88 s / 6.5 GB to 5.3 s / 200 MB — the -g1 baseline is 4.3 s / 178 MB (see the commit message for the measurements). Chunk overhead is one musttail call per boundary — a jump.

Correctness rests on the split being a pure rearrangement: no computation moves, is duplicated, or is dropped; the boundary call passes the live variables as owned arguments; an exit or panic in a chunk leaves the whole chain, as a tail call's callee's return is the caller's. The semantic oracle is the existing differential testing (issue #219): every tests/run program runs natively with the pass on (optimised and not) against the interpreter, which never runs it.

Consequences

  • clang -O1 -g on a big program's generated C is bounded per function; peak RSS falls from gigabytes to hundreds of megabytes (issue #281's repro), and the tsan workflow fits in memory again.
  • A function's chunks are named f$split1, f$split2, … and share f's module, so they land in f's C unit (ADR 0029) and their symbols are as stable as f's.
  • A recursive function's chain closes its strongly connected component (f → f$split1 → … → f), so every chunk of it keeps an entry preemption point (ADR 0046) and its self-calls stay musttail.
  • A non-recursive chain whose carried values are only read keeps borrowed parameters at its boundaries; those boundaries are ordinary calls (a bounded chain of frames, the trade-off issue #102 already accepts for cross-group calls). When the values are consumed, the boundaries are musttail.
  • Debuggability at -g is unchanged in kind: the chunks keep the lets' #line spans, and a chunk boundary is a call like any other.

Alternatives considered

  • Inline less. Rejected: the inliner is the optimiser's workhorse; throttling it to keep C functions small trades real run-time performance for a C-compiler limitation, and the threshold would be just as arbitrary.
  • Split at C emission (one Core function, several C functions). Rejected: the emitter's block labels, drop tracking and unwind tables are per function; cutting there duplicates the liveness work this pass does once, in Core, where Perceus and the verifier can check the result like any other code.
  • Cut inside blocks by outlining a block's continuation. Rejected for v1: a block's Dest::Assign label cannot cross a C function boundary, so outlining needs a data-flow join the uniform signature does not have; the top-level chain covers the observed giants (inlined derive/impl bodies are straight-line code).