From 4afd8a55bd63e82cee8ac1d33a1941d78d50e456 Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:02:09 +0100 Subject: [PATCH] Recompiler: compact literal indices are read from one table word on AMD The compact form (#3261) gives each block of a parameterized class its literal through two tables: a bit-packed index per block (xcl_lit_idx) and the pool of distinct literals (xcl_lit_pool). An index could straddle two words of xcl_lit_idx and was read with a two-word funnel extraction. AMD's driver (an RX 7800 XT, 32.0.32029, and a Ryzen 7800X3D's RDNA2 iGPU, 32.0.21030) gets the pool index wrong from that extraction: gpu_compact_form_with_micro_blocks_and_literal_table_runs_ as_the_plain_form read 0 for every literal (s1 = 0 instead of 10045), in native and split wave64. The same DXIL runs correctly on NVIDIA, and the index is right when used as a value; only its use as the next table's index goes wrong (with a clamp, it read the last pool entry). The index width is now a power of two, so an index never straddles two words and the lookup reads one word. The table grows at most 2x (an 8-bit index stays 8 bits; 9 bits take 16). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/compact.rs | 13 ++++------ crates/xcl/src/gpu/dxil/compact_tests.rs | 32 ++++++++++++++++++------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/compact.rs b/crates/xcl/src/gpu/dxil/compact.rs index 65c1a51b00..8c0febd196 100644 --- a/crates/xcl/src/gpu/dxil/compact.rs +++ b/crates/xcl/src/gpu/dxil/compact.rs @@ -801,7 +801,10 @@ pub fn emit_with_nest(lw: &mut Lw, cfg: &Cfg, p: &Plan, head: Block, exit: Block }); blk_idx.push(idx); } - let lit_w = bits(pool.len()).min(32); + // a power of two, so no index straddles two words: AMD's driver (RDNA2 / RDNA3) reads + // a wrong pool index from the two-word extraction (`gpu_compact_form_with_micro_blocks_ + // and_literal_table_runs_as_the_plain_form` read 0 for every literal) + let lit_w = bits(pool.len()).next_power_of_two().min(32); let mut idx_packed = vec![0u32; (n * lit_w as usize).div_ceil(32) + 1]; for (b, &id) in blk_idx.iter().enumerate() { let v = id as u64; @@ -861,13 +864,7 @@ pub fn emit_with_nest(lw: &mut Lw, cfg: &Cfg, p: &Plan, head: Block, exit: Block let li = lw.lshrc(lbit, 5); let lsh = lw.andc(lbit, 31); let llo = word(lw, idx_tbl, li); - let li1 = lw.addc(li, 1); - let lhi = word(lw, idx_tbl, li1); - let llo = lw.lshr(llo, lsh); - let lhi = lw.shlc(lhi, 1); - let lup = lw.sub(k31, lsh); - let lhi = lw.shl(lhi, lup); - let le = lw.or(llo, lhi); + let le = lw.lshr(llo, lsh); lw.andc(le, (1u32 << lit_w) - 1) }; word(lw, pool_tbl, id) diff --git a/crates/xcl/src/gpu/dxil/compact_tests.rs b/crates/xcl/src/gpu/dxil/compact_tests.rs index cf56c97c85..a0c88f3975 100644 --- a/crates/xcl/src/gpu/dxil/compact_tests.rs +++ b/crates/xcl/src/gpu/dxil/compact_tests.rs @@ -151,6 +151,19 @@ fn loads_and_literals(copies: usize) -> Vec { v } +/// Each block's literal index sits in one word of `xcl_lit_idx` (a power-of-two width), so the +/// lookup reads one word: AMD's driver got the index wrong from a two-word extraction. +#[test] +fn a_literal_index_is_read_from_one_word() { + for copies in [200, 300] { + let o = KernelOptions { wave64: true, split: true, mem_windows: 1, sampler_base: 0 }; + let k = translate_full_as(&loads_and_literals(copies), o, true).expect("compact form"); + let dis = super::validate::disassemble(&k.dxil).expect("disassemble"); + let reads = dis.lines().filter(|l| l.contains("getelementptr") && l.contains("@xcl_lit_idx,")).count(); + assert_eq!(reads, 1, "{copies} copies: {reads} reads of the literal index table"); + } +} + #[test] fn repeated_loads_split_into_micro_blocks_and_literals_into_a_table() { let code = loads_and_literals(200); @@ -174,15 +187,18 @@ fn gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form() let mut st = KernelState::default(); // s[2:3] = 0: the loads read mem[0]; the early out is taken in the 10th copy st.sgpr[4] = 50; - let code = loads_and_literals(200); let o = KernelOptions { wave64: true, split: runner::split_wave64(), mem_windows: 1, sampler_base: 0 }; - let run = |compact: bool| { - let k = translate_full_as(&code, o, compact).unwrap_or_else(|e| panic!("compact {compact}: {e}")); - runner::run_with_statics(&k.dxil, &st, &[5, 0, 0, 0], &[], &[], &k.static_samplers).expect("GPU run") - }; - let (c, p) = (run(true), run(false)); - assert_eq!((c.sgpr[0], c.sgpr[1], c.sgpr[5], c.sgpr[8]), (50, (1000..1010).sum::(), 1, 5)); - assert_eq!((c.sgpr, c.vgpr, c.scc), (p.sgpr, p.vgpr, p.scc)); + // 200 copies: 8-bit literal indices; 300: 9 bits, which the table stores in 16 + for copies in [200, 300] { + let code = loads_and_literals(copies); + let run = |compact: bool| { + let k = translate_full_as(&code, o, compact).unwrap_or_else(|e| panic!("compact {compact}: {e}")); + runner::run_with_statics(&k.dxil, &st, &[5, 0, 0, 0], &[], &[], &k.static_samplers).expect("GPU run") + }; + let (c, p) = (run(true), run(false)); + assert_eq!((c.sgpr[0], c.sgpr[1], c.sgpr[5], c.sgpr[8]), (50, (1000..1010).sum::(), 1, 5), "{copies} copies"); + assert_eq!((c.sgpr, c.vgpr, c.scc), (p.sgpr, p.vgpr, p.scc), "{copies} copies"); + } } #[test]