Skip to content

A constructor of any width builds on C: term_drop walks a node past 247 words as an array - #1068

Merged
nicolas-abril merged 4 commits into
mainfrom
wide-nodes-walk-as-arrays
Sep 25, 2026
Merged

nicolas-abril merged 4 commits into
mainfrom
wide-nodes-walk-as-arrays

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #991. Supersedes #1044.

Why

A record of 256 one-word fields is checked, interpreted and run on JS, but the C build refuses it with an arity over 255. The limit comes from term_drop, not from the language. It frees a tree without a stack, and for each node it packs the position (40 bits), child index (8), arity (8) and size class (8) into one word. #944's lay_wide and #1044 both work around that packing in the compiler.

The same walker already frees arrays of any size. It takes an array's cell count from its size class and keeps the running index in the node's own slot 1, which has already been visited. This PR lets wide constructors use that path.

What changes (bend2/comp.ts, +20 / −16)

  • CID_ARITY_T stays u8. A constructor past 247 words stores 240 + log2 of its node size there (8 to 15, so up to 32,768 words). Only term_drop and io_exec read the table, and effect nodes are small.
  • term_drop: one line. An entry past 247 takes the array walk at that size class.
  • lay_node: a node past 247 words is padded to its size class. Construction already writes 0 to every word of a constructor it doesn't fill, so the tail comes out zero with no extra code.
  • WIDE becomes 247, for flat layouts and segments, so only a constructor's entry can pass it. That keeps the check in term_drop a single comparison. A value past WIDE words travels boxed (lay_of).
  • lay_wide is gone. It boxed a datatype's multi-word fields only to keep its node under 255 words. Its one remaining use, a def's parameters (C backend: wide monomorphic records bypass boxing and hit the 255-word CID limit #944), is inlined in sig_def. The emitted C for every tests/compile program, the wide-record tests and the benches is byte-identical before and after that inline.

A program without such a node emits the same C as main, apart from that one line in term_drop, and its tables are identical.

Tests and docs

  • tests/reg/wide_record_drop comes from Box wide constructor fields within runtime arity #1044 (thanks @oxura). It drops six unopened 256-field records, then builds six 300-field ones. Its header is updated for this design. Box wide constructor fields within runtime arity #1044's record_256_fields isn't included: its literal record folds away, so its C never built a wide node.
  • tests/reg/wide_record_stale (new): 250-field nodes, padded to 256 words, are built in blocks that still hold String pointers from opened 256-field records, then dropped unopened. If the padding isn't written as zero, C prints 77777!/77777!77777!/7 instead of 777777!/7/77.
  • Updated headers: arity_wall (a 255-field record now travels boxed), record_wide_node (R2 is now one 512-word node) and record_deep_boxed (the 512-word level is one box).
  • guide/SHADERS.md: a def or continuation now takes at most 247 words.

Verification (macOS, Apple M4)

  • Wide records: these agree in the interpreter, JS and C: records of 256, 512 and 520 fields; multi-word fields mixed with a run of 260 one-word fields; a sum type with a 300-field arm; wide records inside Maybe and List; a pure main printing a 258-field record.
  • Local test gate (gates/test.ts's shard script run in a local bash): 1464 / 1467. The failures are the two gfx tests that need a machine without a display, and one timing test whose sleeps 5108d04 shortened (io_spawn_sleep in one run, io_fork_join in another), which passes 10 / 10 when run alone.
  • bun gates/repo.ts: 46 / 46. comp.ts grows by 58 ttok.
  • Runtime benches, 16 × SEQ-CPU / PAR-CPU / PAR-GPU against main, alternating runs: within ±2.5%, with the same outputs.
    • editdist is about 20% faster, from code layout.
    • lexer SEQ-CPU is +2.6% in the default build and identical with -falign-functions=64 -falign-loops=64.
    • A program that only builds and frees 2²¹-leaf trees is within 0.995–1.014.
  • Cluster gates: not run. This machine's key is refused by the minis.

Limits

  • Segments: a function or continuation that keeps more than 247 one-word values live across a call still fails to compile. The cap drops from 255 to 247. That limit belongs to the register bank and the C calling convention: raising it made clang crash. That's tracked in C backend: a segment with more than 247 live one-word values fails to compile #1069.
  • Constructor size: a single constructor wider than 32,768 words fails to compile.

🤖 Generated with Claude Code

…47 words as an array

term_drop frees a tree without a stack, packing each node's position,
child index and arity into one word, 8 bits each: that packing, not the
language, capped a node at 255 words, so a record of 256 one-word fields
was checked, interpreted and run on JS but refused by C (#991). The same
walker already frees an array of any size, taking its cell count from the
size class and keeping its index in the node's own slot 1.

A constructor past 247 words now keeps 240 plus its size class in its u8
CID_ARITY_T entry, and term_drop walks it as an array; node_fill zeroes
the rest of its block so the walk skips it. WIDE drops to 247 for flat
layouts and segments, so only a constructor's entry can pass it, and a
value past WIDE words travels boxed. A program without such a node emits
the same C but for one line in term_drop.

tests/reg/record_256_fields and wide_record_drop come from #1044;
wide_record_stale drops 250-field nodes in blocks that still hold String
pointers, which an unzeroed tail frees (77777!/77777!77777!/7).

Closes #991.

Co-authored-by: oxura <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril and others added 2 commits September 25, 2026 18:56
…elds to fit a node

A node past WIDE words is laid out to its size class, the words past its
fields written as zero like every unset word of a constructor, so
node_fill needs no loop and CID_ARITY_T holds 240 plus the exact log2.
lay_wide boxed the multi-word fields of a datatype only to keep its node
under 255 words; nodes now take any width, so it stays for a def's
parameters (#944) alone. The comment above WIDE is gone.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… 247

Its literal record folds away, so its C never built a wide node;
wide_record_drop builds a 256-field one from IO.args and drops it. The
headers of arity_wall and record_deep_boxed, and SHADERS.md's segment
limit, now say what the compiler does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril nicolas-abril self-assigned this Sep 25, 2026
Datatypes no longer box fields to fit a node, so the only layouts that
box their multi-word parts past WIDE words are a def's parameters (#944).
The helper and its comment go; the emitted C is byte-identical for every
tests/compile program, the wide-record tests and the runtime benches.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit b31f943 into main Sep 25, 2026
@nicolas-abril
nicolas-abril deleted the wide-nodes-walk-as-arrays branch September 25, 2026 17:58
VictorTaelin added a commit that referenced this pull request Sep 26, 2026
… descent is linear

bend2/bend.ts (43,884 -> 42,275 ttok):
- The parser reads every infix operator from one INFIX table (longest
  spelling wins), not a list, three special branches and an if-chain.
- The parser's binder stack and frame count live on Parse: Scope, Entry
  and Loc go; parse_book resets them once per declaration.
- A reply is its term: the Reply type goes (Body = Match | Local | LTerm).
- parse_term sets the span of a node that lacks one, so the branches that
  set the same span go, and parse_grow goes.
- {a == b : T}, {a != b : T} and {a : T} share one tail; parse_book and
  parse_def read a word once; parse_lookup, parse_term_do_call go.
- term_higher, term_lower and term_snf return a leaf as it is; the dead Sub
  cases of term_lower and term_snf go; Sub.v is a Patt.
- term_wnf's MAT frame applies its lambda-match in one place.
- term_compare: one case for Qnt, Efq and Rfl.
- uses_close closes a binder (quant_used, uses_get, uses_del go);
  ctrs_find goes; book_fam reads tele_unbind; def_inst checks its ~
  arguments with tele_check; book_valid checks a def in place.
- The book adds an open law to hols: the open field goes.
- err_show prints a context type with expr_show; char_is_name extends
  char_is_head.
- #983: term_descend skips the field that already missed, so a descent
  on a Nat literal is linear, not 2^n (tests/halt/literal_descent_linear).
- Header comments that were false now match the code.

bend2/comp.ts (63,523 -> 62,584 ttok), effs/chan.c, effs/tcp_poll.c,
effs/file_read.c, effs/window_frame.c:
- One type walk, ty_holds, finds closures and recursive datatypes:
  lay_cyclic and its cache go.
- An IO fiber is its IoWork: the IoAct wrapper and io_wait_time go.
- The pool counts with the a32 atomics: its _Atomic globals go.
- A Halt or a deadlock ends the program where the IO loop meets it; the
  CLI fails through err_fail (cli_fail goes); same texts, same codes.
- A term's spine holds its call: the Call type and call_kind go.
- One effect_srcs reads each foreign source once for both lanes.
- One mat_rows gives both lanes a match's rows; Seg.host goes.
- term_keep takes its count; the kernel votes in its group's block;
  GA32, ALC_WORDS and fid_seqk go.
- The compiler's types go from 31 to 16. One File record holds a
  compile, and a nested scope is a copy of it (Carb, Src, Dst and Show
  go); a def's raised arity and body live in its Fun, and the book is
  never copied (Def, TLD and Sig go); a flat layout's arms map a
  constructor to its fields' layouts (Arm and Field go); Nat and word
  match rows are one Row (Chain and Leaf go); HAdt, HLet, UMap and Probe
  are written as Of<K>.
- The C runtime's type names go from 26 to 14: it speaks u64, u32 and
  pointers (Loc, Cls, Fid, Err, Ring, Nat, U32, Stk, Corpus go), one
  atomic family serves host, Metal and CUDA (g32 and Cur go), the five
  u8 id tables are two byte-row tables FID_T and CID_T, and an IO queue
  is a circular list held by its last IoWork (IoQue goes).
- The JS lane runs 2.3x faster than #1061's (benches: bun 2.7x, node
  2.9x; other programs: bun 2.5x, node 1.5x) and recurses at least as
  deep: a Nat is a JS number, exact below the 2^48 cap C shares. #1061's
  tail-cycle loops stay the one call design (loop_of; a one-def cycle
  needs no $pc), and a call passes through run_loop only when its callee
  may return a closure's bounce: a marker per call resolves once every
  def is out, so a def that never bounces is called directly (bun: fib
  0.40 s against 0.65 s with run_loop on every call; node: gameoflife
  0.26 s against 0.76 s). A Nat from the host enters through nat_host: an
  integer up to 2^53 is exact, any other value fails like C's Nat check
  (tests/io/nat_host_bounds, nat_host_neg, nat_host_chan); an array
  converts in place, as before; a match field an arm never reads is not
  bound.
- Dead internal guards go; term_any walks a tree without a visited set.
- On main since b531388: #1068's wide nodes (WIDE 247, a node past it
  keeps 240 plus its size class in CID_T), #1038's intrinsic argument
  layouts, #988's --bend-help and #1061's Nat.min/max rows, in this
  model; {a != b : T} underlines its != as #1063 does.

WONTFIX.txt, guide/GUIDE.md: stale lines match the CLI and the runtime.

The emitted C differs only in the runtime text above (a program's
segments are the same, but a foreign request's local names). The emitted
JS behaves as main's for every test, demo and generated program, except a
bad Nat from the host (negative, past 2^53, not an integer), which now
fails as the C lane does instead of printing garbage. 8,286 programs and
mutants against 11c65a2: the same check, interpreter, JS (bun and node)
and C output (1,414 binaries), except those host-Nat cases and
literal_descent_linear, which main does not finish. C lane on the M4
minis (16 benches, min of 5): SEQ 0.999, PAR 0.999, GPU 1.000; emit
0.98. Linux x86 (clang-19, RTX 4090): every test, bench and CUDA test
prints as on main.

bend.ts and comp.ts follow STYLE.md: no prose comment sits in the middle
of a section (a fact the code does not show moves, in one short sentence,
into its section's doc block; comp.ts goes from 150 comment blocks to 47);
trailing comments line up; a checker function sits in Check (tele_check,
term_descend, uses_close) and f32_show in Show; each comp.ts section is a
type or a verb and holds its subject's functions (Live is Dom, Anf is ANF,
the type Gen is Tpl, emit_nats and emit_lits are mat_nats and mat_lits);
every case of a switch is a block; the title comes first; TAB_BAD,
RUNTIME_ADTS, OWNED, IDS and TAKEN move into Constants; no comp.ts line
passes 80 chars. The code tokens are unchanged: the emitted C and JS
differ only in runtime comments and whitespace.

Co-Authored-By: Claude Opus 5.5 (1M context) <[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.

C backend: a constructor with 256 single-word fields still exceeds CID_ARITY_T after #944

1 participant