Skip to content
Draft
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ next to `xcl.exe`, directly next to `xcl.exe`, in `%LOCALAPPDATA%\XCL\D3D12\`, o
masks and lane values ([`docs/wave64-on-wave32.md`](docs/wave64-on-wave32.md)) – slower than
64-lane waves, most for groups of several waves; wave64 vertex and pixel shaders that read lanes 32–63
can still differ there. Recognized compiler-pattern wave64 vertex and pixel min/max/and/or
reductions are rebuilt over the lanes the GPU launched; other missing-lane reads and scalar
spills can still differ in partial waves. With `XCL_DIRECT_DXIL=1` and `XCL_PER_LANE=1`, compute groups
reductions and constant-lane scalar spill slots preserve missing partial-wave lanes; other
missing-lane reads can still differ. With `XCL_DIRECT_DXIL=1` and `XCL_PER_LANE=1`, compute groups
made up of complete native waves can use the per-lane form for branches on whole-wave masks;
split-wave64 programs that access LDS stay on the HLSL path to preserve instruction order
between their two hardware wave32 halves.
Expand Down
17 changes: 8 additions & 9 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,14 @@ fn translate_on_this_thread(
let kernel = matches!(entry, Entry::Kernel);
let follow_loads = matches!(entry, Entry::Kernel | Entry::Compute(_));
let gfx = matches!(entry, Entry::Vertex(_) | Entry::Pixel(_));
if gfx
&& o.wave64
&& o.split
&& crate::gpu::recomp::upper_lanes::analyze(&cfg.insts)
.reads
.values()
.any(|&r| r != crate::gpu::recomp::upper_lanes::Read::Native)
{
return Err("wave64 VS / PS in 32-lane waves: a read of lanes 32–63".into());
if gfx && o.wave64 {
let upper = crate::gpu::recomp::upper_lanes::analyze(&cfg.insts);
if upper.reads.values().any(|&r| r == crate::gpu::recomp::upper_lanes::Read::Shadow) {
return Err("wave64 graphics v_writelane spill needs the HLSL path".into());
}
if o.split && upper.reads.values().any(|&r| r != crate::gpu::recomp::upper_lanes::Read::Native) {
return Err("wave64 VS / PS in 32-lane waves: a read of lanes 32–63".into());
}
}
let compute = matches!(entry, Entry::Compute(_));
let split = o.wave64 && o.split && !gfx;
Expand Down
29 changes: 23 additions & 6 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ impl Em {
self.opt.wave64 && self.opt.split_wave64 && matches!(self.opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_) | Mode::Geometry(_))
}

/// An upper-lane spill shadow may need to distinguish a present native lane from a lane
/// that the PC wave did not launch.
fn upper_presence(&self) -> bool {
self.upper.as_ref().is_some_and(|u| u.shadows.iter().any(|&(_, l)| l >= 32))
}

/// `ballot` of the program's wave (both halves' words in the split form, the helper lanes'
/// bits in a pixel shader).
fn ballot_fn(&self) -> &'static str {
Expand Down Expand Up @@ -4139,9 +4145,8 @@ impl Em {
}
let rla = self.rla_uniform_fn();
let read = format!("{rla}({a}, {b} & {wmask}u)");
// wave64 VS / PS (`upper_lanes`): in a 32-lane wave, lanes 32..63 without a lane
// read; a spill slot < 32 from its shadow whatever the wave size (its lane may
// not have been launched)
// wave64 VS / PS (`upper_lanes`): replace absent upper lanes with known values;
// read a spill slot from its shadow when its physical lane was not launched
let slot = super::upper_lanes::slot(d);
match (self.upper.as_ref().and_then(|u| u.reads.get(&d.pc)).copied(), slot) {
(Some(Read::Const(c)), Some(_)) => self.line(&format!("uint {t} = {c:#010x}u; if (!xcl_hw32) {t} = {read};")),
Expand All @@ -4150,7 +4155,10 @@ impl Em {
self.line(&format!("{t} = select(and(xcl_hw32, ({b} & 63u) >= 32u), {c:#010x}u, {t});"));
}
(Some(Read::Shadow), Some((v, l))) if l < 32 => self.line(&format!("uint {t} = xcl_sh{v}_{l};")),
(Some(Read::Shadow), Some((v, l))) => self.line(&format!("uint {t} = xcl_sh{v}_{l}; if (!xcl_hw32) {t} = {read};")),
(Some(Read::Shadow), Some((v, l))) => {
self.line(&format!("uint {t} = xcl_sh{v}_{l};"));
self.line(&format!("if (lanebit_at(xcl_present.x, xcl_present.y, {l}u)) {t} = {read};"));
}
_ => self.line(&format!("uint {t} = {read};")),
}
// split wave64 (no `upper` there): the last arm, an exchange
Expand Down Expand Up @@ -4965,8 +4973,11 @@ impl Em {
self.x64_prelude(&mut h);
}
if let Some(u) = &self.upper {
// wave64 VS / PS on a GPU with 32-lane waves only (`upper_lanes`)
h.push_str("// the wave has 32 lanes: it is a console wave whose lanes 32..63 are not populated\nstatic bool xcl_hw32 = false;\n");
// wave64 VS / PS missing-lane values (`upper_lanes`)
h.push_str("static bool xcl_hw32 = false;\n");
if self.upper_presence() {
h.push_str("static uint2 xcl_present = uint2(0u, 0u); // physical lanes (PS: whole live quads)\n");
}
for (v, l) in &u.shadows {
let _ = writeln!(h, "static uint xcl_sh{v}_{l} = 0u; // v_writelane slot: v{v} lane {l}");
}
Expand Down Expand Up @@ -5056,6 +5067,9 @@ impl Em {
if self.upper.is_some() {
h.push_str(" xcl_hw32 = WaveGetLaneCount() < 64u;\n");
}
if self.upper_presence() {
h.push_str(" xcl_present = ballot(true);\n");
}
load_sgprs(&mut h);
h.push_str(" uint scc = 0u;\n { uint2 e0 = ballot(true); exec_lo = e0.x; exec_hi = e0.y; }\n");
for &v in &self.vgprs {
Expand Down Expand Up @@ -5412,6 +5426,9 @@ impl Em {
if self.upper.is_some() {
h.push_str(" xcl_hw32 = WaveGetLaneCount() < 64u;\n");
}
if self.upper_presence() {
h.push_str(" xcl_present = ballot_px(true);\n");
}
load_sgprs(&mut h);
h.push_str(" uint scc = 0u;\n { uint2 e0 = ballot(true); exec_lo = e0.x; exec_hi = e0.y; }\n");
let sys = |bit: u32| ps_input_vgpr(ps.input_ena, bit);
Expand Down
21 changes: 15 additions & 6 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,12 +1680,21 @@ fn translate_inner(code: &[u8], opt: &Options) -> Result<Translation, String> {
} else if matches!(opt.mode, Mode::Pixel(_)) {
em.mask_whole_live = x64_masks::by_pc(&insts, false);
}
// 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
if opt.wave64 && opt.split_wave64 && matches!(opt.mode, Mode::Vertex(_) | Mode::Pixel(_)) {
let a = upper_lanes::analyze(&insts);
if a.reads.values().any(|r| matches!(r, upper_lanes::Read::Const(_) | upper_lanes::Read::Shadow)) {
// Wave64 VS / PS: absent upper lanes on a 32-lane GPU, and v_writelane slots whose target
// lane may not have been launched in a partial wave. Constants only need the 32-lane form;
// spill shadows are needed at either hardware wave size.
if opt.wave64 && matches!(opt.mode, Mode::Vertex(_) | Mode::Pixel(_)) {
let mut a = upper_lanes::analyze(&insts);
let shadow = a.reads.values().any(|r| matches!(r, upper_lanes::Read::Shadow));
let split_const = opt.split_wave64 && a.reads.values().any(|r| matches!(r, upper_lanes::Read::Const(_)));
if shadow || split_const {
if !opt.split_wave64 {
for r in a.reads.values_mut() {
if matches!(r, upper_lanes::Read::Const(_)) {
*r = upper_lanes::Read::Native;
}
}
}
em.upper = Some(a);
}
}
Expand Down
53 changes: 35 additions & 18 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,34 @@ fn spill_slots(slot: fn(u32) -> u32) -> Vec<u8> {
code
}

#[test]
fn wave64_graphics_spill_shadows_cover_native_partial_waves() {
let code = program_of(&[
ins("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 30), ("SRC0", 20), ("SRC1", 128 + 5)], None),
ins("V_WRITELANE_B32", "ENC_VOP3", &[("VDST", 30), ("SRC0", 21), ("SRC1", 128 + 36)], None),
ins("V_READLANE_B32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V + 30), ("SRC1", 128 + 5)], None),
ins("V_READLANE_B32", "ENC_VOP3", &[("VDST", 23), ("SRC0", V + 30), ("SRC1", 128 + 36)], None),
// With the analyzer's missing upper lanes EXEC_HI is zero, so this clears the low
// shadow but leaves the console's lane-36 spill. A physically present lane 36 does
// execute the move, however, and its native value must win over that shadow.
ins("V_MOV_B32", "ENC_VOP1", &[("VDST", 30), ("SRC0", 20)], None),
ins("V_READLANE_B32", "ENC_VOP3", &[("VDST", 24), ("SRC0", V + 30), ("SRC1", 128 + 36)], None),
]);
for split_wave64 in [false, true] {
let ps = translate(&code, &Options { mode: Mode::Pixel(draw_psi()), wave64: true, split_wave64, ..Default::default() }).unwrap().hlsl;
assert!(ps.contains("xcl_present = ballot_px(true);") && ps.contains(" = xcl_sh30_5;"), "{ps}");
assert_eq!(ps.matches("lanebit_at(xcl_present.x, xcl_present.y, 36u)").count(), 2, "{ps}");
let vs = translate(&code, &Options { mode: Mode::Vertex(draw_vsi()), wave64: true, split_wave64, ..Default::default() }).unwrap().hlsl;
assert!(vs.contains("xcl_present = ballot(true);"), "{vs}");
assert_eq!(vs.matches("lanebit_at(xcl_present.x, xcl_present.y, 36u)").count(), 2, "{vs}");
}
for split in [false, true] {
let o = crate::gpu::dxil::kernel::KernelOptions { wave64: true, split, mem_windows: 1, sampler_base: 0 };
let e = crate::gpu::dxil::kernel::translate_pixel(&code, o, &draw_psi()).err().expect("direct DXIL accepted a spill shadow");
assert!(e.contains("v_writelane spill needs the HLSL path"), "{e}");
}
}

/// The compiler's whole-wave reduction: wave64 adds the upper half through lane 32.
fn whole_reduction(op: &str, identity: u64, out: u64, wave64: bool, saved_mask: bool, upper_lane: u64, clobber: Option<u64>) -> Vec<u8> {
let width = if wave64 { "B64" } else { "B32" };
Expand Down Expand Up @@ -2677,17 +2705,9 @@ fn gpu_wave64_ps_spill_slots() {
let small = draw_console(None, Some(&code), &[[0.0, 0.0], [3.2, 0.0], [0.0, 3.2]]);
let covered: Vec<(usize, &[f32; 4])> = small.iter().enumerate().filter(|(_, p)| p[3] == 1.0).collect();
assert_eq!(covered.len(), 6);
// the 32-lane form keeps slots < 32 whatever the wave size, slots ≥ 32 in 32-lane waves;
// the native form (GPUs with 64-lane waves) still loses the slots of lanes not launched
let (lo, hi) = (runner::graphics_wave32(), runner::wave64_supported() == Ok(false));
for (i, p) in covered {
println!("pixel {i}, partial wave: {:?} (want {want:?})", &p[..2]);
if lo {
assert_eq!(p[0], want[0], "pixel {i}, partial wave: slots 0..31");
}
if hi {
assert_eq!(p[1], want[1], "pixel {i}, partial wave: slots 32..47");
}
assert_eq!([p[0], p[1]], want, "pixel {i}, partial wave: slots 0..31, 32..47");
}
}

Expand Down Expand Up @@ -2722,17 +2742,14 @@ fn gpu_wave64_vs_spill_slots_and_identity() {
]);
let px = draw_console(Some(&code), None, &[[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]]);
let want = [(0..32).map(slot).sum::<u32>() as f32, (32..48).map(slot).sum::<u32>() as f32, 1.0, 1.0];
// as in `gpu_wave64_ps_spill_slots`: slots < 32 in the 32-lane form; slots ≥ 32 and the
// identity in lane 32 in 32-lane waves (in a 64-lane wave of three vertices, lane 32 was
// not launched either)
let (lo, hi) = (runner::graphics_wave32(), runner::wave64_supported() == Ok(false));
// Spill slots are exact at either hardware wave size. The unrelated whole-wave identity is
// still reconstructed only in the 32-lane form.
let identity = runner::wave64_supported() == Ok(false);
println!("{:?} (want {want:?})", px[0]);
for (i, p) in px.iter().enumerate() {
if lo {
assert_eq!(p[0], want[0], "pixel {i}: slots 0..31");
}
if hi {
assert_eq!(&p[1..], &want[1..], "pixel {i}: slots 32..47, lane 32");
assert_eq!(&p[..2], &want[..2], "pixel {i}: slots 0..31, 32..47");
if identity {
assert_eq!(&p[2..], &want[2..], "pixel {i}: lane 32 identity");
}
}
}
Expand Down
37 changes: 18 additions & 19 deletions docs/wave64-on-wave32.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ wave64 VS / PS (13 210 programs; 749 – 748 PS, 1 VS – were flagged by the ol

### The form

For a wave64 VS / PS with `split_wave64`, `upper_lanes.rs` follows what the console's lanes
32..63 hold through the program, a forward data-flow pass over its control flow:
For a wave64 VS / PS, `upper_lanes.rs` follows what the console's lanes 32..63 hold through the
program, a forward data-flow pass over its control flow. Constants are used only in the
`split_wave64` form; spill shadows are also used in native partial waves:
- EXEC's upper word, with the SGPRs as words known to be 0, all ones, or anything: 0 at the
start, 0 in every mask a VALU instruction writes while it is 0, combined by the bitwise SALU
and saveexec forms. While it is 0 no VALU instruction writes lanes 32..63.
Expand All @@ -240,28 +241,30 @@ Each read then gets:
which no program can depend on;
- a constant: that constant (`uint t = c; if (!xcl_hw32) t = rla(…);`);
- a slot: its shadow `xcl_sh<vgpr>_<lane>`, a scalar every `v_writelane` of that slot also
writes;
writes. A read uses the native lane when it was physically launched and the shadow otherwise
(pixel helper lanes are present as members of a live quad);
- unknown: stays a hazard – `wave_hazards` reports only these.

Spill slots < 32 are read from their shadows as well, whatever the wave size, as long as no
other instruction has written the VGPR since the slot's `v_writelane`: in a partial wave (a
primitive's edge) a lane that was not launched loses what `v_writelane` put there. 287 pixel
shaders of the pack (3 540 reads) spill to lanes < 32.
Spill slots < 32 are read directly from their shadows, as long as no other instruction has
written the VGPR since the slot's `v_writelane`. For slots ≥ 32 the entry physical-presence mask
selects the native lane when it exists and the shadow when it did not launch. 287 pixel shaders
of the pack (3 540 reads) spill to lanes < 32.

The wave size is checked at run time (`xcl_hw32 = WaveGetLaneCount() < 64`), so a driver that
picks 64 lanes runs the program as before.
The wave size is checked at run time (`xcl_hw32 = WaveGetLaneCount() < 64`) for constants. Spill
slots additionally capture the physical lanes at entry (`ballot(true)`, or helper-inclusive
`ballot_px(true)` for pixels).

Which GPUs get the form: every GPU whose wave range includes 32 (`pso::graphics_wave32`), not
only those without 64-lane waves. D3D12 has no `WaveSize` for graphics shaders, so the driver
Which GPUs get the constant form: every GPU whose wave range includes 32 (`pso::graphics_wave32`),
not only those without 64-lane waves. Spill shadows are emitted at every wave size. D3D12 has no
`WaveSize` for graphics shaders, so the driver
picks, and an RDNA2 driver (RX 6600 XT, issue #42) runs a wave64 PS in waves of 32 although it
reports 32–64: without the form, `v_readlane` 32 wrapped to lane 0 there
(`gpu_wave64_ps_whole_wave_reductions` summed twice). Compute, mesh and geometry programs set
`[WaveSize(64)]` and still get the split form only on GPUs without 64-lane waves.

On a GPU with 64-lane waves only, nothing changes: the HLSL of every program of the pack is
identical to the one before
(`retail_wave64_graphics_lane_forms` prints digests of the native form, and of the other modes'
32-lane form, to compare builds).
On a GPU with 64-lane waves only, programs with spill slots gain their shadows; other programs'
native HLSL is unchanged (`retail_wave64_graphics_lane_forms` prints digests of the forms to
compare builds).

In the pack every read resolves: 1 082 constants, 355 unwritten, 3 740 slot reads; none is
left unsafe. Only a program with a constant or a slot to read gets the form: the HLSL changes
Expand All @@ -281,10 +284,6 @@ The three-quad case that previously made min return 0 is covered by
`gpu_wave64_ps_whole_wave_reductions`. Other whole-wave sequences that do not match this exact
pattern can still read undefined values from lanes the PC did not launch.

Spill slots of lanes that were not launched are also still lost in the native form (GPUs with
64-lane waves, which keep the HLSL as it was), and in the 32-lane form for slots ≥ 32 when the
driver picks 64 lanes.

## Relation to earlier work

* **PR #33** (ElHombreAlex, draft): first wave64 → wave32 "rendezvous" through groupshared
Expand Down
Loading