Skip to content

hk: give G17P dependencies memory scope while a writable import lives - #472

Merged
RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/hk-g17p-import-alias-scope
Nov 19, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/hk-g17p-import-alias-scope

Conversation

@jgoyvaerts

Copy link
Copy Markdown

What

G17P's GPU caches are tagged by virtual address. A write through one mapping becomes visible to a read through another mapping only after a memory-scope flush (hk_cdm_alias_scope()). G17P takes that flush while sparse or capture/replay resources live, but not for memory imports, and an fd import or a host import gets its own address while another import can map the same pages. On the M4 Pro, every word written through one import and read through another after a barrier is stale.

This adds the import half of the visibility problem #266 fixed for M5, for G17P. #266's M5 rule decides once per device (any device that enables sparse, capture/replay or an import extension flushes with memory scope at every dependency). This change keeps G17P's existing record-time counter instead, so a device that imports nothing pays nothing.

How

hk_AllocateMemory() marks fd imports and host imports the GPU may write (anything but AGX_HOST_PTR_READ_ONLY) as alias_import. They count in va_alias_resources while they live, like capture/replay memory already does. The comment above hk_cdm_alias_scope() now lists imports as covered.

Still not covered, and already documented there: a capture/replay buffer or import created after a command buffer was recorded and reached through an address the command buffer loads. M5's device-wide rule covers that, at its cost.

Testing

MacBook Pro 16" M4 Pro (J616s, T6040, G17P), GPU firmware 25G83, Linux 7.3.0-m4.171, on AC. Release builds from one build directory with the same meson options:

Guarded runs, MESA_SHADER_CACHE_DISABLE=true, 0 GPU recoveries.

Visibility through a second address (#266's bt)

Sources: Erik's gist from #266 (https://gist.github.com/erikwb/992e9d6d37eaba85b191807d34b1ec44), built as its build.sh does. bt <mode> 3 64: chains of 64 levels that alternately write a buffer through one address and read it through another, with an application barrier between levels, over 4 shapes (1024 or 16384 words, 16384 or 262144 threads), 3 runs each. The cells are wrong words over words checked, summed over the shapes. The device enables bufferDeviceAddressCaptureReplay, VK_KHR_external_memory_fd, VK_EXT_external_memory_dma_buf and VK_EXT_external_memory_host.

mode case aurora-wip this PR
alias same address 0/104448 0/104448
alias other address 104448/104448 0/104448
hostalias 1 alloc, 2 imports: same 0/104448 0/104448
hostalias 1 alloc, 2 imports: other 104448/104448 0/104448
hostalias overlapping imports: other 104448/104448 0/104448
hostalias memfd mapped twice: other 104448/104448 0/104448
dmabuf dma-buf + read-only import: same 0/104448 0/104448
dmabuf two dma-bufs, same pages: other 104448/104448 0/104448
dmabuf overlapping dma-bufs: other 104448/104448 0/104448
dmabuf one dma-buf imported twice 0/104448 0/104448
xsub fence between 0/104448 0/104448
xsub semaphore between 0/104448 0/104448
xsub one submit, barrier at Y start 0/104448 0/104448
xsub B created while X runs, fence 0/104448 0/104448
late alive while recording 0/104448 0/104448
late slot = x itself (no alias) 0/104448 0/104448
late created after recording 104448/104448 104448/104448
late created after a first run 104448/104448 104448/104448
late created during recording 104448/104448 104448/104448
late secondary, created after 104448/104448 104448/104448

The late rows created after recording are the documented remaining gap: unchanged, and they need capture/replay tooling (GFXReconstruct-style replays).

Cost

bt cost 64 measures per-level cost, best of 7, on the same device with all those features enabled and nothing imported. Diff is the per-level cost of the barrier in µs:

kernel / threads / push aurora-wip this PR
nop 1 thr push 16 0.872 0.862
nop 1 thr push 256 0.833 0.842
nop 1 thr p256 same 0.840 0.844
nop grid push 16 0.454 0.475
nop grid push 256 0.452 0.495
snop 1 thr push 0 0.788 0.811
snop grid push 0 0.510 0.489
sxform 1 thr push 0 0.913 0.946
sxform grid push 0 0.897 0.857
xform 1 thr push 32 0.919 0.953
xform 1 thr push 256 0.938 0.919
xform grid push 32 0.927 0.917
xform grid push 256 0.910 0.947
xform30 1 thr push 256 1.790 1.844
xform30 grid push 256 1.074 1.056
xfpre 1 thr push 40 1.050 0.998
xfpre grid push 40 0.989 0.964

The two builds match within run-to-run noise. While a writable import lives, compute and transfer dependencies take the memory-scope flush. On our downstream stack, forcing that flush costs up to about 1 µs per level in the same benchmark, the price M5 pays on every device that enables these features.

Conformance

On aurora-wip + this commit: meson --suite asahi --suite vulkan 199/199; dEQP-VK.api.external.memory.{dma_buf,host,opaque_fd}.* and dEQP-VK.memory.external_memory_host.* (108 cases): 94 Pass, 14 NotSupported, 0 Fail.

Commands
for k in none nop xform xform30 xformpre stream fill pp ppt snop sxform peek; do
  glslc -O -fshader-stage=compute -DKERNEL_$k bk.comp -o $k.spv; done
cc -O2 -Wall -o bt bt.c -lvulkan
MESA_SHADER_CACHE_DISABLE=true VK_DRIVER_FILES=<build>/vk.json ./bt alias|hostalias|dmabuf|xsub|late 3 64
MESA_SHADER_CACHE_DISABLE=true VK_DRIVER_FILES=<build>/vk.json ./bt cost 64     # twice per build, alternating

🤖 Generated with Claude Code

G17P's GPU caches are tagged by virtual address, so a write through one
mapping becomes visible to a read through another only after a
memory-scope flush (hk_cdm_alias_scope()). G17P takes that flush while
sparse or capture/replay resources live, but not for memory imports, and
an fd import or a host import gets an address of its own while another
import may map the same pages. aurora-silicon#266's bt suite (gist linked from
aurora-silicon#266), on the M4 Pro: every word written through one
import and read through another after a barrier was stale (two host
imports of one allocation, overlapping host imports, a memfd mapped
twice, two dma-bufs of the same pages, two overlapping dma-bufs): 3072
of 3072 and 49152 of 49152 words wrong per shape. One dma-buf imported
twice is the same BO and address, and was already exact.

Count live fd imports and host imports the GPU may write in
va_alias_resources, as capture/replay memory already is. All those cases
read exact data now. A device that imports nothing is unchanged: bt's
per-level barrier cost on a device with capture/replay and both import
extensions enabled but no import stays the same (17 kernel, grid and
push shapes, best of 7 per level: 0.45-1.79 us on aurora-wip, 0.48-1.84
us with this change, within run-to-run noise). While an import lives,
compute and transfer dependencies take the memory-scope flush (up to
+1 us per level, the cost M5 pays on every device that enables these
features).

Still not covered, as documented in hk_cdm_alias_scope(): a capture/replay
buffer or import created after a command buffer was recorded and reached
through an address the command buffer loads (bt late, pending, simul,
event, xsem), which M5's device-wide rule covers at that cost.

meson asahi+vulkan 199/199 on aurora-wip, 0 GPU recoveries.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Assisted-by: LLM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants