From 3a71471c80b842d8e6f2bffd74556947fc11921b Mon Sep 17 00:00:00 2001 From: Emu Synths Date: Wed, 25 Nov 2026 13:02:59 -0600 Subject: [PATCH] Recompiler: enable scalar reload CSE for direct DXIL vertex and pixel shaders Enable reload::analyze for Entry::Vertex and Entry::Pixel in the direct DXIL translator. Unlike two-phase Entry::Geometry, vertex and pixel shaders use the same single-entry Cfg::build as compute, so repeated s_load_dword* descriptor reloads can safely reuse their first values and avoid redundant memory-window switches. Part of #2088 Assisted-by: Antigravity (Gemini 4 Neon) Assisted-by: Codex (gpt-6.2-astra) --- crates/xcl/src/gpu/dxil/kernel.rs | 6 +-- crates/xcl/src/gpu/dxil/reload.rs | 38 +++++++++++++++++ crates/xcl/src/gpu/recomp/tests.rs | 68 ++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 3fe8c6ce4a..f4691d580d 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -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; @@ -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 = None; if let Some(p) = &plan { diff --git a/crates/xcl/src/gpu/dxil/reload.rs b/crates/xcl/src/gpu/dxil/reload.rs index 47beaa5103..96dfc2cdf1 100644 --- a/crates/xcl/src/gpu/dxil/reload.rs +++ b/crates/xcl/src/gpu/dxil/reload.rs @@ -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 { + 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] diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 0b018edc36..3e28bc026e 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -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]