Skip to content

A fork-free segment takes its sequential path on the host without testing seq - #2176

Draft
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/fork-free-seq
Draft

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/fork-free-seq

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

comp.ts is 66,000 ttok, exactly at the 66,000 cap (main is 65,966): WL_SEQ's #define has one space where its neighbours are aligned, which is the token that keeps it there. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run.

What it does

A non-tail call has two ways to keep its continuation: a frame on the stack (if (seq)) or a task node on the heap (the else), and a jump into a fork-free def from a forking one makes a task (!DEVICE && !seq && fid_nofk(F)). seq is set whenever the task at hand is fork-free (FID_ENTER and FID_EXIT or in fid_nofk), so a segment that reaches no fork tested a flag that was always set, and carried a task path it never took.

WL_SEQ(F) is seq, or on the host true when segment F is fork-free. FID_T is a constant table, so clang folds it per segment. In a fork-free segment the host build then drops:

  • the test before each frame and before each jump to another def;
  • the task path, and with it the calls to task_node and heap_alloc_miss. Those calls were what made clang give the segment a C frame: eval in symreg saved x29/x30 and set three constants on every entry, for code it never ran.

A forking segment and every device segment still test seq.

The one case that changes behaviour: a fork-free closure body entered through a Clo~apply that may fork (some other closure forks), from a forking def running with seq clear. It used to keep its continuations in task nodes; it now keeps them on the stack above the task's frame, as a fork-free task does. Everything it reaches is fork-free too, so the frames are popped before control returns to the forking code. The new test runs that path.

Results

One M4, GPU off, one thread unless said. Instructions retired and cycles from /usr/bin/time -l, builds run in turn, minimum of 3 to 5 runs.

bench instructions, main this
symreg 50.77G 42.75G -15.8%
queens 89.82G 84.13G -6.3%
lexer 53.41G 51.92G -2.8%
tree-matmul 62.58G 62.81G +0.4% (the worst)
the other 13 within 0.02%

Cycles move much less than instructions. Each build was swept over 16 code positions (nops at the head of work_loop, 4 bytes a step), cycles on a P-core, minimum of 2 or 3 rounds per position:

bench main, min / median / max this, min / median / max
lexer 8.41G / 8.45G / 8.95G 8.07G / 8.09G / 8.13G 1.04x, the ranges do not overlap
symreg 12.19G / 12.63G / 13.42G 12.15G / 12.43G / 12.72G the ranges overlap
queens 21.92G / 22.13G / 22.40G 21.82G / 22.05G / 22.28G the ranges overlap
nbody (same instructions) 21.33G / 22.03G / 22.69G 21.33G / 21.46G / 22.65G placement only: either build is 21.4G or 22.7G by position

On an E-core (taskpolicy -b, one run each): symreg 17.29G to 17.13G cycles, queens 28.70G to 27.95G, lexer 16.02G to 15.67G. At 8 threads the wall time does not move (symreg 0.52 s, queens 0.91 s to 0.90 s, lexer 0.39 s to 0.37 s).

Why symreg keeps its cycles: it is bound by taken branches, not by instructions. Counted with Instruments' CPU Counters over the whole run: 8.13G taken branches before and after, 0.044G mispredictions before and after, no cache misses. Over twelve variants of the bench (C twins and hand edits of the emitted C, 37.9G to 51.2G instructions) cycles follow taken branches (R² 0.95, about 1.2 cycles each) and not instructions (R² 0.23). The instructions removed here are stores, moves and a never-taken test.

Three small recursive kernels (scaled copies of the fib, hanoi and tak ports in koru-benchmarks' osprey-compute-kernels, nothing but calls and returns), instructions / cycles at one placement:

kernel main this
fib(42) 17.89G / 2.86G 16.65G / 2.74G
hanoi(29) 15.85G / 2.88G 10.76G / 2.32G
tak(38, 19, 9) 57.07G / 10.07G 44.92G / 9.75G

Elsewhere: the emitted C of every program changes (main pushes a frame too), by the spelling of the test only where the segment may fork.

Checks

All local, against main f0cf0a7c:

  • Native lane: every test built and run with the GPU off at the default thread count by both compilers: 883 print their #| lines on both, 21 differ from them and 1 times out on both (IO, gfx and network tests), no status differs.
  • Memory: the 905 tests that build, with -DBEND_POISON and a count of live heap words at exit: the same output, exit code and count as main on every one.
  • The path that changes: a build that counts fork-free segments running with seq clear. The new test reaches it tens of thousands of times at 8 threads (50,280 and 100,464 in two builds) and never at one; a larger copy 7 to 10 million times at 2, 4 and 8 threads. Both print main's lines at every thread count, and pass the memory check at 1 and 8 threads.
  • New test: tests/run/fork_free_closure.bend: fork-free closure bodies with non-tail calls, a deep recursion and frames live across allocations, called from a forking def beside a closure that forks. Its lines are main's; it prints them on the C lane (1 and 8 threads), under bun and under bend file.bend.
  • JS lane: the JS of all 975 programs that emit it (tests and benches) is byte-identical to main's.
  • Metal: the device still tests seq (WL_SEQ is seq there). The 47 tests that make a ! call print the same with --gpu on on main and on A call into a tree recursion runs the callee's returning arms itself and jumps straight to its continuation #2177, which contains this commit.
  • repo.ts passes (56 / 56): comp.ts is at the cap, not over it.

Not covered

  • Cluster gates and CUDA have not run. x86 and Linux were not measured.
  • tsc was not run.
  • A forking segment still tests seq at run time and keeps both paths.

Where this came from

symreg is 1.37x behind its C twin and no draft had moved it. eval's segment showed a frame set up on every entry for calls that sit on the task path, which a fork-free def never takes. The cycles are a different matter: they follow taken branches, and #2177, which stacks on this one, removes those (symreg 12.4G to 8.2G cycles).

🤖 Generated with Claude Code

A segment that reaches no fork tested seq before each frame it pushed and
before each call, and carried the task path it never takes: FID_ENTER and
FID_EXIT set seq for a fork-free task, and a forking segment hands a
fork-free callee to a task of its own. WL_SEQ(F) is seq, or on the host
true for a fork-free segment, so clang drops the test, the task path and
the frame the path's calls forced on every entry.

The one such segment that ran with seq clear is a closure body entered
through a Clo~apply that may fork; it now keeps its frames on the stack
above the task's frame, as a fork-free task does.

symreg -15.8% instructions, queens -6.3%, lexer -2.8% (one thread, M4).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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