diff --git a/crates/victory-shader/src/gfx10/mod.rs b/crates/victory-shader/src/gfx10/mod.rs index 151a4c7a2f..13b2b4b112 100644 --- a/crates/victory-shader/src/gfx10/mod.rs +++ b/crates/victory-shader/src/gfx10/mod.rs @@ -74,6 +74,81 @@ pub fn decode(code: &[u8]) -> Result, ShaderError> { if !code.len().is_multiple_of(4) { return Err(isa(code.len(), "code size is not a multiple of 4")); } + decode_aligned(code).unwrap_or_else(|| decode_any(code)) +} + +/// The most dwords an instruction takes (an image instruction with three +/// dwords of extra address registers). +const MAX_WORDS: usize = 5; + +/// [`decode`] while every offset it reaches is a dword's (None at the first +/// that is not, which only a far jump's literal can give): what is found is +/// kept in a table by dword, and put in order once, instead of in a tree +/// per instruction. +fn decode_aligned(code: &[u8]) -> Option, ShaderError>> { + // Per dword: the index in `found` of the instruction starting there. + // It grows to the furthest dword reached, so bytes past the code (data, + // padding) cost nothing; room for short programs is made at once. + let mut at: Vec = Vec::with_capacity((code.len() / 4).min(4096)); + let mut found: Vec = Vec::new(); + let mut pending = vec![0usize]; + while let Some(mut offset) = pending.pop() { + loop { + if !offset.is_multiple_of(4) { + return None; + } + if at.get(offset / 4).is_some_and(|&i| i != u32::MAX) { + break; + } + let inst = match instruction(code, offset) { + Ok(inst) => inst, + Err(e) => return Some(Err(e)), + }; + let next = offset + 4 * inst.words.len(); + // The instruction found last below `end`, as the tree has it, + // where it can end at `end` (all a far jump looks for): within + // the longest instruction's reach. + let before = |end: usize| { + (1..=MAX_WORDS.min(end / 4)) + .map(|k| at.get(end / 4 - k).copied().unwrap_or(u32::MAX)) + .find(|&i| i != u32::MAX) + .map(|i| &found[i as usize]) + }; + let flow = match control(&inst, code.len(), before) { + Ok(flow) => flow, + Err(e) => return Some(Err(e)), + }; + if at.len() <= offset / 4 { + at.resize(offset / 4 + 1, u32::MAX); + } + at[offset / 4] = found.len() as u32; + found.push(inst); + match flow { + Flow::Next => offset = next, + Flow::Stop => break, + Flow::Jump(target) => offset = target, + Flow::Branch(target) => { + pending.push(target); + offset = next; + } + } + } + } + found.sort_unstable_by_key(|i| i.offset); + if let Some(w) = found + .windows(2) + .find(|w| w[0].offset + 4 * w[0].words.len() > w[1].offset) + { + return Some(Err(isa( + w[1].offset, + "a branch lands inside an instruction", + ))); + } + Some(Ok(found)) +} + +/// [`decode`] at any offsets. +fn decode_any(code: &[u8]) -> Result, ShaderError> { let mut found = BTreeMap::new(); let mut pending = vec![0usize]; while let Some(mut offset) = pending.pop() { @@ -496,6 +571,69 @@ mod tests { ); } + /// Instructions found by dword (`decode_aligned`) are the tree's + /// (`decode_any`): branches both ways, a loop, data after the end, far + /// jumps both ways and bad code. A far jump to an offset that is no + /// dword's leaves it to the tree. + #[test] + fn the_table_decodes_as_the_tree_does() { + let far = |literal: u32| { + [ + 0xBE88_1F00, // s_getpc_b64 s[8:9] + 0x8008_FF08, + literal, // s_add_u32 s8, s8, literal + 0x8209_8009, // s_addc_u32 s9, s9, 0 + 0xBE80_2008, // s_setpc_b64 s[8:9] + 0xFFFF_FFFF, // not code + S_ENDPGM, + S_ENDPGM, + ] + }; + let programs: [&[u32]; 8] = [ + &[ + sopp(0x04, 1), + sopp(0x02, 1), + S_ENDPGM, + S_ENDPGM, + S_ENDPGM, + 0xFFFF_FFFF, + ], + // A loop: a backward branch to an instruction already found. + &[ + 0x4A00_0081, + 0xD52C_0001, + 0x0002_0501, + sopp(0x04, -4), + S_ENDPGM, + ], + &far(20), + &[ + sopp(0x02, 1), + S_ENDPGM, + 0xBE88_1F00, + 0x8008_FF08, + 0xFFFF_FFF8, + 0x8209_C109, + 0xBE80_2008, + ], + &[sopp(0x04, 1), 0xBE80_03FF, S_ENDPGM, S_ENDPGM], + &[sopp(0x02, 100)], + &[0xBE80_03FF], + &far(21), + ]; + for (n, code) in programs.iter().enumerate() { + let code = bytes(code); + let tree = format!("{:?}", decode_any(&code)); + match decode_aligned(&code) { + Some(table) => assert_eq!(format!("{table:?}"), tree, "program {n}"), + None => { + assert_eq!(n, 7, "only the unaligned far jump leaves it to the tree"); + assert_eq!(format!("{:?}", decode(&code)), tree); + } + } + } + } + #[test] fn rejects_bad_code() { assert!(decode(&[0; 3]).is_err(), "size not a multiple of 4");