From 234ec72cc5ac3b3d544b4444a26a03b6164694a4 Mon Sep 17 00:00:00 2001 From: coolpersonnn <61058300+coolpersonnn@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:32:58 +0000 Subject: [PATCH] Recompiler: decode only requested components in buffer_load_format_* Narrow buffer_load_format_* and tbuffer_load_format_* instructions previously pre-decoded all four format components into {t}_c via fmt_comp even when only 1-3 components were selected. On format-heavy compute shaders (especially wide unrolled kernels), the unused decodes inflated emitted HLSL, DXIL size, and DXC compile time. Add a branch-free fmt_sel helper right after dst_sel in PRELUDE_MEM that routes only the needed format components into dst_sel, and update BufKind::FormatLoad to use fmt_sel whenever n_comp < 4 (including d16 loads, binding {t}_s{c} once before the 16-bit pack so the format decode is not evaluated twice). Measured on 4 extracted compute shaders from a retail shader pack (split-wave64, isolated caches): - 2,809 insts (9 x, 1 xy): DXIL 157184 B -> 141620 B (-9.9%), DXC 1.07 s -> 1.04 s - 7,305 insts (25 x, 2 xyz): DXIL 489424 B -> 450520 B (-7.9%), DXC 4.13 s -> 4.18 s - 16,414 insts (37 x, 6 xy, 4 xyz): DXIL 1092324 B -> 1030584 B (-5.7%), DXC 11.16 s -> 10.97 s - 88,863 insts (3,281 x): DXIL 29750432 B -> 20197536 B (-32.1%), DXC 130.59 s -> 94.61 s (-27.5%) Tests: .\scripts\test.ps1 passed (753 passed, 0 failed, 41 ignored; 2 expected wave64 GPU skips on RTX 5090), plus targeted Radeon GPU runs for gpu_buffer_load_format_swizzled_narrow_loads and gpu_typed_buffer_loads_and_stores. Assisted-by: Antigravity via T3 Code (Gemini Pro) --- crates/xcl/src/gpu/recomp/emit.rs | 26 +++++-- crates/xcl/src/gpu/recomp/tests.rs | 114 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 96966005b6..3b41bf391d 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -1781,13 +1781,26 @@ impl Em { self.line(&format!("bool {t}_int = ((({t}_info >> 3) & 7u) == 4u) || ((({t}_info >> 3) & 7u) == 5u);")); self.line(&format!("bool {t}_ok = and(({t}_info & 7u) != 0u, {});", in_range(format!("fmt_bytes({t}_info)")))); self.line(&format!("uint4 {t}_r = ld_raw({t}_a);")); - self.line(&format!( - "uint4 {t}_c = uint4(fmt_comp({t}_r, {t}_info, 0u), fmt_comp({t}_r, {t}_info, 1u), fmt_comp({t}_r, {t}_info, 2u), fmt_comp({t}_r, {t}_info, 3u));" - )); + let n_comp = d16_format.map_or(count, |(_, components, _)| components); + if n_comp == 4 { + self.line(&format!( + "uint4 {t}_c = uint4(fmt_comp({t}_r, {t}_info, 0u), fmt_comp({t}_r, {t}_info, 1u), fmt_comp({t}_r, {t}_info, 2u), fmt_comp({t}_r, {t}_info, 3u));" + )); + } + let sel_expr = |component: u64| { + let sel = format!("(({d3} >> {}u) & 7u)", 3 * component); + if n_comp == 4 { + format!("dst_sel({t}_c, {sel}, {t}_int)") + } else { + format!("fmt_sel({t}_r, {t}_info, {sel}, {t}_int)") + } + }; if let Some((_, components, high)) = d16_format { + for c in 0..components { + self.line(&format!("uint {t}_s{c} = {};", sel_expr(c))); + } let half = |component: u64| { - let selected = format!("dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int)", 3 * component); - format!("select({t}_int, {selected} & 0xFFFFu, f32tof16(asfloat({selected})))") + format!("select({t}_int, {t}_s{component} & 0xFFFFu, f32tof16(asfloat({t}_s{component})))") }; let mut assigns = Vec::new(); if high { @@ -1807,7 +1820,7 @@ impl Em { let mut assigns = Vec::new(); for i in 0..count { let v = self.vreg(vdata + i); - assigns.push(format!("{v} = select({t}_ok, dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int), 0u);", 3 * i)); + assigns.push(format!("{v} = select({t}_ok, {}, 0u);", sel_expr(i))); } self.line(&format!("if ({act}) {{ {} }}", assigns.join(" "))); } @@ -5395,6 +5408,7 @@ void st_bytes(uint a, uint4 r, uint n) { if (s != 0u && s + 8u * n > 32u) st_part(b + 4u, m >> (32u - s), r.x >> (32u - s)); } uint dst_sel(uint4 c, uint sel, bool isint) { return select(sel == 0u, 0u, select(sel == 1u, select(isint, 1u, 0x3F800000u), pick4(c, (sel - 4u) & 3u))); } +uint fmt_sel(uint4 r, uint info, uint sel, bool isint) { return select(sel == 0u, 0u, select(sel == 1u, select(isint, 1u, 0x3F800000u), fmt_comp(r, info, (sel - 4u) & 3u))); } "#; /// A console wave64 as two hardware wave32 halves of one thread group (docs/wave64-on-wave32.md): diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 55ef19082f..e7a551b3d4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1627,6 +1627,120 @@ fn gpu_typed_buffer_loads_and_stores() { assert_eq!(r.sgpr[50], 1 | 1 << 5, "s_bitset1"); } +#[test] +fn buffer_load_format_decodes_only_requested_components() { + let cases = [ + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", 1usize, 0usize), + ("BUFFER_LOAD_FORMAT_XY", "ENC_MUBUF", 2, 0), + ("BUFFER_LOAD_FORMAT_XYZ", "ENC_MUBUF", 3, 0), + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", 0, 4), + ("TBUFFER_LOAD_FORMAT_X", "ENC_MTBUF", 1, 0), + ("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", 1, 0), + ("BUFFER_LOAD_FORMAT_D16_HI_X", "ENC_MUBUF", 1, 0), + ("BUFFER_LOAD_FORMAT_D16_XY", "ENC_MUBUF", 2, 0), + ("BUFFER_LOAD_FORMAT_D16_XYZ", "ENC_MUBUF", 3, 0), + ("BUFFER_LOAD_FORMAT_D16_XYZW", "ENC_MUBUF", 0, 4), + ]; + for &(inst, enc, want_sel, want_comp) in &cases { + let mut fields = vec![("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)]; + if enc == "ENC_MTBUF" { + fields.push(("FORMAT", 64)); + } + let code = prog(&[(inst, enc, &fields, None)]); + let t = translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{inst}: {:?}", t.unsupported_why); + let body = t.hlsl.split("void main(").nth(1).expect("main"); + assert_eq!(body.matches("fmt_sel(").count(), want_sel, "{inst}: fmt_sel count\n{body}"); + assert_eq!(body.matches("fmt_comp(").count(), want_comp, "{inst}: fmt_comp count\n{body}"); + if super::dxc::location().is_ok() && matches!(inst, "BUFFER_LOAD_FORMAT_X" | "BUFFER_LOAD_FORMAT_D16_XYZ" | "BUFFER_LOAD_FORMAT_XYZW") { + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{inst}: {e}\n{}", t.hlsl)); + } + } +} + +#[test] +fn gpu_buffer_load_format_swizzled_narrow_loads() { + if !gpu_ready() { + return; + } + let swiz = |x: u32, y: u32, z: u32, w: u32| x | (y << 3) | (z << 6) | (w << 9); + let vs = |base: u32, fmt: u32, sel: u32| [base, 0, 0x100, sel | fmt << 12]; + let code = prog(&[ + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 11), ("SRSRC", 4), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 12), ("SRSRC", 5), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 13), ("SRSRC", 6), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 14), ("SRSRC", 7), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 15), ("SRSRC", 8), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 16), ("SRSRC", 9), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 17), ("SRSRC", 10), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_XY", "ENC_MUBUF", &[("VDATA", 18), ("SRSRC", 11), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_XYZ", "ENC_MUBUF", &[("VDATA", 20), ("SRSRC", 12), ("SOFFSET", 128)], None), + ("TBUFFER_LOAD_FORMAT_X", "ENC_MTBUF", &[("VDATA", 23), ("SRSRC", 13), ("SOFFSET", 128), ("FORMAT", 64)], None), + ("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", &[("VDATA", 24), ("SRSRC", 14), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_D16_HI_X", "ENC_MUBUF", &[("VDATA", 25), ("SRSRC", 15), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_D16_XYZ", "ENC_MUBUF", &[("VDATA", 26), ("SRSRC", 16), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 28), ("SRSRC", 12), ("SOFFSET", 128), ("OFFSET", 0x200)], None), + ("BUFFER_LOAD_FORMAT_D16_HI_X", "ENC_MUBUF", &[("VDATA", 29), ("SRSRC", 12), ("SOFFSET", 128), ("OFFSET", 0x200)], None), + ]); + let mut mem = vec![0u32; 512]; + mem[0x100 / 4] = u32::from_le_bytes([0x10, 0x80, 0xFF, 0x40]); // 8_8_8_8_UNORM + mem[0x110 / 4] = 11; + mem[0x114 / 4] = 22; + mem[0x118 / 4] = 33; + mem[0x11C / 4] = 44; // 32_32_32_32_UINT + mem[0x120 / 4] = 0x3E00 | (0xC100 << 16); // 16_16_FLOAT (1.5, -2.5) + mem[0x124 / 4] = 7 | (9 << 16); // 16_16_UINT (7, 9) + mem[0x130 / 4] = f(1.5); + mem[0x134 / 4] = f(-3.25); // 32_32_FLOAT + let mut st = KernelState::default(); + let descriptors = [ + vs(0x100, 56, swiz(6, 0, 0, 0)), // SRSRC 3: Z of 8_8_8_8_UNORM + vs(0x100, 56, swiz(5, 0, 0, 0)), // SRSRC 4: Y of 8_8_8_8_UNORM + vs(0x110, 75, swiz(7, 0, 0, 0)), // SRSRC 5: W of 32_32_32_32_UINT + vs(0x120, 29, swiz(6, 0, 0, 0)), // SRSRC 6: missing Z on 16_16_FLOAT -> 0.0f + vs(0x120, 29, swiz(7, 0, 0, 0)), // SRSRC 7: missing W on 16_16_FLOAT -> 1.0f + vs(0x124, 27, swiz(7, 0, 0, 0)), // SRSRC 8: missing W on 16_16_UINT -> 1u + vs(0x124, 27, swiz(1, 0, 0, 0)), // SRSRC 9: SEL_1 on 16_16_UINT -> 1u + vs(0x120, 29, swiz(0, 0, 0, 0)), // SRSRC 10: SEL_0 on 16_16_FLOAT -> 0.0f + vs(0x110, 75, swiz(7, 5, 0, 0)), // SRSRC 11: XY = [W, Y] + vs(0x110, 75, swiz(6, 1, 4, 0)), // SRSRC 12: XYZ = [Z, 1, X] + vs(0x130, 0, swiz(5, 0, 0, 0)), // SRSRC 13: tbuffer Y of 32_32_FLOAT + vs(0x120, 29, swiz(5, 0, 0, 0)), // SRSRC 14: d16_x Y of 16_16_FLOAT + vs(0x124, 27, swiz(7, 0, 0, 0)), // SRSRC 15: d16_hi_x missing W on 16_16_UINT -> 1u + vs(0x120, 29, swiz(5, 7, 0, 4)), // SRSRC 16: d16_xyz = [Y (-2.5), W (1.0), 0] + ]; + for (i, v) in descriptors.iter().enumerate() { + st.sgpr[12 + 4 * i..16 + 4 * i].copy_from_slice(v); + } + for r in &mut st.vgpr { + r[25] = 0xDEAD_BEEF; + r[28] = 0x1234_5678; + r[29] = 0xCAFE_BABE; + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + let near = |got: u32, want: f32| (got as i64 - want.to_bits() as i64).abs() <= 2; + for l in 0..64 { + let v = &r.vgpr[l]; + assert_eq!(v[10], f(1.0), "lane {l} unorm8 Z"); + assert!(near(v[11], 128.0 / 255.0), "lane {l} unorm8 Y: {:#x}", v[11]); + assert_eq!(v[12], 44, "lane {l} uint4 W"); + assert_eq!(v[13], 0, "lane {l} missing Z float"); + assert_eq!(v[14], f(1.0), "lane {l} missing W float"); + assert_eq!(v[15], 1, "lane {l} missing W uint"); + assert_eq!(v[16], 1, "lane {l} SEL_1 uint"); + assert_eq!(v[17], 0, "lane {l} SEL_0 float"); + assert_eq!([v[18], v[19]], [44, 22], "lane {l} XY swizzle"); + assert_eq!([v[20], v[21], v[22]], [33, 1, 11], "lane {l} XYZ swizzle"); + assert_eq!(f32::from_bits(v[23]), -3.25, "lane {l} tbuffer X swizzle"); + assert_eq!(v[24], 0xC100, "lane {l} d16_x swizzle"); + assert_eq!(v[25], 0x0001_BEEF, "lane {l} d16_hi_x swizzle + low half preserved"); + assert_eq!([v[26], v[27]], [0x3C00_C100, 0x0000_0000], "lane {l} d16_xyz swizzle"); + assert_eq!(v[28], 0, "lane {l} out-of-range narrow load"); + assert_eq!(v[29], 0x0000_BABE, "lane {l} out-of-range d16_hi_x preserves low half"); + } +} + fn el(name: &str, index: u32, sv: u32, ct: u32, reg: u32, mask: u8) -> crate::gpu::dxbc::SigElement { crate::gpu::dxbc::SigElement { name: name.into(), index, system_value: sv, component_type: ct, register: reg, mask, rw_mask: mask, stream: 0 } }