From 67d75ffd450e7486cf92cce0d14d034edf66d966 Mon Sep 17 00:00:00 2001 From: 0xkn0wh3r3 <360897260+0xkn0wh3r3@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:03:36 -0500 Subject: [PATCH 1/3] Shaders: decode finds instructions in a table by dword, not a tree `gfx10::decode` follows the control flow from offset 0 and kept every instruction it found in a `BTreeMap` by offset: a lookup per instruction to stop at one already found, an insertion per instruction, a range lookup per instruction for the far-jump check, and a walk of the tree to put them in order. On synthetic programs that was a large part of decoding, and decoding about 8% of a translation (sampled). While every offset it reaches is a dword's, it now records the index of the instruction starting at each dword in a table, keeps the instructions in a vector, and sorts them by offset once at the end. The far-jump check only needs the instruction that ends exactly where the `s_setpc_b64` starts; the nearest one found below it, within the longest instruction's reach (5 dwords), is the tree's answer wherever that can end there. An offset that is no dword's (only a far jump's literal can give one) leaves the whole decode to the tree, as before, so every result and error is the same. Measured on an RTX 5070 Ti host (release, best of 30): 32,001 instructions (compares, saveexec, branches, VOP3, buffer stores, scalar loads) decode in 2.4 ms instead of 6.1 ms. The new test decodes branches both ways, a loop, data after the end, far jumps both ways and bad code with both, and checks that a far jump to an unaligned offset falls back to the tree. --- crates/victory-shader/src/gfx10/mod.rs | 133 +++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/crates/victory-shader/src/gfx10/mod.rs b/crates/victory-shader/src/gfx10/mod.rs index 151a4c7a2f..5be902e956 100644 --- a/crates/victory-shader/src/gfx10/mod.rs +++ b/crates/victory-shader/src/gfx10/mod.rs @@ -74,6 +74,76 @@ 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. + let mut at = vec![u32::MAX; code.len() / 4]; + 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[end / 4 - k]) + .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)), + }; + 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 +566,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"); From e1076d4dde209e883c1c4f255fd80171070f4ff3 Mon Sep 17 00:00:00 2001 From: 0xkn0wh3r3 <360897260+0xkn0wh3r3@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:20:34 -0500 Subject: [PATCH 2/3] Shaders: the decode table grows to the furthest dword reached Bytes past the code (data, padding) no longer cost a table entry each: a short program followed by 32 KiB of padding filled 8,192 entries up front (about 0.2 us). --- crates/victory-shader/src/gfx10/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/victory-shader/src/gfx10/mod.rs b/crates/victory-shader/src/gfx10/mod.rs index 5be902e956..6db1eaf81f 100644 --- a/crates/victory-shader/src/gfx10/mod.rs +++ b/crates/victory-shader/src/gfx10/mod.rs @@ -87,7 +87,9 @@ const MAX_WORDS: usize = 5; /// per instruction. fn decode_aligned(code: &[u8]) -> Option, ShaderError>> { // Per dword: the index in `found` of the instruction starting there. - let mut at = vec![u32::MAX; code.len() / 4]; + // It grows to the furthest dword reached, so bytes past the code (data, + // padding) cost nothing. + let mut at: Vec = Vec::new(); let mut found: Vec = Vec::new(); let mut pending = vec![0usize]; while let Some(mut offset) = pending.pop() { @@ -108,7 +110,7 @@ fn decode_aligned(code: &[u8]) -> Option, ShaderError>> // the longest instruction's reach. let before = |end: usize| { (1..=MAX_WORDS.min(end / 4)) - .map(|k| at[end / 4 - k]) + .map(|k| at.get(end / 4 - k).copied().unwrap_or(u32::MAX)) .find(|&i| i != u32::MAX) .map(|i| &found[i as usize]) }; @@ -116,6 +118,9 @@ fn decode_aligned(code: &[u8]) -> Option, ShaderError>> 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 { From 12876d139a0becb7a0c0b41db14ce0bd2a3239a5 Mon Sep 17 00:00:00 2001 From: 0xkn0wh3r3 <360897260+0xkn0wh3r3@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:36:44 -0500 Subject: [PATCH 3/3] Shaders: the decode table has room for a short program at once Grown entry by entry, a short program's table was reallocated several times: up to 4,096 entries (16 KiB) are reserved, still none filled, so padding past the code costs nothing. A 25-instruction program decodes in 1.32 us here, from 1.45. --- crates/victory-shader/src/gfx10/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/victory-shader/src/gfx10/mod.rs b/crates/victory-shader/src/gfx10/mod.rs index 6db1eaf81f..13b2b4b112 100644 --- a/crates/victory-shader/src/gfx10/mod.rs +++ b/crates/victory-shader/src/gfx10/mod.rs @@ -88,8 +88,8 @@ const MAX_WORDS: usize = 5; 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. - let mut at: Vec = Vec::new(); + // 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() {