Repository navigation
Fuse a recursive consumer with the recursive producer it consumes (deforestation) - #3
Draft
nicolas-abril wants to merge 4 commits into
Draft
nicolas-abril wants to merge 4 commits into
nicolas-abril wants to merge 4 commits into
Conversation
This was referenced Nov 24, 2026
Fuse a recursive consumer with the recursive producer it consumes (deforestation)
bendlang/bend#2031
Closed
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]>
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
force-pushed
the
opt/deforestation
branch
from
December 1, 2026 21:55
6886d14 to
c687016
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On main (
6180be5b)This PR is a port onto main, on
main: 4 commits of its own, with no integration history (headc687016d).Port notes
term_map(main removed it in refactor(comp): arm_self walks its arm itself, and term_map is gone bendlang/bend#2108) next to its one callerdef_fuse. Its edits to Tail recursion modulo context: a constructor's field filled by the call (shared constructors too), and any associative op carried #30's tests are dropped (those tests are Tail recursion modulo context: a constructor's field filled by the call (shared constructors too), and any associative op carried #30's).Checks
run/array_slabfails on main too).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)
comp.tsis 67,762 ttok, 1,762 over the 66,000 cap, sorepo-gatefails until that is settled. This is independent of #2 (closure inlining). Both editfun_of's body line and addlt_any,term_underandlet_end, so whichever merges second shares them.2026-12-01, on opt/integration 3019f02 (merge 177fd5c, fixes 08a5eb0 and 6886d14): integration's
lt_anyandFL.madeare shared,term_mapis back (bendlang#2108 dropped it), andfun_offuses the body literal folding stored (FL.cfh). Two fixes: a fused arm takes every field of the constructor, erased ones too (ctr_fldsdropped them and shifted the rest:tests/rfc/erased_record_sumprinted 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.bendis 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 defc<p(xs, ys), where c is a consumer and p a producer. Both must call themselves and take only live, closed datatypes.c<pis p's body with c applied at each result.c<pagain.letof a producer call forwards into its use. A result's loneletof a constructor (a b = p(..) p(..); Ctr{a, b}) forwards into it, so the producer's fork becomes the consumer's.sum<bump<bump<grow. Fused defs live inFL.made; the book is not mutated.The comptime twins (
tests/comptime/hand.bend,tmpl.bend) now consume the mapped list throughsum(acc, xs), which takes the list second. Otherwise fusion consumesList.map~0and 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.
collect(change(grow(24, s), 17))changesBench suite:
tree-radixis the only one of the 17bench/runtimeprograms whose C changes (to_arrfused throughto_map'smerge). 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 newtests/compile/deforest.bend.compile_bookover all tests took 18.2 s against main's 18.0 s, in one concurrent run. Warmcompile_bookon the benches is within 0.5 ms of main excepttree-radix(7.2 to 16.5 ms) andmerkle(32.7 to 37.2 ms).tscshows no new errors incomp.ts.repo.tsfails only on the ttok cap.Not covered
keephelper on its own. The consumer and producer around it still fuse.Cost
+1,767 ttok. Getting under the cap needs that many tokens cut elsewhere in
comp.ts, or a raise.🤖 Generated with Claude Code