Skip to content
Closed
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
44 changes: 44 additions & 0 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,50 @@ impl Em {
}
return Ok(());
}
// ds_append / ds_consume: the counter at M0[15:0] + offset16 grows / shrinks by the
// wave's active lane count in one atomic, and every active lane gets its old value.
// Lane 0 of the console wave does the atomic (no change for an empty EXEC); in the
// split wave64 form that is the lower half's lane 0, read by both halves after an
// exchange.
if matches!(name, "ds_append" | "ds_consume") {
if f("GDS") != 0 {
return Err("GDS");
}
self.uses_lds = true;
let m0 = self.sreg(124);
let at = format!("(({m0} & 0xFFFFu) + {off16}u)");
let slot = if self.x64() {
format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_X_LDS_TOP)]")
} else if self.safe_barrier {
format!("xcl_lds[min((({at}) >> 2) & 8191u, XCL_LDS_TOP)]")
} else {
format!("xcl_lds[(({at}) >> 2) & 8191u]")
};
let n = if self.opt.wave64 { "countbits(exec_lo) + countbits(exec_hi)" } else { "countbits(exec_lo)" };
self.line(&format!("uint {t}_n = {n};"));
let delta = if name == "ds_append" {
format!("{t}_n")
} else {
// the negated count goes through a lane read (the same value in every lane
// of the console wave): NVIDIA's driver (32.0.16.1742) refuses a pipeline
// whose groupshared atomic add takes `0 - x` straight (E_OUTOFMEMORY)
let rd = if self.x64() { format!("WaveReadLaneFirst(0u - {t}_n)") } else { format!("rla(0u - {t}_n, 0u)") };
self.line(&format!("uint {t}_d = {rd};"));
format!("{t}_d")
};
if self.x64() {
// the halves are two hardware waves: the upper half's earlier LDS accesses
// (e.g. a store of the counter) are done before the lower half's atomic
self.line("xcl_x_meet();");
}
self.line(&format!("uint {t} = 0u;"));
self.line(&format!("if (lane_id == 0u && {t}_n != 0u) InterlockedAdd({slot}, {delta}, {t});"));
let rla = self.rla_uniform_fn();
self.line(&format!("{t} = {rla}({t}, 0u);"));
let dst = Dst::V(f("VDST"));
self.write32(&dst, &t);
return Ok(());
}
self.uses_lds = true;
// the address is read before any result is written (ds_read2_b32 v[0:1], v0 …)
let addr = self.vreg(f("ADDR"));
Expand Down
98 changes: 98 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3920,6 +3920,104 @@ fn gpu_ds_addtid_roundtrip_and_exec_masking() {
}
}

/// `DS_APPEND` / `DS_CONSUME` (LDS form) translate with their counter at `M0[15:0] + offset16`;
/// `GDS=1` is reported as unsupported.
#[test]
fn ds_append_consume_translate_and_reject_gds() {
let code = prog(&[
("DS_APPEND", "ENC_DS", &[("VDST", 3), ("OFFSET0", 0x10)], None),
("DS_CONSUME", "ENC_DS", &[("VDST", 4), ("OFFSET0", 0x10)], None),
]);
for opt in [Options::default(), Options { wave64: false, hw_wave: 32, ..Options::default() }, runner::options(Mode::Kernel)] {
let t = translate(&code, &opt).unwrap();
assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl);
assert!(t.hlsl.contains("((m0 & 0xFFFFu) + 16u)"), "{}", t.hlsl);
}
let gds = prog(&[("DS_APPEND", "ENC_DS", &[("VDST", 3), ("GDS", 1)], None)]);
let t = translate(&gds, &Options::default()).unwrap();
assert_eq!(t.unsupported.get("ds_append"), Some(&1), "{:?}", t.unsupported);
assert!(!t.hlsl.contains("groupshared uint xcl_lds"), "{}", t.hlsl);
}

