Repository navigation
Description
A ! call whose body recurses non-tail over a list (Base's List.append, one frame per element of its first list) dies on the Metal lane with bend: memory fault (machine stack overflow?) once the list has 1023 elements. 1022 passes in the recorded runs. The same binary with --gpu off completes at the tested lengths through 100000. There are no parallel calls inside the !; it is one sequential def.
Program (deep.bend)
import Base
# [n-1, ..., 1, 0], 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)
# Sum with a tail loop.
def Sum(xs: List<&2, U32>, +acc: U32) -> U32:
match xs:
case Nil{}:
acc
case Con{h, t}:
Sum(t, (acc + h : U32))
# The only non-tail recursion: List.append over n elements.
def Deep(n: Nat) -> U32:
xs = Gen(n, 0, [])
ys = List.append(&2, U32, xs, [7])
Sum(ys, 0)
def Run(m: Maybe<&2, U32>) -> IO(Unit):
match m:
case Some{n}:
IO.print(U32.show(Deep!(U32.to_nat(n))))
case None{}:
IO.die(Unit, 2, "usage: deep <n>")
def Args(args: List<String>) -> IO(Unit):
match args:
case Con{_, Con{n, _}}:
Run(U32.read(n))
case _:
IO.die(Unit, 2, "usage: deep <n>")
def main() -> IO(Unit):
do IO<Unit>:
args : List<String> <- IO.args()
Args(args)Commands and output
$ bend deep.bend -o deep
$ ./deep 1022
521738
[exit 0]
$ ./deep 1023
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./deep 1023 --gpu off
522760
[exit 0]
$ ./deep 100000
bend: memory fault (machine stack overflow?)
[exit 1]
$ ./deep 100000 --gpu off
704982711
[exit 0]
The 1022/1023 boundary is exact and deterministic (3 of 3 runs each). The program and these commands are the whole repro.
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 (right spine of forks, fixed in 2.0.23) and Metal GPU regression since 2.0.22: continuation-stack overflow #930 (fork-leaf handoff, fixed in 2.0.24) hit the same message. This one has no forks at all. The close note on #930 says "a bang's root and a fork kid still run on the lane stack on the device". My inference, not a runtime diagnosis: about two words a frame on that 2048-word lane stack would fit the observed boundary. This may be the known remaining limit rather than a regression.- What I would ask for: either the lane stack spills to the heap (the general answer Metal GPU regression since 2.0.22: continuation-stack overflow #930 mentions), or the fault names the cause ("non-tail recursion deeper than the device lane stack under
!"), or the guide states the limit. Today a program that runs with--gpu offdies with the GPU on, and the message does not say why.
Environment
- bend 2.0.34 (
bend version). Later releases are untested here, including 2.0.36; this report makes no claim about their behavior. - 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 deep.bend -o deep; the.gpuprogram sits beside the binary.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Giulio2002 commentedon Oct 8, 2026
The same fault reproduces on the CUDA lane, at the same boundary — this doesn't look Metal-specific after all. Measured on Linux x86_64, bend 2.0.36 (commit 1d895ca), NVIDIA RTX PRO 6000 Blackwell (driver 595.91.07, CUDA 13.2), bun 1.4.2, clang 21.
What you did
Non-tail recursion under a bang, with the list built tail-recursively so construction is not the source of depth (the recursion being tested is the sum walk):
What happened
By depth, on the same binary:
./t --gpu off(CPU pool)./t(CUDA)bend: memory fault (machine stack overflow?)bend: memory fault (machine stack overflow?)The 1023/1024 boundary matches this issue's Metal threshold exactly, 10/10 deterministic at n=2000 — so the per-lane device stack cap looks shared by (or mirrored in) the CUDA build, not a Metal quirk. The CPU pool completes through 1,000,000 elements, as here.
For completeness, two adjacent CUDA-path behaviors checked while there (both clean, both negative results for their respective open issues): with
--gpu 512MB/--gpu 1GBcaps, exceeding the device heap reports the gracefulbend: out of memory: run again with a bigger span, as in --gpu 8GBand exits 1 (#1300's fault/hang class did not reproduce on CUDA at the allocation-failure points I could construct); and a large[0 : U32^28n]allocation inside a bang does run on the device (visible in nvidia-smi) but takes ~20 s of host CPU time where--gpu offdoes the same work in 1.2 s wall — possibly the same per-lane serialization, possibly host-side page servicing; I did not chase that down far enough to call it a finding.Found by an automated AI-model differential-testing campaign against bend 2.0.36; the threshold table and determinism count were re-measured in isolation.
wakamex commentedon Oct 10, 2026
Same limit on CUDA here, but whether you hit it depends on how many tasks the bang spreads to. While some lanes are free, a lane can recurse much deeper than 1,023. Once every lane has a task, any recursion past about 1,024 calls faults. Measured on main (d5fe656), RTX 3090, driver 615.71, CUDA 13.1.
This forks 2^d leaves under one bang. Leaf i builds a list of n copies of i and sums it with the same non-tail
lsumas above:./deep D N --gpu 8GB, each list N long:--gpu offgets every cell right, and a 20 GB heap faults the same way. The 3090 has 82 SMs, soCUBE_LOGis 6 and the bag is 128 << 6 = 8,192 lanes (comp.ts#L5019): the fault starts exactly when there are as many leaves as lanes. The single-leaf row is interesting too: the samelsum20,000 deep works as one leaf ofleaves!, butprobe!(2000n)from the comment above still faults on this commit. So I'd guess the fork-free case, or the case where every lane is busy, runs on the fixed lane stack (STAK_LEN2^11 words, comp.ts#L3501), andWL_ROOMpostsERR_DEEPwhen it fills (comp.ts#L3423). I haven't confirmed that though.This is what caps the batch size in a real workload. I run nanoGPT inference as a batch of independent generations, one per leaf, over shared weights (bend-bench ML.md). It works up to 4,096 sequences and faults within a second from 8,192, at any heap size, while the CPU runs 8,192 fine. So the GPU never gets past half its lanes on that workload.
Two smaller things:
ERR_DEEP, "memory fault (machine stack overflow?)", sounds like a heap or host problem. Something like "a GPU lane's stack is full: recursion deeper than about 1,000 calls" would have saved me a few rounds of heap sizes.README reports the machine-learning results; ML.md links the two upst…
A device lane whose stack fills moves the words below the calling def…
!over a two-leaf fork tree (a 365-fork spine beside a 3-fork spine) faults; each leaf alone, a balanced 365+365, and--gpu offall pass #1394 as a duplicate of this issueMetal: 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…
Lorenzobattistela commentedon Oct 21, 2026
Reopening: the fix from #1439 is removed in #1809.
lane_spilljoins the device code of every!program, so it made the Metal GPU program's build (./main +RTS --gpu-build, part ofbend -o) 17% slower: 0.257 s → 0.300 s on an M4 mini. That pushed the perf gate's COMPILER column past its 1.15× limit (symreg 1.16×, merkle 1.17×). I bisected it over v2.0.37..main, and it reproduces on a second mini and on an M4 Pro. The data is in #1809.The fault at 1023 frames is back until the spill returns in a form that only programs needing it pay for, for example built into the device program only when a
!actually reaches deep non-tail recursion.tests/compile/bang_lane_spill.bendfrom #1439 is the test to bring back with it.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…
jkbennitt commentedon Nov 10, 2026
Confirming this still reproduces on Bend 2.0.40 (
bend version), Apple M4 / Metal 4, macOS 27.0.1 (Darwin 27.0.0 arm64, Apple clang 21.0.0). I hit it from a slightly different shape than the original sequentialList.append: a divide-and-conquer map that mid-splits and thenList.appends the two halves, inside a nearest-reference argmin. That looks like the join-append case wakamex mentioned (overflow around 2,048 sequences). It came up while benchmarking a U-NSGA-III implementation in Bend.With 3 reference points, N = 2046 crashes (first half has 1023 elements) and N = 2045 passes (first half 1022). The same binary with
--gpu offprints the expected length at both sizes. Two GPU runs at N = 2046 both exited 1 withbend: memory fault (machine stack overflow?).Standalone 37-line repro (N = 2046, R = 3)
Changing both
2046nto2045ninmainsucceeds on the GPU and prints2045.A full device lane moves its stack to the heap, so non-tail recursion…
wakamex commentedon Nov 14, 2026
Thanks for the repro. It's the same cause as the original report:
List.appendisn't tail-recursive, so inside a!every element of the first list is a frame on the lane's 2,048-word stack, 2 words each, and a 1,023-element half overflows it. I see the same boundary on CUDA (RTX 3090) with my own divide-and-conquer append: 2,045 runs, 2,046 faults.#1819 makes that shape finish: 2,046 and 100,000 elements on CUDA, and 100,000 on an M4 in 0.19 s. I just pushed a rework there for Lorenzo's review, and it's up to him. Until it lands, writing the append as two tail loops (reverse the first list, then move it onto the second) keeps the stack flat, as nuxyel did for their matrix product.