Skip to content
Merged
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
31 changes: 31 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,34 @@ fn gpu_direct_unaligned_multi_dword_format_store() {
let got: Vec<u8> = r.mem.iter().flat_map(|v| v.to_le_bytes()).collect();
assert_eq!(&got[0xFC..0x118], &want[0xFC..0x118], "bytes 0xFC..0x118 after the store at 0x102");
}

#[test]
fn window_at_lowers_to_32bit_carry_without_int64() {
let ko = KernelOptions { wave64: false, split: false, mem_windows: 4, sampler_base: 0 };
let with_adds = prog(&[
("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 1), ("ADDR", 0), ("SADDR", 2), ("OFFSET", 16)], None),
("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 2), ("ADDR", 0), ("SADDR", 2), ("OFFSET", 0xFFC)], None),
("S_LOAD_DWORD", "ENC_SMEM", &[("SDATA", 10), ("SBASE", 2), ("OFFSET", 0x1F_FFFC), ("SOFFSET", 4)], None),
("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 3), ("VADDR", 0), ("SRSRC", 2), ("SOFFSET", 128), ("IDXEN", 1), ("OFFEN", 1), ("OFFSET", 4)], None),
]);
let dxil = translate(&with_adds, ko).expect("multi-window with adds translates");
super::validate::validate(&dxil).expect("validates without Int64Ops");
if let Ok(dis) = super::validate::disassemble(&dxil) {
let body = dis.split("define void @main()").nth(1).and_then(|s| s.split("\n}\n").next()).expect("main body");
assert!(!dis.contains("64-bit integers") && !body.contains("i64"), "window_at with adds must not emit i64:\n{dis}");
assert!(!dis.contains("binaryWithCarryOrBorrow"), "window_at should avoid uaddc struct overhead:\n{dis}");
assert!(body.contains("icmp ult i32"), "expected scalar 32-bit carry compare in DXIL:\n{dis}");
assert!(dis.contains("dx.op.binaryWithTwoOuts.i32"), "expected UMul in DXIL:\n{dis}");
}

let zero_off = prog(&[
("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 1), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)], None),
]);
let dxil0 = translate(&zero_off, ko).expect("zero-offset multi-window translates");
super::validate::validate(&dxil0).expect("zero-offset validates");
if let Ok(dis0) = super::validate::disassemble(&dxil0) {
let body0 = dis0.split("define void @main()").nth(1).and_then(|s| s.split("\n}\n").next()).expect("main body");
assert!(!dis0.contains("64-bit integers") && !body0.contains("i64"), "zero-offset window_at must not emit i64:\n{dis0}");
assert!(!body0.contains("icmp ult"), "zero-offset window_at must not emit carry compare:\n{dis0}");
}
}
88 changes: 74 additions & 14 deletions crates/xcl/src/gpu/dxil/lower_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! `xcl_m*` accessors with `Options::heap_windows` (`recomp::emit::mem_accessors`).

use super::lower::{Lw, VAR_WIN};
use super::module::{Pred, Value};
use super::module::{CastOp, Pred, Value};
use crate::gpu::rdna2::Decoded;

/// A term of a 64-bit effective address (`window_at`).
Expand Down Expand Up @@ -59,21 +59,81 @@ impl Lw {
if !self.windowed() {
return;
}
let mut ea = self.pack64(lo, hi);
for &t in adds {
let x = match t {
Ea::U(v) => self.zext64(v),
Ea::K(k) => self.c64(k as u64),
Ea::S(v) => self.sext64(v),
Ea::Mul(a, b) => {
let (a, b) = (self.zext64(a), self.zext64(b));
self.mul(a, b)
let non_zero = |t: &Ea| match *t {
Ea::K(0) => false,
Ea::U(v) | Ea::S(v) => self.s.m.const_u64(v) != Some(0),
Ea::Mul(a, b) => self.s.m.const_u64(a) != Some(0) && self.s.m.const_u64(b) != Some(0),
Ea::K(_) => true,
};
let terms: Vec<Ea> = adds.iter().copied().filter(non_zero).collect();
let (mut lo, mut hi) = (lo, hi);
let uadd = |lw: &mut Self, lo: &mut Value, hi: &mut Value, add_lo: Value, add_hi: Option<Value>| {
if let Some(ah) = add_hi {
*hi = lw.add(*hi, ah);
}
if lw.s.m.const_u64(*lo) == Some(0) {
*lo = add_lo;
} else {
let sum = lw.add(*lo, add_lo);
let carry = lw.cmp(Pred::ULt, sum, *lo);
*lo = sum;
let i32 = lw.s.m.i32();
let c32 = lw.s.m.cast(CastOp::ZExt, carry, i32);
*hi = lw.add(*hi, c32);
}
};
for (idx, t) in terms.iter().copied().enumerate() {
let has_more = idx + 1 < terms.len();
match t {
Ea::K(k) => {
let kv = self.c(k);
uadd(self, &mut lo, &mut hi, kv, None);
}
};
ea = self.add(ea, x);
Ea::U(v) => uadd(self, &mut lo, &mut hi, v, None),
Ea::S(v) => match self.s.m.const_u64(v) {
Some(k) if (k as i32) < 0 => {
let neg_k = (k as u32).wrapping_neg();
if self.s.m.const_u64(lo) == Some(0) {
lo = v;
hi = self.addc(hi, u32::MAX);
} else {
let borrow = self.cmpc(Pred::ULt, lo, neg_k);
if has_more {
lo = self.add(lo, v);
}
let i32 = self.s.m.i32();
let b32 = self.s.m.cast(CastOp::ZExt, borrow, i32);
hi = self.sub(hi, b32);
}
}
Some(_) => uadd(self, &mut lo, &mut hi, v, None),
None => {
let sign = self.ashrc(v, 31);
uadd(self, &mut lo, &mut hi, v, Some(sign));
}
},
Ea::Mul(a, b) => match (self.s.m.const_u64(a), self.s.m.const_u64(b)) {
(Some(1), _) => uadd(self, &mut lo, &mut hi, b, None),
(_, Some(1)) => uadd(self, &mut lo, &mut hi, a, None),
(Some(ka), Some(kb)) => {
let prod = (ka as u32 as u64) * (kb as u32 as u64);
let (plo, phi) = (prod as u32, (prod >> 32) as u32);
if phi != 0 {
hi = self.addc(hi, phi);
}
if plo != 0 {
let plov = self.c(plo);
uadd(self, &mut lo, &mut hi, plov, None);
}
}
_ => {
let (phi, plo) = self.s.umul_wide(a, b);
uadd(self, &mut lo, &mut hi, plo, Some(phi));
}
},
}
}
let (_, h) = self.unpack64(ea);
self.window_hi(h, write);
self.window_hi(hi, write);
}

/// `window_at` of a high dword already computed (`window_read` / `window_write`).
Expand Down
82 changes: 82 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,88 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) {
assert_eq!(*word(w1 + 0x3000), CANARY1, "out-of-UMA global_atomic_inc must not clobber w1");
}

#[test]
fn gpu_window_at_32bit_carry_edge_cases() {
if !gpu_ready() {
return;
}
let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap();
let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64);
assert_eq!(w1 - w0, 1 << 32);
let base_hi = (w0 >> 32) as u32;
assert!(base_hi >= 4);
let word = |a: u64| unsafe { &mut *(a as *mut u32) };

*word(w0 + 0x0000) = 0x2222_3333;
*word(w0 + 0x0010) = 0x9999_AAAA;
*word(w0 + 0x0020) = 0xDEAD_DEAD;
*word(w1 + 0x0000) = 0x1111_2222;
*word(w1 + 0x0004) = 0x7777_8888;
*word(w1 + 0x0008) = 0x3333_4444;
*word(w1 + 0x0010) = 0x5555_6666;
*word(w1 + 0x0020) = 0xCAFE_CAFE;

let glbl = |n: &'static str, f: &[(&str, u64)]| -> (&'static str, &'static str, Vec<(&'static str, u64)>, Option<u32>) {
(n, "ENC_FLAT_GLBL", f.iter().map(|&(a, b)| (unsafe { std::mem::transmute::<&str, &'static str>(a) }, b)).collect(), None)
};
let list = vec![
// 1. Ea::U carry: (w0 + 0xFFFF_FFFC) + v0(4) = w1 + 0x0000 -> 0x1111_2222
glbl("GLOBAL_LOAD_DWORD", &[("VDST", 10), ("ADDR", 0), ("SADDR", 2), ("OFFSET", 0)]),
// 2. Ea::S positive constant carry: (w0 + 0xFFFF_FFF8) + OFFSET(+16) = w1 + 0x0008 -> 0x3333_4444
glbl("GLOBAL_LOAD_DWORD", &[("VDST", 11), ("ADDR", 1), ("SADDR", 4), ("OFFSET", 16)]),
// 3. Ea::Mul pure high carry: w0 + (0x1000 * 0x10_0000) + v3(0x10) = w1 + 0x10 -> 0x5555_6666
("BUFFER_LOAD_DWORD", "ENC_MUBUF", vec![("VDATA", 12), ("VADDR", 2), ("SRSRC", 2), ("SOFFSET", 128), ("IDXEN", 1), ("OFFEN", 1), ("OFFSET", 0)], None),
// 4. Ea::Mul simultaneous lo-carry + phi: ((base_hi - 1):0xFFFF_F004) + (0x1000 * 0x10_0001) = w1 + 4 -> 0x7777_8888
("BUFFER_LOAD_DWORD", "ENC_MUBUF", vec![("VDATA", 13), ("VADDR", 4), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1), ("OFFEN", 0), ("OFFSET", 0)], None),
// 5. S_LOAD_DWORD negative Ea::S(OFFSET = -16) borrows base_hi -> base_hi-1, then Ea::U(SOFFSET = s16 = 0x20) carries back to w0 + 0x10 -> 0x9999_AAAA
("S_LOAD_DWORD", "ENC_SMEM", vec![("SDATA", 20), ("SBASE", 3), ("OFFSET", 0x1F_FFF0), ("SOFFSET", 16)], None),
// 6. Carry past end of UMA: (w1 + 0xFFFF_FFF0) + v6(0x20) = base_hi+2 -> read 0, drop store
glbl("GLOBAL_LOAD_DWORD", &[("VDST", 14), ("ADDR", 6), ("SADDR", 24), ("OFFSET", 0)]),
glbl("GLOBAL_STORE_DWORD", &[("ADDR", 6), ("DATA", 7), ("SADDR", 24), ("OFFSET", 0)]),
// 7. Ea::S negative OFFSET(-16) with uaddc carry=1 cancels -1+1=0: (w0 + 0x0010) + (-16) = w0 + 0 -> 0x2222_3333
glbl("GLOBAL_LOAD_DWORD", &[("VDST", 15), ("ADDR", 1), ("SADDR", 18), ("OFFSET", 0xFF0)]),
// 8. Ea::S negative OFFSET(-16) without carry borrows below w0: (w0 + 0x0030) - 0x30 + (-16) => store at (w0 + 0x0000) - 16 is dropped, w0 + 0x0020 intact
glbl("GLOBAL_STORE_DWORD", &[("ADDR", 1), ("DATA", 8), ("SADDR", 6), ("OFFSET", 0xFF0)]),
];
let refs: Vec<(&str, &str, &[(&str, u64)], Option<u32>)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect();
let p = prog(&refs);
let t = translate(&p, &on_gpu(Options { mem_windows: 2, ..Default::default() })).unwrap();
let hlsl_dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap();
let dxil = direct_or(&p, &on_gpu(Options { mem_windows: 2, ..Default::default() }), hlsl_dxil);

let mut st = KernelState::default();
st.sgpr[2..4].copy_from_slice(&[0xFFFF_FFFC, base_hi]); // s[2:3] = w0 + 0xFFFF_FFFC
st.sgpr[4..6].copy_from_slice(&[0xFFFF_FFF8, base_hi]); // s[4:5] = w0 + 0xFFFF_FFF8
st.sgpr[6..8].copy_from_slice(&[0, base_hi]); // s[6:7] = w0 + 0 (SBASE 3)
st.sgpr[8..12].copy_from_slice(&[0, (base_hi & 0xFFFF) | (0x1000 << 16), 0x20_0000, 0]); // SRSRC 2 (s[8:11])
st.sgpr[12..16].copy_from_slice(&[0xFFFF_F004, ((base_hi - 1) & 0xFFFF) | (0x1000 << 16), 0x20_0000, 0]); // SRSRC 3 (s[12:15])
st.sgpr[16] = 0x20; // SOFFSET for S_LOAD_DWORD
st.sgpr[18..20].copy_from_slice(&[0x0010, base_hi]); // s[18:19] = w0 + 0x10
st.sgpr[24..26].copy_from_slice(&[0xFFFF_FFF0, base_hi + 1]); // s[24:25] = w1 + 0xFFFF_FFF0
for r in st.vgpr.iter_mut() {
r[0] = 4;
r[1] = 0;
r[2] = 0x10_0000;
r[3] = 0x10;
r[4] = 0x10_0001;
r[6] = 0x20;
r[7] = 0xDEAD_C0DE;
r[8] = 0x1234_5678;
r[14] = 0x9999_9999;
}

let r = runner::run_in_arena(&dxil, &st, &arena).unwrap();
assert_eq!(r.vgpr[0][10], 0x1111_2222, "Ea::U carry w0 -> w1");
assert_eq!(r.vgpr[0][11], 0x3333_4444, "Ea::S positive carry w0 -> w1");
assert_eq!(r.vgpr[0][12], 0x5555_6666, "Ea::Mul high dword carry w0 -> w1");
assert_eq!(r.vgpr[0][13], 0x7777_8888, "Ea::Mul lo-carry + phi w0-1 -> w1");
assert_eq!(r.sgpr[20], 0x9999_AAAA, "S_LOAD_DWORD borrow then SOFFSET carry-back to w0");
assert_eq!(r.vgpr[0][14], 0, "carry past UMA limit returns 0");
assert_eq!(r.vgpr[0][15], 0x2222_3333, "Ea::S negative OFFSET with carry=1 stays in w0");
assert_eq!(*word(w0 + 0x0020), 0xDEAD_DEAD, "store borrowing below w0 is dropped");
assert_eq!(*word(w1 + 0x0020), 0xCAFE_CAFE, "store past UMA limit dropped");
}

/// The start-up check of the coherent UMA windows (`real::check_uma_import`): the GPU sees what
/// the CPU writes there. Fails, as it should, where the import fell back to memory of its own
/// (vkd3d-proton under a Wine without `userfaultfd` write watches, #175).
Expand Down
Loading