From 71d3a384d9b35ed845f066ea5c62d7030e8be68a Mon Sep 17 00:00:00 2001 From: upipibi Date: Tue, 24 Nov 2026 07:36:01 -0600 Subject: [PATCH] Recompiler: split wave64 spin loops poll with a compare-exchange, not an add of 0 (#847) The halves' meet (`xcl_x_meet`) and the early-exit-safe barrier poll their counters with `InterlockedAdd(x, 0u, c)`. Mesa's NIR lowers an atomic add of 0 to a load of shared memory and hoists it out of the loop (found and shown in the RADV ISA by SzczepanGrela on #847), so on RADV a half reads the other half's counter once and spins to the bound whenever the other half arrives a little late: under async compute load the split tests fail (66 of 80). A compare-exchange of 0 with 0 never changes the word and returns it like the add did; NIR keeps it inside the loop (checked on #847 with DXC 1.9 and Mesa 26.2.5). The three polls use it now. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 10 +++++++--- crates/xcl/src/gpu/recomp/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..829399ecbd 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5426,8 +5426,11 @@ bool xcl_x_meet() { InterlockedAdd(XCL_XC(xcl_cw * 2u + xcl_half), lanes, o); uint need = (k + 1u) * xcl_xn; [loop] for (uint n = 0u; ; n++) { + // the poll is a compare-exchange that never changes the word: Mesa's NIR turns an + // add of 0 into a load and hoists it out of the loop (#847), so the wave would + // never see the other half arrive and always run to the bound uint c; - InterlockedAdd(XCL_XC(xcl_cw * 2u + (xcl_half ^ 1u)), 0u, c); + InterlockedCompareExchange(XCL_XC(xcl_cw * 2u + (xcl_half ^ 1u)), 0u, 0u, c); if ((int)(c - need) >= 0) break; if (n >= (1u << 20)) { ok = 0u; break; } } @@ -5510,9 +5513,10 @@ void xcl_s_barrier() { uint _next = ((_gen + 1u) & 0xFFFFFu) << 12; [loop] for (uint _n = 0u; ; _n++) { uint _s, _alive; - InterlockedAdd(XCL_BAR_STATE, 0u, _s); + // compare-exchanges that change nothing, not adds of 0 (see `xcl_x_meet`) + InterlockedCompareExchange(XCL_BAR_STATE, 0u, 0u, _s); if ((_s >> 12) != _gen) break; - InterlockedAdd(XCL_BAR_ALIVE, 0u, _alive); + InterlockedCompareExchange(XCL_BAR_ALIVE, 0u, 0u, _alive); if ((int)_alive <= 0 || (_s & 0xFFFu) >= _alive) { uint _orig; InterlockedCompareExchange(XCL_BAR_STATE, _s, _next, _orig); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d1..15978fb086 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7329,6 +7329,33 @@ fn fspx_compute_user_sgprs_from_rsrc2() { assert_eq!(counts, [Some(4), Some(8)]); } +/// The split form's spin loops (the halves' meet, the early-exit-safe barrier) poll with a +/// compare-exchange that changes nothing, never an add of 0: Mesa's NIR lowers that add to a +/// load and hoists it out of the loop, so on RADV a half never sees the other arrive (#847). +#[test] +fn split_wave64_spin_polls_are_not_adds_of_zero() { + use super::CsInterface; + let p = prog(&[ + ("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 10), ("SRC0", V)], None), + ("S_BARRIER", "ENC_SOPP", &[], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 10)], None), + ]); + let cs = CsInterface { threads: [128, 1, 1], user_sgprs: 2, tgid: [false, false, false], tg_size: false, tid_components: 1 }; + let t = translate(&p, &Options { mode: Mode::Compute(cs), wave64: true, split_wave64: true, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + assert!(t.hlsl.contains("bool xcl_x_meet()") && t.hlsl.contains("void xcl_s_barrier()"), "{}", t.hlsl); + assert!(t.hlsl.contains("InterlockedCompareExchange(XCL_XC(xcl_cw * 2u + (xcl_half ^ 1u)), 0u, 0u, c);")); + assert!(t.hlsl.contains("InterlockedCompareExchange(XCL_BAR_STATE, 0u, 0u, _s);")); + assert!(t.hlsl.contains("InterlockedCompareExchange(XCL_BAR_ALIVE, 0u, 0u, _alive);")); + for line in t.hlsl.lines().filter(|l| l.contains("InterlockedAdd(")) { + assert!(!line.contains(", 0u, "), "a poll by an add of 0: {line}"); + } + if super::dxc::location().is_ok() { + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e} +{}", t.hlsl)); + } +} + /// Verifies early-exit-safe, bounded `xcl_s_barrier()` on real multi-warp compute shaders: /// 1. All 4 hardware wave32s in `numthreads(128, 1, 1)` exchange LDS values across two back-to-back /// `s_barrier` generations (both split-wave64 and native wave32).