Skip to content

Metal: one ! 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 off all pass #1394

Closed as duplicate

Description

@Eziowl

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 3 and d = 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 same List.append from 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

Environment

Activity

  1. Lorenzobattistela commented on Oct 16, 2026

    @Lorenzobattistela
    Collaborator

    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!

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