/// The counter program of the `DS_APPEND` / `DS_CONSUME` GPU tests: the counter at byte 272
/// (M0 = 0xABCD_0100: M0[15:0] = 256, offset 16) is seeded with v2 through v1 = 272; then, with
/// EXEC = s[0:1], append into v3 and consume into v4; with an empty EXEC, append into v7; with
/// every lane, append into v5 and read the counter into v6.
fn ds_append_consume_program() -> Vec<u8> {
prog(&[
("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 255)], Some(0xABCD_0100)),
("DS_WRITE_B32", "ENC_DS", &[("ADDR", 1), ("DATA0", 2)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 0)], None),
("DS_APPEND", "ENC_DS", &[("VDST", 3), ("OFFSET0", 0x10)], None),
("DS_CONSUME", "ENC_DS", &[("VDST", 4), ("OFFSET0", 0x10)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 128)], None),
("DS_APPEND", "ENC_DS", &[("VDST", 7), ("OFFSET0", 0x10)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)], None),
("DS_APPEND", "ENC_DS", &[("VDST", 5), ("OFFSET0", 0x10)], None),
("DS_READ_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 1)], None),
])
}

fn ds_append_consume_state(exec_lo: u32, exec_hi: u32) -> KernelState {
let mut st = KernelState::default();
st.sgpr[0] = exec_lo;
st.sgpr[1] = exec_hi;
for (l, r) in st.vgpr.iter_mut().enumerate() {
let l = l as u32;
r[1] = 272;
r[2] = 1000;
r[3] = 0xCAFE_0000 + l;
r[4] = 0xBEEF_0000 + l;
r[7] = 0xF00D_0000 + l;
}
st
}

/// `DS_APPEND` / `DS_CONSUME` on the GPU, wave64 (split into two wave32 halves on a GPU with
/// 32-lane waves only): one atomic per wave by its active lane count (lanes in both halves),
/// the old counter value in every active lane, inactive lanes untouched, an empty EXEC a no-op.
#[test]
fn gpu_ds_append_consume_wave64() {
if !gpu_ready() {
return;
}
let st = ds_append_consume_state(0x0000_00FF, 0x0000_0003); // lanes 0..7, 32, 33
let r = runner::run_code(&ds_append_consume_program(), &st, &[]).unwrap();
for l in 0..64 {
let lu = l as u32;
let active = l < 8 || l == 32 || l == 33;
let v = &r.vgpr[l];
assert_eq!(v[3], if active { 1000 } else { 0xCAFE_0000 + lu }, "lane {l}: ds_append");
assert_eq!(v[4], if active { 1010 } else { 0xBEEF_0000 + lu }, "lane {l}: ds_consume");
assert_eq!(v[7], 0xF00D_0000 + lu, "lane {l}: ds_append with an empty EXEC");
assert_eq!(v[5], 1000, "lane {l}: ds_append, every lane");
assert_eq!(v[6], 1064, "lane {l}: the counter after it");
}
}

/// `DS_APPEND` / `DS_CONSUME` on the GPU, a wave32 program in a 32-lane wave.
#[test]
fn gpu_ds_append_consume_wave32() {
if !gpu_ready() {
return;
}
let t = translate(&ds_append_consume_program(), &Options { wave64: false, hw_wave: 32, ..Options::default() }).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap();
let st = ds_append_consume_state(0x8000_0005, 0); // lanes 0, 2, 31
let r = runner::run(&dxil, &st, &[]).unwrap();
for l in 0..32 {
let lu = l as u32;
let active = matches!(l, 0 | 2 | 31);
let v = &r.vgpr[l];
assert_eq!(v[3], if active { 1000 } else { 0xCAFE_0000 + lu }, "lane {l}: ds_append");
assert_eq!(v[4], if active { 1003 } else { 0xBEEF_0000 + lu }, "lane {l}: ds_consume");
assert_eq!(v[7], 0xF00D_0000 + lu, "lane {l}: ds_append with an empty EXEC");
assert_eq!(v[5], 1000, "lane {l}: ds_append, every lane");
assert_eq!(v[6], 1032, "lane {l}: the counter after it");
}
}

/// Ray tracing: image_bvh(64)_intersect_ray on a hand-built BVH (box32 root with 4 children,
/// a box16 node, triangles), several rays; sorted and unsorted box results, triangle hit as
/// (t, denominator, i, j) numerators, misses, and the 64-bit node pointer form.
Expand Down
Loading