Skip to content

Recompiler: emit globallycoherent on compute memory windows only when needed - #2346

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
gefydev:globallycoherent-rebased
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
gefydev:globallycoherent-rebased

Conversation

@gefydev

@gefydev gefydev commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Continues #1850 by @certified-retart, whose branch stopped merging into main after #2054 and other changes (conflicts in tests.rs and emit.rs). The author has not answered since, and people are waiting for this change, so it is opened here at their request. The commit is the author's, rebased onto main 0829441 as 83ad6a5, with their authorship and message; the change itself was not altered. If the author comes back they are welcome to take it over, and #1850 and this PR should not both be merged.

Only emit globallycoherent on compute shaders' flat memory windows (mem, mem1… and XCL_HEAP_WINDOWS heap views) when the RDNA2 program actually uses cross-workgroup flat-memory coherence (GLC/DLC/SLC, flat-memory atomics, or data cache invalidation). Part of #675 and #1269.

Previously both the HLSL emitter and the direct DXIL translator marked every compute shader's flat memory UAVs globallycoherent unconditionally so cross-workgroup algorithms (such as bottom-up BVH builds) would see other workgroups' writes. On NVIDIA D3D12 hardware, globallycoherent bypasses SM L1 for every RWByteAddressBuffer load—including uniform SMEM loads that all threads in a wave fetch from the same address—turning L1 broadcasts into serialized L2 traffic (and on imported host-memory heaps, multi-microsecond GPU-wide serialized loads).

On RDNA2 hardware itself, a workgroup can span both CUs of a WGP (each with its own GL0 cache), so any console shader relying on cross-wave or cross-workgroup flat-memory visibility already emits GLC/DLC/SLC, atomics, or cache invalidations. Plain compute shaders without any of those only access read-only or disjointly written memory within the dispatch and can safely keep L1 caching enabled.

What is in it

  • crates/xcl/src/gpu/recomp/mod.rs: add shared needs_coherent_mem(insts: &[Decoded]) -> bool returning true iff the program contains any flat-memory atomic (BUFFER_ATOMIC_*, GLOBAL_ATOMIC_*, FLAT_ATOMIC_*), GLC/DLC/SLC on ENC_SMEM/ENC_MUBUF/ENC_MTBUF/ENC_FLAT/ENC_FLAT_GLBL, or cache invalidation (S_DCACHE_INV, S_GL1_INV, BUFFER_GL0_INV, BUFFER_GL1_INV); set em.coherent_mem in translate().
  • crates/xcl/src/gpu/recomp/emit.rs: emit globallycoherent on root mem/mem1… declarations and XCL_HEAP_WINDOWS accessors (XCL_MW) only when self.coherent_mem is set.
  • crates/xcl/src/gpu/dxil/kernel.rs & crates/xcl/src/gpu/recomp/lower_dxil.rs: pass cs.is_some() && recomp::needs_coherent_mem(insts) to mem_windows in Lw::new (covering both direct DXIL and lower_dxil).
  • crates/xcl/src/gpu/recomp/tests.rs: add compute_globallycoherent_only_when_needed (HLSL root/switch/heap, Mode::Kernel, and Direct DXIL annotateHandle IsGloballyCoherent bit checks across all triggers) and gpu_compute_coherent_and_cached_loads (real GPU execution of cached plain compute and multi-workgroup coherent atomic+GLC compute across both HLSL and Direct DXIL).

Measured GPU impact (RTX 5070)

  • Controlled SMEM microbenchmark (80× Load4, 320×180×1 groups of 8×8×1):
    • Root UAV: 6.140 ms (globallycoherent) → 0.552 ms (cached) — 11.1× faster
    • Heap window UAV (XCL_HEAP_WINDOWS=1): 6.975 ms → 0.538 ms — 13.0× faster
  • Isolated retail compute shader (320×180×1, zero-initialized memory):
    • Switch mode: 4.921 ms → 2.297 ms (2.14× faster)
    • Heap windows: 5.514 ms → 2.171 ms (2.54× faster)
  • Live heavy retail title 3D world load (XCL_GPU_PROFILE=1, XCL_HEAP_WINDOWS=1):
    • Heavy 4×4×4 / 8×8×1 world-load compute passes that previously took 17–494 ms per dispatch (1,001 ms in 460 ops) dropped by 6×–37× (e.g. an 85.4 ms 16×16×16 pass dropped to 2.3 ms), allowing the title to advance through 10+ consecutive 3D world frames (~24k dispatches).

What the rebase changed

  • crates/xcl/src/gpu/recomp/emit.rs: two conflicts of two changes adding next to each other. main had added the img_writes field of Em (and image_write_srsrcs) where this change adds coherent_mem; both fields are kept, and both initialisers in the constructor.
  • crates/xcl/src/gpu/recomp/tests.rs: both tests appended at the end of the file are kept, main's resinfo one and this change's compute_globallycoherent_only_when_needed and gpu_compute_coherent_and_cached_loads.
  • The numbers under "Measured GPU impact" are the author's, from Recompiler: emit globallycoherent on compute memory windows only when needed #1850 on an RTX 5070; they were not repeated here.

Tested

  • NVIDIA GeForce GTX 1650 4 GB (32-lane waves), driver 621.24, AMD Ryzen 5 1600, 16 GB RAM, Windows 11 Pro 26300, Rust 1.100.0, debug, CARGO_BUILD_JOBS=2.
  • compute_globallycoherent_only_when_needed and gpu_compute_coherent_and_cached_loads pass; the GPU one ran, it was not skipped.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4 on 83ad6a5: 1274 passed, 0 failed, 46 ignored. 27 SKIPPED, none on a path this change touches: 21 wave64 cases (no wave64 on this GPU), 3 per-lane f16 cases the per-lane form refuses, 1 "no heap windows", 2 for the D3D12 debug layer not being installed.

Regression run

  • There is no xcl-smoke pair for this head yet: the machine used for the rebase runs out of memory when it runs the title, and two attempts of other runs were stopped by the system. PR checks will flag it until someone runs scripts/smoke.ps1 on 83ad6a5; a run from a reviewer, on a card with more memory, is welcome.
  • For reference, the author's pair on ecc9723 (the same change on main cbc296a, RTX 5070) is in Recompiler: emit globallycoherent on compute memory windows only when needed #1850: 1685 frames and 15 660 MB for the PR against 1682 and 15 030 MB for main, no device removal and no crash.

Not tested

AI use

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.

3 participants