From 12ff308a834982a4a81ebc2bef305c686b31045e Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Mon, 30 Nov 2026 23:02:53 -0600 Subject: [PATCH] d3d12-shader: decode instruction fields and effects once in ir::build For a program with v_writelane_b32/v_readlane_b32, unspill has to walk every instruction to find parked scalars, decoding fields and computing effects for each, and then build walks them again doing both. unspill now returns a cache indexed by instruction (name, fields, effects), and build takes it. The cache is sound because unspill only rewrites list in place and only after it has already scanned everything; any instruction unspill rewrites has its cache entry cleared, and build recomputes effects after its own parked-scalar substitution. Programs without a lane save or restore, and any program where unspill_scan bails, get no cache and decode exactly as before. Same values, same errors, same order. DXIL-identity over the full shader archive needs game files; checked here with the leonida-shader unit tests and the hand-written translator semantic suite. AI-assisted: yes --- d3d12/crates/leonida-shader/src/ir.rs | 77 ++++++++++++++++++--------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/d3d12/crates/leonida-shader/src/ir.rs b/d3d12/crates/leonida-shader/src/ir.rs index 75c8555050..f32680b711 100644 --- a/d3d12/crates/leonida-shader/src/ir.rs +++ b/d3d12/crates/leonida-shader/src/ir.rs @@ -152,7 +152,7 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result Result (d.name, d.fields, d.effects), + None => ( + machine.name().ok_or(ShaderError::Unsupported { + offset: machine.offset, + what: "unknown opcode", + })?, + machine.fields(wave64), + None, + ), + }; // A move to or from a scalar parked in a VGPR lane (`unspill`): its register // codes may be ones the machine word cannot hold. if let Ok(k) = parked.binary_search_by_key(&n, |m| m.0) @@ -186,6 +193,8 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result Result Exit::End, }; } - if let Some((mut reads, writes)) = effects(&machine, &fields, name, lanes, vgprs)? { + let effects = match cached_effects { + Some(e) => Some(e), + None => effects(&machine, &fields, name, lanes, vgprs)?, + }; + if let Some((mut reads, writes)) = effects { // Lanes disabled in EXEC keep the old value. for w in writes.iter().filter(|w| matches!(w, Reg::V(_))) { if !reads.contains(w) { @@ -266,6 +279,15 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result, Vec)>, +} + fn unspill( list: &mut [gfx10::Instruction], flows: &[Flow], @@ -273,7 +295,7 @@ fn unspill( wave64: bool, lanes: u8, vgprs: u16, -) -> Vec<(usize, u8, u8)> { +) -> (Vec<(usize, u8, u8)>, Option>>) { // Nothing is parked in a lane unless the code has a save or a restore of one, and the scan // takes the fields and the effects of every instruction to find that out: most programs // have none. @@ -281,7 +303,7 @@ fn unspill( .iter() .any(|i| matches!(i.name(), Some("v_writelane_b32" | "v_readlane_b32"))) { - return Vec::new(); + return (Vec::new(), None); } unspill_scan(list, flows, leaders, wave64, lanes, vgprs) } @@ -295,7 +317,7 @@ fn unspill_scan( wave64: bool, lanes: u8, vgprs: u16, -) -> Vec<(usize, u8, u8)> { +) -> (Vec<(usize, u8, u8)>, Option>>) { /// `v_writelane_b32 vN, sX, K` / `v_readlane_b32 sX, vN, K`: (save, N, X, K). fn parked(inst: &gfx10::Instruction, fields: &Fields) -> Option<(bool, u8, u8, u32)> { let Fields::Valu(v) = fields else { return None }; @@ -324,22 +346,23 @@ fn unspill_scan( let mut parks: Vec> = Vec::with_capacity(list.len()); let mut written: Vec> = Vec::with_capacity(list.len()); let mut read: Vec> = Vec::with_capacity(list.len()); + let mut cache: Vec> = Vec::with_capacity(list.len()); for inst in list.iter() { - let Some(name) = inst.name() else { return Vec::new() }; + let Some(name) = inst.name() else { return (Vec::new(), None) }; let fields = inst.fields(wave64); let park = parked(inst, &fields); let Ok(effects) = effects(inst, &fields, name, lanes, vgprs) else { - return Vec::new(); + return (Vec::new(), None); }; let mut codes = Vec::new(); let mut from = Vec::new(); - if let Some((reads, writes)) = effects { - for ® in &writes { + if let Some((reads, writes)) = &effects { + for ® in writes { if let Reg::S(c) = reg { codes.push(c); } } - for ® in &reads { + for ® in reads { if let Reg::S(c) = reg { from.push(c); } @@ -350,7 +373,7 @@ fn unspill_scan( // ordinary ones. (Relative writes still do.) let relative = name == "v_movrels_b32"; let n_reads = reads.len(); - for (k, reg) in reads.into_iter().chain(writes).enumerate() { + for (k, ®) in reads.iter().chain(writes.iter()).enumerate() { if relative && k < n_reads && matches!(reg, Reg::V(_)) { continue; } @@ -365,6 +388,7 @@ fn unspill_scan( parks.push(park); written.push(codes); read.push(from); + cache.push(Some(Decoded { name, fields, effects })); } let leader = |i: usize| leaders.binary_search(&list[i].offset).is_ok(); for reg in 0..256 { @@ -391,7 +415,7 @@ fn unspill_scan( } } if parks.iter().all(Option::is_none) { - return Vec::new(); + return (Vec::new(), None); } // Lane pairs (register, lower lane) that always move together: each // save of the lower lane is followed, in the same block and before its @@ -583,7 +607,7 @@ fn unspill_scan( } Some((of, pairs, codes.into_iter().flatten().collect())) }; - let Some(mut chosen) = plan(false) else { return Vec::new() }; + let Some(mut chosen) = plan(false) else { return (Vec::new(), None) }; let lonely = |(of, pairs, _): &(Vec, Vec<(usize, usize)>, Vec)| { (0..list.len()).any(|i| { parks[i].is_some_and(|p| p.0) @@ -599,8 +623,9 @@ fn unspill_scan( chosen = split; } let (store, pairs, codes) = chosen; - let put = |list: &mut [gfx10::Instruction], i: usize, encoding, opcode: u16, word: u32| { + let put = |list: &mut [gfx10::Instruction], cache: &mut [Option], i: usize, encoding, opcode: u16, word: u32| { list[i] = gfx10::Instruction::new(list[i].offset, encoding, opcode, vec![word]); + cache[i] = None; }; // `s_mov_b32` / `s_mov_b64 dst, src` (SOP1, opcodes 3 and 4); `s_nop`. A code // from `PARKED` up does not fit the word: s0 stands in for it there. @@ -614,7 +639,7 @@ fn unspill_scan( }; let slot = codes[store[i]]; if pairs.iter().any(|p| p.0 == store[i]) { - let Some(j) = partner(i) else { return Vec::new() }; + let Some(j) = partner(i) else { return (Vec::new(), None) }; // A save moves the pair where its second half is saved; a // restore where its first half is restored. let (at, gone) = if save { (j, i) } else { (i, j) }; @@ -632,14 +657,14 @@ fn unspill_scan( for (i, edit) in edits { match edit { Some((wide, dst, src)) => { - put(list, i, gfx10::Encoding::Sop1, 3 + u16::from(wide), mov(wide, dst, src)); + put(list, &mut cache, i, gfx10::Encoding::Sop1, 3 + u16::from(wide), mov(wide, dst, src)); moves.push((i, dst, src)); } - None => put(list, i, gfx10::Encoding::Sopp, 0, NOP), + None => put(list, &mut cache, i, gfx10::Encoding::Sopp, 0, NOP), } } moves.sort_unstable(); - moves + (moves, Some(cache)) } /// Rename register reads and writes after changing edges in a reducible graph. @@ -1904,11 +1929,11 @@ mod tests { let mut scanned = quick.clone(); let by_unspill = unspill(&mut quick, &flows, &leaders, wave64, lanes, 256); let by_scan = unspill_scan(&mut scanned, &flows, &leaders, wave64, lanes, 256); - assert_eq!(by_unspill, by_scan, "{name}, wave64 {wave64}"); + assert_eq!(by_unspill.0, by_scan.0, "{name}, wave64 {wave64}"); assert_eq!(quick, scanned, "{name}, wave64 {wave64}"); // The cases that park something do have the scan make moves. - assert_eq!(!by_scan.is_empty(), parks, "{name}, wave64 {wave64}"); - found += by_scan.len(); + assert_eq!(!by_scan.0.is_empty(), parks, "{name}, wave64 {wave64}"); + found += by_scan.0.len(); } } assert!(found >= 8, "{found}");