From 5835139244ebb0025c431808696fb9567330274b Mon Sep 17 00:00:00 2001 From: PHeitor1 <157995735+PHeitor1@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:52:54 -0300 Subject: [PATCH] Recompiler: LDS ds_sub_u32 hides its negation from the driver NVIDIA's RTX 50 (Blackwell) drivers refuse a compute pipeline with E_OUTOFMEMORY when it has a groupshared atomic subtract, and newer ones (seen from 617.14 to 621.24) also when it has a groupshared atomic add of a negated, non-constant value, the form both translators emit for ds_sub_u32. DXC's InterlockedAdd(g, 0u - v) is refused the same way. ds_sub_u32 / ds_sub_rtn_u32 now add (0 - v) | WaveActiveBitOr(0), on the HLSL and the direct DXIL path. The wave op is always 0; with it the driver does not see the negation, and it stays one atomic. The fuzz tests emit their Sub the same way. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/fuzz_tests.rs | 12 ++++--- crates/xcl/src/gpu/dxil/lower_mem.rs | 13 ++++++-- crates/xcl/src/gpu/recomp/emit.rs | 5 ++- crates/xcl/src/gpu/recomp/tests.rs | 45 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/fuzz_tests.rs b/crates/xcl/src/gpu/dxil/fuzz_tests.rs index b0f25311b8..6827a5c409 100644 --- a/crates/xcl/src/gpu/dxil/fuzz_tests.rs +++ b/crates/xcl/src/gpu/dxil/fuzz_tests.rs @@ -16,7 +16,7 @@ use super::container::Stage; use super::module::{fmf, BinOp, CastOp, Pred, RmwOp, Value}; -use super::ops::{barrier, BitCount, Binary, GroupShared}; +use super::ops::{barrier, BitCount, Binary, GroupShared, WaveBit}; use super::shader::{RawUav, Shader}; use super::validate; use crate::gpu::recomp::runner; @@ -733,12 +733,14 @@ impl Emit<'_> { let init = self.s.m.bin(BinOp::Xor, init, kc); self.s.gs_store(g, idx, init); self.s.barrier(barrier::GROUP_SYNC); - // `atomicrmw sub` as an add of the negation, the form DXC and the translator - // emit: NVIDIA Blackwell drivers refuse pipelines with a groupshared - // `atomicrmw sub` (CreateComputePipelineState E_OUTOFMEMORY). + // `atomicrmw sub` as the translator emits it: NVIDIA Blackwell drivers refuse pipelines + // (E_OUTOFMEMORY) with a groupshared `atomicrmw sub`, and newer ones also with an add + // of a negation they can see. The wave op is always 0 and hides it. let (op, v) = if *op == RmwOp::Sub { let zero = self.s.m.i32c(0); - (RmwOp::Add, self.s.m.bin(BinOp::Sub, zero, v)) + let n = self.s.m.bin(BinOp::Sub, zero, v); + let o = self.s.wave_active_bit(zero, WaveBit::Or); + (RmwOp::Add, self.s.m.bin(BinOp::Or, n, o)) } else { (*op, v) }; diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e740..94551a724d 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -5,7 +5,7 @@ use super::lower::{err, Dst, LResult, Lw, VAR_VGPR}; use super::module::{BinOp, Pred, RmwOp, Value}; -use super::ops::AtomicOp; +use super::ops::{AtomicOp, WaveBit}; use super::lower_window::Ea; use super::shader::RawUav; use crate::gpu::rdna2::{self, Decoded}; @@ -1455,7 +1455,16 @@ impl Lw { let z = self.c(0); Ok(match op { "add" | "add_u32" => direct(self, AtomicOp::Add, RmwOp::Add, v), - "sub" | "sub_u32" => { + // LDS: NVIDIA's Blackwell drivers refuse a pipeline (E_OUTOFMEMORY) with a group-shared + // subtract or add of a negated value. The wave op is always 0; it hides the negation + // from the driver + "sub_u32" => { + let n = self.sub(z, v); + let o = self.s.wave_active_bit(z, WaveBit::Or); + let n = self.or(n, o); + direct(self, AtomicOp::Add, RmwOp::Add, n) + } + "sub" => { let n = self.sub(z, v); direct(self, AtomicOp::Add, RmwOp::Add, n) } diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..e1579a560d 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5440,7 +5440,10 @@ fn rmw32(op: &str, at: &At, v: &str, v2: &str, old: &str, t: &str) -> Option direct("InterlockedAdd", v), - "sub" | "sub_u32" => direct("InterlockedAdd", &format!("(0u - {v})")), + "sub" => direct("InterlockedAdd", &format!("(0u - {v})")), + // LDS: NVIDIA's Blackwell drivers refuse a pipeline (E_OUTOFMEMORY) with a group-shared + // add of a negated value. The wave op is always 0; it hides the negation from the driver + "sub_u32" => direct("InterlockedAdd", &format!("((0u - {v}) | WaveActiveBitOr(0u))")), "and" | "and_b32" => direct("InterlockedAnd", v), "or" | "or_b32" => direct("InterlockedOr", v), "xor" | "xor_b32" => direct("InterlockedXor", v), diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..38dcb987bf 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -5257,6 +5257,51 @@ fn gpu_global_atomics_and_sub_dword_access() { } } +/// LDS subtracts of a register: one slot per lane without and with the old value returned, and +/// one slot all lanes subtract from. NVIDIA's Blackwell drivers refuse a pipeline (E_OUTOFMEMORY) +/// with a group-shared subtract or an add of a negated value. +#[test] +fn gpu_ds_sub_u32_of_a_register() { + if !gpu_ready() { + return; + } + let ds = |n: &'static str, f: &[(&str, u64)]| encode(n, "ENC_DS", f, None).unwrap_or_else(|x| panic!("{n}: {x}")); + let barrier = encode("S_BARRIER", "ENC_SOPP", &[], None).unwrap(); + let mut code = Vec::new(); + for (a, d) in [(1, 2), (3, 4), (5, 6)] { + code.extend(ds("DS_WRITE_B32", &[("ADDR", a), ("DATA0", d)])); + } + code.extend(&barrier); + code.extend(ds("DS_SUB_U32", &[("ADDR", 1), ("DATA0", 7)])); + code.extend(ds("DS_SUB_RTN_U32", &[("VDST", 10), ("ADDR", 3), ("DATA0", 7)])); + code.extend(ds("DS_SUB_U32", &[("ADDR", 5), ("DATA0", 7)])); + code.extend(&barrier); + for (d, a) in [(11, 1), (12, 3), (13, 5)] { + code.extend(ds("DS_READ_B32", &[("VDST", d), ("ADDR", a)])); + } + code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let l = l as u32; + r[1] = 4 * l; + r[2] = 10 * l; // below the subtrahend of some lanes: wraps + r[3] = 512 + 4 * l; + r[4] = 5000 + l; + r[5] = 1024; + r[6] = 1_000_000; + r[7] = 3 * l + 100; + } + let total: u32 = (0..64).map(|l| 3 * l + 100).sum(); + let r = runner::run_code(&code, &st, &[]).unwrap(); + for (l, v) in r.vgpr.iter().enumerate() { + let l = l as u32; + let x = 3 * l + 100; + assert_eq!(v[11], (10 * l).wrapping_sub(x), "lane {l}: ds_sub_u32"); + assert_eq!((v[10], v[12]), (5000 + l, 5000 + l - x), "lane {l}: ds_sub_rtn_u32 (old, new)"); + assert_eq!(v[13], 1_000_000 - total, "lane {l}: the shared slot"); + } +} + /// The instructions added for code outside the retail pack: global inc / dec / csub / /// 64-bit add, LDS signed min / max, cmpst, mskor, float add (CAS loops), global sub-dword /// and d16 accesses, scratch, SAD / lerp, the division helpers, saturating adds, packs, swap.