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]