Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions crates/xcl/src/gpu/dxil/fuzz_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
};
Expand Down
13 changes: 11 additions & 2 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5440,7 +5440,10 @@ fn rmw32(op: &str, at: &At, v: &str, v2: &str, old: &str, t: &str) -> Option<Str
};
Some(match op {
"add" | "add_u32" => 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),
Expand Down
45 changes: 45 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading