From cc8930997d3109bc669d265c533b3c37ca20d301 Mon Sep 17 00:00:00 2001 From: pvfreis <13662235+pvfreis@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:42:32 -0300 Subject: [PATCH] Recompiler: compute programs follow S# constants across blocks too 82e4a9bb follows S# constants through branches in vertex and pixel programs only. In kernel and compute programs a block's start still forgets them, so a sample after a branch falls back to SamplerDescriptorHeap[word3 & 0x7FF]. For an S# built in code that is the border-colour pointer, usually 0: sampler-heap entry 0 (point, wrap, mip 0). This turns `follow` on for kernel and compute programs in both translators (HLSL and direct DXIL), and makes s_movreld and s_movrelsd_2 (writes to SDST + an offset from M0) forget every known constant. 82e4a9bb's test now expects a kernel to find the sampler after the branch. The direct translator handed constant_samplers the starts of its Cfg blocks that have instructions left once the branch is taken out, so a block that is only an s_branch or s_endpgm had none. constant_samplers takes a block's way on from its last instruction: `s_cbranch X; s_branch Y` read as one block lost the way to X, and a constant set on only one path could stay. It now gets every block's start, as the HLSL path does. For vertex and pixel programs on the direct path (XCL_DIRECT_DXIL=all) this also follows programs that branch to such a block, which fell back to block by block before. Tests: kernel and compute, both translators, an S# set before an if, an if/else, a loop and a divergent branch is the static sampler; one with other words on one path, an s_load, an s_movreld or s_movrelsd_2 reaching the S# through M0, or an S# from memory keeps the heap lookup; the same on the GPU with heap entry 0 linear; d3602331a's s_bfm / 64-bit literal S# before a branch; other words set past a lone s_branch (the direct translator's case above) keep the heap lookup, both translators. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/kernel.rs | 6 +- crates/xcl/src/gpu/recomp/mod.rs | 13 +- crates/xcl/src/gpu/recomp/tests.rs | 215 ++++++++++++++++++++++++++++- 3 files changed, 226 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 1a638d59f6..f7aabe546f 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -249,7 +249,9 @@ fn translate_on_this_thread( let gfx = matches!(entry, Entry::Vertex(_) | Entry::Pixel(_)); let compute = matches!(entry, Entry::Compute(_)); let split = o.wave64 && o.split && !gfx; - let leaders: BTreeSet = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); + // every block's start, those that are only a branch or an s_endpgm too: `constant_samplers` + // takes a block's way on from its last instruction + let leaders: BTreeSet = cfg.blocks.iter().map(|b| b.pc).collect(); let mut lw = Lw::new(&cfg.insts, &leaders, o, &entry); lw.rodata = rodata; let uses_lds = lw.lds.is_some(); @@ -669,7 +671,7 @@ impl Lw { slot = slot.max(w); } } - lw.static_samplers = crate::gpu::recomp::constant_samplers(insts, leaders, matches!(entry, Entry::Vertex(_) | Entry::Pixel(_))); + lw.static_samplers = crate::gpu::recomp::constant_samplers(insts, leaders, matches!(entry, Entry::Vertex(_) | Entry::Pixel(_) | Entry::Kernel | Entry::Compute(_))); let group_threads: Option = match entry { Entry::Vertex(_) | Entry::Pixel(_) => None, Entry::Kernel => None, diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index e63624adec..4ebd9df4c9 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -1492,9 +1492,8 @@ fn scalar_constant(d: &Decoded, code: u64) -> Option { /// every block that leads there leaves it with the same one. A program with a branch whose /// target is not known is read block by block, as nothing is known at a block's start then. /// -/// `follow`: the stages that draw (vertex and pixel programs). The other programs are read -/// block by block as before: on a retail title, compute programs read this way gave garbage -/// counts to `ExecuteIndirect` (2 055 in 75 s, none before) and the device hung. +/// `follow`: vertex, pixel, kernel and compute programs. The others are read block by block +/// as before. pub(crate) fn constant_samplers(insts: &[Decoded], leaders: &BTreeSet, follow: bool) -> BTreeMap { type Known = std::collections::HashMap; // one block's instructions on `known`; the S# of its MIMG instructions into `out` @@ -1508,6 +1507,12 @@ pub(crate) fn constant_samplers(insts: &[Decoded], leaders: &BTreeSet, foll } } } + // s_movreld and s_movrelsd_2 write SDST + an offset from M0, a register the operands + // do not name + if d.inst.name.starts_with("S_MOVRELD") || d.inst.name == "S_MOVRELSD_2_B32" { + known.clear(); + continue; + } let set: Vec<(u64, u32)> = match d.inst.name { "S_MOV_B32" => f("SDST").zip(f("SSRC0").and_then(|c| scalar_constant(d, c))).map(|(r, v)| vec![(r, v)]).unwrap_or_default(), "S_MOVK_I32" => f("SDST").zip(f("SIMM16")).map(|(r, v)| vec![(r, v as u16 as i16 as i32 as u32)]).unwrap_or_default(), @@ -1684,7 +1689,7 @@ pub fn translate(code: &[u8], opt: &Options) -> Result { em.coherent_mem = matches!(opt.mode, Mode::Compute(_)) && needs_coherent_mem(&insts); em.configure_round_modes(&insts, branches == 0); em.writes_lds = emit::lds_order_wanted() && emit::writes_lds(&insts); - em.static_ss = constant_samplers(&insts, &leaders, matches!(opt.mode, Mode::Vertex(_) | Mode::Pixel(_))); + em.static_ss = constant_samplers(&insts, &leaders, matches!(opt.mode, Mode::Vertex(_) | Mode::Pixel(_) | Mode::Kernel | Mode::Compute(_))); // split wave64: masks only read lane by lane skip the halves' exchange (geometry mode // continues from the ES into the GS through s_setpc: left as it is); pixel shaders: they // skip the quad reads that give the helper lanes' bits diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index c1d00bf2ae..8e87cda15a 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3140,6 +3140,25 @@ fn static_sampler_words_from_s_bfm_and_a_64_bit_literal_move_are_constants() { assert!(!t.hlsl.contains("SamplerDescriptorHeap"), "no sampler heap lookup left"); } +/// The same S# built before a branch and sampled after it in a kernel: still a static sampler. +#[test] +fn s_sharp_from_s_bfm_and_a_64_bit_literal_move_before_a_branch_is_a_constant_in_a_kernel() { + let clamp = 2 | 2 << 3 | 2 << 6; + let linear = [clamp, 0x00FF_F000u32, 0x1850_0000u32, 0]; + let code = prog(&[ + ("S_MOVK_I32", "ENC_SOPK", &[("SDST", 24), ("SIMM16", clamp as u64)], None), + ("S_BFM_B32", "ENC_SOP2", &[("SDST", 25), ("SSRC0", 128 + 12), ("SSRC1", 128 + 12)], None), + ("S_MOV_B64", "ENC_SOP1", &[("SDST", 26), ("SSRC0", 255)], Some(linear[2])), + // (both ways go to the next instruction: it starts a block) + ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 0)], None), + ("IMAGE_SAMPLE_LZ", "ENC_MIMG", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)], None), + ]); + let t = translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + assert_eq!(t.static_samplers, vec![(linear, false)], "the S# set before the branch is a static sampler"); + assert!(!t.hlsl.contains("SamplerDescriptorHeap"), "no sampler heap lookup left"); +} + /// An S# set before a branch and sampled after it (the compiler sets the S# of a blur's taps /// before the loop, of an effect before its `if`) is a static sampler all the same: the /// constants are followed into the next block. Before, a block's start forgot them and the @@ -3162,9 +3181,10 @@ fn static_sampler_set_before_a_branch_is_a_constant_after_it() { assert!(t.complete(), "{:?}", t.unsupported_why); assert_eq!(t.static_samplers, vec![(linear, false)], "the S# set before the branch is a static sampler"); assert!(!t.hlsl.contains("SamplerDescriptorHeap"), "no sampler heap lookup left"); - // a program that does not draw is read block by block, as before + // a kernel follows them too let t = translate(&code, &Options::default()).unwrap(); - assert!(t.static_samplers.is_empty(), "{:?}", t.static_samplers); + assert_eq!(t.static_samplers, vec![(linear, false)], "the S# set before the branch is a static sampler"); + assert!(!t.hlsl.contains("SamplerDescriptorHeap"), "no sampler heap lookup left"); } /// Two ways into a block that leave different words in an S# register: not a constant there, @@ -3220,6 +3240,197 @@ fn gpu_static_sampler_word_from_s_brev() { } } +/// An S# set from constants in s[24:27] before a branch, sampled after it: a branch writes +/// only the PC, so the sample reads the same four words. Word 3 is 0, so the sampler-heap +/// fallback (`word3 & 0x7FF`) would read entry 0. +const SSX_POINT: [u32; 4] = [2 | 2 << 3 | 2 << 6, 0x00FF_F000, 0, 0]; + +type SsxInst = (&'static str, &'static str, Vec<(&'static str, u64)>, Option); + +fn ssx_mov(r: u64, v: u32) -> SsxInst { + match v { + 0 => ("S_MOV_B32", "ENC_SOP1", vec![("SDST", r), ("SSRC0", 128)], None), + _ => ("S_MOV_B32", "ENC_SOP1", vec![("SDST", r), ("SSRC0", 255)], Some(v)), + } +} + +fn ssx_i(n: &'static str, e: &'static str, f: &[(&'static str, u64)]) -> SsxInst { + (n, e, f.to_vec(), None) +} + +/// image_sample_lz v[10:13], v[0:1], T# s[16:23] (SRSRC 4), S# s[24:27] (SSAMP 6) +fn ssx_sample() -> SsxInst { + ssx_i("IMAGE_SAMPLE_LZ", "ENC_MIMG", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)]) +} + +fn ssx_prog(list: Vec) -> Vec { + let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); + prog(&refs) +} + +/// (name, code, loops): the point S# set in s[24:27], the control flow, the sample. +fn ssx_programs() -> Vec<(&'static str, Vec, bool)> { + let set: Vec = (0..4).map(|k| ssx_mov(24 + k, SSX_POINT[k as usize])).collect(); + let cmp = ssx_i("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 128)]); // scc = (s0 == 0) + let bodies: Vec<(&'static str, Vec, bool)> = vec![ + ("same_block", vec![ssx_sample()], false), + // s0 == 0 jumps over one VALU instruction; both paths reach the sample + ("uniform_if", vec![cmp.clone(), ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 1)]), ssx_i("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 129)]), ssx_sample()], false), + ("uniform_if_else", vec![ + cmp.clone(), + ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 2)]), + ssx_i("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 129)]), + ssx_i("S_BRANCH", "ENC_SOPP", &[("SIMM16", 1)]), + ssx_i("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 130)]), + ssx_sample(), + ], false), + // two iterations (s2 = 2) sampling in the body; the S# was set before the loop + ("loop", vec![ + ssx_i("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 130)]), + ssx_sample(), + ssx_i("S_SUB_U32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 2), ("SSRC1", 129)]), + ssx_i("S_CMP_LG_U32", "ENC_SOPC", &[("SSRC0", 2), ("SSRC1", 128)]), + ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFB)]), // back to the sample + ], true), + // lanes 0..31 (v2 = lane) run one VALU instruction, exec restored, then the sample + ("divergent", vec![ + ssx_i("S_MOV_B64", "ENC_SOP1", &[("SDST", 40), ("SSRC0", 126)]), + ssx_i("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 128 + 32), ("VSRC1", 2)]), + ssx_i("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 1)]), + ssx_i("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 129)]), + ssx_i("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 40)]), + ssx_sample(), + ], false), + ]; + bodies.into_iter().map(|(name, body, loops)| (name, ssx_prog(set.iter().cloned().chain(body).collect()), loops)).collect() +} + +/// Programs where the S# words at the sample are not one set of constants on every path: a +/// path sets other words, a path overwrites them (s_load, s_movreld through M0), or they come +/// from memory (an S# copied from a console heap, which carries its slot in word 3). +fn ssx_not_constant_programs() -> Vec<(&'static str, Vec)> { + let set: Vec = (0..4).map(|k| ssx_mov(24 + k, SSX_POINT[k as usize])).collect(); + let cmp = ssx_i("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 128)]); + let load = ssx_i("S_LOAD_DWORDX4", "ENC_SMEM", &[("SDATA", 24), ("SBASE", 0), ("OFFSET", 0)]); + // s0 != 0 runs `on_one_path` (one instruction), s0 == 0 jumps over it + let one_path = |on_one_path: Vec| -> Vec { + let skip = on_one_path.iter().map(|i| encode(i.0, i.1, &i.2, i.3).unwrap().len() / 4).sum::() as u64; + let mut l = set.clone(); + l.extend([cmp.clone(), ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", skip)])]); + l.extend(on_one_path); + l.push(ssx_sample()); + ssx_prog(l) + }; + vec![ + ("other_words_on_one_path", one_path(vec![ssx_mov(24, 2 | 2 << 3)])), + ("s_load_on_one_path", one_path(vec![load.clone()])), + // m0 = 2: s_movreld s22, s10 writes s24, which the operands do not name + ("s_movreld_on_one_path", one_path(vec![ + ssx_i("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 130)]), + ssx_i("S_MOVRELD_B32", "ENC_SOP1", &[("SDST", 22), ("SSRC0", 10)]), + ])), + // m0 = 2 << 16: s_movrelsd_2 s22, s10 writes s24 + ("s_movrelsd_2_on_one_path", one_path(vec![ + ssx_mov(124, 2 << 16), + ssx_i("S_MOVRELSD_2_B32", "ENC_SOP1", &[("SDST", 22), ("SSRC0", 10)]), + ])), + ("from_memory_before_the_branch", ssx_prog(vec![load, cmp.clone(), ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 1)]), ssx_i("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 129)]), ssx_sample()])), + // s0 == 0 jumps to a block that sets other words; s0 != 0 skips it with a branch alone in + // its block (the direct translator's blocks leave such a branch out) + ("other_words_past_a_lone_branch", ssx_prog(set.iter().cloned().chain([ + cmp.clone(), + ssx_i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 1)]), + ssx_i("S_BRANCH", "ENC_SOPP", &[("SIMM16", 2)]), + ssx_mov(24, 2 | 2 << 3), + ssx_sample(), + ]).collect())), + ] +} + +/// Kernel and compute programs, both translators: an S# set before a branch is the static +/// sampler after it; one that is not the same constants on every path keeps the heap lookup. +#[test] +fn s_sharp_set_before_a_branch_is_a_static_sampler_after_it() { + use crate::gpu::dxil::kernel::{translate_compute, translate_full, KernelOptions}; + let cs = super::CsInterface { threads: [64, 1, 1], user_sgprs: 16, tid_components: 1, ..Default::default() }; + let modes = [("kernel", Mode::Kernel), ("compute", Mode::Compute(cs.clone()))]; + let have_dxc = super::dxc::location().is_ok(); + let o = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; + let direct = |code: &[u8], compute: bool| if compute { translate_compute(code, o, &cs) } else { translate_full(code, o) }; + let mut wrong = Vec::new(); + let cases = ssx_programs().into_iter().map(|(n, c, _)| (n, c, true)).chain(ssx_not_constant_programs().into_iter().map(|(n, c)| (n, c, false))); + for (name, code, constant) in cases { + let want = if constant { vec![(SSX_POINT, false)] } else { Vec::new() }; + for (mode, m) in &modes { + let t = translate(&code, &Options { mode: m.clone(), ..Default::default() }).unwrap(); + assert!(t.complete(), "{mode} {name}: {:?}", t.unsupported_why); + let heap = t.hlsl.contains("SamplerDescriptorHeap["); + if t.static_samplers != want || heap == constant { + wrong.push(format!("hlsl {mode} {name}: static {:x?}, heap lookup {heap}", t.static_samplers)); + } + if have_dxc { + let k = direct(&code, *mode == "compute").unwrap_or_else(|e| panic!("direct DXIL {mode} {name}: {e}")); + if k.static_samplers != want { + wrong.push(format!("direct DXIL {mode} {name}: static {:x?}", k.static_samplers)); + } + } + } + } + if !have_dxc { + runner::skip_test("no DXC"); + } + assert!(wrong.is_empty(), "static samplers (want the point S# exactly where it is the same constants on every path):\n{}", wrong.join("\n")); +} + +/// The branch programs on the GPU, both translators, with sampler-heap entry 0 linear: the +/// sample at a texel boundary picks texel 2 with the S# (point), 1.5 with heap entry 0. No +/// loop program here: one sample, no loop or branch on sampled data. +#[test] +fn gpu_s_sharp_set_before_a_branch() { + if !gpu_ready() { + return; + } + use runner::{TestSampler, TestTexture, TexDim}; + use windows::Win32::Graphics::Direct3D12::*; + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32B32A32_FLOAT; + // 4x4 RGBA32F, texel (x, y) = (x, y, x + 4y, 1) + let mut t0 = Vec::new(); + for y in 0..4 { + for x in 0..4 { + for v in [x as f32, y as f32, (x + 4 * y) as f32, 1.0] { + t0.extend_from_slice(&v.to_le_bytes()); + } + } + } + let tex = vec![TestTexture { dim: TexDim::D2, format: DXGI_FORMAT_R32G32B32A32_FLOAT, width: 4, height: 4, depth: 1, texel_bytes: 16, data: t0, uav: false }]; + let heap = [TestSampler { filter: D3D12_FILTER_MIN_MAG_MIP_LINEAR, address: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, comparison: None }]; + let mut wrong = Vec::new(); + for (name, code, _) in ssx_programs().into_iter().filter(|p| !p.2) { + let t = translate(&code, &runner::options(Mode::Kernel)).unwrap(); + assert!(t.complete(), "{name}: {:?}", t.unsupported_why); + let hlsl = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|x| panic!("{name}: {x}")); + let k = runner::direct_kernel_full(&code, true).unwrap_or_else(|e| panic!("direct DXIL {name}: {e}")); + for (backend, dxil, statics) in [("hlsl", hlsl, t.static_samplers.clone()), ("direct DXIL", k.dxil, k.static_samplers)] { + for s0 in [0u32, 1] { + let mut st = KernelState::default(); + st.sgpr[0] = s0; + st.sgpr[16..24].copy_from_slice(&tsharp(0, 77, 9)); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[0] = f(0.5); // the boundary between texel columns 1 and 2 + r[1] = f(0.125); // row 0 + r[2] = l as u32; + } + let r = runner::run_with_statics(&dxil, &st, &[], &tex, &heap, &statics).unwrap_or_else(|e| panic!("{backend} {name}: {e}")); + let bad = (0..64).filter(|&l| r.vgpr[l][10..14].iter().map(|&w| f32::from_bits(w)).ne([2.0, 0.0, 2.0, 1.0])).count(); + if bad > 0 { + wrong.push(format!("{backend} {name} s0={s0}: {bad} lanes, lane 0 {:?}", r.vgpr[0][10..14].iter().map(|&w| f32::from_bits(w)).collect::>())); + } + } + } + } + assert!(wrong.is_empty(), "the sample after the branch does not use the S# in s[24:27] (point):\n{}", wrong.join("\n")); +} + #[test] fn gpu_images_a16_d16() { if !gpu_ready() {