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
72 changes: 59 additions & 13 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,12 @@ fn translate_on_this_thread(
cs.threads
}
Entry::Mesh(ms) => {
let (tid, _) = lw.setup_lanes_compute([ms.threads, 1, 1], uses_lds);
// (whole console waves: a program can widen EXEC past its declared threads)
let launched = ms.launch_threads(o.wave64);
let (tid, _) = lw.setup_lanes_compute([launched, 1, 1], uses_lds);
lw.safe_barrier_init();
mesh_init(&mut lw, ms, tid, io);
[ms.threads, 1, 1]
mesh_init(&mut lw, ms, tid, io, launched);
[launched, 1, 1]
}
Entry::Geometry(g) => {
let (tid, _) = lw.setup_lanes_compute([64, 1, 1], uses_lds);
Expand Down Expand Up @@ -796,7 +798,8 @@ impl Lw {
Entry::Vertex(_) | Entry::Pixel(_) => None,
Entry::Kernel => None,
Entry::Compute(cs) => Some(cs.threads.iter().product()),
Entry::Mesh(ms) => Some(ms.threads),
// (the launched waves: `launch_threads`)
Entry::Mesh(ms) => Some(ms.launch_threads(o.wave64)),
Entry::Geometry(_) => Some(64),
};
if let (Some(t), true) = (group_threads, split) {
Expand Down Expand Up @@ -1275,10 +1278,11 @@ fn loop_passes(lw: &mut Lw, cfg: &Cfg, threads: u32) -> (Value, BTreeMap<usize,
(cap, inside)
}

/// The mesh entry's registers (the HLSL mesh mode's prologue): SGPRs from `io` with the
/// wave index in s3[27:24], SCC 0, EXEC = the wave's threads, v5 = group · threads + slot,
/// The mesh entry's registers (the HLSL mesh mode's prologue) for a group of `launched`
/// threads (`MsInterface::launch_threads`): SGPRs from `io` with the wave index in s3[27:24],
/// SCC 0, EXEC = the wave's declared threads (`ms.threads`), v5 = group · `group_stride` + slot,
/// other VGPRs 0; and the record slots.
pub fn mesh_init(lw: &mut Lw, ms: &MsInterface, tid: Value, io: super::shader::RawUav) {
pub fn mesh_init(lw: &mut Lw, ms: &MsInterface, tid: Value, io: super::shader::RawUav, launched: u32) {
let lanes: u32 = if lw.wave64 { 64 } else { 32 };
let l = lw.c(lanes);
let wave = lw.bin(super::module::BinOp::UDiv, tid, l);
Expand Down Expand Up @@ -1310,10 +1314,11 @@ pub fn mesh_init(lw: &mut Lw, ms: &MsInterface, tid: Value, io: super::shader::R
let s3 = lw.or(s3, w24);
sgpr.insert(3, s3);
sgpr.insert(VAR_SCC, zero);
let (lo, hi) = entry_exec(lw, ms.threads, tid);
let (lo, hi) = mesh_entry_exec(lw, ms.threads, launched, tid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one, because it depends on unverified hardware behaviour: the entry EXEC is still limited to the declared ms.threads. On the console a group launched with launch_threads lanes probably starts with EXEC covering every launched lane, with the guest compiler guarding API threads itself. If so, a mesh program declaring 32 threads with max_vertices=64 that exports from lanes 32..63 without its own s_mov_b64 exec, -1 still starts those lanes inactive in both backends (here and emit.rs:5401 xcl_slot < threads). The vertex exports are dropped, which is the #3011 symptom. The PR says it didn't check whether retail programs widen EXEC, and the only positive test widens it explicitly.

sgpr.insert(126, lo);
sgpr.insert(127, hi);
let kt = lw.c(ms.threads);
let group_stride = ms.group_stride(lw.wave64);
let kt = lw.c(group_stride);
let group_base_offset = lw.c((crate::gpu::recomp::IO_MESH_GROUP_BASE * 4) as u32);
let group_base = lw.s.raw_load(io, group_base_offset, 1)[0];
let console_gid = lw.add(gid, group_base);
Expand All @@ -1322,12 +1327,11 @@ pub fn mesh_init(lw: &mut Lw, ms: &MsInterface, tid: Value, io: super::shader::R
let mut vgpr = HashMap::new();
vgpr.insert(5, v5);
lw.init.compute = Some(ComputeInit { sgpr, vgpr, zero });
lw.mesh = Some(super::lower::MeshCtx { rec, vtx, prm, vok, pok, outputs: ms.outputs.clone() });
lw.mesh = Some(super::lower::MeshCtx { rec, wave, vtx, prm, vok, pok, outputs: ms.outputs.clone() });
}

/// EXEC at entry: the console wave's threads (static per wave in the split form: no
/// exchange); a constant when every wave of the group is full, so vector writes need no
/// lane guard.
/// The compute entry's EXEC at entry, kept as before: a constant full mask on a full wave,
/// otherwise the split-wave mask or the full native-wave ballot.
fn entry_exec(lw: &mut Lw, total: u32, tid: Value) -> (Value, Value) {
let zero = lw.c(0);
let wave_threads: u32 = if lw.wave64 { 64 } else { 32 };
Expand Down Expand Up @@ -1366,6 +1370,48 @@ fn entry_exec(lw: &mut Lw, total: u32, tid: Value) -> (Value, Value) {
(lo, hi)
}

/// The mesh entry's EXEC at entry: only declared threads are active, even when the group
/// launches extra whole waves (static per wave in the split form, with no exchange).
fn mesh_entry_exec(lw: &mut Lw, total: u32, launched: u32, tid: Value) -> (Value, Value) {
let zero = lw.c(0);
let wave_threads: u32 = if lw.wave64 { 64 } else { 32 };
let (lo, hi) = if total == launched && total % wave_threads == 0 {
let all = lw.c(u32::MAX);
(all, if lw.wave64 { all } else { zero })
} else if lw.split {
let base = lw.andc(tid, !63);
let t = lw.c(total);
// (a wave past the declared threads: none)
let base = lw.umin(base, t);
let n = lw.sub(t, base);
let k64 = lw.c(64);
let n = lw.umin(n, k64);
let all = lw.c(u32::MAX);
let one = lw.c(1);
let n31 = lw.andc(n, 31);
let part = lw.bin(super::module::BinOp::Shl, one, n31);
let part = lw.sub(part, one);
let ge32 = lw.cmpc(Pred::UGe, n, 32);
let lo = lw.sel(ge32, all, part);
let k32 = lw.c(32);
let nh = lw.sub(n, k32);
let nh31 = lw.andc(nh, 31);
let parth = lw.bin(super::module::BinOp::Shl, one, nh31);
let parth = lw.sub(parth, one);
let ge64 = lw.cmpc(Pred::UGe, n, 64);
let gt32 = lw.cmpc(Pred::UGt, n, 32);
let hi = lw.sel(gt32, parth, zero);
let hi = lw.sel(ge64, all, hi);
(lo, hi)
} else {
let t = lw.cmpc(Pred::ULt, tid, total);
let w = lw.s.wave_ballot(t);
// HLSL ballot(): (m.x, m.y) — the second word is 0 in a 32-lane wave
(w[0], if lw.wave64 { w[1] } else { zero })
};
(lo, hi)
}

/// The geometry entry's registers (the HLSL geometry mode's prologue): the group's
/// primitives from `io`, s2 = the GS output area, s3 = ES vertices | GS primitives << 8,
/// SCC 0, EXEC = every lane; v0 / v1 = LDS offsets of each GS lane's triangle vertices,
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub struct ComputeInit {
/// primitive slots are in `res` (dwords), and the per-vertex outputs.
pub struct MeshCtx {
pub rec: Value,
/// this console wave's index in the mesh group
pub wave: Value,
pub vtx: Value,
pub prm: Value,
/// this thread has a vertex slot / a primitive slot in the record (its index is below the
Expand Down
9 changes: 7 additions & 2 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2642,16 +2642,21 @@ impl Lw {
// ------------------------------------------------------------ exports

/// Mesh mode `s_sendmsg`: GS_ALLOC_REQ (M0 = primitives << 12 | vertices) into the
/// record header by lane 0 of each wave.
/// record header by lane 0 of wave 0, regardless of its EXEC mask.
pub fn mesh_sendmsg(&mut self, d: &Decoded) -> LResult<()> {
if Self::fv(d, "SIMM16") & 0xF != 9 {
return Ok(());
}
let m0 = self.sreg(124);
let rec = self.mesh.as_ref().expect("mesh").rec;
let res = self.res.expect("res");
// GS_ALLOC_REQ is one request per group, issued by its first console wave; the scalar
// instruction ignores EXEC, and later waves may widen it themselves.
let lane = self.lane;
let first = self.cmpc(Pred::Eq, lane, 0);
let lane0 = self.cmpc(Pred::Eq, lane, 0);
let wave = self.mesh.as_ref().expect("mesh").wave;
let wave0 = self.cmpc(Pred::Eq, wave, 0);
let first = self.land(lane0, wave0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one: with the header written only by wave 0 (also emit.rs:1378), a program whose GS_ALLOC_REQ sits in a block wave 0 skips never writes it. That happens with an execz branch taken because wave 0's lanes are all done, or a capped loop that ends wave 0's lanes early in direct DXIL, which the PR notes as open. Header dwords 0/1 stay zero or hold the previous frame's counts, so the passthrough reads nv/np = 0 or stale. Before, any wave's lane 0 still wrote it. Writing it from the first wave that reaches s_sendmsg (e.g. a per-group flag) would cover both.

self.guarded(first, &mut |lw| {
let v = lw.andc(m0, 0x3FF);
let p = lw.lshrc(m0, 12);
Expand Down
9 changes: 9 additions & 0 deletions crates/xcl/src/gpu/dxil/robustness_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ fn a_group_barrier_of_several_hardware_waves_uses_safe_barrier_directly() {
}
let one_ms = kernel::translate_mesh(&code, o(true, false), &ms(64)).expect("a mesh pass of one 64-lane wave");
assert!(!super::validate::disassemble(&one_ms.dxil).expect("dis").contains("@xcl_bar"));
// a mesh group runs in whole console waves covering its threads, vertices and primitives
// (`MsInterface::launch_threads`): 32 threads exporting 64 vertices are two 32-lane waves,
// whose barrier is the safe one; with 32 vertices and primitives they are one wave
let one_wave = crate::gpu::recomp::MsInterface { max_vertices: 32, max_primitives: 32, ..ms(32) };
let one = kernel::translate_mesh(&code, o(false, false), &one_wave).expect("a mesh pass of one 32-lane wave");
assert!(!super::validate::disassemble(&one.dxil).expect("dis").contains("@xcl_bar"));
let two = kernel::translate_mesh(&code, o(false, false), &ms(32)).expect("32 threads with 64 vertices");
let dis = super::validate::disassemble(&two.dxil).expect("dis");
assert!(dis.contains("@xcl_bar"), "32 threads with 64 vertices are two 32-lane waves: {dis}");
// With LDS, safe barrier counters live inside xcl_lds[8189..8190] so total groupshared never
// exceeds D3D12's 32 KiB limit (no extra @xcl_bar allocation).
let with_lds = prog(&[
Expand Down
17 changes: 10 additions & 7 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl Em {
fn compute_or_mesh_hw_waves(&self) -> Option<u32> {
let threads = match &self.opt.mode {
Mode::Compute(cs) => cs.threads.iter().product::<u32>(),
Mode::Mesh(ms) => ms.threads,
Mode::Mesh(ms) => ms.launch_threads(self.opt.wave64),
_ => return None,
};
let hw_lanes = if self.x64() || !self.opt.wave64 { 32 } else { 64 };
Expand Down Expand Up @@ -1371,11 +1371,11 @@ impl Em {
Flow::Next
}
"s_sendmsg" if matches!(self.opt.mode, Mode::Mesh(_)) => {
// GS_ALLOC_REQ: M0 = primitives << 12 | vertices (scalar: whatever EXEC is)
// GS_ALLOC_REQ stores one per-group header from wave 0, independent of EXEC.
if rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) & 0xF == 9 {
let m0 = self.sreg(124);
self.line(&format!(
"if (lane_id == 0u) {{ res.Store(xcl_rec * 4u, {m0} & 0x3FFu); res.Store(xcl_rec * 4u + 4u, ({m0} >> 12) & 0x7FFu); }}"
"if (lane_id == 0u && xcl_wave == 0u) {{ res.Store(xcl_rec * 4u, {m0} & 0x3FFu); res.Store(xcl_rec * 4u + 4u, ({m0} >> 12) & 0x7FFu); }}"
));
}
Flow::Next
Expand Down Expand Up @@ -4945,7 +4945,7 @@ impl Em {
fn x64_threads(&self) -> u32 {
match &self.opt.mode {
Mode::Compute(cs) => cs.threads.iter().product(),
Mode::Mesh(ms) => ms.threads,
Mode::Mesh(ms) => ms.launch_threads(self.opt.wave64),
_ => 64,
}
}
Expand Down Expand Up @@ -5378,7 +5378,8 @@ static uint xcl_cap = 1u << 16;
h.push_str("groupshared uint xcl_lds[8192]; // D3D12 limit: 32 KB (the console allows 64 KB)\n");
}
self.safe_barrier_prelude(&mut h);
let t = ms.threads;
// whole console waves: a program can widen EXEC past its declared threads
let t = ms.launch_threads(self.opt.wave64);
let _ = writeln!(h, "[WaveSize({native_lanes})]\n[numthreads({t}, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID)\n{{");
if self.safe_barrier {
h.push_str(" xcl_s_barrier_init(gi);\n");
Expand Down Expand Up @@ -5407,13 +5408,15 @@ static uint xcl_cap = 1u << 16;
if sg.contains(&3) {
h.push_str(" s3 = (s3 & 0xF0FFFFFFu) | (xcl_wave << 24);\n");
}
let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}(true); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn());
// EXEC at entry: the declared threads (`true`: no lane is launched past them)
let entry = if ms.threads == ms.launch_threads(self.opt.wave64) { "true".to_string() } else { format!("xcl_slot < {}u", ms.threads) };
let _ = writeln!(h, " uint scc = 0u;\n {{ uint2 e0 = {}({entry}); exec_lo = e0.x; exec_hi = e0.y; }}", self.ballot_fn());
if self.x64() && self.x64_spin() {
h.push_str(if self.safe_barrier { " if (xcl_x_dead) { xcl_s_barrier_exit(); return; }\n" } else { " if (xcl_x_dead) return;\n" });
}
for &v in &self.vgprs {
let init = if v == 5 {
format!("(gid.x + io.Load({}u)) * {t}u + xcl_slot", super::IO_MESH_GROUP_BASE * 4)
format!("(gid.x + io.Load({}u)) * {}u + xcl_slot", super::IO_MESH_GROUP_BASE * 4, ms.group_stride(self.opt.wave64))
} else {
"0u".into()
};
Expand Down
16 changes: 16 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ pub struct MsInterface {
pub const MESH_HEADER: usize = 4;

impl MsInterface {
/// The threads a group of this program has on the console: whole waves (`wave64`: of 64
/// lanes, else 32) covering its threads and the vertices and primitives it may export. The
/// console launches those; the program's EXEC at entry has its declared `threads` only, and
/// it can widen EXEC to export from the other lanes.
pub fn launch_threads(&self, wave64: bool) -> u32 {
let lanes = if wave64 { 64 } else { 32 };
self.threads.max(self.max_vertices).max(self.max_primitives).max(1).next_multiple_of(lanes).min(1024)
}

/// The console's v5 per group: each group gets a disjoint range covering every launched
/// lane. Output extents can exceed declared threads, and guest code can widen EXEC later,
/// so this applies to every program, not only ones whose translation proves EXEC widening.
pub fn group_stride(&self, wave64: bool) -> u32 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one, because it depends on unverified hardware behaviour: group_stride changes v5's per-group step from the declared threads to the rounded launch width for every mesh program where they differ, including ones that worked before. A wave64 program with threads=64, max_vertices=128 that decodes its group as v5 >> 6 used to see group 1 at v5=64, but now sees v5=128 and decodes it as group 2, reading the wrong per-group data. A 32-thread wave64 program now steps by 64 instead of 32. The test fixture decodes v5 >> 7, which matches the new assumption by construction, so it doesn't confirm the console behaves this way.

self.launch_threads(wave64)
}

/// dwords per vertex: pos0 + every param (4 each, raw bits)
pub fn vert_dwords(&self) -> usize {
4 * (1 + ranked(&self.outputs, true).len())
Expand Down
Loading
Loading