Skip to content

2.0.32 makes each read of a shared Array two atomic loads: frames 3 to 4x slower on Metal #1139

Closed

Description

@AdrielSantana

Since 2.0.32 (573002f0, the fix of #975), blk_loc reads a shared Array's redirect through term_peek. That read is rfc_view: two relaxed atomic loads of the cell's halves, plus a device fence when the count is one. Every Array.get on an Array that a fork shares now pays it.

What the atomic load costs depends on the target:

  • On the CPU, a relaxed load is a plain load.
  • On CUDA, a32_load is a volatile load.
  • On Metal, it is a real atomic load, and that is where the time goes.

None of bench/runtime shares an Array (BLK_SHR is 0 in all 16), so the perf gate cannot see this.

What you did

shared_reads.bend: one ! over 65,536 leaves, each reading one shared Array<U32> 256 times.

import Base

# Every leaf of a fork reads the one Array the fork shares, 256 times.
def walk(fuel: Nat, +i: U32, +acc: U32, r: Array<U32> & U32) -> Array<U32> & U32:
  match fuel:
    case 0n:
      (a, x) = r
      (a, (acc + x : U32))
    case 1n+f:
      (a, x) = r
      +j = ((i * 1664525 : U32) + 1013904223 : U32)
      walk(f, j, (acc + x : U32), Array.get(U32, a, j))

def add(l: Array<U32> & U32, r: Array<U32> & U32) -> Array<U32> & U32:
  (a, x) = l
  (b, y) = r
  (Array.join(U32, a, b), (x + y : U32))

@unsafe
def fan(+d: Nat, +a: Array<U32>, +k: U32) -> Array<U32> & U32:
  match d:
    case 0n:
      walk(256n, k, 0, Array.get(U32, a, k))
    case 1n+p:
      l r = fan(p, a, (k * 2 : U32)) fan(p, a, ((k * 2 : U32) + 1 : U32))
      add(l, r)

def done(r: Array<U32> & U32) -> IO(Unit):
  (a, x) = r
  IO.print(U32.show(x))

def main() -> IO(Unit):
  done(fan!(16n, [1 : U32*4096n], 0))

#|16842752
bend shared_reads.bend -o shared_reads && ./shared_reads

What happened

The output is right on both builds, but the ! takes 6.6 times as long on 2.0.32. Bendcraft, a voxel ray caster whose every pixel reads one shared Array<U32>, draws the same pixels 3 to 4 times slower.

GPU time (each round's command buffer, GPUEndTime - GPUStartTime), on an Apple M5 (8 GPU cores):

84a16cf3 (before 2.0.32) b2111cf4 (main) main / before 95% interval
shared_reads.bend, the ! 1.7 ms 13.2 ms 6.6x [5.7, 8.0]
Bendcraft, 512 x 512 8.0 ms 28.6 ms 4.0x [2.9, 4.3]
Bendcraft, 512 x 512, 300 blocks built 8.1 ms 26.1 ms 3.7x [2.6, 4.2]
Bendcraft, 735 x 398 18.8 ms 54.4 ms 2.9x [2.2, 3.8]
Bendcraft, 960 x 540 36.4 ms 106.4 ms 2.9x [2.3, 4.0]
Bendcraft, 1470 x 796 76.2 ms 222.3 ms 2.9x [2.5, 3.4]
Bendcraft, 1920 x 1080 152.1 ms 427.0 ms 2.9x [2.4, 3.1]

How these were measured:

  • Runs go in pairs, before then after and after then before in turn: 10 pairs of each program, on a machine in use. A Bendcraft row is the fastest frame of its size.
  • The machine's load inflates the fast build most. On a quiet machine, the bench's own readout was 47 ms against 204 at 1470 x 796.

Which part costs

I rebuilt Bendcraft's 2.0.32 C with other bodies for blk_loc. Every variant gives the same checksums. The time is the fastest frame at 1470 x 796.

blk_loc reads the cell as ms
one plain 64-bit load (before 2.0.32) 48
two volatile plain u32 halves 48
a volatile plain high half and a relaxed atomic low half 188
two relaxed atomic halves, no fence 203
term_peek (2.0.32: atomic halves through a volatile local, a fence at count one) 207
one plain load, and a fence when the count is one 69

So the atomic load is the cost; neither the volatile local nor the width is. A fence in the read path costs on its own too.

A fix that keeps #975 fixed follows as a PR.

Environment

  • bend --version: 2.0.32 (main at b2111cf4), before it 84a16cf3
  • macOS 26.6.2, Apple M5 (8 GPU cores)

Activity

  1. nicolas-abril commented on Sep 28, 2026

    @nicolas-abril
    Collaborator

    Fixed by #1155 (ef66a7cc). blk_loc now reads a shared Array's redirect word with w64_load: a plain load on Metal and CUDA, and a relaxed 64-bit load on the CPU. Spins also use the storage their caller already resolved instead of going through the redirect.

    This issue's repro on Metal (Apple M4, hyperfine, 30 runs), built from 6de56b97 (right before #1155) and from main:

    before #1155 main
    repro as written 65.6 ms min 58.7 ms min
    scaled to 8192 reads per leaf 238.0 ± 1.1 ms 69.1 ± 2.1 ms

    Both builds print the same output. #1140 is closed in favour of #1155. Thanks for the thorough report!


    This was written with AI assistance and checked by a maintainer.

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