mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Merge pull request #123 from gefydev/recomp-window-carry
This commit is contained in:
@@ -1441,8 +1441,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;"));
|
||||
@@ -1467,7 +1467,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 {
|
||||
@@ -1513,15 +1513,40 @@ 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.
|
||||
/// One window: nothing (the single-window HLSL is unchanged).
|
||||
fn window_at(&mut self, hi: &str, terms: &[String], signed: Option<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 mut sum: Vec<String> = terms.iter().map(|t| format!("(uint64_t)({t})")).collect();
|
||||
if let Some(o) = signed.filter(|&o| o != 0) {
|
||||
sum.push(format!("(uint64_t)(int64_t)(int){}", hex(o)));
|
||||
}
|
||||
self.line(&format!("xcl_w = (({hi}) + (uint)(({}) >> 32)) & 3u;", sum.join(" + ")));
|
||||
}
|
||||
}
|
||||
|
||||
/// `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, 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
|
||||
/// (the stride product in 64 bits).
|
||||
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!("(uint64_t){t}_stride * (uint64_t)({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).
|
||||
@@ -1569,13 +1594,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 {
|
||||
@@ -3487,7 +3512,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();
|
||||
@@ -3497,6 +3521,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 {
|
||||
@@ -3536,13 +3561,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);
|
||||
@@ -3571,9 +3596,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();
|
||||
@@ -3617,9 +3642,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();
|
||||
@@ -3689,9 +3714,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");
|
||||
|
||||
@@ -2573,6 +2573,61 @@ 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<u32>)> = 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();
|
||||
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`)
|
||||
|
||||
Reference in New Issue
Block a user