Skip to content

Metal GPU regression since 2.0.22: continuation-stack overflow #930

Closed

Description

@hg7643

What you did

bend tests/field_d2_probe.bend -o field_probe
./field_probe --gpu-build
./field_probe --gpu on --threads 4 zero gpu 640

What happened

A Metal GPU field evaluator that passes with stock Bend 2.0.21 fails with stock 2.0.22 and 2.0.23 on identical frozen application sources.

Actual output (exit 1):
bend: memory fault (machine stack overflow?)

Expected: exit 0 and 16,384 samples, each exactly 20 C for the source-free zero-field fixture. This fixture deliberately makes the numerical expectation simple. Actual GPU execution is checked by a small IO foreign effect returning the runtime's GPU-active flag; no CPU fallback is counted as a GPU pass.

Environment: Apple M3 Ultra, macOS 27.0 (26A428), Darwin arm64; Homebrew clang 21.1.8; official darwin-arm64 release compilers.

REPRODUCTION
The source-only archive is attached in the first comment and linked below. It contains the exact frozen probe and its project import closure, including the runtime IO effect, plus README build/run commands. It has no compiler binaries or generated C. This is the retained application reproducer, not a claim of a minimal test case.

VERSION/WORKLOAD MATRIX (original field probe)

  • 2.0.21: 16,384 zero-field queries, maximum mode 640: PASS; independent all-point error 0 C.
  • 2.0.22: the same retained large GPU field gate: FAIL.
  • 2.0.23: 61-query small fixture, mode 640: PASS; independent error about 3.20e-14 C.
  • 2.0.23: 16,384 zero-field queries, mode 512 or 640: FAIL.
  • 2.0.23: repeated mode-640 zero-field run: FAIL.
  • 2.0.23: mode-640 zero field with 8 GB GPU heap: FAIL.
  • 2.0.23: 16,384 mixed-zone queries, mode 640: FAIL.
    The small fixture differs in geometry as well as query count; this matrix alone is not a query-count threshold measurement.

DIAGNOSIS (instrumented copies of 2.0.23 emitted C)
The normal error text is ambiguous, so we separately instrumented the device WL_ROOM guard and the host signal handler. The failing run reported a device guard failure in generated Result.bind:
DIAG_DEVICE code=7 line=19102 stack_words=2048 push=1 limit=2048
No host signal was reported. The line number belongs to the instrumented application C, not upstream source.

The captured lane stack contains a recurring five-word continuation cycle:
SUMS_K28 -> RESULT_BIND_K285 -> SUMS_K27 -> RESULT_BIND_K285 -> SUMS_K26
There were approximately 398 modal cycles, plus outer frames and helper temporaries. The Bend sums function ends with a recursive call inside do Result, but generated result-conversion continuations remain pending across iterations.

Two isolated diagnostic changes make the SAME 16,384-query, mode-640 zero-field workload pass with independent error 0 C:

  1. Change STAK_LEN from 1<<11 (2048 words) to 1<<13 (8192).
  2. Keep STAK_LEN=2048, but disable the new emitted static-callee handoff branches of the form "if (!seq && fid_nofk(FID_...))". There are 236 such sites in this emitted program. Other scheduler logic and the current stack guard are retained.
    These are diagnostic experiments, not proposed production fixes or full qualification of patched runtimes.

The generated-code comparison points to the helper-call handoffs introduced in 2.0.22: execution hands fork-free helpers to worker tasks; returning to enclosing modal/Result continuations preserves worker sequential mode, so the accumulating continuations consume the fixed device stack rather than heap continuations. Disabling those handoffs isolates their role in the regression. Increasing heap size alone does not enlarge this fixed stack.

RELATION TO #918
#918 concerns a deep right-spine fork and grow-pass limit. Our original query tree is balanced, depth 12, with at most four query points per leaf. The 2.0.23 fix for #918 does not fix this reproducer.

We would appreciate guidance on the intended stack-safety behavior for Result-based recursive loops under GPU scheduling. A regression test plus a compiler/runtime correction that avoids unbounded continuation accumulation would be preferable to increasing the fixed stack or globally disabling the scheduling optimization.

The file

Download the source-only reproducer

SHA-256: c3d85d16007a6a666ef951f2ce3f5f61d3b10b73cce665e611ddce12a4ca7e36.

Contains 20 exact frozen application/foreign source files plus README and SHA256SUMS. Entry point: tests/field_d2_probe.bend. Relevant implementation: src/field_d2.bend, especially sums and homogeneous_loop. No compiler binaries, generated C, or Python verification dependencies.

bend --version

bend 2.0.23

uname -sm

Darwin arm64

clang --version (the first line)

Homebrew clang version 21.1.8

Activity

  1. added
    bug
    on Sep 21, 2026
  2. hg7643 commented on Sep 21, 2026

    @hg7643
    Author

    Exact source-only reproducer: 20 frozen source files, README and SHA256SUMS; 46,568 bytes.

    SHA-256: c3d85d16007a6a666ef951f2ce3f5f61d3b10b73cce665e611ddce12a4ca7e36.

    Extract the archive, enter bend-metal-regression, and follow README.md. Use separate output directories for each stock compiler version.

    bend-metal-result-continuation-regression.zip

  3. VictorTaelin commented on Sep 21, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.24, which is out now (bend update). Thank you for the exact reproducer: it built and failed here on the first try, and it passes now (16,384 samples at 20 C, exit 0).

    What happened. 2.0.22 merged PR #876, which hands a fork-free leaf reached from a forking def inside a ! off as its own task, on the host and on the device. On the host that task runs on a 2 GiB stack. On the device it runs on its lane's fixed 2048-word stack. Your sums loop keeps two frames per mode: do Result is Result.bind(value(..), amplitude => ...), the generic Result.bind returns a boxed value while sums returns Totals as flat words, so each turn leaves one continuation in sums and one in Result.bind (the SUMS_K28 → RESULT_BIND_K285 cycle you captured). 640 modes are 3200 words. 2.0.21 put them on heap continuations; 2.0.23 put them on the lane stack. Your diagnosis was exactly right.

    The fix. The handoff now emits only for the host (!DEVICE). On the device a leaf in that position runs on heap continuations again, as in 2.0.21, at any depth. The host keeps the handoff and PR #876's gains. Measured on an M4 Max, the two GPU benches that exercise sequential leaves are the same or faster without the device handoff (symreg 0.34 → 0.32 s, queens within noise), so nothing was traded. A regression test with your loop's shape (tests/run/fork_leaf_result_loop.bend, a 1000-turn do Result loop under a fork inside a bang) is in the gate.

    What stays: a bang's root and a fork kid still run on the lane stack on the device, as they did in 2.0.21; and a spill from the lane stack to the heap would be the general answer if the device handoff ever pays for itself.

    @nicolas-abril, this touches your PR #876, and I would value your review of the call. Two things to check: (1) we disabled the leaf handoff on the device entirely rather than bounding it, because on an M4 Max symreg and queens on Metal measure the same or faster without it (2.0.21 symreg 0.55 s, 2.0.23 0.34 s, 2.0.24 0.32 s: the GPU gain came from elsewhere in 2.0.22, not from the device handoff branch); (2) in the review round before 2.0.22 we also dropped the caller eligibility test from emit_jump, which your PR description says protected Metal queens and symreg. If you see a workload where the device handoff or that test matters, or a better bound than "never on the device", please say so and I will look at 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions