Repository navigation
Description
A ! call over a small binary tree whose two leaves are fork spines of very different lengths dies on the Metal lane with bend: memory fault (machine stack overflow?). Each leaf alone passes under !, two equal leaves (365 + 365) pass, and the CPU pool (--gpu off) runs every shape. The fault is deterministic (3 of 3 runs of 365 3, and every shape below gave the same result on each rerun of the transcript), and --gpu 48GB does not change it.
Shape: the tree is Tree(WTwo{a, b}) → x y = Tree(a) Tree(b); each leaf of size k is a spine of k nested parallel calls a b = Spine(p, ...) Leaf(i, ...); each Leaf appends a generated list of d numbers to one of w numbers with Base's List.append (so d non-tail frames) and folds the result with a tail loop. All input is generated in code.
Program (forks.bend)
# Repro: one `!` call over a small, unbalanced fork tree faults on Metal,
# while each of its parts passes alone, a balanced tree of the large part
# passes, and the CPU runs every shape.
#
# bend forks.bend -o forks
# ./forks <w> <d> <a> <b> ... # Metal: one `!` over items a, b, ...
# ./forks <w> <d> <a> <b> ... --gpu off # CPU
#
# Every input is built in code. An item of size k is a "spine": k nested
# parallel calls, each pairing the rest of the spine with one leaf job. A leaf
# job appends a generated list of d numbers to one of w numbers with Base's
# List.append (d non-tail frames, under the 1022 that one lane holds when
# it is a bang's root, per the companion deep-recursion repro), then folds the
# result with a tail loop. The items sit as the leaves of a balanced binary
# tree, and the whole tree is one `!` call.
import Base
# [n-1, ..., 1, 0] plus a seed, built with a tail loop.
def Gen(n: Nat, +k: U32, acc: List<&2, U32>) -> List<&2, U32>:
match n:
case 0n:
acc
case 1n+p:
Gen(p, (k + 1 : U32), k <> acc)
# A small mixing fold, tail recursive.
def Mix(xs: List<&2, U32>, +acc: U32) -> U32:
match xs:
case Nil{}:
acc
case Con{h, t}:
Mix(t, ((acc * 31 : U32) + h : U32))
# One leaf job: append a generated list of d numbers to one of w numbers
# (List.append: d non-tail frames), then fold the result.
def Leaf(+i: U32, +w: U32, +d: U32) -> U32:
Mix(List.append(&2, U32, Gen(U32.to_nat(d), i, []), Gen(U32.to_nat(w), i, [])), i)
# An item of size k: k nested parallel calls along a spine.
def Spine(n: Nat, +i: U32, +w: U32, +d: U32) -> U32:
match n:
case 0n:
0
case 1n+p:
a b = Spine(p, (i + 1 : U32), w, d) Leaf(i, w, d)
(a + b : U32)
type Work is Data:
WNone{}
WOne{k: Nat}
WTwo{l: Work, r: Work}
# Pair neighbours, one level up.
def Pairs(ws: List<&2, Work>) -> List<&2, Work>:
match ws:
case Con{a, Con{b, t}}:
WTwo{a, b} <> Pairs(t)
case _:
ws
def Join(n: Nat, ws: List<&2, Work>) -> Work:
match n ws:
case _ Nil{}:
WNone{}
case _ Con{w, Nil{}}:
w
case 0n _:
WNone{}
case 1n+p _:
Join(p, Pairs(ws))
# The items as a balanced binary tree; both halves of a node run in parallel.
def Tree(t: Work, +w: U32, +d: U32) -> U32:
match t:
case WNone{}:
0
case WOne{k}:
Spine(k, 0, w, d)
case WTwo{l, r}:
a b = Tree(l, w, d) Tree(r, w, d)
(a + b : U32)
def Item(m: Maybe<&2, U32>) -> U32:
match m:
case Some{n}:
n
case None{}:
0
def Items(args: List<String>, acc: List<&2, Work>) -> List<&2, Work>:
match args:
case Nil{}:
List.reverse(&2, Work, acc)
case Con{s, t}:
Items(t, WOne{U32.to_nat(Item(U32.read(s)))} <> acc)
def Run(+w: U32, +d: U32, ws: List<&2, Work>) -> IO(Unit):
IO.print(U32.show(Tree!(Join(32n, ws), w, d)))
def Args(args: List<String>) -> IO(Unit):
match args:
case Con{_, Con{w, Con{d, t}}}:
Run(Item(U32.read(w)), Item(U32.read(d)), Items(t, []))
case _:
IO.die(Unit, 2, "usage: forks <leaf width> <leaf depth> <size> ...")
def main() -> IO(Unit):
do IO<Unit>:
args : List<String> <- IO.args()
Args(args)Commands and output
w = 64, d = 800 unless noted.
$ bend forks.bend -o forks
$ ./forks 64 800 365
4280936846
[exit 0]
$ ./forks 64 800 3
2078004979
[exit 0]
$ ./forks 64 800 365 365
4266906396
[exit 0]
$ ./forks 64 800 365 3
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./forks 64 800 365 3 --gpu off
2063974529
[exit 0]
$ ./forks 64 800 173 3
2170616609
[exit 0]
$ ./forks 64 800 174 3
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./forks 64 800 365 3 3 3 3
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./forks 64 417 365 3
2125310752
[exit 0]
$ ./forks 64 418 365 3
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./forks 64 800 365 3 --gpu 48GB
bend: memory fault (machine stack overflow?)
[exit 1]
The corresponding --gpu off runs complete and print numeric checksums rather than the Metal fault. The program and these commands are the whole repro.
What the boundaries say, as far as I can read them:
- With two leaves
N 3andd = 800, N = 173 passes and N = 174 faults. - With leaves
365 3, d = 417 passes and d = 418 faults, while a single leaf of 365 passes at d = 800 (and the single-lane limit for the sameList.appendfrom a bang's root is 1022 frames, see the companion issue). So in the unbalanced tree, a leaf job seems to get about half the lane stack it gets otherwise. [My inference from the numbers, not from reading the runtime.] - Equal leaves (365 + 365) pass at d = 800; small equal leaves (3 3 3 3 3; 1 1) pass; 10 + 3 passes, and so do 10 or 30 beside four 3s; 100 + 100 and 100 + 50 pass; 365 + 200 faults.
Relation to earlier issues
!over a right-spine fork of ~1000 levels dies with "memory fault (machine stack overflow?)" on Metal; the CPU pool and a balanced tree of the same leaves are fine #918 (a right spine of ~1000 forks, fixed in 2.0.23): here a spine of 365 alone passes, and only the pairing with a short spine faults.- Metal GPU regression since 2.0.22: continuation-stack overflow #930 (fork-leaf handoff, fixed in 2.0.24): its close note says a fork kid still runs on the lane stack on the device. This may be the same lane stack shared by two tasks once the tree is unbalanced, but I have not confirmed it.
Environment
- bend 2.0.34 (
bend version). Later releases are untested here, including 2.0.36, whose notes include a work-pass change after a grow (The work pass after a grow runs each lane's own ring, up to the put the grow left (#925) #1132). This report does not establish whether the fault persists there. - macOS 26.5.2 (25F84), Apple M3 Ultra, Darwin arm64; clang on PATH: Apple clang 21.0.0 (clang-2100.1.1.101).
- Built with
bend forks.bend -o forks; the.gpuprogram sits beside the binary.
Activity
A device lane whose stack fills moves the words below the calling def…
Lorenzobattistela commentedon Oct 16, 2026
Closing as a duplicate of #1393: both hit the same Metal lane-stack limit ("memory fault (machine stack overflow?)"), and PR #1439 fixes both. Tracking there. Thanks for the repro!
Metal: a full lane stack spills to the heap, so non-tail recursion un…
Remove the Metal lane spill (#1439): `bend -o` of a `!` program built…
A full device lane jumps to one spill site after loading r0..rn, so n…
A device lane short of stack moves it to the heap and back, so non-ta…
A device lane short of stack moves it to the heap and back, so non-ta…
A full device lane moves its stack to the heap, so non-tail recursion…