Repository navigation
Description
On a device, heap_free checks the error flag before it frees:
INLINE void heap_free(Env e, u32 cls, u64 loc) {
if (err_seen(e.mem)) {
return;
}
err_seen is a32_load(a32_at(H, H_ERROR_CODE)), so every free on every lane is an atomic load of the same word. On Metal this costs tree-bitonic about 12% of its GPU time and tree-radix about 7%.
I have not sent a PR, because the cheap forms of this read carry risks that are yours to weigh (below).
What I measured
These numbers come from the emitted C of c23aa1f4 (#1140 over main b2111cf4), with one site changed at a time.
- The time is the GPU's, summed over each round's command buffer (
GPUEndTime - GPUStartTime). - Runs alternate: in pairs, before then after and after then before in turn, or, with more than two builds, in an order that rotates and reverses every round. Each row gives the median ratio of before over after, and a bootstrap 95% interval of it.
- Every output equals the unchanged build's.
There are three per-call checks of err_seen on the device. On tree-bitonic (--gpu 768MB, 12 rounds), heap_free's is the whole cost:
| tree-bitonic, the check dropped from | as is / changed | 95% interval |
|---|---|---|
heap_free |
1.128 | [1.115, 1.143] |
BLK_ALLOC |
0.998 | [0.985, 1.000] |
FID_EXIT in the work loop |
0.981 | [0.968, 0.995] |
| all three | 1.125 | [1.114, 1.133] |
Across benches (10 rounds), I compared the build with all three dropped, as a ceiling, against a build that keeps heap_free's check but reads the flag with a volatile load, *(volatile DEV u32*)a32_at(e.mem, H_ERROR_CODE) != 0:
| bench | all three dropped | 95% interval | volatile read in heap_free |
95% interval |
|---|---|---|---|---|
| tree-bitonic | 1.127 | [1.119, 1.138] | 1.116 | [1.104, 1.124] |
| tree-radix | 1.073 | [1.068, 1.085] | 1.070 | [1.066, 1.076] |
| tree-matmul | 1.032 | [1.015, 1.038] | 0.977 | [0.953, 1.001] |
| merkle | 1.008 | [0.992, 1.015] | ||
| hashmap | 0.996 | [0.988, 1.008] | ||
| bfs | 0.997 | [0.988, 1.010] |
- tree-matmul's own runs varied from 570 to 667 ms between sessions, so I read its rows as noise.
- A Bendcraft frame did not move: its hot loop does not allocate.
The likely cause is that every lane loads one address atomically on each free, and atomics on one address are served one at a time. A volatile load of the same word can be served from the cache.
Why I have not sent a PR
- A volatile device load is a new load form. At MSL 3.2 it compiles to
air.load.device_coherent.volatile. No M1 pipeline has compiled it here, anda32_load's comment says M1's pipeline build dies on some load forms. If it did on this one, every!on an M1 would fail. - Below MSL 3.2, the load is not coherent. There
DEVis plaindevice. A lane that postsERR_HEAPwitha32_cmpxcan keep reading 0 from its L1 and go on freeing what its failed allocations left atHEAP_OFF, which may write out of bounds. - Dropping the check changes what a lane does after an error, so I only took that as a ceiling.
Options I see
- Read the flag with a volatile load on MSL 3.2 and later only, after the pipeline builds on an M1.
- Poll the flag in
heap_freeevery N frees, aserr_spundoes in the loops. A lane would then free a few blocks after another lane's error. Its ownERR_HEAPwould need a local test instead, for example onheap_alloc_missreturningHEAP_OFF. - Keep it as it is, if 7-12% on the allocating trees is not worth either.
Environment
bend --version: 2.0.32 (main atb2111cf4; measured with A device peek reads a redirect with one plain load, as before 2.0.32 (#1139) #1140 applied)- macOS 26.6.2, Apple M5 (8 GPU cores)
Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from Adriel's M5.
Activity
On MSL 3.2+, heap_free reads the error flag with a coherent volatile …
On MSL 3.2+, heap_free reads the error flag with a coherent volatile …