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: 20 additions & 11 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::cfg::{Cfg, Term};
use super::container::Stage;
use super::lower::{ComputeInit, Init, Lw, Partner, VAR_BUDGET, VAR_SCC, VAR_VGPR};
use super::module::{Block, Pred, Value};
use super::shader::Shader;
use super::shader::{RawUav, Shader};
use super::ssa::Ssa;
use crate::gpu::recomp::{CsInterface, MsInterface, PsInterface, VsInterface, KERNEL_SCC_SLOT, KERNEL_SGPRS, KERNEL_VGPRS, MESH_HEADER};

Expand All @@ -26,7 +26,8 @@ pub struct KernelOptions {
/// the GPU runs only 32-lane waves: a wave64 program runs as two halves
pub split: bool,
/// 4 GiB memory windows the program may address (`recomp::Options::mem_windows`):
/// `mem` at u2, then `mem1`.. at u3..
/// `mem` at u2, then `mem1`.. at u3.. (with `recomp::HEAP_WINDOWS`: descriptors
/// `MEM_HEAP_BASE`.. of the resource heap)
pub mem_windows: u32,
/// first register (space 1) of the static samplers (another stage may come first)
pub sampler_base: u32,
Expand All @@ -35,7 +36,7 @@ pub struct KernelOptions {
/// Version of the direct translator's output, part of the on-disk translation cache's
/// identity (`recomp::pso`): bump it on any change that alters the emitted containers
/// (the `gpu/dxil` sources are not in build.rs's translator source hash).
pub const TRANSLATOR_VERSION: u32 = 4;
pub const TRANSLATOR_VERSION: u32 = 5;

/// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this
/// translator, with the HLSL path as the fallback for what it cannot translate yet.
Expand Down Expand Up @@ -100,11 +101,6 @@ pub fn translate_compute(code: &[u8], o: KernelOptions, cs: &CsInterface) -> Res
}

fn translate_entry(code: &[u8], o: KernelOptions, entry: Entry) -> Result<Kernel, String> {
// memory windows through the descriptor heap (`recomp::HEAP_WINDOWS` in the count,
// `XCL_HEAP_WINDOWS=1`) are not translated yet: the caller falls back to HLSL
if o.mem_windows & crate::gpu::recomp::HEAP_WINDOWS != 0 {
return Err("unsupported: heap memory windows (XCL_HEAP_WINDOWS)".into());
}
// SSA construction recurses along the CFG (one level per guarded memory access between
// a register's definition and its use): a thread with a large stack for big programs
let code = code.to_vec();
Expand All @@ -120,6 +116,20 @@ fn translate_entry(code: &[u8], o: KernelOptions, entry: Entry) -> Result<Kernel
})?
}

/// The memory windows of `mem_windows` and whether they come from the descriptor heap
/// (`recomp::HEAP_WINDOWS` in the count, `XCL_HEAP_WINDOWS=1`; Some(globallycoherent), and
/// `mems` then only counts them). Compute work across groups relies on coherent memory
/// (as the HLSL compute mode's `globallycoherent` windows).
fn mem_windows(s: &mut Shader, mem_windows: u32, coherent: bool) -> (Vec<RawUav>, Option<bool>) {
let heap = mem_windows & crate::gpu::recomp::HEAP_WINDOWS != 0;
let n = (mem_windows & !crate::gpu::recomp::HEAP_WINDOWS).clamp(1, 4);
if heap && n > 1 {
let z = s.m.i32c(0);
return (vec![RawUav { handle: z, binding: None }; n as usize], Some(coherent));
}
((0..n).map(|k| if coherent { s.raw_uav_coherent(2 + k, 0) } else { s.raw_uav(2 + k, 0) }).collect(), None)
}

fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry) -> Result<Kernel, String> {
let cfg = Cfg::build(code)?;
let split = o.wave64 && o.split;
Expand All @@ -142,9 +152,7 @@ fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry) -> Resu
let mut s = Shader::new(stage, (6, 6));
let io = s.raw_uav(0, 0);
let res = if kernel || ms.is_some() { Some(s.raw_uav(1, 0)) } else { None };
// compute work across groups relies on coherent memory (as the HLSL compute mode's
// `globallycoherent` windows)
let mems: Vec<_> = (0..o.mem_windows.clamp(1, 4)).map(|k| if cs.is_some() { s.raw_uav_coherent(2 + k, 0) } else { s.raw_uav(2 + k, 0) }).collect();
let (mems, heap_mem) = mem_windows(&mut s, o.mem_windows, cs.is_some());
let i32 = s.m.i32();
let z = s.m.i32c(0);
let mut lw = Lw {
Expand All @@ -158,6 +166,7 @@ fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry) -> Resu
upper: z,
half: z,
mems,
heap_mem,
res,
fmt_table: None,
ntmp: 0,
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ pub struct Lw {
pub half: Value,
/// the memory windows `mem`, `mem1`.. (u2..)
pub mems: Vec<RawUav>,
/// the windows come from the descriptor heap (`lower_window`; `mems` only counts them):
/// Some(globallycoherent)
pub heap_mem: Option<bool>,
pub res: Option<RawUav>,
/// the buffer format table (`FMT_INFO`), created on first use
pub fmt_table: Option<Value>,
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Lw {
let off = self.bin(BinOp::Shl, off, k3);
let full = self.add(b64, off);
let (_, hi) = self.unpack64(full);
self.window(hi);
self.window_hi(hi, false);
}
let ty = self.andc(node, 7);
let sort = {
Expand Down
55 changes: 17 additions & 38 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use super::lower::{err, Dst, LResult, Lw, VAR_VGPR};
use super::module::{BinOp, Pred, RmwOp, Value};
use super::ops::AtomicOp;
use super::lower_window::Ea;
use super::shader::RawUav;
use crate::gpu::rdna2::{self, Decoded};
use crate::gpu::recomp::emit::buf_format_info;
Expand Down Expand Up @@ -118,6 +119,9 @@ impl Lw {
/// a switch over the windows when the program may address several, as the HLSL kernel's
/// `xcl_m*` accessors; returns `f`'s value (i32 or i64).
fn with_mem(&mut self, f: &mut dyn FnMut(&mut Lw, RawUav) -> Option<Value>) -> Option<Value> {
if let Some(coherent) = self.heap_mem {
return self.with_heap_mem(coherent, f);
}
if self.mems.len() <= 1 {
let m = self.mems[0];
return f(self, m);
Expand All @@ -130,17 +134,9 @@ impl Lw {
let mut kind = 0u8; // 0 none, 1 i32, 2 i64
let mems = self.mems.clone();
for (k, &m) in mems.iter().enumerate() {
// window 0 is the default (`switch` default: any value but 1..)
let is = if k == 0 {
let mut any = self.b(false);
for j in 1..mems.len() as u32 {
let e = self.cmpc(Pred::Eq, w, j);
any = self.lor(any, e);
}
self.lnot(any)
} else {
self.cmpc(Pred::Eq, w, k as u32)
};
// a window outside them matches none (the `switch` default: reads 0, stores
// and atomics dropped)
let is = self.cmpc(Pred::Eq, w, k as u32);
let _ = self.guarded(is, &mut |lw| {
if let Some(v) = f(lw, m) {
let t = lw.s.m.type_of(v);
Expand All @@ -166,13 +162,6 @@ impl Lw {
}
}
}
/// `xcl_w = hi & 3` (several memory windows only).
pub fn window(&mut self, hi: Value) {
if self.mems.len() > 1 {
let v = self.andc(hi, 3);
self.wr(super::lower::VAR_WIN, v);
}
}
fn mraw_atomic(&mut self, op: AtomicOp, a: Value, v: Value) -> Value {
self.with_mem(&mut |lw, m| Some(lw.s.raw_atomic(m, op, a, v))).expect("atomic value")
}
Expand Down Expand Up @@ -659,9 +648,9 @@ impl Lw {
let mut vals = Vec::new();
if name.starts_with("s_buffer_load") {
let (d0, d1, d2) = (self.sreg(sbase), self.sreg(sbase + 1), self.sreg(sbase + 2));
self.window(d1);
let k = self.c(imm);
let off = self.add(k, soff);
self.buffer_window(d0, d1, &[Ea::U(k), Ea::U(soff)], false);
let st = self.lshrc(d1, 16);
let st = self.andc(st, 0x3FFF);
let sz = self.cmpc(Pred::Eq, st, 0);
Expand All @@ -679,8 +668,8 @@ impl Lw {
let imm = ((imm << 11) as i32 >> 11) as u32;
let b = self.sreg(sbase);
let hi = self.sreg(sbase + 1);
self.window(hi);
let k = self.c(imm);
self.window_at(hi, b, &[Ea::S(k), Ea::U(soff)], false);
let a = self.add(b, k);
let a = self.add(a, soff);
for i in 0..count {
Expand Down Expand Up @@ -734,7 +723,6 @@ impl Lw {
let soff = self.code32(f("SOFFSET"), None, false).ok_or_else(|| format!("{}: op", rdna2::format(d)))?;
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 { self.c(0) };
let voff = if offen { self.vreg(vaddr + idxen as u64) } else { self.c(0) };
let stride = self.lshrc(d1, 16);
Expand All @@ -744,6 +732,7 @@ impl Lw {
let a = self.add(a, off);
let si = self.mul(stride, index);
let a = self.add(a, si);
self.buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(f("OFFSET") as u32), Ea::Mul(stride, index)], name.contains("store"));
let act = self.act();
let szero = self.cmpc(Pred::Eq, stride, 0);
let idx_ok = self.cmp(Pred::ULt, index, d2);
Expand Down Expand Up @@ -1206,7 +1195,6 @@ impl Lw {
let soff = self.code32(f("SOFFSET"), None, false).ok_or_else(|| format!("{}: op", rdna2::format(d)))?;
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 { self.c(0) };
let voff = if offen { self.vreg(vaddr + idxen as u64) } else { self.c(0) };
let x2 = op.ends_with("_x2");
Expand All @@ -1219,6 +1207,7 @@ impl Lw {
let a = self.add(a, off);
let si = self.mul(stride, index);
let a = self.add(a, si);
self.buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(f("OFFSET") as u32), Ea::Mul(stride, index)], true);
let a = self.andc(a, !(bytes - 1));
let szero = self.cmpc(Pred::Eq, stride, 0);
let e = self.addc(off, bytes);
Expand Down Expand Up @@ -1260,11 +1249,11 @@ impl Lw {

// ------------------------------------------------------------ FLAT / global

fn flat_base(&mut self, d: &Decoded) -> (Value, bool) {
fn flat_base(&mut self, d: &Decoded, write: bool) -> (Value, bool) {
let global = d.enc.name == "ENC_FLAT_GLBL";
let addr = self.vreg(Self::fv(d, "ADDR"));
let saddr = Self::fv(d, "SADDR");
self.flat_window(d);
self.flat_window(d, write);
let base = if global && saddr != 0x7D && saddr != 0x7F {
let s = self.sreg(saddr);
self.add(s, addr)
Expand All @@ -1276,16 +1265,6 @@ impl Lw {
(self.addc(base, off), global)
}

/// `flat_window`: the window of a FLAT / global address (SADDR pair or the 64-bit VGPR address).
fn flat_window(&mut self, d: &Decoded) {
if self.mems.len() > 1 {
let global = d.enc.name == "ENC_FLAT_GLBL";
let saddr = Self::fv(d, "SADDR");
let hi = if global && saddr != 0x7D && saddr != 0x7F { self.sreg(saddr + 1) } else { self.vreg(Self::fv(d, "ADDR") + 1) };
self.window(hi);
}
}

pub fn flat(&mut self, d: &Decoded, name: &str) -> LResult<()> {
let op = name.trim_start_matches("global_").trim_start_matches("flat_").trim_start_matches("scratch_");
if d.enc.name == "ENC_FLAT_SCRATCH" {
Expand All @@ -1296,7 +1275,7 @@ impl Lw {
}
let act = self.act();
if let Some((n, signed, store, half)) = flat_small(op) {
let (a, _) = self.flat_base(d);
let (a, _) = self.flat_base(d, store);
if store {
let v = self.vreg(Self::fv(d, "DATA"));
let data = self.d16_data(v, half);
Expand Down Expand Up @@ -1325,7 +1304,7 @@ impl Lw {
_ => return err(d, "FLAT"),
};
let a = if op.ends_with("_addtid") {
self.flat_window(d);
self.addtid_window(d, store);
let saddr = Self::fv(d, "SADDR");
let lane = self.lane;
let l4 = self.shlc(lane, 2);
Expand All @@ -1340,7 +1319,7 @@ impl Lw {
let off = if d.enc.name == "ENC_FLAT_GLBL" { (((raw << 20) as i32) >> 20) as u32 } else { raw };
self.addc(base, off)
} else {
self.flat_base(d).0
self.flat_base(d, store).0
};
if store {
let vals: Vec<Value> = (0..count).map(|i| self.vreg(Self::fv(d, "DATA") + i)).collect();
Expand All @@ -1365,7 +1344,7 @@ impl Lw {
}

fn flat_atomic(&mut self, d: &Decoded, op: &str) -> LResult<()> {
let (a, _) = self.flat_base(d);
let (a, _) = self.flat_base(d, true);
let x2 = op.ends_with("_x2");
let op = op.trim_end_matches("_x2");
let a = self.andc(a, if x2 { !7 } else { !3 });
Expand Down
Loading
Loading