Skip to content

Fuse a recursive consumer with the recursive producer it consumes (deforestation) - #3

Draft
nicolas-abril wants to merge 4 commits into
mainfrom
opt/deforestation
Draft

nicolas-abril wants to merge 4 commits into
mainfrom
opt/deforestation

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

On main (6180be5b)

This PR is a port onto main, on main: 4 commits of its own, with no integration history (head c687016d).

Port notes

Checks

  • Cluster gate (every test, every lane): 1874 / 1874 pass.
  • Locally: no new tsc errors against main, no new comp.ts lines over 80, its tests pass on the C and JS lanes (plus 40 lane runs of existing tests; run/array_slab fails on main too).
  • comp.ts: +2371 tokens (ttok -m gpt-3.5-turbo) over main, which is at 65,989 of the 66,000 cap.

Performance was measured on integration when this change merged there (see the original description or the replaced PRs); it has not been re-measured on main.

Original description (written for integration)

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

comp.ts is 67,762 ttok, 1,762 over the 66,000 cap, so repo-gate fails until that is settled. This is independent of #2 (closure inlining). Both edit fun_of's body line and add lt_any, term_under and let_end, so whichever merges second shares them.

2026-12-01, on opt/integration 3019f02 (merge 177fd5c, fixes 08a5eb0 and 6886d14): integration's lt_any and FL.made are shared, term_map is back (bendlang#2108 dropped it), and fun_of fuses the body literal folding stored (FL.cfh). Two fixes: a fused arm takes every field of the constructor, erased ones too (ctr_flds dropped them and shifted the rest: tests/rfc/erased_record_sum printed wrong values); and a producer result that forks (a forwarded parallel let, or two calls in its fields) is not fused into an arm that tail-calls the consumer, since the loop would run the fork's calls one after another (parallel-ml/high-frag at 10 threads went 183 to 242 ms; it is now unchanged). Cluster, vs integration, 1 thread instructions (one run) and cycles (median of 5), 10 threads wall (median of 7): C changes in 14 of the 244 programs (23 fused defs); the 9 outside the set-apart list: cycles geomean 0.827, instructions 0.858 (mapfold 0.278, respelled/exprtree 0.719, tree-radix 0.930, osprey/exprtree 0.939; worst regex-redux 1.026 at 1.013 instructions, nqueens 1.010, primes 1.006, subset-sum 1.005 and 1.036 at 10 threads). Set apart (binarytrees ports, msort-fip): cycles 0.601; ocaml-binarytrees5 at 10 threads 0.337. Over all 244: 0.993. No loss over 5%. CardDemo chain: b40k 0.971, b250k 0.892 (tranrept 0.78-0.80), outputs identical; tar's C is unchanged; bendquest: 27 of 48 programs change C, every check prints the same. Emit time: 244 programs 60.2 to 60.7 s, tests 283.3 to 283.6 s, tar 1.01 to 1.02 s, CardDemo 1.47 to 1.50 s, bendquest 144.3 to 143.6 s. Checks: all 244 print expected.txt (100 parallel ports also at 10 threads); test gate: no failing test (three runs, each test passing in at least one; the rest were nodes with no bun); 0 live words at 1 and 8 threads with BEND_POISON on the 47 tests whose C changes; GPU on/off for tree-radix and the 5 changed bang tests. tests/run/deforest_interact.bend is new; the tail-modulo-cons tests (tmc_dest, ctor_tail_dest, ctor_tail_dest_bang) keep their producers built through identity defs and emit integration's C again. Of three wrong versions, the tests catch two (erased fields: 3 tests; swapped extra arguments: 24 tests); a third that silently turns fusion off is caught by none. +2,285 ttok over integration.

What it does

A call c(p(xs), ys) runs a new def c<p(xs, ys), where c is a consumer and p a producer. Both must call themselves and take only live, closed datatypes.

  • Body: c<p is p's body with c applied at each result.
  • Deforestation: where a result is a constructor, it meets c's leading match, the arm is selected and the node is never built. Inside that arm, c called on a call of p becomes c<p again.
  • Only when it pays: a fused def is kept only if some constructor result meets the match.
  • Lets: a lone let of a producer call forwards into its use. A result's lone let of a constructor (a b = p(..) p(..); Ctr{a, b}) forwards into it, so the producer's fork becomes the consumer's.
  • Chains: fused defs fuse again, up to three levels, e.g. sum<bump<bump<grow. Fused defs live in FL.made; the book is not mutated.

