From 709a6bbb16865fa0d211302cb6500ede398380f5 Mon Sep 17 00:00:00 2001 From: Kal0s-design <356578040+Kal0s-design@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:39:15 +0100 Subject: [PATCH] d3d12 shader: find the wave sections after an unmatched saveexec wave::sections() looks in a block for whole-wave sections: a saveexec of every lane, the code under it, and the EXEC restore that ends it. When a saveexec's restore was not in the block (it lies in a later one), the scan stopped there (`let Some(end) = end else { break; }`), so every whole-wave section later in the same block was missed and its reduction translated the general way instead of as the section's shape. Such a saveexec is no section; the scan now goes on past it. Test: an_unmatched_saveexec_leaves_the_later_sections_of_its_block puts an unmatched s_or_saveexec_b64 before the wave64 OR reduction the other tests use and expects that section and its shape at index 1. On main it fails ("the section after an unmatched saveexec", left None). Port of shh's fix and test in the sibling VIctory tree (TaynanGT/Vice-Tay 17e3e2c, GPL-3.0). The shader cache key covers leonida-shader/src, so cached translations are made again. Co-Authored-By: shh Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-shader/src/emit/wave.rs | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-shader/src/emit/wave.rs b/d3d12/crates/leonida-shader/src/emit/wave.rs index b56751f548..8634da6237 100644 --- a/d3d12/crates/leonida-shader/src/emit/wave.rs +++ b/d3d12/crates/leonida-shader/src/emit/wave.rs @@ -183,8 +183,11 @@ pub(crate) fn sections(p: &Program, block: usize) -> HashMap { insts[j].write(Reg::S(EXEC)).is_some() && insts[j].read(Reg::S(saved_code(inst))) == Some(saved) }); + // A saveexec whose restore is not in the block (it lies in a later one) is no section; the + // sections after it in the block are still looked for. let Some(end) = end else { - break; + i += 1; + continue; }; let (vec, uni) = simulate(&insts[i + 1..end], wave, saved); let mut reads = HashMap::new(); @@ -1354,4 +1357,30 @@ mod tests { assert_eq!(per_lane(&bad_ident, all), None); } } + + /// A saveexec of every lane whose restore is not in the block (it lies in a later one) is no + /// section, and the sections after it in the block are still found: the scan used to stop at the + /// first unmatched one. (shh, ported from the sibling VIctory tree, TaynanGT/Vice-Tay 17e3e2c.) + #[test] + fn an_unmatched_saveexec_leaves_the_later_sections_of_its_block() { + let total = |w: &[u32], start: usize| { + let p = build(w, true); + let insts = &p.blocks[0].insts; + let saved = insts[start].write(Reg::S(4)).unwrap(); + let v7 = p.inputs.iter().find_map(|&(r, v)| (r == Reg::V(7)).then_some(v)).unwrap(); + // ..., v_readlane_b32 s7, v1, 0; s_mov_b64 exec, s[4:5]; s_endpgm + let s7 = insts[insts.len() - 3].write(Reg::S(7)).unwrap(); + let secs = sections(&p, 0); + assert!(start == 0 || !secs.contains_key(&0), "the unmatched saveexec is no section"); + let shape = secs.get(&start).and_then(|sec| sec.shapes.get(&s7).copied()); + (shape, Shape::Active(Op::Or, v7, saved, 0)) + }; + let (alone, want) = total(&WAVE_OR_V7_TO_S7, 0); + assert_eq!(alone, Some(want)); + // s_or_saveexec_b64 s[2:3], -1, restored nowhere in the block + let mut w = vec![sop1(2, 0x25, 193)]; + w.extend(WAVE_OR_V7_TO_S7); + let (after, want) = total(&w, 1); + assert_eq!(after, Some(want), "the section after an unmatched saveexec"); + } }