You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
! 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 ! call whose parallel lets form a long right spine (a b = f(x) go(rest), the natural shape of "one task per element of a list") dies on the Metal lane with bend: memory fault (machine stack overflow?) once the spine is roughly 1000 levels deep. --gpu off runs the same program fine at any depth, and a balanced fork tree over the same number of leaves runs fine on the GPU.
Program
importBase# a right spine of forks under one bang: each leaf builds a short listdefrow(n: Nat, +y: U32, acc: List<&2, U32>) ->List<&2, U32>:
matchn:
case0n:
acccase1n++p:
row(p, y, U32.add(y, U32.from_nat(p)) <>acc)
defspine(n: Nat, +w: U32) ->List<List<&2, U32>>:
matchn:
case0n:
Nil{}
case1n++p:
ab=row(U32.to_nat(w), U32.from_nat(p), Nil{}) spine(p, w)
a<>bdeftotal(xs: List<List<&2, U32>>, acc: U32) ->U32:
matchxs:
caseNil{}:
acccasex<>rest:
total(rest, U32.add(acc, U32.from_nat(List.length(&2, U32, x))))
defmain() ->IO(Unit):
IO.print(U32.show(total(spine!(1000n, 96), 0)))
bend spine.bend -o spine
./spine --gpu off # 96000
./spine --gpu on # bend: memory fault (machine stack overflow?) exit 1
What varies it
spine length
--gpu off
--gpu on
200, 300, 400, 500
ok
ok
1000
ok
memory fault
3000, 10000
ok
memory fault
1000 with w = 1 (a one-word leaf)
ok
memory fault
So it is the depth of the spine, not the work per leaf or the size of the result. The same 1000 leaves as a balanced tree are fine on the GPU:
deftree(d: Nat, +y: U32, +w: U32) ->U32:
matchd:
case0n:
U32.from_nat(List.length(&2, U32, row(U32.to_nat(w), y, Nil{})))
case1n++p:
ab=tree(p, y, w) tree(p, U32.add(y, U32.shln(1, p)), w)
U32.add(a, b)
defmain() ->IO(Unit):
IO.print(U32.show(tree!(10n, 0, 96))) # 98304 on both lanes
Where I hit it first: a ray tracer whose ! forks a list of tiles as a spine, each tile forking its rows as a tree; there 300 tiles were enough, presumably because the per-tile trees add their own depth. The upstream guide already recommends balanced trees for lane utilisation, so this may be "as designed" for performance; but the failure mode is a host segfault reported as a stack overflow rather than an error naming the cause, and the CPU pool accepts the same shape, so a program that works with --gpu off dies when the GPU is on.
Environment
bend 2.0.20; macOS 26.7 on an Apple M2 Pro; Apple clang 21.0.0 (clang-2100.3.34.2). Built with bend file.bend -o file (the .gpu program is written and used; the balanced-tree program proves the lane works).
Fixed in 2.0.23, thank you for the table that isolated the depth. The cause was on the device: the grow pass stopped after 128 turns per kernel, so 256 levels of the spine were unrolled as tasks and the rest ran on one lane's 2048-word stack in sequential mode, four words a level, which is why 767 passed and 768 died. The cores' grow pass has no such cap. The device now grows until the group is full or nothing grows, like the cores, so your program prints 96000 on the GPU and a 10000-level spine runs in 0.17 s. A spine of forks is still slower than a balanced tree, as the guide says, but it no longer dies. Pinned as tests/compile/fork_spine_unroll.bend. bend update brings it.
Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
Activity
A right spine of forks under ! runs on the GPU at any depth the cores…
VictorTaelin commentedon Sep 21, 2026
Fixed in 2.0.23, thank you for the table that isolated the depth. The cause was on the device: the grow pass stopped after 128 turns per kernel, so 256 levels of the spine were unrolled as tasks and the rest ran on one lane's 2048-word stack in sequential mode, four words a level, which is why 767 passed and 768 died. The cores' grow pass has no such cap. The device now grows until the group is full or nothing grows, like the cores, so your program prints 96000 on the GPU and a 10000-level spine runs in 0.17 s. A spine of forks is still slower than a balanced tree, as the guide says, but it no longer dies. Pinned as
tests/compile/fork_spine_unroll.bend.bend updatebrings it.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
A right spine of forks under ! runs on the GPU at any depth the cores…