Skip to content

Inline calls that pass a lambda to a non-recursive def, through matches on runtime values - #2

Closed
nicolas-abril wants to merge 1 commit into
mainfrom
opt/closure-specialization
Closed

nicolas-abril wants to merge 1 commit into
mainfrom
opt/closure-specialization

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

Moved from bendlang#2030 (opened 2026-11-03).

comp.ts is 67,625 ttok, 1,625 over the 66,000 cap, so repo-gate fails until that is settled. Supersedes bendlang#1866: it covers the same cases with no more closures on its fixtures (included here), and adds callees that match on a runtime value with more than one arm.

What it does

def_simp runs on each def's body in fun_of, before the analyses (flatness, spins, call sites) read it:

  • Beta: a lambda applied in place is reduced.
  • Inline: a saturated call that passes a lambda to a def that cannot reach itself (judged on its checked body) is inlined. The lambda is then applied in place, so no closure is built.
  • Eager arguments: a live argument that is not a value is let-bound first, so it still runs once.
  • Matches on a runtime value: when the callee opens a match on a runtime value, the arguments left over move into the arms. If a let binds the call, the rest of that let moves into the arms too. So f = choose(c, λa, λb); f(x) applies λa or λb directly in each arm.
  • Emitter: it now takes a match applied to its scrutinee in tail position. file_book and term_any keep such a match's arms as tail positions.

Callees that return more lambdas than the call supplies (IO.bind and the rest of CPS IO) are left alone. A pre-check on the checked term keeps defs without a lambda argument untouched.

Results

Paired cluster runs: each case on 3 nodes, 5 rotated samples per node after a warm-up, wall time, medians.

program (300M iterations) main this
helper called with a fresh lambda 0.546 s 0.205 s 2.7x
same, the lambda captures a variable 0.746 s 0.273 s 2.7x
function chosen by a Bool match, applied after a let 0.546 s 0.344 s 1.6x
function chosen by a match, applied in place 0.581 s 0.344 s 1.7x

All four match the hand-inlined program on SEQ-CPU. On PAR-CPU, the chosen-function loop goes from 6.571 s to 2.017 s.

Closure sites in the C (term_clo(FID_…)) on the fixtures from bendlang#1288/bendlang#1866, main / bendlang#1866 / this:

fixture main bendlang#1866 this
known_callback 11 2 2
known_callback_array 4 2 2
known_callback_edges 84 80 74
known_callback_layouts 41 35 35
known_callback_shapes 60 47 47

Checks

  • test.ts: 1832/1832, with the new tests/compile/known_fn_inline.bend and the five tests/run/known_callback*.bend. In one of four earlier runs, io_bundle_inline [js] timed out; that test also flakes on output that is byte-identical to main.
  • Bench C is unchanged: all 17 bench/runtime programs compile to the same C as main. A paired SEQ/PAR/GPU run of all of them showed only node noise.
  • Compile time: compile_book over all tests took 16.7 s against main's 18.0 s, in one concurrent run. Warm compile_book on the benches is within about 0.5–2 ms of main.
  • Output across the corpus: C changes in 59 of the tests and JS in 196. Most JS changes only reorder closure captures, because the pass opens lambdas before the emitter does.
  • tsc shows no new errors in comp.ts.
  • repo.ts fails only on the ttok cap.

Not covered

  • Recursive callees with a lambda argument. ~ templates still cover those.
  • A lambda passed to a call that is itself an argument, when the callee opens a match on a runtime value. It stays a closure apply.
  • Named functions passed as values.

Cost

+1,630 ttok. Getting under the cap needs that many tokens cut elsewhere in comp.ts, or a raise.

Updates

  • 2026-12-01: checked against opt/integration (1d0f594), not merged: nothing in it is missing there. Its fixtures are already in integration (closure-elim perf(comp): a lambda passed to a def runs in a copy of the def, its captures passed as parameters #31 absorbed them) and pass. On the 244 programs it changes C on 52 (against its own main e2a7f60); on every one integration builds as few closure sites as this branch or fewer (closures/pipeline 9 here vs 7, twolevel 9 vs 7, subset-sum 9 vs 7, the rest equal). The only case where this branch does better is its own tests/compile/known_fn_inline (2 closure sites against integration's 4), a cold test.

🤖 Generated with Claude Code

def_simp runs on a def's body before the analyses read it. It reduces a
lambda applied in place, and inlines a saturated call that passes a
lambda to a def that cannot reach itself (by its checked body). An
argument that is no value is bound first, so it runs once. When the
callee opens a match on a runtime value, the arguments left and the rest
of the let that binds the call move into the match's arms (the
emitter now takes a match applied to its scrutinee in tail position),
so a function chosen by a match is applied in each arm.

Callees whose body returns more lambdas than the call supplies (IO.bind
and the rest of CPS IO) are left alone.

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

Copy link
Copy Markdown
Owner Author

Superseded: #31's closure pass (in integration) covers the lambda-passing cases with fewer closures on these fixtures, and #53 (commit 4f162fb) adds the remaining one, a function picked among lambdas and then applied (known_fn_inline: 2 closures, as here).

nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…opy's arms

#2's missing case, in the closure pass:

- A lambda argument is known by its own function type where its
  parameter's type is a variable (choose(-A: Type, flag, a: A, b: A)), so
  choose(flag, λa, λb) gets a copy at all.
- An over-applied call with a known lambda knows its extra arguments too
  (variables, as captures, or constants): choose(flag, λa, λb)(x) calls a
  copy whose arms apply λa or λb to x in place (clo_rm applies a known
  argument to whatever term is left past the case tree).
- f = choose(flag, λa, λb); ...; f(x): a let of such a call, read once as
  a call's function and under no lambda, puts the call there.

tests/compile/known_fn_inline: 4 closures to 2 (#2's count); #2's other
fixtures as before. A loop of 300M picks, applied after a let or in place:
2.70x fewer cycles (-89% instructions), as fast as a fresh lambda.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4f162fb)
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