From b7177b1b5283e8462354bc1256b33bd5a147f180 Mon Sep 17 00:00:00 2001 From: gefydev Date: Mon, 23 Nov 2026 11:51:10 -0300 Subject: [PATCH] recomp: memory window from the full address, carry in 32 bits #123 again (reverted in 70f8dc4): with several memory windows, smem, MUBUF and FLAT / global picked the window from the base's high dword before the offsets were added, so an offset that carried out of the low dword went to the base's window at the wrapped address. #123 summed the address in uint64_t at every access, and the AMD shader compiler crashed on that in XDefiant (CS 8e4af296b9d7fd9c). window_at now adds the terms in 32 bits and counts the carries with a compare per term (a borrow for a negative constant offset); no 64-bit integers. With one window it emits nothing, so the single-window HLSL is unchanged. Test gpu_memory_window_carry, now also checking that the window lines have no 64-bit arithmetic. --- crates/xcl/src/gpu/recomp/emit.rs | 61 +++++++++++++++++++++++------- crates/xcl/src/gpu/recomp/tests.rs | 58 ++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 13 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 605126f4e..9317d85ac 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -1446,8 +1446,8 @@ impl Em { if name.starts_with("s_buffer_load") { // V#: base = dw0 | dw1[15:0] << 32, stride = dw1[29:16], num_records = dw2 let (d0, d1, d2) = (self.sreg(sbase), self.sreg(sbase + 1), self.sreg(sbase + 2)); - self.window(&d1); self.line(&format!("uint {t}_off = {} + {soff};", hex(imm))); + self.window_at(&d1, &[d0.clone(), format!("{t}_off")], None); self.line(&format!("uint {t}_st = ({d1} >> 16) & 0x3FFFu;")); self.line(&format!("uint {t}_lim = select({t}_st == 0u, {d2}, {d2} * {t}_st);")); self.line(&format!("uint {t}_a = ({d0} + {t}_off) & ~3u;")); @@ -1472,7 +1472,7 @@ impl Em { let imm = ((imm << 11) as i32 >> 11) as u32; // 21-bit signed let b = self.sreg(sbase); let hi = self.sreg(sbase + 1); - self.window(&hi); + self.window_at(&hi, &[b.clone(), soff.clone()], Some(imm)); // the dwords are contiguous: Load2 / Load4 (one window switch per four dwords) self.line(&format!("uint {t}_a = ({b} + {} + {soff}) & ~3u;", hex(imm))); match count { @@ -1518,15 +1518,50 @@ impl Em { } } - /// `window` for a FLAT / global address: SADDR pair (global with SADDR) or the 64-bit - /// VGPR address. - fn flat_window(&mut self, global: bool, saddr: u64, addr: u64) { + /// `window` for the address `hi:lo + offsets`: `terms` are the low dword and the unsigned + /// 32-bit offsets, `signed` a sign-extended one. Their carry (or borrow) out of the low + /// dword moves the address to the next (previous) window, as it does on the console. 32-bit + /// adds and compares only (64-bit integers made the AMD shader compiler crash on a large + /// program, #123). One window: nothing (the single-window HLSL is unchanged). + fn window_at(&mut self, hi: &str, terms: &[String], signed: Option) { + if self.opt.mem_windows <= 1 { + return; + } + let terms: Vec<&String> = terms.iter().filter(|t| t.as_str() != "0u").collect(); + let Some((first, rest)) = terms.split_first() else { + return self.window(hi); + }; + let mut body = format!("uint xcl_ws = {first}, xcl_wc = 0u, xcl_wt;"); + for t in rest { + body += &format!(" xcl_wt = {t}; xcl_ws += xcl_wt; xcl_wc += (uint)(xcl_ws < xcl_wt);"); + } + match signed.filter(|&o| o != 0) { + Some(o) if (o as i32) > 0 => body += &format!(" xcl_ws += {0}; xcl_wc += (uint)(xcl_ws < {0});", hex(o)), + Some(o) => body += &format!(" xcl_wc -= (uint)(xcl_ws < {0}); xcl_ws -= {0};", hex((o as i32).unsigned_abs())), + None => {} + } + self.line(&format!("{{ {body} xcl_w = (({hi}) + xcl_wc) & 3u; }}")); + } + + /// `window_at` for a FLAT / global address: the SADDR pair plus the VGPR offset (global + /// with SADDR) or the 64-bit VGPR address, plus the signed instruction offset. + fn flat_window(&mut self, global: bool, saddr: u64, addr: u64, off: u32) { if self.opt.mem_windows > 1 { - let hi = if global && saddr != 0x7D && saddr != 0x7F { self.sreg(saddr + 1) } else { self.vreg(addr + 1) }; - self.window(&hi); + let (hi, terms) = if global && saddr != 0x7D && saddr != 0x7F { + (self.sreg(saddr + 1), vec![self.sreg(saddr), self.vreg(addr)]) + } else { + (self.vreg(addr + 1), vec![self.vreg(addr)]) + }; + self.window_at(&hi, &terms, Some(off)); } } + /// `window_at` for a MUBUF address: V# base + SOFFSET + `{t}_off` + `{t}_stride` · index. + fn buffer_window(&mut self, d0: &str, d1: &str, soff: &str, t: &str, index: &str) { + let terms = [d0.to_string(), soff.to_string(), format!("{t}_off"), format!("{t}_stride * ({index})")]; + self.window_at(d1, &terms, None); + } + /// The sampler of the MIMG instruction at `pc`: a static sampler when its S# is constant /// (register `static_sampler_base + i`, space 1), else the sampler heap entry S# word 3 /// [11:0] names (XCL shader ABI). @@ -1574,13 +1609,13 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2, d3) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2), self.sreg(srsrc + 3)); - self.window(&d1); let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};")); + self.buffer_window(&d0, &d1, &soff, &t, &index); let act = self.act(); let in_range = |bytes: String| format!("select({t}_stride == 0u, {t}_off + {bytes} <= {d2}, {index} < {d2})"); if let Some((n, signed, store)) = small { @@ -3511,7 +3546,6 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2)); - self.window(&d1); let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); @@ -3521,6 +3555,7 @@ impl Em { self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); self.line(&format!("uint {t}_a = ({d0} + {soff} + {t}_off + {t}_stride * {index}) & ~{}u;", bytes - 1)); + self.buffer_window(&d0, &d1, &soff, &t, &index); self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + {bytes}u <= {d2}, {index} < {d2});")); let act = self.act(); if x2 { @@ -3560,13 +3595,13 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2)); - self.window(&d1); let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};")); + self.buffer_window(&d0, &d1, &soff, &t, &index); let ok = format!("select({t}_stride == 0u, {t}_off + {n}u <= {d2}, {index} < {d2})"); let act = self.act(); let v = self.vreg(vdata); @@ -3595,9 +3630,9 @@ impl Em { let addr = self.vreg(f("ADDR")); let saddr = f("SADDR"); let base = if global && saddr != 0x7D && saddr != 0x7F { format!("{} + {addr}", self.sreg(saddr)) } else { addr }; - self.flat_window(global, saddr, f("ADDR")); let raw = f("OFFSET") as u32; let off = if global { (((raw << 20) as i32) >> 20) as u32 } else { raw }; + self.flat_window(global, saddr, f("ADDR"), off); let t = self.t(); self.line(&format!("uint {t}_a = {base} + {};", hex(off))); let act = self.act(); @@ -3641,9 +3676,9 @@ impl Em { addr } }; - self.flat_window(global, saddr, f("ADDR")); let raw = f("OFFSET") as u32; let off = if global { (((raw << 20) as i32) >> 20) as u32 } else { raw }; + self.flat_window(global, saddr, f("ADDR"), off); let t = self.t(); self.line(&format!("uint {t}_a = {base} + {};", hex(off))); let act = self.act(); @@ -3713,9 +3748,9 @@ impl Em { let addr = self.vreg(f("ADDR")); let saddr = f("SADDR"); let base = if global && saddr != 0x7D && saddr != 0x7F { format!("{} + {addr}", self.sreg(saddr)) } else { addr }; - self.flat_window(global, saddr, f("ADDR")); let raw = f("OFFSET") as u32; let off = if global { (((raw << 20) as i32) >> 20) as u32 } else { raw }; + self.flat_window(global, saddr, f("ADDR"), off); let t = self.t(); let x2 = op.ends_with("_x2"); let op = op.trim_end_matches("_x2"); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 01cff4362..8ddf931f0 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2896,6 +2896,64 @@ fn gpu_memory_windows_above_4_gib() { assert_eq!(*word(w0 + 0x6000), 0, "nothing in window 0 at the same low address"); } +/// An address whose offset carries out of the low dword is in the next window: the base sits +/// at 0xFFFF_F000 of window 0, base + offset in window 1 (s_load, s_buffer_load, buffer load / +/// store, global with SADDR, global atomic). +#[test] +fn gpu_memory_window_carry() { + 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, w0 as u32), (1 << 32, 0)); + let word = |a: u64| unsafe { &mut *(a as *mut u32) }; + *word(w1 + 0x1000) = 0xC0FFEE; + *word(w1 + 0x1004) = 0x1234; + for l in 0..64u64 { + *word(w1 + 0x2000 + 4 * l) = 1000 + l as u32; + *word(w1 + 0x4000 + 4 * l) = 5000 + l as u32; + } + let list: Vec<(&str, &str, &[(&str, u64)], Option)> = vec![ + ("S_LOAD_DWORDX2", "ENC_SMEM", &[("SDATA", 20), ("SBASE", 1), ("OFFSET", 0x2000)], None), + ("S_BUFFER_LOAD_DWORD", "ENC_SMEM", &[("SDATA", 22), ("SBASE", 4), ("OFFSET", 0x3000)], None), + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 2), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("BUFFER_STORE_DWORD", "ENC_MUBUF", &[("VDATA", 2), ("VADDR", 6), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 11), ("ADDR", 1), ("SADDR", 16), ("OFFSET", 0)], None), + ("GLOBAL_ATOMIC_ADD", "ENC_FLAT_GLBL", &[("VDST", 12), ("ADDR", 3), ("DATA", 13), ("SADDR", 16), ("OFFSET", 0), ("GLC", 1)], None), + ]; + let p = prog(&list); + let t = translate(&p, &on_gpu(Options { mem_windows: 2, ..Default::default() })).unwrap(); + // 32-bit adds and compares only: 64-bit integers here crashed the AMD shader compiler (#123) + let carries: Vec<&str> = t.hlsl.lines().filter(|l| l.contains("xcl_w =")).collect(); + assert!(carries.len() >= 6 && carries.iter().all(|l| !l.contains("64")), "{carries:#?}"); + let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap(); + let hi0 = (w0 >> 32) as u32; + let mut st = KernelState::default(); + st.sgpr[2..4].copy_from_slice(&[0xFFFF_F000, hi0]); + st.sgpr[8..12].copy_from_slice(&[0xFFFF_F000, hi0 & 0xFFFF, 0x10000, 0]); + st.sgpr[12..16].copy_from_slice(&[0xFFFF_F000, hi0 & 0xFFFF, 0x10000, 0]); + st.sgpr[16..18].copy_from_slice(&[0xFFFF_F000, hi0]); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let l = l as u32; + r[0] = 0x3000 + 4 * l; // → w1 + 0x2000 + r[1] = 0x5000 + 4 * l; // → w1 + 0x4000 + r[2] = 7 * l; + r[3] = 0x7000; // → w1 + 0x6000, one counter + r[6] = 0x9000 + 4 * l; // → w1 + 0x8000 + r[13] = 1; + } + let r = runner::run_in_arena(&dxil, &st, &arena).unwrap(); + assert_eq!((r.sgpr[20], r.sgpr[21]), (0xC0FFEE, 0x1234), "s_load carried into window 1"); + assert_eq!(r.sgpr[22], 1000, "s_buffer_load carried into window 1"); + for l in 0..64u64 { + assert_eq!(r.vgpr[l as usize][10], 1000 + l as u32, "lane {l}: buffer_load carried into window 1"); + assert_eq!(r.vgpr[l as usize][11], 5000 + l as u32, "lane {l}: global_load (SADDR) carried into window 1"); + assert_eq!(*word(w1 + 0x8000 + 4 * l), 7 * l as u32, "lane {l}: buffer_store carried into window 1"); + } + assert_eq!(*word(w1 + 0x6000), 64, "global_atomic_add carried into window 1"); +} + /// End-to-end GPU test of the sparse-window VRAM runtime layout: /// - Window 0..2 = sparse VRAM (tiled reserved buffers on DEFAULT heaps) /// - Window 3 = committed coherent RAM (`OpenExistingHeapFromAddress`)