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
6 changes: 3 additions & 3 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn translate_on_this_thread(
None => Cfg::build(code)?,
};
let kernel = matches!(entry, Entry::Kernel);
let follow_loads = matches!(entry, Entry::Kernel | Entry::Compute(_));
let follow_loads = matches!(entry, Entry::Kernel | Entry::Compute(_) | Entry::Vertex(_) | Entry::Pixel(_));
let gfx = matches!(entry, Entry::Vertex(_) | Entry::Pixel(_));
let compute = matches!(entry, Entry::Compute(_));
let split = o.wave64 && o.split && !gfx;
Expand Down Expand Up @@ -329,8 +329,8 @@ fn translate_on_this_thread(
lw.edge(Block(0), h0);
}
let switch = if two_phase { Some(lw.block(None)) } else { None };
// scalar loads the program repeats keep their first values (compute programs and kernels:
// the other stages enter their code by edges the CFG does not hold)
// scalar loads the program repeats keep their first values (compute, vertex, pixel and
// kernel: geometry enters its GS blocks by edges the CFG does not hold)
let reloads = if follow_loads { super::reload::analyze(&cfg) } else { Default::default() };
let mut gs_entry: Option<usize> = None;
if let Some(p) = &plan {
Expand Down
38 changes: 38 additions & 0 deletions crates/xcl/src/gpu/dxil/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,44 @@ mod tests {
assert_eq!(buffer_loads(&[LOAD, ADD, store, LOAD, sum, END]), buffer_loads(&[LOAD, ADD, store, sum, END]) + 1, "a store in between: loaded again");
}

#[test]
fn graphics_stages_emit_a_repeated_load_once() {
use super::super::kernel::{translate_pixel, translate_vertex, KernelOptions};
use crate::gpu::recomp::{PsInterface, VsInterface};

let encode = |insts: &[I]| -> Vec<u8> {
insts.iter().flat_map(|(n, e, f)| rdna2::encode(n, e, f, None).unwrap()).collect()
};
let count_loads = |dxil: &[u8]| -> usize {
super::super::validate::validate(dxil).expect("valid DXIL");
let dis = super::super::validate::disassemble(dxil).expect("disassemble");
dis.lines().filter(|l| l.contains("@dx.op.rawBufferLoad") && l.contains(" = call ")).count()
};
let opts = KernelOptions { wave64: false, split: false, mem_windows: 4, sampler_base: 0 };
let sum: I = ("S_ADD_U32", "ENC_SOP2", &[("SDST", 21), ("SSRC0", 20), ("SSRC1", 12)]);
let store: I = ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 0), ("DATA", 2), ("SADDR", 125)]);
let once_code = encode(&[LOAD, ADD, END]);
let repeat_code = encode(&[LOAD, ADD, LOAD, sum, END]);
let store_once_code = encode(&[LOAD, ADD, store, sum, END]);
let store_repeat_code = encode(&[LOAD, ADD, store, LOAD, sum, END]);

let vs = VsInterface::default();
let vs_once = count_loads(&translate_vertex(&once_code, opts, &vs).expect("VS once").dxil);
let vs_repeat = count_loads(&translate_vertex(&repeat_code, opts, &vs).expect("VS repeat").dxil);
assert_eq!(vs_repeat, vs_once, "VS repeated s_load_dwordx4 should reuse cached values");
let vs_store_once = count_loads(&translate_vertex(&store_once_code, opts, &vs).expect("VS store once").dxil);
let vs_store_repeat = count_loads(&translate_vertex(&store_repeat_code, opts, &vs).expect("VS store repeat").dxil);
assert_eq!(vs_store_repeat, vs_store_once + 4, "VS store should invalidate reload across 4 windows");

let ps = PsInterface::default();
let ps_once = count_loads(&translate_pixel(&once_code, opts, &ps).expect("PS once").dxil);
let ps_repeat = count_loads(&translate_pixel(&repeat_code, opts, &ps).expect("PS repeat").dxil);
assert_eq!(ps_repeat, ps_once, "PS repeated s_load_dwordx4 should reuse cached values");
let ps_store_once = count_loads(&translate_pixel(&store_once_code, opts, &ps).expect("PS store once").dxil);
let ps_store_repeat = count_loads(&translate_pixel(&store_repeat_code, opts, &ps).expect("PS store repeat").dxil);
assert_eq!(ps_store_repeat, ps_store_once + 4, "PS store should invalidate reload across 4 windows");
}

