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
38 changes: 31 additions & 7 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub(super) struct Em {
/// split wave64: pc → SGPRs whose whole value may be read after that instruction
/// (`x64_masks`); a mask written to none of them stays in its half (no exchange)
pub x64_whole_live: BTreeMap<u64, u128>,
/// compute mode: the group is padded to whole console waves (`partial_waves`), the
/// padding lanes inactive at the start
pub cs_pad: bool,
/// wave64 VS / PS on a GPU with 32-lane waves only: what the console's lanes 32..63 hold
/// where the program reads them (`upper_lanes`), used when the wave has 32 lanes
pub upper: Option<super::upper_lanes::UpperLanes>,
Expand Down Expand Up @@ -481,6 +484,7 @@ impl Em {
uses_scratch: false,
x64_words: 1,
x64_whole_live: BTreeMap::new(),
cs_pad: false,
upper: None,
cur_pc: 0,
static_ss: BTreeMap::new(),
Expand All @@ -496,7 +500,9 @@ impl Em {
/// Hardware wave count for Compute / Mesh threadgroups (`None` for other stages).
fn compute_or_mesh_hw_waves(&self) -> Option<u32> {
let threads = match &self.opt.mode {
Mode::Compute(cs) => cs.threads.iter().product::<u32>(),
// a padded group launches its padding too (in split wave64, a group of 32 threads or
// fewer padded to 64 is two hardware waves)
Mode::Compute(cs) => self.cs_threads(cs),
Mode::Mesh(ms) => ms.threads,
_ => return None,
};
Expand Down Expand Up @@ -4160,10 +4166,21 @@ impl Em {

// ---------------------------------------------------------------- whole shader

/// Split wave64: threads of the console's thread group.
/// Compute mode: the threads of the PC group – the console's, or whole console waves of
/// them (`cs_pad`).
fn cs_threads(&self, cs: &super::CsInterface) -> u32 {
let threads: u32 = cs.threads.iter().product();
if self.cs_pad {
threads.next_multiple_of(if self.opt.wave64 { 64 } else { 32 })
} else {
threads
}
}

/// Split wave64: threads of the thread group.
fn x64_threads(&self) -> u32 {
match &self.opt.mode {
Mode::Compute(cs) => cs.threads.iter().product(),
Mode::Compute(cs) => self.cs_threads(cs),
Mode::Mesh(ms) => ms.threads,
_ => 64,
}
Expand Down Expand Up @@ -4385,7 +4402,10 @@ impl Em {
// thread indices (AMD RDNA4 lays them out in 2 x 2 quads: lanes 0, 1, 4, 5, 8 …
// of an 8 x 8 group; NVIDIA differs too), the console does – a hardware wave
// takes the next `lanes` console threads as it arrives, lane l the l-th of them
let reorder = !self.x64() && y * z > 1;
// a padded group (`cs_pad`) is declared 1-D and takes its threads the same
// way, so the lanes of its last wave that the console launches are its first
let padded = self.cs_pad.then(|| self.cs_threads(cs));
let reorder = !self.x64() && (y * z > 1 || padded.is_some());
if reorder {
// console threads handed to waves so far: the program's top LDS word while
// the waves take their threads (the console's LDS starts undefined; D3D12's
Expand All @@ -4397,9 +4417,10 @@ impl Em {
}
}
self.safe_barrier_prelude(&mut h);
let [nx, ny, nz] = padded.map_or([x, y, z], |t| [t, 1, 1]);
let _ = writeln!(
h,
"[WaveSize({hw_lanes})]\n[numthreads({x}, {y}, {z})]\nvoid main(uint3 gid : SV_GroupID, uint3 gtid : SV_GroupThreadID, uint gi : SV_GroupIndex)\n{{"
"[WaveSize({hw_lanes})]\n[numthreads({nx}, {ny}, {nz})]\nvoid main(uint3 gid : SV_GroupID, uint3 gtid : SV_GroupThreadID, uint gi : SV_GroupIndex)\n{{"
);
if self.safe_barrier {
h.push_str(" xcl_s_barrier_init(gi);\n");
Expand All @@ -4409,7 +4430,7 @@ impl Em {
self.x64_entry(&mut h);
let _ = writeln!(h, " gtid = uint3(xcl_tid % {x}u, (xcl_tid / {x}u) % {y}u, xcl_tid / {}u);\n gi = xcl_tid;", x * y);
} else if reorder {
let t = x * y * z;
let t = padded.unwrap_or(x * y * z);
let _ = writeln!(
h,
" if (gi == 0u) XCL_WT = 0u;\n GroupMemoryBarrierWithGroupSync();\n lane_id = WaveGetLaneIndex();\n {{\n bool full = WaveActiveCountBits(true) == {lanes}u;\n uint base = {}u;\n if (WaveIsFirstLane() && full) InterlockedAdd(XCL_WT, {lanes}u, base);\n uint xcl_tid = WaveReadLaneFirst(base) + lane_id;\n gtid = uint3(xcl_tid % {x}u, (xcl_tid / {x}u) % {y}u, xcl_tid / {}u);\n gi = xcl_tid;\n }}\n GroupMemoryBarrierWithGroupSync(); // all waves have their threads before the program writes LDS",
Expand All @@ -4434,7 +4455,10 @@ impl Em {
// waves in the group: a last, partial wave counts (96 threads: 2 of 64)
let _ = writeln!(h, " {} = {}u | ((gi / {lanes}u) << 6);", sname(next).unwrap(), (x * y * z).div_ceil(lanes));
}
let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}(true); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn());
// a padded group: the lanes past the console's threads start inactive (`gi` is
// the console thread's index here)
let launched = if padded.is_some() { format!("gi < {}u", x * y * z) } else { "true".into() };
let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}({launched}); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn());
for &v in &self.vgprs {
let init = match v {
0 => "gtid.x",
Expand Down
42 changes: 40 additions & 2 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod dxc;
pub(crate) mod emit;
pub mod mesh_dispatch;
pub mod mesh_wave;
pub mod partial_waves;
pub mod x64_masks;
pub mod pso;
pub use emit::mesh_passthrough;
Expand Down Expand Up @@ -510,11 +511,29 @@ pub struct Options {
/// structured control flow when the program allows it (false, or `XCL_RECOMP_NO_STRUCTURED`:
/// always the guarded block chain)
pub structured: bool,
/// compute groups that are not whole console waves
pub pad_waves: PadWaves,
}

/// A compute group that is not a whole number of console waves (40 threads: one wave64 with
/// lanes 0..39 launched) as a PC group of whole waves, its extra lanes inactive at the start
/// as the console's are: for a program that can tell them (`partial_waves`: it turns them on
/// in EXEC, or writes them with `v_writelane`). Every other program keeps its group.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PadWaves {
/// where the program can tell; `XCL_CS_PAD_ALL=1` / `XCL_NO_CS_PAD=1` make it `Always` /
/// `Never` (for comparison)
#[default]
WhereSeen,
/// every group that is not whole waves
Always,
/// never: the group as the console declares it
Never,
}

impl Default for Options {
fn default() -> Self {
Options { mode: Mode::Kernel, wave64: true, hw_wave: 0, split_wave64: false, static_sampler_base: 0, mem_windows: 1, heap_windows: false, structured: true }
Options { mode: Mode::Kernel, wave64: true, hw_wave: 0, split_wave64: false, static_sampler_base: 0, mem_windows: 1, heap_windows: false, structured: true, pad_waves: PadWaves::WhereSeen }
}
}

Expand Down Expand Up @@ -1417,13 +1436,32 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
}
}
let mut em = emit::Em::new(opt.clone());
em.configure_round_modes(&insts, branches == 0);
em.static_ss = constant_samplers(&insts, &leaders);
// split wave64: masks only read lane by lane skip the halves' exchange (geometry mode
// continues from the ES into the GS through s_setpc: left as it is)
if opt.wave64 && opt.split_wave64 && matches!(opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_)) && std::env::var_os("XCL_X64_ALL_MASKS").is_none() {
em.x64_whole_live = x64_masks::by_pc(&insts, matches!(opt.mode, Mode::Kernel));
}
// a compute group that is not whole console waves: padded to whole waves where the
// program can tell the lanes its last wave does not launch
if let Mode::Compute(cs) = &opt.mode {
let lanes = if opt.wave64 { 64 } else { 32 };
let threads: u32 = cs.threads.iter().product();
let pad = match opt.pad_waves {
PadWaves::WhereSeen if std::env::var_os("XCL_NO_CS_PAD").is_some() => PadWaves::Never,
PadWaves::WhereSeen if std::env::var_os("XCL_CS_PAD_ALL").is_some() => PadWaves::Always,
p => p,
};
em.cs_pad = !threads.is_multiple_of(lanes)
&& threads.next_multiple_of(lanes) <= 1024
&& match pad {
PadWaves::Always => true,
PadWaves::Never => false,
PadWaves::WhereSeen => partial_waves::sees_lanes_not_launched(&insts, opt.wave64, threads % lanes).is_some(),
};
}
// after the padding: the group's hardware waves count its padding
em.configure_round_modes(&insts, branches == 0);
// a wave64 VS / PS on a GPU that can run 32-lane waves: what lanes 32..63 hold where the
// program reads them (the driver may still pick 64 lanes: decided at run time); only a
// program with a read to change gets the form
Expand Down
Loading
Loading