Files
betterxen/crates/decoder/src/lib.rs
T
elforeign 964026901f shaders: decode inspected instructions at their stream offset
xenora shaders inspect passed each identified instruction to decode() on
its own. decode() checks that every branch target lands on an instruction
in the stream it was given, and a lone branch has no other instruction to
land on, so every supported s_branch and s_cbranch_* was reported as
"unsupported operands" (3 of 15 rows in bitwise-branch.bin).

Add decode_at(), which decodes one instruction at its real stream offset
without the stream-wide target check, and DecodedInstruction::branch_target().
inspect now identifies the whole stream first and checks each branch
target against those boundaries, reporting a bad target separately.
decode() is rebuilt on decode_at() with unchanged behaviour.

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
2026-11-24 00:40:55 -06:00

648 lines
22 KiB
Rust

//! Decoder for Xenora's bounded little-endian gfx1030 instruction stream.
use std::fmt;
pub use xenora_isa::{
Architecture, ExportTarget, Instruction, InterpolationPhase, ScalarSource, VectorSource,
};
use xenora_isa::{
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_BRANCH, S_CBRANCH_EXECZ,
S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM, S_MOV_B32, S_WAITCNT, S_WAITCNT_VSCNT,
V_ADD_F32, V_ADD_NC_U32, V_AND_B32, V_ASHRREV_I32, V_INTERP_P1_F32, V_INTERP_P2_F32,
V_LSHLREV_B32, V_LSHRREV_B32, V_MOV_B32, V_MUL_F32, V_OR_B32, V_XOR_B32,
};
use xenora_isa::meta::EncodingFamily;
/// Metadata forms that [`decode`] turns into an [`Instruction`] for at least one
/// operand combination. Operand restrictions are listed in docs/status.toml;
/// every other form in [`xenora_isa::meta::FORMS`] is rejected as unsupported.
pub const SEMANTIC_FORMS: [(&str, EncodingFamily); 25] = [
("BUFFER_LOAD_DWORD", EncodingFamily::Mubuf),
("BUFFER_STORE_DWORD", EncodingFamily::Mubuf),
("EXP", EncodingFamily::Exp),
("IMAGE_SAMPLE_LZ", EncodingFamily::Mimg),
("S_BRANCH", EncodingFamily::Sopp),
("S_CBRANCH_EXECZ", EncodingFamily::Sopp),
("S_CBRANCH_SCC0", EncodingFamily::Sopp),
("S_CBRANCH_VCCZ", EncodingFamily::Sopp),
("S_CMP_EQ_U32", EncodingFamily::Sopc),
("S_ENDPGM", EncodingFamily::Sopp),
("S_MOV_B32", EncodingFamily::Sop1),
("S_WAITCNT", EncodingFamily::Sopp),
("S_WAITCNT_VSCNT", EncodingFamily::Sopk),
("V_ADD_F32", EncodingFamily::Vop2),
("V_ADD_NC_U32", EncodingFamily::Vop3),
("V_AND_B32", EncodingFamily::Vop2),
("V_ASHRREV_I32", EncodingFamily::Vop2),
("V_INTERP_P1_F32", EncodingFamily::Vintrp),
("V_INTERP_P2_F32", EncodingFamily::Vintrp),
("V_LSHLREV_B32", EncodingFamily::Vop2),
("V_LSHRREV_B32", EncodingFamily::Vop2),
("V_MOV_B32", EncodingFamily::Vop1),
("V_MUL_F32", EncodingFamily::Vop2),
("V_OR_B32", EncodingFamily::Vop2),
("V_XOR_B32", EncodingFamily::Vop2),
];
/// A successfully decoded instruction and its location in the input stream.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecodedInstruction {
/// Byte offset from the start of the stream.
pub offset: usize,
/// Total size in bytes, including any literal word.
pub byte_len: u8,
/// The decoded instruction and operand fields.
pub instruction: Instruction,
}
impl DecodedInstruction {
/// Destination byte offset of a branch, from the start of the stream.
#[must_use]
pub fn branch_target(&self) -> Option<usize> {
match self.instruction {
Instruction::Branch { target }
| Instruction::BranchSccZero { target }
| Instruction::BranchExecZero { target }
| Instruction::BranchVccZero { target } => Some(target),
_ => None,
}
}
}
impl fmt::Display for DecodedInstruction {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{:04x}: {}", self.offset, self.instruction)
}
}
/// Decoder failures classified for callers.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DecodeErrorKind {
/// An instruction, second word or literal extends past the available input.
Truncated {
/// Number of bytes required at the instruction start.
needed: u8,
/// Number of bytes available at the instruction start.
available: usize,
},
/// No supported opcode pattern matches the first word.
UnsupportedOpcode {
/// First instruction word in host integer order.
word: u32,
},
/// A supported opcode uses an operand or modifier outside the subset.
UnsupportedForm {
/// Opcode mnemonic.
mnemonic: &'static str,
/// Details of the unsupported or reserved fields.
detail: &'static str,
},
/// A forward branch leaves the stream or lands inside an instruction.
InvalidBranchTarget {
/// Destination byte offset from the start of the stream.
target: usize,
},
}
/// A decoder error and the offset of the offending instruction.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecodeError {
/// Byte offset from the start of the stream.
pub offset: usize,
/// Decoder-specific failure.
pub kind: DecodeErrorKind,
}
impl fmt::Display for DecodeError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "gfx1030 decode at byte 0x{:x}: ", self.offset)?;
match self.kind {
DecodeErrorKind::Truncated { needed, available } => {
write!(
formatter,
"instruction requires {needed} bytes, found {available}"
)
}
DecodeErrorKind::UnsupportedOpcode { word } => {
write!(formatter, "unsupported opcode in 0x{word:08x}")
}
DecodeErrorKind::UnsupportedForm { mnemonic, detail } => {
write!(formatter, "unsupported {mnemonic} form: {detail}")
}
DecodeErrorKind::InvalidBranchTarget { target } => {
write!(
formatter,
"branch target 0x{target:x} is outside instruction boundaries"
)
}
}
}
}
impl std::error::Error for DecodeError {}
/// Decodes a stream of instructions. Program termination, memory hazards and
/// register initialization are checked by later stages.
///
/// # Errors
///
/// Returns the failing byte offset for a truncated instruction, unsupported
/// opcode, unsupported form or invalid branch target. An empty stream
/// decodes to an empty list.
pub fn decode(
architecture: Architecture,
bytes: &[u8],
) -> Result<Vec<DecodedInstruction>, DecodeError> {
let mut result = Vec::new();
let mut offset = 0;
while offset < bytes.len() {
let decoded = decode_at(architecture, bytes, offset)?;
offset += usize::from(decoded.byte_len);
result.push(decoded);
}
for decoded in &result {
if let Some(target) = decoded.branch_target()
&& result
.binary_search_by_key(&target, |item| item.offset)
.is_err()
{
return Err(DecodeError {
offset: decoded.offset,
kind: DecodeErrorKind::InvalidBranchTarget { target },
});
}
}
Ok(result)
}
/// Decodes the one instruction that starts at `offset` in `bytes`. Branch
/// targets are stream offsets, but whether they land on an instruction
/// boundary is left to the caller, which knows the rest of the stream.
///
/// # Errors
///
/// Returns `offset` with a truncated instruction, unsupported opcode or
/// unsupported form. An `offset` at or past the end is truncated.
pub fn decode_at(
architecture: Architecture,
bytes: &[u8],
offset: usize,
) -> Result<DecodedInstruction, DecodeError> {
let decode_instruction = match architecture {
Architecture::Gfx1030 => decode_gfx1030,
};
let rest = bytes.get(offset..).unwrap_or_default();
let (instruction, byte_len) =
decode_instruction(rest, offset).map_err(|kind| DecodeError { offset, kind })?;
Ok(DecodedInstruction {
offset,
byte_len,
instruction,
})
}
fn word(bytes: &[u8], end: u8) -> Result<u32, DecodeErrorKind> {
let end_index = usize::from(end);
let Some(word) = bytes
.get(end_index - 4..end_index)
.and_then(|word| <[u8; 4]>::try_from(word).ok())
else {
return Err(DecodeErrorKind::Truncated {
needed: end,
available: bytes.len(),
});
};
Ok(u32::from_le_bytes(word))
}
fn unsupported(mnemonic: &'static str, detail: &'static str) -> DecodeErrorKind {
DecodeErrorKind::UnsupportedForm { mnemonic, detail }
}
fn decode_gfx1030(bytes: &[u8], offset: usize) -> Result<(Instruction, u8), DecodeErrorKind> {
let first = word(bytes, 4)?;
if BUFFER_LOAD_DWORD.matches(first) {
let len = BUFFER_LOAD_DWORD.byte_len();
let second = word(bytes, len)?;
let (data, index, resource) = buffer_operands(first, second, "buffer_load_dword")?;
return Ok((
Instruction::BufferLoadDword {
data,
index,
resource,
},
len,
));
}
if BUFFER_STORE_DWORD.matches(first) {
let len = BUFFER_STORE_DWORD.byte_len();
let second = word(bytes, len)?;
let (data, index, resource) = buffer_operands(first, second, "buffer_store_dword")?;
return Ok((
Instruction::BufferStoreDword {
data,
index,
resource,
},
len,
));
}
if EXP.matches(first) {
let len = EXP.byte_len();
let sources = word(bytes, len)?.to_le_bytes();
// RDNA 2 ISA Table 56. Match all operand and reserved bits, including
// the four enable bits, compression, DONE and valid-mask flags.
let target = match first & 0x03ff_ffff {
0x08cf => ExportTarget::Position0,
0x020f => ExportTarget::Parameter0,
0x180f => ExportTarget::RenderTarget0,
_ => {
return Err(unsupported(
"exp",
"expected full uncompressed POS0 done, PARAM0, or MRT0 done vm",
));
}
};
return Ok((Instruction::Export { target, sources }, len));
}
if IMAGE_SAMPLE_LZ.matches(first) {
let len = IMAGE_SAMPLE_LZ.byte_len();
let second = word(bytes, len)?;
let (dst, coord, resource, sampler) =
image_sample_operands(first, second, "image_sample_lz")?;
return Ok((
Instruction::ImageSampleLz {
dst,
coord,
resource,
sampler,
},
len,
));
}
for (opcode, phase) in [
(V_INTERP_P1_F32, InterpolationPhase::First),
(V_INTERP_P2_F32, InterpolationPhase::Second),
] {
if opcode.matches(first) {
return Ok((
Instruction::Interpolate {
phase,
dst: (first >> 18) as u8,
coordinate: first as u8,
attribute: ((first >> 10) & 0x3f) as u8,
component: ((first >> 8) & 3) as u8,
},
opcode.byte_len(),
));
}
}
if V_MOV_B32.matches(first) {
return move_b32(first, bytes);
}
if V_ADD_NC_U32.matches(first) {
let len = V_ADD_NC_U32.byte_len();
let second = word(bytes, len)?;
return Ok((add_u32(first, second)?, len));
}
if S_MOV_B32.matches(first) {
let dst = ((first >> 16) & 0x7f) as u8;
if !matches!(dst, 0..=107 | 126..=127) {
return Err(unsupported(
"s_mov_b32",
"expected a normal SGPR or EXEC/VCC destination",
));
}
let (src, len) = scalar_source(first as u8, bytes, "s_mov_b32")?;
return Ok((Instruction::ScalarMoveB32 { dst, src }, len));
}
if S_CMP_EQ_U32.matches(first) {
let src0_bits = first as u8;
let src1_bits = (first >> 8) as u8;
if src0_bits == 255 && src1_bits == 255 {
return Err(unsupported(
"s_cmp_eq_u32",
"only one source may select a literal",
));
}
let (src0, len0) = scalar_source(src0_bits, bytes, "s_cmp_eq_u32")?;
let (src1, len1) = scalar_source(src1_bits, bytes, "s_cmp_eq_u32")?;
return Ok((
Instruction::ScalarCompareEqU32 { src0, src1 },
len0.max(len1),
));
}
if S_BRANCH.matches(first) {
let target = branch_target(first, offset, "s_branch")?;
return Ok((Instruction::Branch { target }, S_BRANCH.byte_len()));
}
if S_CBRANCH_SCC0.matches(first) {
let target = branch_target(first, offset, "s_cbranch_scc0")?;
return Ok((
Instruction::BranchSccZero { target },
S_CBRANCH_SCC0.byte_len(),
));
}
if S_CBRANCH_EXECZ.matches(first) {
let target = branch_target(first, offset, "s_cbranch_execz")?;
return Ok((
Instruction::BranchExecZero { target },
S_CBRANCH_EXECZ.byte_len(),
));
}
if S_CBRANCH_VCCZ.matches(first) {
let target = branch_target(first, offset, "s_cbranch_vccz")?;
return Ok((
Instruction::BranchVccZero { target },
S_CBRANCH_VCCZ.byte_len(),
));
}
if V_LSHLREV_B32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_lshlrev_b32_e32")?;
return Ok((
Instruction::VectorLshlrevB32 { dst, src0, src1 },
V_LSHLREV_B32.byte_len(),
));
}
if V_ASHRREV_I32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_ashrrev_i32_e32")?;
return Ok((
Instruction::VectorAshrrevI32 { dst, src0, src1 },
V_ASHRREV_I32.byte_len(),
));
}
if V_LSHRREV_B32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_lshrrev_b32_e32")?;
return Ok((
Instruction::VectorLshrrevB32 { dst, src0, src1 },
V_LSHRREV_B32.byte_len(),
));
}
if V_AND_B32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_and_b32_e32")?;
return Ok((
Instruction::VectorAndB32 { dst, src0, src1 },
V_AND_B32.byte_len(),
));
}
if V_OR_B32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_or_b32_e32")?;
return Ok((
Instruction::VectorOrB32 { dst, src0, src1 },
V_OR_B32.byte_len(),
));
}
if V_XOR_B32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_xor_b32_e32")?;
return Ok((
Instruction::VectorXorB32 { dst, src0, src1 },
V_XOR_B32.byte_len(),
));
}
if V_ADD_F32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_add_f32_e32")?;
return Ok((
Instruction::VectorAddF32 { dst, src0, src1 },
V_ADD_F32.byte_len(),
));
}
if V_MUL_F32.matches(first) {
let (dst, src0, src1) = vop2_registers(first, "v_mul_f32_e32")?;
return Ok((
Instruction::VectorMulF32 { dst, src0, src1 },
V_MUL_F32.byte_len(),
));
}
if S_WAITCNT.matches(first) {
return scalar_form(
first,
S_WAITCNT.byte_len(),
0xbf8c_3f70,
Instruction::WaitLoad,
"expected vmcnt(0) alone",
);
}
if S_WAITCNT_VSCNT.matches(first) {
return scalar_form(
first,
S_WAITCNT_VSCNT.byte_len(),
0xbbfd_0000,
Instruction::WaitStore,
"expected null, 0",
);
}
if S_ENDPGM.matches(first) {
return scalar_form(
first,
S_ENDPGM.byte_len(),
0xbf81_0000,
Instruction::EndProgram,
"expected zero immediate",
);
}
Err(DecodeErrorKind::UnsupportedOpcode { word: first })
}
fn scalar_source(
bits: u8,
bytes: &[u8],
mnemonic: &'static str,
) -> Result<(ScalarSource, u8), DecodeErrorKind> {
let value = u32::from(bits);
match bits {
0..=105 => Ok((ScalarSource::Register(bits), 4)),
128..=192 => Ok((ScalarSource::Immediate(value - 128), 4)),
193..=208 => Ok((ScalarSource::Immediate(192u32.wrapping_sub(value)), 4)),
255 => Ok((ScalarSource::Immediate(word(bytes, 8)?), 8)),
_ => Err(unsupported(
mnemonic,
"expected a normal SGPR, inline integer or literal",
)),
}
}
fn branch_target(
first: u32,
offset: usize,
mnemonic: &'static str,
) -> Result<usize, DecodeErrorKind> {
let displacement = first as u16 as i16;
let words = usize::try_from(displacement)
.map_err(|_| unsupported(mnemonic, "expected a forward branch"))?;
offset
.checked_add(4 + words * 4)
.ok_or_else(|| unsupported(mnemonic, "target byte offset overflowed"))
}
fn vop2_registers(first: u32, mnemonic: &'static str) -> Result<(u8, u8, u8), DecodeErrorKind> {
if first & 0x100 == 0 {
return Err(unsupported(
mnemonic,
"expected two unmodified VGPR sources",
));
}
Ok(((first >> 17) as u8, first as u8, (first >> 9) as u8))
}
fn image_sample_operands(
first: u32,
second: u32,
mnemonic: &'static str,
) -> Result<(u8, u8, u8, u8), DecodeErrorKind> {
// In word 0:
// Bits 1..2: NSA (must be 0 for contiguous VADDR)
// Bits 3..5: DIM (1 = SQ_RSRC_IMG_2D)
// Bit 7: DLC (0)
// Bits 8..11: DMASK (0xf for RGBA 4-component return)
// Bit 12: UNORM (0)
// Bit 13: GLC (0)
// Bit 14: DA (0)
// Bit 15: R128 (0)
// Bit 16: TFE (0)
// Bit 25: SLC (0)
// Bit 0 & Bits 18..24: OP (0x27 / 39)
// Bits 26..31: ENCODING (0x3c)
// Mask off OP and ENCODING (bits 0, 18..24, 26..31).
// The expected variable bits for 2D RGBA8 sampling:
// dim = 1 (bits 3..5 = 1 << 3 = 0x8), dmask = 0xf (bits 8..11 = 0xf << 8 = 0xf00).
// All other modifier flags (NSA, DLC, UNORM, GLC, DA, R128, TFE, SLC) must be zero.
const MODIFIER_MASK: u32 = 0x0203_fffe;
const EXPECTED_2D_RGBA: u32 = 0x0000_0f08;
if first & MODIFIER_MASK != EXPECTED_2D_RGBA {
return Err(unsupported(
mnemonic,
"expected dim:SQ_RSRC_IMG_2D and dmask:0xf with zero modifier flags",
));
}
// In word 1:
// Bits 0..7: VADDR (u8)
// Bits 8..15: VDATA (u8)
// Bits 16..20: SRSRC >> 2 (5 bits)
// Bits 21..25: SSAMP >> 2 (5 bits)
// Bits 26..29: reserved (must be 0)
// Bit 30: A16 (0)
// Bit 31: D16 (0)
if second & 0xfc00_0000 != 0 {
return Err(unsupported(
mnemonic,
"expected 32-bit float coordinates and data without A16/D16",
));
}
let coord = (second & 0xff) as u8;
let dst = ((second >> 8) & 0xff) as u8;
let resource = (((second >> 16) & 0x1f) * 4) as u8;
let sampler = (((second >> 21) & 0x1f) * 4) as u8;
// RDNA 2 resource descriptor (T#) requires 8 SGPRs; sampler (S#) requires 4 SGPRs.
// SGPR range is 0..=105 for normal allocatable registers.
if resource > 98 {
return Err(unsupported(
mnemonic,
"resource descriptor must fit in normal SGPRs s[0:105]",
));
}
if sampler > 102 {
return Err(unsupported(
mnemonic,
"sampler descriptor must fit in normal SGPRs s[0:105]",
));
}
Ok((dst, coord, resource, sampler))
}
fn buffer_operands(
first: u32,
second: u32,
mnemonic: &'static str,
) -> Result<(u8, u8, u8), DecodeErrorKind> {
// OFFSET, OFFEN, IDXEN, GLC, DLC, LDS and the reserved bit before OP.
const ADDRESS_MODE_MASK: u32 = 0x0003_ffff;
const IDXEN: u32 = 1 << 13;
// SOFFSET is inline zero. TFE, SLC and the reserved high operand bit are zero.
if first & ADDRESS_MODE_MASK != IDXEN || second & 0xffe0_0000 != 0x8000_0000 {
return Err(unsupported(
mnemonic,
"expected idxen alone with zero offsets",
));
}
let index = second as u8;
let data = (second >> 8) as u8;
let resource = ((second >> 16) & 0x1f) as u8 * 4;
// AMD RDNA 2 ISA §3.6.2. A resource descriptor requires four normal SGPRs.
if resource > 102 {
return Err(unsupported(
mnemonic,
"resource descriptor must fit in normal SGPRs",
));
}
Ok((data, index, resource))
}
fn move_b32(first: u32, bytes: &[u8]) -> Result<(Instruction, u8), DecodeErrorKind> {
let dst = (first >> 17) as u8;
let source = first & 0x1ff;
let base_len = V_MOV_B32.byte_len();
let (src, len) = match source {
128..=192 => (VectorSource::Immediate(source - 128), base_len),
193..=208 => (
VectorSource::Immediate(192u32.wrapping_sub(source)),
base_len,
),
// AMD RDNA 2 ISA Table 77: VOP1 source selector 242 supplies +1.0f.
242 => (VectorSource::Immediate(0x3f80_0000), base_len),
255 => {
let len = base_len + 4;
(VectorSource::Immediate(word(bytes, len)?), len)
}
256..=511 => (VectorSource::Register(source as u8), base_len),
_ => {
return Err(unsupported(
"v_mov_b32_e32",
"expected a VGPR, inline integer, inline +1.0f or literal",
));
}
};
Ok((Instruction::MoveB32 { dst, src }, len))
}
fn add_u32(first: u32, second: u32) -> Result<Instruction, DecodeErrorKind> {
let src0 = second & 0x1ff;
let src1 = (second >> 9) & 0x1ff;
// All output and input modifiers are clear. The unused third source uses
// the gfx1030 assembler's inline-zero operand encoding.
if first & 0xff00 != 0 || second >> 18 != 128 || src0 < 256 || src1 < 256 {
return Err(unsupported(
"v_add_nc_u32_e64",
"expected two VGPR sources and no modifiers",
));
}
Ok(Instruction::AddNoCarryU32 {
dst: first as u8,
src0: src0 as u8,
src1: src1 as u8,
})
}
fn scalar_form(
word: u32,
byte_len: u8,
expected: u32,
instruction: Instruction,
detail: &'static str,
) -> Result<(Instruction, u8), DecodeErrorKind> {
if word == expected {
Ok((instruction, byte_len))
} else {
Err(unsupported(instruction.mnemonic(), detail))
}
}