/// Every output operand type of the instruction tables is one `changes` knows: a new one
/// must be looked at here, not passed over.
#[test]
Expand Down
68 changes: 68 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,74 @@ fn ps_exp_vm_hlsl_and_direct_dxil_codegen() {
}
}

#[test]
fn gpu_graphics_scalar_reloads_match_hlsl() {
if !gpu_ready() {
return;
}
use super::{PsInterface, VsInterface};
use crate::gpu::dxil::kernel::{translate_pixel, translate_vertex, KernelOptions};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;

let load = ("S_LOAD_DWORDX4", "ENC_SMEM", &[("SDATA", 12), ("SBASE", 2), ("OFFSET", 0x40), ("SOFFSET", 125)][..], None);
// The repeat must restore a value even after its destination was overwritten.
let clobber = ("S_MOV_B32", "ENC_SOP1", &[("SDST", 12), ("SSRC0", 128)][..], None);
let vs_code = prog(&[
load,
("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 14)], None),
clobber,
load,
("V_ADD_F32", "ENC_VOP2", &[("VDST", 0), ("SRC0", 12), ("VSRC1", 4)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V + 5)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 242)], None),
("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 0xF), ("VSRC0", 0), ("VSRC1", 1), ("VSRC2", 2), ("VSRC3", 3), ("DONE", 1)], None),
]);
let ps_code = prog(&[
load,
("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 13)], None),
clobber,
load,
("V_MOV_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", 12)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 14)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 242)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 0), ("VSRC1", 1), ("VSRC2", 2), ("VSRC3", 3), ("DONE", 1), ("VM", 1)], None),
]);
let vsi = VsInterface {
inputs: vec![el("POSITION", 0, 0, 3, 0, 3)],
outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)],
attr_base: 4,
params: None,
};
let psi = PsInterface {
outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)],
..Default::default()
};
let wave64 = !runner::graphics_wave32();
let ko = KernelOptions { wave64, split: false, mem_windows: 1, sampler_base: 0 };
let vs_dxil = translate_vertex(&vs_code, ko, &vsi).unwrap().dxil;
let ps_dxil = translate_pixel(&ps_code, ko, &psi).unwrap().dxil;
let compile = |code: &[u8], mode, profile| {
let translated = translate(code, &Options { mode, wave64, ..Default::default() }).unwrap();
assert!(translated.complete(), "{:?}", translated.unsupported);
super::dxc::compile(&translated.hlsl, "main", profile, &[]).unwrap()
};
let vs_hlsl = compile(&vs_code, Mode::Vertex(vsi), "vs_6_6");
let ps_hlsl = compile(&ps_code, Mode::Pixel(psi), "ps_6_6");

let verts = [-1.0f32, -1.0, -1.0, 3.0, 3.0, -1.0];
let layout = [runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 }];
let clear = [0.0, 0.0, 0.0, 1.0];
let mut mem = [0; 20];
mem[16..19].copy_from_slice(&[f(1.0), f(0.25), f(0.5)]);
let draw = |vs: &[u8], ps: &[u8]| runner::draw(vs, ps, &layout, &verts, 8, &[], &mem, 16, clear).unwrap();
let reference = draw(&vs_hlsl, &ps_hlsl);
assert_eq!(draw(&vs_dxil, &ps_dxil), reference);
for row in reference.chunks_exact(16) {
assert!(row[..8].iter().all(|&pixel| pixel == clear), "{row:?}");
assert!(row[8..].iter().all(|&pixel| pixel == [1.0, 0.25, 0.5, 1.0]), "{row:?}");
}
}

/// GPU draw regression for XCL-BT-012 (#1488): verifies all four `EXP` `VM` pixel-survival
/// cases on the GPU through both the HLSL and Direct DXIL pixel-shader backends.
#[test]
Expand Down
Loading