Skip to content

! 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

Closed

Description

@lukaszsamson

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

import Base

# a right spine of forks under one bang: each leaf builds a short list
def row(n: Nat, +y: U32, acc: List<&2, U32>) -> List<&2, U32>:
  match n:
    case 0n:
      acc
    case 1n++p:
      row(p, y, U32.add(y, U32.from_nat(p)) <> acc)

def spine(n: Nat, +w: U32) -> List<List<&2, U32>>:
  match n:
    case 0n:
      Nil{}
    case 1n++p:
      a b = row(U32.to_nat(w), U32.from_nat(p), Nil{}) spine(p, w)
      a <> b

def total(xs: List<List<&2, U32>>, acc: U32) -> U32:
  match xs:
    case Nil{}:
      acc
    case x <> rest:
      total(rest, U32.add(acc, U32.from_nat(List.length(&2, U32, x))))

def main() -> 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:

def tree(d: Nat, +y: U32, +w: U32) -> U32:
  match d:
    case 0n:
      U32.from_nat(List.length(&2, U32, row(U32.to_nat(w), y, Nil{})))
    case 1n++p:
      a b = tree(p, y, w) tree(p, U32.add(y, U32.shln(1, p)), w)
      U32.add(a, b)

def main() -> 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).

Activity

  1. VictorTaelin commented on Sep 21, 2026

    @VictorTaelin
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions