Skip to content

ty_holds walks a type family's body, so a bang over a pair no longer puts every closure on the device - #1336

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
chaserelock:fix/gpu-pair-closure-reach
Oct 8, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
chaserelock:fix/gpu-pair-closure-reach

Conversation

@chaserelock

@chaserelock chaserelock commented Oct 5, 2026 •

Copy link
Copy Markdown

Cause

A & B is Sigma<A, _ => B>, so a pair's second half is a type family: a
Lam. ty_clo answered true for any Lam, so a bang with a pair parameter
set wide. The device then took every closure in fl.clos, plus everything
those closures reach. Any bang over an array pair hits this.

A bang head! over Array<U32> & Array<U32>, in a main with a few host-only
IO.bind continuations (device segments: those the C does not wrap in
#if !DEVICE):

parameter main this PR
p: Array<U32> & Array<U32> 24 of 28 5 of 28
a: Array<U32>, b: Array<U32> 5 of 28 5 of 28

Fix

The change has two parts, both in ty_holds in comp.ts:

  1. Open a family's body. ty_holds walks a Lam's body, as
    type_adts already does, so its predicates no longer see a Lam.
  2. Skip an argument that takes a value. A datatype's argument is walked
    unless its parameter's kind holds no sort (ty_value): a quantity, an
    equality, a datatype that holds none, or a function into one. A datatype
    kind is walked with ty_holds to tell, because a datatype value can carry
    a type: in Hold<b: TyB> with TyB{T: Type}, the argument of
    Hold<TyB{U32 -> U32}> is what names the function.

js_marshal runs a new Nat walk at each level of an effect's type, and each
walk now reaches into a pair's second half. So a right-nested tuple with a
Nat at the bottom costs O(n²) in its length. This was already so for
nesting through datatype arguments (Maybe<Maybe<…<Nat>>>). The walk runs
once per foreign def and .mjs export, on its signature, not per call, so
it is left uncached.

ty_clo is unchanged: a closure in either half of a pair, at any depth,
still puts the program's closures on the device, and a pair of closure-free
halves no longer does.

The other two ty_holds walks had the same blind spot, and the change closes
it for both:

  • JS marshaling: js_marshal's Nat walk found U32 & Nat's Nat through
    the Sigma's fields. In U32 & U32 & Nat it had already seen Sigma, so
    that Nat crossed the effect seam, and left a .mjs export, as a number,
    not the BigInt that io/marshal_nat_bigint pins. It's a BigInt now.
  • Layouts: lay_of's check for a field that re-enters its own datatype
    now also sees one through a nested pair's second half. No layout in the
    repository changes.

Alternatives

  • Adding "Lam" to ty_clo's list fixes the array pair but loses real
    closures. ty_holds stops at a datatype it has seen, so in
    U32 & U32 & (U32 -> U32) nothing reaches the inner Sigma's second half,
    and the lambda minted on the host has no device code.
    compile/gpu_reach_closure_pair fails on the GPU with that patch:
    bend: a function the device does not hold.
  • Opening the Lam only for ty_clo, by having it answer null and
    ty_holds open it only then, emits the same C everywhere. But it leaves the
    marshal hole open, and it splits one rule about types between the walk and
    one of its predicates. It is also +45 ttok against this PR's +28.
  • Keying seen by the instantiated type rather than its name also reaches
    the inner Sigma. But polymorphic recursion (Nest<A> holding a
    Nest<List<A>>) has unboundedly many instances; the name is what ends the
    walk.

Checks

The branch is rebased on main (7b7559c).

For the whole branch (on one Mac):

  • New test: compile/gpu_reach_closure_typed is a bang over
    Hold<TyB{U32}> & Hold<TyB{U32 -> U32}>. It prints 43n on the GPU, the
    CPU and JS. With the earlier rule, which counted every datatype kind as a
    value, it fails on the GPU with bend: a function the device does not hold.
  • All gpu_reach* tests and io/marshal_nat_pair pass on the GPU, the CPU
    and JS.
  • Output: for the 1,268 tests under tests/ that import Base, the
    emitted .c and .js match the earlier, larger version of this fix
    byte for byte.
  • Not here: a family that unfolds to itself
    (def Stream(-A: Type) -> Type: A & Stream(A)) makes ty_holds walk
    without end. On main, type_adts and facts_hot already overflow the
    stack on such a family, so that bound belongs in its own change, with a
    test that builds end to end.
  • Size: comp.ts is 63,718 ttok, under the 64k cap.
  • Gates and types: bun gates/repo.ts passes (50/50). tsc -p bend2/pack
    reports no errors in comp.ts.

For the first commit only, measured before the later commits:

  • Output: of the 1,654 .bend files under tests/, demos/ and
    bench/, 1,169 check. For each, the emitted .c, .js and .mjs are
    byte-identical to main's except:
    • C: reg/spin_array_hold, whose fan! takes an
      Array<U32> & Array<U32>. Its device segments go from 27 of 28 to 8.
    • JS: the new io/marshal_nat_pair.
    • .mjs: run/literal_sugar, show/literal_readback and
      io/readback_sugars. Each exports a Nat in a nested pair's second half,
      which is now a BigInt.
  • New tests:
    • compile/gpu_reach_closure_pair hands the device a closure in the second
      half of a pair's second half and applies it there. It prints 43n on the
      GPU, the CPU, JS and the interpreter. It also passes on main, which puts
      every closure on the device; it guards the first alternative above.
    • io/marshal_nat_pair hands two JS effects U32 & Nat and
      U32 & U32 & Nat and prints the Nat's typeof. main prints bigint,
      number.
  • Test gate: I ran gates/test.ts's own shard script on one Mac, with
    the ssh hop swapped for a local shell. Result: PASS: 1568 / 1571. The 3
    failures fail the same way on main there:
    • gfx_clicks and gfx_window expect no display, but this Mac has one.
    • io_process_run fails its inherited-pipe cases.
  • Benches (Apple M5):
    • All 17 runtime benches emit byte-identical C, so the SEQ-CPU, PAR-CPU and
      PAR-GPU columns cannot move. Each prints its pinned OUTPUT in all three
      modes.
    • Emit time (-o main.c, hyperfine, 15 runs each): branch/main
      geomean 1.008 with main run first and 0.985 with the branch first. No
      bench moves more than 4%.
    • COMPILER (-o main, median of 5, interleaved): 0.98–1.04×.
    • CHECKER: all ALL PROOFS CHECK, and the emitted code is identical.
      A median of 3 gave 0.97–1.06×. I re-measured generics_3200's 1.06 with
      hyperfine, 30 runs in each order: 0.988 with main first and 1.040 with
      the branch first. The ratio follows the run order, not the code.
  • Not run: the cluster gates (gates/perf.ts against the apple_m4 pins,
    and gates/test.ts on the minis), and CUDA, Linux and x86.

🤖 Generated with Claude Code

@chaserelock
chaserelock force-pushed the fix/gpu-pair-closure-reach branch from 045cab3 to 1b74da2 Compare October 5, 2026 05:40
@chaserelock chaserelock changed the title ty_holds opens a type family, so a bang over a pair no longer puts every closure on the device ty_holds walks a type family's body, so a bang over a pair no longer puts every closure on the device Oct 5, 2026
@chaserelock

Copy link
Copy Markdown
Author

Measured this branch against main on a real program: a Llama inference runner (llama2.c's stories15M) written in Bend. Its only bang is a matmul fork tree that takes Array & Array pairs. M5, macOS, Metal.

  main this branch
segments the device program holds (emitted without #if !DEVICE) 780 of 781 8
cold --gpu-build 461 s 0.22 s
GPU program 11.6 MB 175 KB
decode, 64 steps, --gpu on 5.5 tok/s 38.5–39.0 tok/s

@chaserelock
chaserelock force-pushed the fix/gpu-pair-closure-reach branch 2 times, most recently from 6e66b08 to c996bfd Compare October 5, 2026 18:05
@nicolas-abril nicolas-abril self-assigned this Oct 5, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks, the diagnosis is right, and the change to open a family's body is needed. I pushed one commit on top (d3141f9) that turns it into a more general fix of the same root cause.

The root cause is wider than pairs

ty_holds hands every argument of a datatype to its predicate as if it were a type. A pair's family is one argument that isn't a type, but there are others:

  • value indices, as in type Box<-n: Nat>, where Box<3n> has the argument 3n;
  • the DUMMY a dependent family opens with: in Exists(Nat, n => Box<n>), opening the family gives Box<DUMMY>;
  • quantities.

ty_clo takes any of these that isn't Typ/Qua/Min/Eql for a possible closure, so with this PR alone a bang over them still puts every closure on the device. Device segments, measured as in the PR description, with a main that has a few host-only continuations:

bang parameter main c996bfd d3141f9
Array<U32> & Array<U32> 19 of 20 5 5
a: Array<U32>, b: Array<U32> (baseline) 5 of 20 5 5
Box<3n> 19 of 20 19 5
Exists(Nat, n => Box<n>) 19 of 20 19 5
U32 & Box<3n> 19 of 20 19 5
U32 & (U32 -> U32) (a real closure) 20 of 21 20 20

The fix

ty_holds now walks an argument unless its parameter surely takes a value. It reads the parameter's kind from the datatype's telescope (tld.T), and ty_value says yes only when that kind is surely not a sort. That means a quantity (Qnt, Min), a datatype (its values are never types), an equality, or a function into one of those.

The polarity is deliberately conservative. Any kind it can't resolve, such as a variable, a stuck family or a def that doesn't reduce, is still walked. A missed closure fails on the device ("a function the device does not hold"), while an extra one only costs device code, which is the cost this PR removes. Sigma's second half has kind A -> Kind(b), a family into a sort, so it is still walked, and your Lam opening still walks its body.

The same rule applies to ty_holds's other two users, js_marshal's Nat walk and lay_of's re-entry check. Their predicates already answered false for values, so nothing changes for them.

Test

compile/gpu_reach_closure_indexed follows gpu_reach_closure_pair, but goes through the paths the new rule touches. The bang takes U32 & Exists(Nat, n => Fn<n>), where Fn<-n: Nat> holds a U32 -> U32. The inner pair is a datatype the walk has already seen, so only its arguments are walked: the quantities and the index are skipped as values, and the family naming Fn is opened.

  • With the fix: it prints 43n on the GPU (--gpu on), on the CPU (--gpu off), in JS and in the interpreter.
  • With a walk that skips every argument: --gpu on fails with bend: a function the device does not hold.
  • With a walk that also skips families: the same failure.

The device overreach itself doesn't show up in any program's output, so the table above is measured rather than pinned by a test.

Checks (Apple M4, on this branch)

  • Output: for all 1,654 .bend files under tests/, demos/ and bench/, the emitted .c, .js and .mjs, and every build message, are byte-identical to c996bfd's, since no existing program has a bang over an indexed or dependent type. Against main they differ in exactly the 15 outputs your description lists, plus the new test.
  • All tests (gates/test.ts's shard script on one Mac): 1,570 of 1,572 pass, and the two gfx tests fail as on main.
  • Cluster gates: repo 50/50, test 1,572/1,572, perf 120/124 (with the emitted code byte-identical, the cells over 1.15x are run-to-run noise).
  • Size: comp.ts goes from 63,160 to 63,300 ttok (+140 over c996bfd).

🤖 Generated with Claude Code

chaserelock and others added 2 commits October 8, 2026 07:11
…puts every closure on the device

A & B is Sigma<A, _ => B>. ty_clo answered true for that Lam, so a bang
with a pair parameter put every closure of the program on the device.
ty_holds now walks a family's body, under the same seen set, before asking
its predicate, as type_adts does.

Answering false for the Lam instead loses real closures: ty_holds stops at
a datatype it has seen, so in U32 & U32 & (U32 -> U32) the inner Sigma's
second half went unwalked. The marshal walk had the same hole: U32 & U32 &
Nat's Nat crossed the effect seam as a number, U32 & Nat's as a BigInt.

compile/gpu_reach_closure_pair applies a closure from a nested pair on the
device; io/marshal_nat_pair hands JS effects a Nat at both depths.
…s a value, so an index or a dependent pair no longer puts every closure on the device

ty_holds handed every argument of a datatype to its predicate as a type.
A pair's family was one argument that is no type; a value index (Box<3n>)
and the DUMMY a dependent family opens with (Exists(Nat, n => Box<n>)) are
others, and ty_clo took each for a closure: a bang over any of them still
put every closure of the program on the device (19 of 20 segments, where
5 is the closure-free baseline). ty_holds now skips an argument only when
its parameter's kind is surely no sort: a quantity, a datatype, an
equality, or a function into one (ty_value). Any kind it cannot resolve is
still walked, since a missed closure fails on the device where an extra
one only costs code. A family into a sort, as Sigma's second half, is
walked and opened as before.

compile/gpu_reach_closure_indexed applies, on the device, a closure that
only the walk of a seen pair's family argument reaches, through a
value-indexed datatype: it prints 43n on the GPU, the CPU, JS and the
interpreter, and fails with "a function the device does not hold" under a
walk that skips every argument or one that also skips families. The
emitted C, JS and .mjs are byte-identical to the parent's for every file
under tests/, demos/ and bench/.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril force-pushed the fix/gpu-pair-closure-reach branch 2 times, most recently from ce3dc7a to bf755ce Compare October 8, 2026 08:12
…, so a datatype value that carries a type keeps its closures on the device

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.

2 participants