Skip to content

An erased last argument no longer cuts its call, so a def whose last parameter is erased keeps its fork (#1374) - #1377

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
nuxyel:fix/1374-erased-last-fork
Oct 7, 2026

Conversation

@nuxyel

@nuxyel nuxyel commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #1374.

Cause

anf's spine cut the call prefix before every argument, live or erased. In tree(q, i, z) with z erased, the prefix tree(q, i) already carries every live argument, so term_spine reads it as a complete call. anf cut it into a sequential h = tree(q, i), the parallel let's values became plain variables, and the let compiled with continuations and no join. With -z anywhere else, the prefix before it isn't a complete call, so nothing was cut. That's why only an erased last parameter showed it.

The same cut also hit tail calls into a def that isn't flat. Word.add_comm.go ends each arm with Word.add_comm.arm(p, at, bt, k, h), whose last parameter -h is erased, so each of its eight arms was emitted as a cut plus a continuation (FID_WORD_ADD_COMM_GO_K4 … K11) instead of a tail call.

Change

In anf, only a live argument over-applies a call, so the prefix is cut only before one. An erased argument stays in the application, as it already did when it sat in the middle. Variables keep their rule: a variable applied to erased arguments is the variable. The header comment above anf now says "a live over-application".

  • Size: comp.ts 63,849 → 63,858 ttok (+9; cl100k_base, ttok's default). gates/repo.ts passes 50 / 50.
  • New test: tests/compile/fork_erased_last.bend is a parallel let in a def whose last parameter is erased. Its C now has the join (FID_TREE_J4), and main's has none. Stdout can't show a fork, so main prints the same #| line: the test runs the forked shape on every lane but doesn't catch a revert, and its header says so. The evidence for the fork is the C and the timings below.

Checked

Linux x86-64 (Core Ultra 7 155H, 22 threads), bun 1.4.2, clang 22.1.8. Main is 0ad47fc.

The repros from #1374 (best of 3; all print 3942408568):

program join in the C (main / this PR) --threads 1 (main / this PR) --threads 16 (main / this PR)
tree(+d, +i) J2 / J2 1.010 / 1.027 s 0.165 / 0.170 s
tree(+d, +i, -z) none / J2 1.010 / 1.017 s 1.013 / 0.169 s
tree(+d, -z, +i) J2 / J2 1.010 / 1.058 s 0.169 / 0.170 s

The two controls emit byte-identical C on main and here, so their timing differences are noise.

Same output elsewhere.

  • C and JS: for all 1,659 .bend files under tests/, bench/runtime/ and demos/, each lane builds the same files as main (770 C, 794 JS), and every error message is identical. Every emitted JS file is byte-identical. Two C files differ:
    • proof/word_add_comm: the eight cuts above become tail calls;
    • reg/erased_call_cut: one local is renamed (_h_0 → _v0_0), and its binary is byte-identical to main's.
  • .mjs and binaries: built with -o t.js -o t.mjs -o t for all 1,593 tests. Every .mjs is byte-identical. Of the 736 binaries, two differ: proof/word_add_comm and the new test.
  • Runs: every test, run with the interpreter, as a C binary and as JS under bun, prints the same output on main and on this branch (1,593 interpreter runs, 736 C, 760 JS). The 16 that don't match their #| lines fail identically on main: display tests, io/process_run, io/chan_try_late, and helper modules under tests/import and tests/reg/respelled.
  • --checkup on reg/erased_call_cut, proof/word_add_comm and the new test prints their #| lines.
  • tsc -p bend2/pack: the same 12 errors as main, none in comp.ts.

Performance. For each of the 17 bench/runtime programs I ran SEQ-CPU (--threads 1 --gpu off) and PAR-CPU (--threads 32 --gpu off) after one warm run, taking the median of 5 runs interleaved with main. For every bench:

  • the emitted C and the binary built with the perf gate's cc line are byte-identical to main's;
  • every output is the same.
main → this PR
runtime, geomean of the 17 benches SEQ 0.999, PAR 1.004 (identical binaries, so noise)
compile time (-o main.c), geomean 1.007; peak RSS the same
bench/checker, min of 3 0.97 to 1.00

Memory. A side effect, not pinned by any test, since output can't show it. A tail-recursive loop that forks in its body and has an erased last parameter got one continuation per iteration on main. Over 3M iterations, peak RSS was 30 MB on main and 12 MB here at 1 thread, and 104 MB against 14 MB at 16 threads. The output is the same.

Not run: the cluster gates (gates/test.ts, gates/perf.ts, gates/safe.ts, gates/ping.ts), Metal and CUDA. This machine has no access to the minis and no Apple GPU.

…parameter is erased keeps its fork (bendlang#1374)

anf's spine cut the call prefix before every argument, live or erased.
In tree(q, i, z) with z erased, the prefix tree(q, i) already carries
every live argument, so term_spine reads it as a complete call: anf cut
it into a sequential `h = tree(q, i)` and the parallel let's values
became plain variables, so the let compiled with continuations and no
join. Only a live argument over-applies a call now, so the prefix is
cut only before one; an erased argument stays in the application, as
it already did when it sat in the middle.

The same cut made a tail call into a non-flat def with an erased last
parameter a call plus a continuation; it is a tail call again
(Word.add_comm.go's calls to Word.add_comm.arm).

Fixes bendlang#1374.
@Lorenzobattistela
Lorenzobattistela merged commit f76c251 into bendlang:main Oct 7, 2026
1 check passed
nuxyel added a commit to nuxyel/bend-ml that referenced this pull request Oct 8, 2026
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 13, 2026
This was referenced Oct 14, 2026
nuxyel added a commit to nuxyel/bend-ml that referenced this pull request Oct 17, 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.

C backend: a def whose last parameter is erased compiles its parallel let as two sequential calls

2 participants