The comptime twins (tests/comptime/hand.bend, tmpl.bend) now consume the mapped list through sum(acc, xs), which takes the list second. Otherwise fusion consumes List.map~0 and the gate's "only tmpl declares List.map" probe fails. Both twins change the same way, so they still compare template output to the hand-written version.

Results

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

program SEQ main SEQ this PAR main PAR this
collect(change(grow(24, s), 17)) 0.147 s 0.040 s 3.7x 0.038 s 0.008 s 4.5x
two changes 0.194 s 0.040 s 4.8x 0.048 s 0.009 s 5.4x
chain producer, consumer with a salt 0.145 s 0.052 s 2.8x 0.144 s 0.053 s 2.7x
filter between them 0.150 s 0.063 s 2.4x 0.151 s 0.063 s 2.4x
producer shared with a second use 0.249 s 0.143 s 1.7x 0.113 s 0.036 s 3.1x

Bench suite: tree-radix is the only one of the 17 bench/runtime programs whose C changes (to_arr fused through to_map's merge). In the paired run it took 3.342 s against 3.573 s on SEQ-CPU (0.935x), 0.602 s against 0.641 s on PAR-CPU (0.939x), and 1.108 s against 1.118 s on GPU (0.991x). Every other bench compiles to byte-identical C.

Checks

  • test.ts: 1827/1827, with the new tests/compile/deforest.bend.
  • Compile time: compile_book over all tests took 18.2 s against main's 18.0 s, in one concurrent run. Warm compile_book on the benches is within 0.5 ms of main except tree-radix (7.2 to 16.5 ms) and merkle (32.7 to 37.2 ms).
  • Output across the corpus: C changes in 42 tests and JS in 58.
  • tsc shows no new errors in comp.ts.
  • repo.ts fails only on the ttok cap.

Not covered

  • Non-recursive producers, such as a filter's keep helper on its own. The consumer and producer around it still fuse.
  • Erased parameters or dependent signatures on either side.
  • Multi-output fusion (tupling) of two consumers of one producer.

Cost

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

🤖 Generated with Claude Code

nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…results as one def

Fork PR #3. Vs integration 3019f02 on the cluster: 14 of 244 programs change
C; cycles geomean 0.827 on the 9 outside the set-apart list, 0.601 on the
binarytrees ports and msort-fip; nothing worse than +2.6% (regex-redux).
CardDemo 0.97/0.89, tar C unchanged, emit time +0.8%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril and others added 4 commits December 1, 2026 22:02
A call c(p(xs), ys), where c and p call themselves and take live closed
datatypes, runs a def c<p(xs, ys) made from p's body with c applied at
each result. A constructor result meets c's match there and is never
built, and c called on a call of p inside is c<p again. The fused def is
kept only if some constructor meets the match. A lone let of a producer
call forwards into its use, and a result's lone let of a constructor
forwards into it, so producer forks become the consumer's. Fused defs
fuse again, up to three levels.

The comptime twins pass the mapped list second (through sum), so no
fusion consumes the map and they still compare template to hand-written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 96eec1e)
…nes too

The match arm binds all of a constructor's fields; ctr_flds drops the
erased ones, so the fields after one shifted (tests/rfc/erased_record_sum).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 08a5eb0)
…loop

A producer result that forks (a parallel let forwarded into it, or two
calls in its fields) fused into an arm that tail-calls the consumer runs
the fork's calls one after the other: parallel-ml/high-frag at 10 threads
went 183 to 242 ms. Such a pair is not fused.

The tail-modulo-cons tests keep their producers built (fusion would
remove the defs they test), and tests/run/deforest_interact.bend covers
fusion next to the other passes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 6886d14)
); wrap a line

def_fuse rewrites a body through term_map, which main removed with its
other caller (arm_self). The fusion branch carried its own copy next to
def_fuse; this is that copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…33, #34, #36, #39, #44, #61-#68): record their heads

Each is a port onto main of integration's own branch, its content already
here (integration keeps the original commits; the ports add type fixes,
line wraps, moved pieces and #68's book_passes, applied in the previous
commit). Groups: #61 (#9 #11 #15 #32 #41 #42), #62 (#26 #56), #63 (#48 #50
and compile-time), #64 (#19 #20 #45), #67 (#55 #58), #68 (#54); par-defer
(#43) is now part of #34, lazy-startup's parts are in #16 and #34. This
merge records their heads (-s ours: no content change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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