From 93d26d6e458d2519a83b0acc0672e1ed909df42b Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:08:00 -0600 Subject: [PATCH 1/7] test(vopc): independent oracle for 16- and 64-bit integer compares (#215) B1 adds v_cmp_{lt..ge}_{i16,u16}_e32 and v_cmp_{f..t}_{i64,u64}_e32 as registry forms. To check them without trusting the registry, decoder or shader, both interpreters now decode and evaluate the 28 forms from the RDNA 2 guide alone: 16-bit forms read bits 15:0 and ignore the upper half, 64-bit forms read register pairs low dword first, inline integers keep their value at the operand width, and VCC_LO = EXEC & lanes & test. Points the guide does not settle (literals, vcc as a pair, odd SGPR bases, v255 as a pair base) are rejected. vopc_wide.rs derives the expected lanes per form: 6212 cases on each interpreter, 1128 rejections. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- crates/ref/src/lib.rs | 141 +++- tests/validation/independent_ref.rs | 93 ++- tests/validation/tests/vopc_wide.rs | 1172 +++++++++++++++++++++++++++ 3 files changed, 1357 insertions(+), 49 deletions(-) create mode 100644 tests/validation/tests/vopc_wide.rs diff --git a/crates/ref/src/lib.rs b/crates/ref/src/lib.rs index e391d45..8bfdfab 100644 --- a/crates/ref/src/lib.rs +++ b/crates/ref/src/lib.rs @@ -55,8 +55,10 @@ pub enum Op { ScalarUnary(ScalarUnary, u8, Source), /// VOP1 move of a normal SGPR or `vcc_lo` (106) into a VGPR. MoveScalar(u8, u8), - /// VOPC 32-bit integer compare writing the Wave32 mask to `vcc_lo`. - Compare(Comparison, CompareSource, u8), + /// VOPC 16-, 32- or 64-bit integer compare writing the Wave32 mask to + /// `vcc_lo`. For 64-bit forms a `Vector` or `Scalar` source and VSRC1 name + /// the low register of a pair. + Compare(Comparison, CompareWidth, CompareSource, u8), /// SOP2 operation writing an SGPR and SCC. Scalar(ScalarAlu, u8, Source, Source), /// SOPC `s_cmp_eq_{i32,u32}` (opcodes 0 and 6, identical per guide 12.4). @@ -89,18 +91,27 @@ pub struct Comparison { } impl Comparison { - /// The per-lane result, from an exact comparison of the widened operands. + /// The per-lane result for 32-bit operands. fn holds(self, s0: u32, s1: u32) -> bool { - let widen = |bits: u32| { - if self.signed { - i64::from(bits as i32) - } else { - i64::from(bits) + self.holds_at(CompareWidth::Word, u64::from(s0), u64::from(s1)) + } + + /// The per-lane result, from an exact comparison of the operands widened + /// from their low `width` bits; higher bits are ignored. + fn holds_at(self, width: CompareWidth, s0: u64, s1: u64) -> bool { + let widen = |bits: u64| -> i128 { + match (width, self.signed) { + (CompareWidth::Half, true) => i128::from(bits as u16 as i16), + (CompareWidth::Half, false) => i128::from(bits as u16), + (CompareWidth::Word, true) => i128::from(bits as u32 as i32), + (CompareWidth::Word, false) => i128::from(bits as u32), + (CompareWidth::Double, true) => i128::from(bits as i64), + (CompareWidth::Double, false) => i128::from(bits), } }; let (s0, s1) = (widen(s0), widen(s1)); match self.offset { - // Table 61: V_CMP_F_{I,U}32 `D[threadId] = 0`. + // Table 61: V_CMP_F_{I,U}{32,64} `D[threadId] = 0`. 0 => false, 1 => s0 < s1, 2 => s0 == s1, @@ -108,12 +119,45 @@ impl Comparison { 4 => s0 > s1, 5 => s0 != s1, 6 => s0 >= s1, - // Table 61: V_CMP_T_{I,U}32 `D[threadId] = 1`. + // Table 61: V_CMP_T_{I,U}{32,64} `D[threadId] = 1`. _ => true, } } } +/// Operand width of a VOPC integer compare (guide 6.2: the data size is the +/// one named by the instruction). +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum CompareWidth { + /// `_I16`/`_U16`: bits 15:0 of each 32-bit source (6.2.5 and 6.8: VOPC + /// selects another half only through SDWA, which is not accepted). + Half, + /// `_I32`/`_U32`. + Word, + /// `_I64`/`_U64`: register pairs with the low dword in the lower register + /// (3.6.3 for SGPRs; for VGPRs the guide states it for 64-bit addresses, + /// 9.1: "ADDR has the LSBs, and ADDR+1 has the MSBs"). + Double, +} + +impl CompareWidth { + /// Guide 12.9 Tables 59-61: the base opcode of each accepted group plus + /// the relation offset; 16-bit groups have no F (offset 0) or T (7). + fn from_opcode(opcode: u16) -> Option<(Comparison, Self)> { + let (base, signed, width) = match opcode { + 128..=135 => (128, true, Self::Word), + 192..=199 => (192, false, Self::Word), + 137..=142 => (136, true, Self::Half), + 169..=174 => (168, false, Self::Half), + 160..=167 => (160, true, Self::Double), + 224..=231 => (224, false, Self::Double), + _ => return None, + }; + let offset = (opcode - base) as u8; + Some((Comparison { offset, signed }, width)) + } +} + /// First source of a VOPC compare or a VOP2 integer form. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum CompareSource { @@ -581,20 +625,33 @@ pub fn decode(code: &[u8]) -> Result, ReferenceError> { } // VOPC (bits 31-25 = 0b0111110): SRC0 bits 0-8, VSRC1 bits 9-16, OP bits 17-24. [lo, packed, upper, tag @ 0x7c..=0x7d, tail @ ..] - if matches!( + if CompareWidth::from_opcode( (u16::from(*tag & 1) << 7) | u16::from(*upper >> 1), - 128..=135 | 192..=199 - ) => + ) + .is_some() => { let opcode = (u16::from(*tag & 1) << 7) | u16::from(*upper >> 1); - let comparison = Comparison { - offset: (opcode & 0x3f) as u8, - signed: opcode < 192, - }; + let (comparison, width) = CompareWidth::from_opcode(opcode).unwrap(); let vsrc1 = (packed >> 1) | ((upper & 1) << 7); + let selector = u16::from(*lo) | (u16::from(packed & 1) << 8); + // docs/shaders/vector-compare.md subset: no literal for 16-bit; + // 64-bit takes v[n:n+1] (n <= 254), s[2k:2k+1] (k <= 52) or + // an inline integer, with VSRC1 v[m:m+1] (m <= 254). + let accepted = match width { + CompareWidth::Half => selector != 255, + CompareWidth::Word => true, + CompareWidth::Double => { + vsrc1 != 255 + && matches!(selector, 0..=104 | 128..=208 | 256..=510) + && (selector >= 128 || selector % 2 == 0) + } + }; + if !accepted { + return Err(error(offset, "unsupported VOPC source")); + } let (src0, length) = decode_vector_source(*lo, *packed, tail, offset, "unsupported VOPC source")?; - (Op::Compare(comparison, src0, vsrc1), length) + (Op::Compare(comparison, width, src0, vsrc1), length) } // VOP2 integer forms (bit 31 = 0): OP bits 25-30, VDST 17-24, // VSRC1 9-16, SRC0 0-8. @@ -1109,30 +1166,60 @@ fn execute_wave(program: &[DecodedInstruction], input: &[u32]) -> Result { + Op::Compare(comparison, width, src0, src1) => { let mut mask = 0u32; if exec != 0 { + // The registers each source occupies: a pair for 64-bit forms. + let span = |register: u8| { + if width == CompareWidth::Double { + vec![register, register + 1] + } else { + vec![register] + } + }; // Sources are checked once for the wave, even if no present lane is active. let check = |register: u8| { - (0..lanes).try_for_each(|lane| read(lane, register).map(|_| ())) + span(register).into_iter().try_for_each(|register| { + (0..lanes).try_for_each(|lane| read(lane, register).map(|_| ())) + }) + }; + let join = |values: Vec| { + values + .iter() + .rev() + .fold(0u64, |acc, &value| (acc << 32) | u64::from(value)) + }; + let read_pair = |lane: usize, register: u8| { + span(register) + .into_iter() + .map(|register| read(lane, register)) + .collect::, _>>() + .map(join) + }; + let read_scalar_pair = |register: u8| { + span(register) + .into_iter() + .map(|register| read_scalar(Source::Register(register))) + .collect::, _>>() + .map(join) }; match src0 { CompareSource::Vector(register) => check(register)?, CompareSource::Scalar(register) => { - read_scalar(Source::Register(register))?; + read_scalar_pair(register)?; } CompareSource::Immediate(_) => {} } check(src1)?; for lane in (0..lanes).filter(|&lane| executes(lane)) { let s0 = match src0 { - CompareSource::Vector(register) => read(lane, register)?, - CompareSource::Scalar(register) => { - read_scalar(Source::Register(register))? - } - CompareSource::Immediate(bits) => bits, + CompareSource::Vector(register) => read_pair(lane, register)?, + CompareSource::Scalar(register) => read_scalar_pair(register)?, + // Table 20: an inline integer is the value 0..64 or + // -1..-16 at the operand's width (sign extended). + CompareSource::Immediate(bits) => bits as i32 as i64 as u64, }; - if comparison.holds(s0, read(lane, src1)?) { + if comparison.holds_at(width, s0, read_pair(lane, src1)?) { mask |= 1 << lane; } } diff --git a/tests/validation/independent_ref.rs b/tests/validation/independent_ref.rs index f4fc5f2..545bbee 100644 --- a/tests/validation/independent_ref.rs +++ b/tests/validation/independent_ref.rs @@ -27,8 +27,8 @@ //! production operator. SOP2 arithmetic runs through a bit-serial ripple //! adder (subtraction as addition of the complement), not host integer ops. //! -//! Wave32 VOPC integer compares (`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32}_e32`, -//! contract `docs/shaders/vector-compare.md`) need all lanes of a wave, so execution +//! Wave32 VOPC integer compares (`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32,i64,u64}_e32` +//! and `v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_e32`, contract `docs/shaders/vector-compare.md`) need all lanes of a wave, so execution //! runs lockstep waves of 32 consecutive dispatch indices with wave-uniform //! scalar state and per-lane vector state. A lane-local failure stops only that //! lane and the lowest-index error is reported, so programs without compares @@ -76,9 +76,11 @@ enum Insn { /// `v_mov_b32_e32 vD, vcc_lo`. VMovFromVcc(u8), /// VOPC compare into VCC_LO: relation 0 f, 1 lt, 2 eq, 3 le, 4 gt, 5 ne, 6 ge, 7 t. + /// `bits` is 16, 32 or 64; 64-bit register operands name the low register of a pair. VCmp { relation: u8, signed: bool, + bits: u32, src0: CmpSrc, src1: u8, }, @@ -196,6 +198,7 @@ fn run_wave(program: &[(usize, Insn)], wave: &[u32]) -> Result, String> Insn::VCmp { relation, signed, + bits, src0, src1, } => { @@ -208,7 +211,7 @@ fn run_wave(program: &[(usize, Insn)], wave: &[u32]) -> Result, String> &lanes, &sgpr, execution_mask, - (relation, signed, src0, src1), + (relation, signed, bits, src0, src1), offset, ) .map(|mask| { @@ -545,29 +548,58 @@ fn compare( lanes: &[Lane], sgpr: &[Option; 128], execution_mask: u32, - (relation, signed, src0, src1): (u8, bool, CmpSrc, u8), + (relation, signed, bits, src0, src1): (u8, bool, u32, CmpSrc, u8), offset: usize, ) -> Result { if execution_mask == 0 { // Encoding already validated; nothing is read (contract 5). return Ok(0); } - let scalar_src0 = match src0 { - CmpSrc::Sgpr(reg) => { - Some(sgpr[reg as usize].ok_or_else(|| format!("{offset:#x}: uninitialized s{reg}"))?) + // Registers per operand: a 64-bit operand is {reg (bits 31:0), reg+1 (63:32)}. + let count: u8 = if bits == 64 { 2 } else { 1 }; + let read_s = |reg: u8| -> Result { + let mut value = 0u64; + for half in (0..count).rev() { + let r = reg + half; + let word = + sgpr[r as usize].ok_or_else(|| format!("{offset:#x}: uninitialized s{r}"))?; + value = value * (1 << 32) + u64::from(word); } - CmpSrc::Vcc => Some(sgpr[106].ok_or_else(|| format!("{offset:#x}: uninitialized vcc_lo"))?), - CmpSrc::Imm(value) => Some(value), + Ok(value) + }; + let read_v = |lane: &Lane, reg: u8| -> Result { + let mut value = 0u64; + for half in (0..count).rev() { + value = value * (1 << 32) + u64::from(read_vgpr(lane, reg + half, offset)?); + } + Ok(value) + }; + let scalar_src0 = match src0 { + CmpSrc::Sgpr(reg) => Some(read_s(reg)?), + CmpSrc::Vcc => { + Some(u64::from(sgpr[106].ok_or_else(|| { + format!("{offset:#x}: uninitialized vcc_lo") + })?)) + } + // Inline integers -16..64 keep their value at every width; a + // 32-bit literal (32-bit forms only) is used as its 32 bits. + CmpSrc::Imm(value) if bits == 64 && value >= 0x8000_0000 => { + Some(u64::from(value) + 0xffff_ffff_0000_0000) + } + CmpSrc::Imm(value) => Some(u64::from(value)), CmpSrc::Vgpr(_) => None, }; // VGPR readiness is wave-uniform; validate it on every existing lane so // acceptance does not depend on which lanes are active. for lane in lanes { if let CmpSrc::Vgpr(reg) = src0 { - read_vgpr(lane, reg, offset)?; + read_v(lane, reg)?; } - read_vgpr(lane, src1, offset)?; + read_v(lane, src1)?; } + // Only the low `bits` bits take part (16-bit forms ignore bits 31:16). + let keep = u64::MAX >> (64 - bits); + let sign = 1u64 << (bits - 1); let mut mask = 0u32; for (n, lane) in lanes.iter().enumerate() { if (execution_mask >> n) & 1 == 0 { @@ -575,16 +607,13 @@ fn compare( } let a = match (scalar_src0, src0) { (Some(value), _) => value, - (None, CmpSrc::Vgpr(reg)) => read_vgpr(lane, reg, offset)?, + (None, CmpSrc::Vgpr(reg)) => read_v(lane, reg)?, (None, _) => unreachable!(), }; - let b = read_vgpr(lane, src1, offset)?; + let b = read_v(lane, src1)?; + let (a, b) = (a & keep, b & keep); // Signed order equals unsigned order after flipping the sign bit. - let (a, b) = if signed { - (a ^ 0x8000_0000, b ^ 0x8000_0000) - } else { - (a, b) - }; + let (a, b) = if signed { (a ^ sign, b ^ sign) } else { (a, b) }; // Guide 12.9 Table 59 offsets; F (0) and T (7) ignore the operand // values but still read them (contract 5: sources are validated). let pass = match relation { @@ -1042,17 +1071,37 @@ fn decode_one(code: &[u8], offset: usize) -> Result<(Insn, usize), String> { // VOPC: bits 31:25 = 0b0111110, OP 24:17, VSRC1 16:9, SRC0 8:0. if first >> 25 == 0b011_1110 { let opcode = (first >> 17) & 0xff; - let (signed, relation) = match opcode { - 128..=135 => (true, opcode - 128), - 192..=199 => (false, opcode - 192), + // Guide 12.9 Table 61 opcode groups (16-bit groups lack F and T). + let (signed, bits, relation) = match opcode { + 128..=135 => (true, 32, opcode - 128), + 192..=199 => (false, 32, opcode - 192), + 137..=142 => (true, 16, opcode - 136), + 169..=174 => (false, 16, opcode - 168), + 160..=167 => (true, 64, opcode - 160), + 224..=231 => (false, 64, opcode - 224), _ => return Err(fail("unsupported VOPC opcode")), }; let (src0, len) = vector_src0(code, offset, first, "VOPC")?; + let src1 = (first >> 9) as u8; + // Accepted subset: no literal for 16-bit forms; 64-bit forms take an + // even SGPR pair below VCC, a VGPR pair below v255 or an inline integer. + let allowed = match (bits, src0) { + (16 | 64, CmpSrc::Imm(_)) if len == 8 => false, + (64, CmpSrc::Vcc) => false, + (64, CmpSrc::Sgpr(reg)) => reg % 2 == 0, + (64, CmpSrc::Vgpr(reg)) => reg != 255 && src1 != 255, + (64, _) => src1 != 255, + _ => true, + }; + if !allowed { + return Err(fail("unsupported VOPC operand")); + } let insn = Insn::VCmp { relation: relation as u8, signed, + bits, src0, - src1: (first >> 9) as u8, + src1, }; return Ok((insn, len)); } diff --git a/tests/validation/tests/vopc_wide.rs b/tests/validation/tests/vopc_wide.rs new file mode 100644 index 0000000..a46c079 --- /dev/null +++ b/tests/validation/tests/vopc_wide.rs @@ -0,0 +1,1172 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +//! Independent per-lane expected values for the 28 Wave32 VOPC 16-bit and +//! 64-bit integer compares (#215 B1): `v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}` +//! (opcodes 137-142, 169-174) and `v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i64,u64}` +//! (160-167, 224-231), `_e32` only, under the contract in +//! `docs/shaders/vector-compare.md` (sections 2-7). +//! +//! The expectations are written below from the public AMD "RDNA 2" Instruction +//! Set Architecture Reference Guide only: 12.9 (Tables 59-61), 3.9, 3.6.3, +//! 6.2 (Table 20 and 6.2.1), 6.2.5, 6.8 and 9.1. Nothing here comes from the +//! ISA crate, the semantic registry, the decoder or the shader compiler. Every +//! case runs on the reference interpreter `xenora_ref` and on the second +//! interpreter `independent_ref`; the ignored test also runs the translated MSL +//! on native Metal. +//! +//! Readings of the guide (not hardware evidence): +//! - Table 59: offsets F 0, LT 1, EQ 2, LE 3, GT 4, LG/NE 5, GE 6, TRU 7 with +//! `D.u = (S0 op S1)`, `D.u = 0`, `D.u = 1`; Table 61 lists the 16-bit groups +//! without F and T (136, 143, 168 and 175 are class compares or unlisted). +//! - 16-bit: 6.2 says the operand data size is the one in the instruction +//! name; 6.2.5 says VOPC selects a half only through SDWA, and 6.8 that SDWA +//! is what references the upper half. So the e32 form reads bits 15:0 of the +//! 32-bit VGPR, SGPR or VCC_LO and ignores bits 31:16. This is an inference: +//! the guide never states the non-SDWA half in so many words. `_I16` treats +//! bits 15:0 as two's complement, `_U16` as unsigned. +//! - 64-bit: 3.6.3 "the LSBs are in SGPR[n], and the MSBs are in SGPR[n+1]" +//! and the pair must be even-aligned; for VGPRs 3.6.4 gives no alignment +//! rule and 9.1 says for 64-bit addresses "ADDR has the LSBs, and ADDR+1 has +//! the MSBs", read here as the general VGPR pair order. `_I64` is two's +//! complement over all 64 bits, `_U64` unsigned. +//! - Table 20: selectors 128-192 are the integers 0..64 and 193-208 are -1..-16; +//! they are values, so at 16 bits -1 is 0xffff and at 64 bits it is +//! 0xffff_ffff_ffff_ffff (sign extended, also for `_U64`). The guide's +//! zero-extension rule for `_U64` (6.2.1) is about 32-bit literals, which +//! the subset rejects. +//! - Section 3.9: `VCC[n] = EXEC[n] & (test passed for thread[n])`, "VCC is +//! fully written"; in wave32 only VCC_LO contributes to VCCZ. +//! - Not settled by the guide, so rejected (contract subset): literals for both +//! widths, VCC as a 64-bit source, odd SGPR bases, v255 as a pair base, and +//! any pair half that is not initialized under nonzero EXEC. + +#[path = "operand_domains/harness.rs"] +#[allow(dead_code)] +mod harness; +#[path = "../independent_ref.rs"] +mod independent_ref; + +use std::collections::BTreeMap; + +use harness::{Case, words}; +use xenora_shader::{Architecture, CompileError, compile}; + +/// One of the 28 forms: ENC_VOPC opcode (Table 61) and Table 59 offset. +#[derive(Clone, Copy, Debug)] +struct Form { + op: u32, + name: &'static str, + bits: u32, + signed: bool, + offset: u32, +} + +const RELATIONS: [&str; 8] = ["f", "lt", "eq", "le", "gt", "ne", "ge", "t"]; + +/// Table 61: 137 + (offset - 1) for I16, 169 + (offset - 1) for U16 (LT..GE), +/// 160 + offset for I64 and 224 + offset for U64 (F..T). +fn forms() -> Vec
{ + let mut forms = Vec::new(); + for (base, bits, signed, offsets) in [ + (136, 16, true, 1..=6), + (168, 16, false, 1..=6), + (160, 64, true, 0..=7), + (224, 64, false, 0..=7), + ] { + for offset in offsets { + let kind = if signed { 'i' } else { 'u' }; + let name = format!("v_cmp_{}_{kind}{bits}", RELATIONS[offset as usize]); + forms.push(Form { + op: base + offset, + name: Box::leak(name.into_boxed_str()), + bits, + signed, + offset, + }); + } + } + forms +} + +/// The operand's integer value. `raw` holds the register bits: for 16-bit +/// forms the 32-bit register (only bits 15:0 count), for 64-bit forms +/// `lo | hi << 32`. +fn value(form: Form, raw: u64) -> i128 { + if form.bits == 16 { + // 16-bit: S.i16 / S.u16 = bits 15:0. + let half = (raw % 0x1_0000) as i128; + if form.signed && half >= 0x8000 { + half - 0x1_0000 + } else { + half + } + } else { + // 64-bit: {SGPR/VGPR[n+1] (MSBs), [n] (LSBs)}. + let (lo, hi) = (raw % (1 << 32), raw / (1 << 32)); + let hi = if form.signed && hi >= 0x8000_0000 { + hi as i128 - (1 << 32) + } else { + hi as i128 + }; + hi * (1 << 32) + lo as i128 + } +} + +/// Table 59 / Table 61: D[threadId] for S0 and S1 of one lane. +fn test(form: Form, s0: u64, s1: u64) -> bool { + let (a, b) = (value(form, s0), value(form, s1)); + match form.offset { + 0 => false, // F: D = 0 + 1 => a < b, // LT + 2 => a == b, // EQ + 3 => a <= b, // LE + 4 => a > b, // GT + 5 => a != b, // NE (<>) + 6 => a >= b, // GE + _ => true, // T: D = 1 + } +} + +/// Section 3.9 and contract section 3: VCC_LO = OR over the lanes n that +/// exist (`present`) of (EXEC[n] & test(n)) << n. +fn vcc(form: Form, exec: u32, present: u32, operands: impl Fn(usize) -> (u64, u64)) -> u32 { + (0..32) + .filter(|&n| (present & exec) >> n & 1 == 1) + .filter(|&n| { + let (s0, s1) = operands(n); + test(form, s0, s1) + }) + .fold(0, |mask, n| mask | 1 << n) +} + +/// Lane inputs (v1). Bits 15:0 cover 0, 1, 0x7fff, 0x8000, 0xffff and their +/// neighbours under varied bits 31:16; the full dwords cover 0, 1, +/// 0x7fff_ffff, 0x8000_0000 and 0xffff_ffff for the 64-bit low dword. +const LANES: [u32; 32] = [ + 0, + 1, + 0x7fff_ffff, + 0x8000_0000, + 0xffff_ffff, + 0xffff_fffe, + 0x8000_0001, + 0x1234_5678, + 0xdead_0000, + 0xbeef_0001, + 0x0001_7fff, + 0xffff_8000, + 0x8000_ffff, + 0x5a5a_7ffe, + 0xa5a5_8001, + 0x0000_7fff, + 0x0000_8000, + 0x0000_ffff, + 0x7fff_0000, + 0xffff_0001, + 0x1357_9bdf, + 0x0f0f_f0f0, + 0xc0de_0005, + 0x0042_0042, + 0x0010_fff0, + 0x8000_7fff, + 0x7fff_8000, + 0x0003_0003, + 0xffff_7fff, + 0x0000_0005, + 0x4000_4000, + 0xfedc_ba98, +]; +/// Uniform 16-bit operands: boundary low halves with garbage high halves. +const B16: [u32; 8] = [ + 0, + 0xffff_0001, + 0x0000_7fff, + 0x1234_8000, + 0xabcd_ffff, + 0x8000_0000, + 0x0001_0005, + 0xdead_7ffe, +]; +/// Uniform 64-bit operands. +const B64: [u64; 8] = [ + 0, + 1, + 0x7fff_ffff_ffff_ffff, + 0x8000_0000_0000_0000, + u64::MAX, + 0x0000_0001_0000_0000, + 0x7fff_ffff_8000_0000, + 0xffff_ffff_0000_0000, +]; +/// High dwords joined to the lane input to make 64-bit lane operands. +const HIGHS: [u32; 5] = [0, 1, 0x7fff_ffff, 0x8000_0000, 0xffff_ffff]; +const SGPR16: [u32; 3] = [0xffff_0000, 0x0000_8000, 0x1234_ffff]; +const SGPR64: [u64; 3] = [0x8000_0000_0000_0000, 0x8000_0000_ffff_ffff, u64::MAX]; +const EXEC_MASKS: [u32; 7] = [ + 0, + 0x5555_5555, + 0xaaaa_aaaa, + 0x8000_0001, + 0x0000_ffff, + 0x7fff_fffe, + u32::MAX, +]; +const STALE_VCC: u32 = 0xa5a5_c33c; +const OLD: u32 = 0x0bad_f00d; + +const F16: usize = 12; +const F64: usize = 16; +const COUNTS: [(&str, usize); 22] = [ + ("vopc16 domain", F16 * 8 * 2), + ("vopc16 aliasing", F16 * 2), + ("vopc16 inline integer", F16 * 81), + ("vopc16 sgpr source", F16 * 2 * 3), + ("vopc16 vcc_lo source", F16 * 4), + ("vopc16 max register", F16 * 3), + ("vopc16 exec", F16 * 7), + ("vopc16 empty exec reads nothing", F16 * 3), + ("vopc16 partial wave", F16 * 3), + ("vopc16 vccz branch", F16 * 4), + ("vopc16 exec unchanged", F16 * 2), + ("vopc64 domain", F64 * 5 * 8 * 2), + ("vopc64 lane-varying high dword", F64 * 8 * 2), + ("vopc64 aliasing", F64 * 4), + ("vopc64 inline integer", F64 * 81 * 2), + ("vopc64 sgpr pair source", F64 * 2 * 3), + ("vopc64 max register", F64 * 3), + ("vopc64 exec", F64 * 7), + ("vopc64 empty exec reads nothing", F64 * 3), + ("vopc64 partial wave", F64 * 3), + ("vopc64 vccz branch", F64 * 4), + ("vopc64 exec unchanged", F64 * 2), +]; +/// 16-bit per form: 4 uninitialized sources, 2 pending loads, a literal with +/// and without its dword, 9 float inline constants, 9 special selectors, a +/// float constant under empty EXEC, SDWA and 3 DPP forms, `_e64`, `v_cmpx` +/// and a lane-derived vccz branch over a scalar instruction. +const REJECTIONS16: usize = 4 + 2 + 2 + 9 + 9 + 1 + 4 + 1 + 1 + 1; +/// 64-bit per form: 7 uninitialized pair halves, 2 pending loads on a high +/// half, vcc (known and under empty EXEC), a literal with and without its +/// dword, 2 odd SGPR bases and one under empty EXEC, v255 as either base and +/// under empty EXEC, then as for 16-bit: 9 float, 9 special, a float under +/// empty EXEC, SDWA/DPP 4, `_e64`, `v_cmpx`, vccz over a scalar instruction. +const REJECTIONS64: usize = 7 + 2 + 2 + 2 + 3 + 3 + 9 + 9 + 1 + 4 + 1 + 1 + 1; + +const LITERAL: u32 = 255; +const VCC_LO: u32 = 106; +const EXEC_LO: u32 = 126; + +/// ENCODING 0b0111110 in bits 25-31, OP 17-24, VSRC1 9-16, SRC0 0-8 (guide +/// 13.3.3); SRC0 256 + n is VGPR n. +fn vopc(op: u32, src0: u32, vsrc1: u32) -> u32 { + 0b011_1110 << 25 | op << 17 | vsrc1 << 9 | src0 +} + +const fn vgpr(n: u32) -> u32 { + 256 + n +} + +/// `v_mov_b32 v, literal`. +fn vmov(reg: u32, value: u32) -> [u32; 2] { + [0x7e00_02ff | reg << 17, value] +} + +/// `v_mov_b32 v, v`. +fn vcopy(dst: u32, src: u32) -> u32 { + 0x7e00_0200 | dst << 17 | vgpr(src) +} + +/// `s_mov_b32 s, literal`. +fn smov(reg: u32, value: u32) -> [u32; 2] { + [0xbe80_03ff | reg << 16, value] +} + +/// v[reg:reg+1] = `value` (low dword in v[reg]). +fn vmov64(reg: u32, value: u64) -> Vec { + let mut code = vmov(reg, value as u32).to_vec(); + code.extend(vmov(reg + 1, (value >> 32) as u32)); + code +} + +/// s[reg:reg+1] = `value` (low dword in s[reg]). +fn smov64(reg: u32, value: u64) -> Vec { + let mut code = smov(reg, value as u32).to_vec(); + code.extend(smov(reg + 1, (value >> 32) as u32)); + code +} + +/// v[4:5] = (v1, `high`): the lane input as the low dword. +fn lane_pair(high: u32) -> Vec { + let mut code = vec![vcopy(4, 1)]; + code.extend(vmov(5, high)); + code +} + +const fn join(lo: u32, hi: u32) -> u64 { + lo as u64 | (hi as u64) << 32 +} + +const FULL_EXEC: u32 = 0xbefe_03c1; +const READ_VCC: u32 = 0x7e04_026a; +/// `v_cmp_lt_u32 vcc_lo, v1, v3`. +const LT_U32_V1_V3: u32 = 0x7d82_0701; +const VCCZ_OVER_ONE: u32 = 0xbf86_0001; +/// `v_xor_b32 v2, v1, v2`. +const XOR_V2_V1: u32 = 29 << 25 | 2 << 17 | 2 << 9 | (256 + 1); +/// `s_mov_b32 s8, 0`. +const SMOV_S8_0: u32 = 0xbe88_0380; + +const PREFIX: [u32; 3] = [0xe030_2000, 0x8000_0100, 0xbf8c_3f70]; +const STORE_V2: [u32; 4] = [0xe070_2000, 0x8001_0200, 0xbbfd_0000, 0xbf81_0000]; + +/// Load v1 = lane input, `setup`, EXEC_LO = `exec` (unless `None`), the compare +/// words, EXEC_LO = -1 again, v2 = vcc_lo, `tail`, then store v2. Returns the +/// program and the compare's byte offset. +fn program(setup: &[u32], exec: Option, compare: &[u32], tail: &[u32]) -> (Vec, usize) { + let mut code = PREFIX.to_vec(); + code.extend(setup); + if let Some(exec) = exec { + code.extend(smov(EXEC_LO, exec)); + } + let offset = 4 * code.len(); + code.extend(compare); + if exec.is_some() { + code.push(FULL_EXEC); + } + code.push(READ_VCC); + code.extend(tail); + code.extend(STORE_V2); + (words(&code), offset) +} + +fn code(setup: &[u32], exec: Option, compare: &[u32]) -> Vec { + program(setup, exec, compare, &[]).0 +} + +fn present(input: &[u32], n: usize) -> u32 { + let in_wave = (input.len() - n / 32 * 32).min(32); + if in_wave == 32 { + u32::MAX + } else { + (1 << in_wave) - 1 + } +} + +fn case( + form: Form, + family: &'static str, + label: String, + code: Vec, + input: Vec, + lane: impl Fn(usize, u32, u32) -> u32, +) -> Case { + let expected = input + .iter() + .enumerate() + .map(|(n, &x)| lane(n, x, present(&input, n))) + .collect(); + Case { + form: form.name, + family, + label: format!("{} {label}", form.name), + code, + input, + expected, + } +} + +/// Table 20: the integer of selectors 128-208. +fn inline_integer(selector: u32) -> i64 { + match selector { + 128..=192 => i64::from(selector) - 128, + 193..=208 => 192 - i64::from(selector), + _ => unreachable!("not an integer inline constant"), + } +} + +/// The inline integer as register bits at the form's width: two's complement +/// over 64 bits (16-bit forms then use bits 15:0). +fn inline_bits(selector: u32) -> u64 { + inline_integer(selector) as u64 +} + +fn full(form: Form, operands: impl Fn(usize) -> (u64, u64)) -> impl Fn(usize, u32, u32) -> u32 { + move |_, _, present| vcc(form, u32::MAX, present, &operands) +} + +fn v1(n: usize) -> u64 { + u64::from(LANES[n % 32]) +} + +fn cases16(form: Form, cases: &mut Vec) { + let op = form.op; + let mut push = |family, label: String, code, lane: Box u32>| { + cases.push(case(form, family, label, code, LANES.to_vec(), lane)); + }; + for b in B16 { + let setup = vmov(3, b); + let f = full(form, move |n| (v1(n), u64::from(b))); + push( + "vopc16 domain", + format!("v1, {b:#x}"), + code(&setup, None, &[vopc(op, vgpr(1), 3)]), + Box::new(f), + ); + let f = full(form, move |n| (u64::from(b), v1(n))); + push( + "vopc16 domain", + format!("{b:#x}, v1"), + code(&setup, None, &[vopc(op, vgpr(3), 1)]), + Box::new(f), + ); + } + let b = 0x7654_8000; + let f = full(form, |n| (v1(n), v1(n))); + push( + "vopc16 aliasing", + "v1, v1".into(), + code(&vmov(3, b), None, &[vopc(op, vgpr(1), 1)]), + Box::new(f), + ); + let f = full(form, move |_| (u64::from(b), u64::from(b))); + push( + "vopc16 aliasing", + "v3, v3".into(), + code(&vmov(3, b), None, &[vopc(op, vgpr(3), 3)]), + Box::new(f), + ); + for selector in 128..=208 { + let value = inline_bits(selector); + let f = full(form, move |n| (value, v1(n))); + let label = format!("inline {selector} ({}), v1", inline_integer(selector)); + push( + "vopc16 inline integer", + label, + code(&[], None, &[vopc(op, selector, 1)]), + Box::new(f), + ); + } + for reg in [8, 105] { + for value in SGPR16 { + let f = full(form, move |n| (u64::from(value), v1(n))); + let label = format!("s{reg} = {value:#x}, v1"); + push( + "vopc16 sgpr source", + label, + code(&smov(reg, value), None, &[vopc(op, reg, 1)]), + Box::new(f), + ); + } + } + for value in SGPR16 { + let f = full(form, move |n| (u64::from(value), v1(n))); + let label = format!("vcc_lo = {value:#x}, v1"); + let compare = [vopc(op, VCC_LO, 1)]; + push( + "vopc16 vcc_lo source", + label, + code(&smov(VCC_LO, value), None, &compare), + Box::new(f), + ); + } + // Lane-derived VCC_LO from v_cmp_lt_u32 v1, v3 (v3 = 0x80000000). + let mut setup = vmov(3, 0x8000_0000).to_vec(); + setup.push(LT_U32_V1_V3); + let old = (0..32) + .filter(|&n| v1(n) < 0x8000_0000) + .fold(0u64, |mask, n| mask | 1 << n); + let f = full(form, move |n| (old, v1(n))); + let label = "vcc_lo lane-derived, v1".into(); + push( + "vopc16 vcc_lo source", + label, + code(&setup, None, &[vopc(op, VCC_LO, 1)]), + Box::new(f), + ); + let b = 0x0bad_8000; + for (src0, vsrc1, label) in [ + (vgpr(255), 1, "v255, v1"), + (vgpr(1), 255, "v1, v255"), + (vgpr(255), 255, "v255, v255"), + ] { + let f = full(form, move |n| { + let pick = |is_v255: bool| if is_v255 { u64::from(b) } else { v1(n) }; + (pick(src0 == vgpr(255)), pick(vsrc1 == 255)) + }); + push( + "vopc16 max register", + label.into(), + code(&vmov(255, b), None, &[vopc(op, src0, vsrc1)]), + Box::new(f), + ); + } + let b = 0xf00f_0005; + let mut setup = vmov(3, b).to_vec(); + setup.extend(smov(VCC_LO, STALE_VCC)); + for exec in EXEC_MASKS { + let label = format!("exec {exec:#x}, stale vcc_lo, v1, {b:#x}"); + let f = move |_, _, p| vcc(form, exec, p, |n| (v1(n), u64::from(b))); + push( + "vopc16 exec", + label, + code(&setup, Some(exec), &[vopc(op, vgpr(1), 3)]), + Box::new(f), + ); + } + for (src0, vsrc1, label) in [ + (20, 1, "s20 (unknown), v1"), + (VCC_LO, 1, "vcc_lo (uninitialized), v1"), + (vgpr(9), 9, "v9, v9 (uninitialized)"), + ] { + let f = |_, _, _| 0; + push( + "vopc16 empty exec reads nothing", + format!("exec 0, {label}"), + code(&[], Some(0), &[vopc(op, src0, vsrc1)]), + Box::new(f), + ); + } + common_tail(form, cases, &vmov(3, 5), vgpr(1), 3, 5, "vopc16"); +} + +fn cases64(form: Form, cases: &mut Vec) { + let op = form.op; + let mut push = |family, label: String, code, lane: Box u32>| { + cases.push(case(form, family, label, code, LANES.to_vec(), lane)); + }; + // v[4:5] = (v1, high), v[6:7] = b. + for high in HIGHS { + for b in B64 { + let mut setup = lane_pair(high); + setup.extend(vmov64(6, b)); + let pair = move |n: usize| join(LANES[n % 32], high); + let f = full(form, move |n| (pair(n), b)); + let label = format!("v[4:5] = (v1, {high:#x}), {b:#x}"); + push( + "vopc64 domain", + label, + code(&setup, None, &[vopc(op, vgpr(4), 6)]), + Box::new(f), + ); + let f = full(form, move |n| (b, pair(n))); + let label = format!("{b:#x}, v[4:5] = (v1, {high:#x})"); + push( + "vopc64 domain", + label, + code(&setup, None, &[vopc(op, vgpr(6), 4)]), + Box::new(f), + ); + } + } + // v[4:5] = (v1, v1): the high dword differs per lane. + for b in B64 { + let mut setup = vec![vcopy(4, 1), vcopy(5, 1)]; + setup.extend(vmov64(6, b)); + let pair = |n: usize| join(LANES[n % 32], LANES[n % 32]); + let f = full(form, move |n| (pair(n), b)); + let family = "vopc64 lane-varying high dword"; + push( + family, + format!("(v1, v1), {b:#x}"), + code(&setup, None, &[vopc(op, vgpr(4), 6)]), + Box::new(f), + ); + let f = full(form, move |n| (b, pair(n))); + push( + family, + format!("{b:#x}, (v1, v1)"), + code(&setup, None, &[vopc(op, vgpr(6), 4)]), + Box::new(f), + ); + } + // Aliasing: the same pair twice, and the overlapping pairs v[4:5], v[5:6]. + let (high, c) = (0x8000_0000, 0x7fff_ffff); + let mut setup = lane_pair(high); + setup.extend(vmov(6, c)); + setup.extend(vmov(7, 0x8000_0000)); + let lo4 = move |n: usize| join(LANES[n % 32], high); + let lo5 = join(high, c); + let lo6 = join(c, 0x8000_0000); + for (src0, vsrc1, label, operands) in [ + ( + 4, + 4, + "v[4:5], v[4:5]", + Box::new(move |n| (lo4(n), lo4(n))) as Box (u64, u64)>, + ), + (6, 6, "v[6:7], v[6:7]", Box::new(move |_| (lo6, lo6))), + (4, 5, "v[4:5], v[5:6]", Box::new(move |n| (lo4(n), lo5))), + (5, 4, "v[5:6], v[4:5]", Box::new(move |n| (lo5, lo4(n)))), + ] { + let f = full(form, operands); + push( + "vopc64 aliasing", + label.into(), + code(&setup, None, &[vopc(op, vgpr(src0), vsrc1)]), + Box::new(f), + ); + } + // Inline integers against v[4:5] = (v1, 0) and (v1, 0xffffffff). + for high in [0, 0xffff_ffff] { + for selector in 128..=208 { + let value = inline_bits(selector); + let pair = move |n: usize| join(LANES[n % 32], high); + let f = full(form, move |n| (value, pair(n))); + let label = format!( + "inline {selector} ({}), (v1, {high:#x})", + inline_integer(selector) + ); + let compare = [vopc(op, selector, 4)]; + push( + "vopc64 inline integer", + label, + code(&lane_pair(high), None, &compare), + Box::new(f), + ); + } + } + // SGPR pairs s[8:9] and s[104:105] against v[4:5] = (v1, 0x80000000). + for reg in [8, 104] { + for value in SGPR64 { + let mut setup = lane_pair(0x8000_0000); + setup.extend(smov64(reg, value)); + let f = full(form, move |n| (value, join(LANES[n % 32], 0x8000_0000))); + let label = format!("s[{reg}:{}] = {value:#x}, (v1, 0x80000000)", reg + 1); + push( + "vopc64 sgpr pair source", + label, + code(&setup, None, &[vopc(op, reg, 4)]), + Box::new(f), + ); + } + } + // v[254:255], the highest pair. + let b = 0x8000_0000_0000_0005; + let mut setup = lane_pair(0); + setup.extend(vmov64(254, b)); + for (src0, vsrc1, label) in [ + (254, 4, "v[254:255], (v1, 0)"), + (4, 254, "(v1, 0), v[254:255]"), + (254, 254, "v[254:255], v[254:255]"), + ] { + let f = full(form, move |n| { + let pick = |top: bool| if top { b } else { v1(n) }; + (pick(src0 == 254), pick(vsrc1 == 254)) + }); + push( + "vopc64 max register", + label.into(), + code(&setup, None, &[vopc(op, vgpr(src0), vsrc1)]), + Box::new(f), + ); + } + let b = 0x0000_0000_0f0f_0f0f; + let mut setup = lane_pair(0); + setup.extend(vmov64(6, b)); + setup.extend(smov(VCC_LO, STALE_VCC)); + for exec in EXEC_MASKS { + let label = format!("exec {exec:#x}, stale vcc_lo, (v1, 0), {b:#x}"); + let f = move |_, _, p| vcc(form, exec, p, |n| (v1(n), b)); + push( + "vopc64 exec", + label, + code(&setup, Some(exec), &[vopc(op, vgpr(4), 6)]), + Box::new(f), + ); + } + for (src0, vsrc1, label) in [ + (20, 4, "s[20:21] (unknown), v[4:5] (unknown)"), + (vgpr(9), 9, "v[9:10], v[9:10] (uninitialized)"), + (vgpr(1), 1, "v[1:2], v[1:2] (v2 uninitialized)"), + ] { + let f = |_, _, _| 0; + push( + "vopc64 empty exec reads nothing", + format!("exec 0, {label}"), + code(&[], Some(0), &[vopc(op, src0, vsrc1)]), + Box::new(f), + ); + } + // b = 5 in v[6:7]; the lane operand is (v1, 0). + let mut setup = lane_pair(0); + setup.extend(vmov64(6, 5)); + common_tail(form, cases, &setup, vgpr(4), 6, 5, "vopc64"); +} + +/// Partial waves, vccz branches and EXEC preservation: SRC0 `src0` holds the +/// lane input (with a zero high dword for 64-bit forms), VSRC1 `vsrc1` holds `b`. +fn common_tail( + form: Form, + cases: &mut Vec, + setup: &[u32], + src0: u32, + vsrc1: u32, + b: u64, + width: &str, +) { + let op = form.op; + let family = + |name: &str| -> &'static str { Box::leak(format!("{width} {name}").into_boxed_str()) }; + let long: Vec = LANES.iter().chain(&LANES[..13]).copied().collect(); + let short = LANES[..7].to_vec(); + for (exec, input, label) in [ + (u32::MAX, short, "7 lanes"), + (u32::MAX, long.clone(), "45 lanes"), + (0x5555_5555, long, "45 lanes, exec 0x55555555"), + ] { + let exec_word = (exec != u32::MAX).then_some(exec); + let copy = input.clone(); + let code = code(setup, exec_word, &[vopc(op, src0, vsrc1)]); + cases.push(case( + form, + family("partial wave"), + label.into(), + code, + input, + move |n, _, p| vcc(form, exec, p, |k| (u64::from(copy[n / 32 * 32 + k]), b)), + )); + } + for exec in [u32::MAX, 0x0000_8000, 0] { + let (code, _) = program( + setup, + Some(exec), + &[vopc(op, src0, vsrc1)], + &[VCCZ_OVER_ONE, XOR_V2_V1], + ); + let label = format!("exec {exec:#x}, then s_cbranch_vccz"); + cases.push(case( + form, + family("vccz branch"), + label, + code, + LANES.to_vec(), + move |_, x, p| { + let mask = vcc(form, exec, p, |n| (v1(n), b)); + if mask == 0 { 0 } else { mask ^ x } + }, + )); + } + let (code, _) = program( + setup, + Some(0), + &[vopc(op, src0, vsrc1)], + &[VCCZ_OVER_ONE, SMOV_S8_0], + ); + let label = "exec 0, then s_cbranch_vccz over s_mov".into(); + cases.push(case( + form, + family("vccz branch"), + label, + code, + LANES.to_vec(), + |_, _, _| 0, + )); + for exec in [0x5555_5555, 0x8000_0001] { + let mut code = PREFIX.to_vec(); + code.extend(setup); + code.extend(vmov(9, OLD)); + code.push(29 << 25 | 2 << 17 | 1 << 9 | vgpr(9)); // v_xor_b32 v2, v9, v1 + code.extend(smov(EXEC_LO, exec)); + code.extend([vopc(op, src0, vsrc1), READ_VCC, FULL_EXEC]); + code.extend(STORE_V2); + let label = format!("exec {exec:#x}, v2 = vcc_lo before EXEC is restored"); + cases.push(case( + form, + family("exec unchanged"), + label, + words(&code), + LANES.to_vec(), + move |n, x, p| { + if exec >> (n % 32) & 1 == 1 { + vcc(form, exec, p, |k| (v1(k), b)) + } else { + x ^ OLD + } + }, + )); + } +} + +fn cases() -> Vec { + let mut cases = Vec::new(); + for form in forms() { + if form.bits == 16 { + cases16(form, &mut cases); + } else { + cases64(form, &mut cases); + } + } + cases +} + +/// Programs both interpreters must refuse, with the refusing byte offset. +fn rejections() -> Vec<(String, Vec, usize)> { + let mut all = Vec::new(); + for form in forms() { + let op = form.op; + let wide = form.bits == 64; + let mut reject = |label: String, (code, offset): (Vec, usize)| { + all.push((format!("{} {label}", form.name), code, offset)); + }; + // v3 (16-bit) or v[4:5] = (v1, 7) (64-bit) is the initialized VSRC1. + let (setup, other) = if wide { + (lane_pair(7), 4) + } else { + (vmov(3, 7).to_vec(), 3) + }; + let with = |extra: &[u32]| { + let mut code = setup.clone(); + code.extend(extra); + code + }; + if wide { + // Pair halves not initialized under nonzero EXEC. + for (extra, src0, vsrc1, label) in [ + (vec![], 20, other, "s[20:21] unknown"), + (smov(8, 1).to_vec(), 8, other, "s9 unknown in s[8:9]"), + (smov(9, 1).to_vec(), 8, other, "s8 unknown in s[8:9]"), + (vec![], vgpr(1), other, "v2 uninitialized in SRC0 v[1:2]"), + (vec![], vgpr(4), 1, "v2 uninitialized in VSRC1 v[1:2]"), + ( + vmov(8, 1).to_vec(), + vgpr(8), + other, + "v9 uninitialized in v[8:9]", + ), + (vec![], vgpr(9), other, "v[9:10] uninitialized"), + ] { + reject( + label.into(), + program(&with(&extra), None, &[vopc(op, src0, vsrc1)], &[]), + ); + } + } else { + for (extra, src0, vsrc1, label) in [ + (vec![], 20, other, "s20 unknown"), + (vec![], VCC_LO, other, "vcc_lo uninitialized"), + (vec![], vgpr(9), other, "v9 uninitialized as SRC0"), + (vec![], vgpr(1), 9, "v9 uninitialized as VSRC1"), + ] { + reject( + label.into(), + program(&with(&extra), None, &[vopc(op, src0, vsrc1)], &[]), + ); + } + } + // A load outstanding on v3 (16-bit) or on v5, the high half of + // v[4:5] (64-bit), in each slot. + let (target, partner) = if wide { (5, 6) } else { (3, 1) }; + let base = if wide { 4 } else { 3 }; + for slot in ["SRC0", "VSRC1"] { + let mut code = PREFIX.to_vec(); + code.extend(&setup); + if wide { + code.extend(vmov64(6, 9)); + } + code.extend([0xe030_2000, 0x8000_0000 | target << 8]); + let offset = 4 * code.len(); + code.push(if slot == "SRC0" { + vopc(op, vgpr(base), partner) + } else { + vopc(op, vgpr(partner), base) + }); + code.extend([READ_VCC, 0xbf8c_3f70]); + code.extend(STORE_V2); + reject( + format!("v{target} load outstanding in {slot}"), + (words(&code), offset), + ); + } + if wide { + reject( + "vcc as SRC0".into(), + program( + &with(&smov(VCC_LO, 1)), + None, + &[vopc(op, VCC_LO, other)], + &[], + ), + ); + reject( + "vcc as SRC0 under empty exec".into(), + program(&setup, Some(0), &[vopc(op, VCC_LO, other)], &[]), + ); + } + // A literal, with its dword and without one. + reject( + "literal".into(), + program(&setup, None, &[vopc(op, LITERAL, other), 5], &[]), + ); + let mut code = PREFIX.to_vec(); + code.extend(&setup); + let offset = 4 * code.len(); + code.push(vopc(op, LITERAL, other)); + reject("missing literal".into(), (words(&code), offset)); + if wide { + for reg in [9, 105] { + let mut extra = smov(reg, 1).to_vec(); + if reg < 105 { + extra.extend(smov(reg + 1, 1)); + } + reject( + format!("odd SGPR base s{reg}"), + program(&with(&extra), None, &[vopc(op, reg, other)], &[]), + ); + } + reject( + "odd SGPR base s9 under empty exec".into(), + program(&setup, Some(0), &[vopc(op, 9, other)], &[]), + ); + let top = with(&vmov(255, 1)); + reject( + "v255 as SRC0 base".into(), + program(&top, None, &[vopc(op, vgpr(255), other)], &[]), + ); + reject( + "v255 as VSRC1 base".into(), + program(&top, None, &[vopc(op, vgpr(other), 255)], &[]), + ); + reject( + "v255 as a base under empty exec".into(), + program(&top, Some(0), &[vopc(op, vgpr(255), other)], &[]), + ); + } + for src0 in (240..=248).chain([107, 124, 125, 126, 127, 251, 252, 253, 254]) { + reject( + format!("src0 {src0}"), + program(&setup, None, &[vopc(op, src0, other)], &[]), + ); + } + reject( + "src0 240 under empty exec".into(), + program(&setup, Some(0), &[vopc(op, 240, other)], &[]), + ); + for (src0, extension) in [ + (249, 0x0606_0601), + (250, 0xff00_e401), + (233, 0xfac6_8801), + (234, 0xfac6_8801), + ] { + let compare = [vopc(op, src0, other), extension]; + reject( + format!("src0 {src0} extension"), + program(&setup, None, &compare, &[]), + ); + } + // VOP3 `_e64`: v_cmp vcc_lo, v1 (or v[4:5]), VSRC1. + let src0 = if wide { vgpr(4) } else { vgpr(1) }; + let vop3 = [0xd400_0000 | op << 16 | VCC_LO, src0 | vgpr(other) << 9]; + reject("_e64".into(), program(&setup, None, &vop3, &[])); + // The matching v_cmpx (Table 61: opcode + 16). + let label = format!("v_cmpx opcode {}", op + 16); + reject( + label, + program(&setup, None, &[vopc(op + 16, src0, other)], &[]), + ); + // Lane-derived result, then a vccz branch over `s_mov_b32 s8, 0`. + let (code, at) = program( + &setup, + None, + &[vopc(op, src0, other)], + &[VCCZ_OVER_ONE, SMOV_S8_0], + ); + reject("lane-derived vccz over s_mov".into(), (code, at + 12)); + } + all +} + +fn assert_rejected_at(label: &str, code: &[u8], offset: usize, input: &[u32]) { + match xenora_ref::execute(code, input) { + Ok(_) => panic!("reference accepted {label}"), + Err(error) => assert_eq!( + error.offset, offset, + "reference rejected {label} elsewhere: {error:?}" + ), + } + match independent_ref::execute(code, input) { + Ok(_) => panic!("independent accepted {label}"), + Err(error) => assert!( + error.starts_with(&format!("{offset:#x}:")), + "independent rejected {label} elsewhere: {error}" + ), + } +} + +fn tally(cases: &[Case], key: fn(&Case) -> &'static str) -> BTreeMap<&'static str, usize> { + let mut counts = BTreeMap::new(); + for case in cases { + *counts.entry(key(case)).or_default() += 1; + } + counts +} + +fn failures( + cases: &[Case], + execute: impl Fn(&[u8], &[u32]) -> Result, String>, +) -> Vec { + let mut failed: BTreeMap<&str, (usize, String)> = BTreeMap::new(); + for case in cases { + let output = execute(&case.code, &case.input); + if output.as_deref().ok() != Some(case.expected.as_slice()) { + let first = format!("{}: {output:x?}, expected {:x?}", case.label, case.expected); + failed.entry(case.family).or_insert((0, first)).0 += 1; + } + } + failed + .into_iter() + .map(|(family, (n, first))| format!(" {family}: {n} failed, first: {first}")) + .collect() +} + +fn checked_cases() -> Vec { + let cases = cases(); + assert_eq!(tally(&cases, |case| case.family), BTreeMap::from(COUNTS)); + assert_eq!(tally(&cases, |case| case.form).len(), 28); + cases +} + +fn form(name: &str) -> Form { + forms().into_iter().find(|form| form.name == name).unwrap() +} + +#[test] +fn expectations_follow_the_guide_examples() { + // Worked by hand from Tables 59-61, 3.6.3 and Table 20. + for (name, s0, s1, expected) in [ + // 16-bit: bits 31:16 are ignored; 0x8000 is -32768 signed, 32768 unsigned. + ("v_cmp_eq_i16", 0xdead_0001, 0x0000_0001, true), + ("v_cmp_eq_u16", 0xffff_0000, 0x0000_0000, true), + ("v_cmp_lt_i16", 0x0000_8000, 0x0000_7fff, true), + ("v_cmp_lt_u16", 0x0000_8000, 0x0000_7fff, false), + ("v_cmp_gt_i16", 0x0001_ffff, 0x0000_0000, false), // -1 > 0 + ("v_cmp_ge_u16", 0x0000_ffff, 0x7fff_0000, true), + ("v_cmp_ne_u16", 0x1234_5678, 0x8765_5678, false), + ("v_cmp_le_i16", 0x0000_ffff, 0xffff_ffff, true), + // 64-bit: {hi, lo}; equal low dword with different high dword. + ( + "v_cmp_eq_u64", + 0x0000_0001_0000_0005, + 0x0000_0000_0000_0005, + false, + ), + ( + "v_cmp_lt_i64", + 0x8000_0000_0000_0000, + 0x7fff_ffff_ffff_ffff, + true, + ), + ( + "v_cmp_lt_u64", + 0x8000_0000_0000_0000, + 0x7fff_ffff_ffff_ffff, + false, + ), + ("v_cmp_gt_i64", u64::MAX, 0, false), // -1 > 0 + ("v_cmp_gt_u64", u64::MAX, 0, true), + ( + "v_cmp_le_u64", + 0x0000_0001_0000_0000, + 0x0000_0000_ffff_ffff, + false, + ), + ( + "v_cmp_ge_i64", + 0xffff_ffff_0000_0000, + 0xffff_fffe_ffff_ffff, + true, + ), + ("v_cmp_f_i64", 0, 0, false), + ("v_cmp_t_u64", 0, 1, true), + ] { + assert_eq!(test(form(name), s0, s1), expected, "{name} {s0:#x} {s1:#x}"); + } + // Table 20 at each width: selector 193 (-1) is 0xffff at 16 bits and + // u64::MAX at 64 bits; 192 is 64. + assert!(test(form("v_cmp_eq_u16"), inline_bits(193), 0xffff)); + assert!(test(form("v_cmp_eq_u64"), inline_bits(193), u64::MAX)); + assert!(test( + form("v_cmp_eq_i64"), + inline_bits(208), + (-16_i64) as u64 + )); + assert!(test(form("v_cmp_eq_u64"), inline_bits(192), 64)); + // Section 3.9: inactive and absent lanes give 0. + let lt = form("v_cmp_lt_u16"); + assert_eq!(vcc(lt, 0x0000_00f0, 0x1ff, |_| (0, 1)), 0xf0); + assert_eq!(vcc(lt, u32::MAX, 0x7f, |_| (0, 1)), 0x7f); + assert_eq!(vcc(lt, 0, u32::MAX, |_| (0, 1)), 0); +} + +#[test] +fn vopc_wide_match_the_guide_on_the_reference() { + let cases = checked_cases(); + let failed = failures(&cases, |code, input| { + xenora_ref::execute(code, input).map_err(|error| format!("{error:?}")) + }); + assert!( + failed.is_empty(), + "reference disagrees with the guide:\n{}", + failed.join("\n") + ); + eprintln!("vopc_wide: {} reference cases", cases.len()); +} + +#[test] +fn vopc_wide_match_the_guide_on_the_independent_interpreter() { + let cases = checked_cases(); + let failed = failures(&cases, independent_ref::execute); + assert!( + failed.is_empty(), + "independent interpreter disagrees with the guide:\n{}", + failed.join("\n") + ); + eprintln!("vopc_wide: {} independent-interpreter cases", cases.len()); +} + +#[test] +fn vopc_wide_outside_the_contract_are_rejected() { + let rejections = rejections(); + assert_eq!(rejections.len(), F16 * REJECTIONS16 + F64 * REJECTIONS64); + for (label, code, offset) in &rejections { + assert_rejected_at(label, code, *offset, &LANES); + } + eprintln!("vopc_wide: {} rejections", rejections.len()); +} + +#[test] +fn neighbouring_compares_stay_unsupported() { + // Table 61 neighbours of the 28 forms: 136 V_CMP_CLASS_F32, 143 + // V_CMP_CLASS_F16, 168 V_CMP_CLASS_F64, 175 (unlisted), the v_cmpx 16-bit + // and 64-bit groups (153, 158, 176, 183, 185, 190, 240, 247) and 232 + // V_CMP_U_F16. + let mut setup = lane_pair(7); + setup.extend(vmov(3, 7)); + for op in [ + 136, 143, 168, 175, 153, 158, 176, 183, 185, 190, 232, 240, 247, + ] { + let (code, offset) = program(&setup, None, &[vopc(op, vgpr(4), 3)], &[]); + assert_rejected_at(&format!("opcode {op}"), &code, offset, &LANES); + } +} + +#[test] +#[ignore = "requires a native Metal device with API and shader validation, and the translator's 16- and 64-bit forms"] +fn vopc_wide_match_the_guide_on_native_metal() { + assert_eq!(std::env::var("MTL_DEBUG_LAYER").as_deref(), Ok("1")); + assert_eq!(std::env::var("MTL_SHADER_VALIDATION").as_deref(), Ok("1")); + for (label, code, offset) in rejections() { + let at = match compile(Architecture::Gfx1030, &code) { + Err(CompileError::Decode(error)) => error.offset, + Err(CompileError::Verify { offset, .. }) => offset, + other => panic!("compiler did not reject {label}: {other:?}"), + }; + assert_eq!(at, offset, "compiler rejected {label} elsewhere"); + } + let cases = checked_cases(); + let (failures, programs) = harness::check_metal(&cases); + failures.assert_none("native Metal disagrees with the guide"); + eprintln!( + "vopc_wide: {} Metal cases in {programs} programs", + cases.len() + ); +} From 01a84454125f9455e1ca7adc29e00711dc9beaed Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:10:26 -0600 Subject: [PATCH 2/7] test(vopc): check pair halves with no active lane; tighten guide notes (#215) A reference that checked only the low register of a 64-bit pair still passed every case, because an executing lane reads both halves anyway. The difference shows only when EXEC_LO enables just absent lanes, so add that rejection per 64-bit form. The guide-only cross-check also pointed out that odd SGPR bases are illegal per 3.6.3 (not unsettled), that the negative inline bit patterns are a reading of Table 20, and that "empty EXEC reads nothing" is the contract's rule; say so in the module notes. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- tests/validation/tests/vopc_wide.rs | 46 +++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/tests/validation/tests/vopc_wide.rs b/tests/validation/tests/vopc_wide.rs index a46c079..283e50a 100644 --- a/tests/validation/tests/vopc_wide.rs +++ b/tests/validation/tests/vopc_wide.rs @@ -29,16 +29,24 @@ //! rule and 9.1 says for 64-bit addresses "ADDR has the LSBs, and ADDR+1 has //! the MSBs", read here as the general VGPR pair order. `_I64` is two's //! complement over all 64 bits, `_U64` unsigned. -//! - Table 20: selectors 128-192 are the integers 0..64 and 193-208 are -1..-16; -//! they are values, so at 16 bits -1 is 0xffff and at 64 bits it is -//! 0xffff_ffff_ffff_ffff (sign extended, also for `_U64`). The guide's -//! zero-extension rule for `_U64` (6.2.1) is about 32-bit literals, which -//! the subset rejects. +//! - Table 20: selectors 128-192 are the integers 0..64 and 193-208 are -1..-16. +//! Read as values at the operand width, -1 is 0xffff at 16 bits and +//! 0xffff_ffff_ffff_ffff at 64 bits (sign extended, also for `_U64`). The +//! guide lists the values, not their bit patterns, so the negative patterns +//! (above all for `_U64`) are a reading, not a quotation. 6.2.1's +//! zero-extension rule for unsigned 64-bit operands is headed "Literal +//! Expansion to 64 bits" and covers literals only. //! - Section 3.9: `VCC[n] = EXEC[n] & (test passed for thread[n])`, "VCC is -//! fully written"; in wave32 only VCC_LO contributes to VCCZ. -//! - Not settled by the guide, so rejected (contract subset): literals for both -//! widths, VCC as a 64-bit source, odd SGPR bases, v255 as a pair base, and -//! any pair half that is not initialized under nonzero EXEC. +//! fully written"; in wave32 only VCC_LO contributes to VCCZ. Lanes past the +//! end of a partial final wave do not exist and give 0 (contract section 3). +//! - Odd SGPR bases are illegal for 64-bit data (3.6.3 "Even-aligned SGPRs are +//! required"), so they are rejected. Literals are rejected by the contract +//! subset: for 16-bit forms the guide does not say which literal bits are +//! used; for 64-bit forms 6.2.1 defines an expansion, which the subset leaves +//! out. VCC as a 64-bit source and v255 as a pair base (v[255:256] does not +//! exist) are also outside the subset. +//! - Under EXEC_LO = 0 the result is 0 (3.9); that no source is read then is +//! the contract's rule (section 5), not a guide statement. #[path = "operand_domains/harness.rs"] #[allow(dead_code)] @@ -1133,6 +1141,26 @@ fn vopc_wide_outside_the_contract_are_rejected() { eprintln!("vopc_wide: {} rejections", rejections.len()); } +#[test] +fn pair_halves_are_checked_without_active_lanes() { + // Contract section 5: with EXEC_LO != 0 every source register is checked + // on every existing lane, even when no existing lane is active. Here the + // wave has 7 lanes and EXEC_LO = 0xffff_ff80 enables only absent lanes; + // the high half v2 of v[1:2] is uninitialized in each slot. + let input = &LANES[..7]; + let mut n = 0; + for form in forms().into_iter().filter(|form| form.bits == 64) { + for (src0, vsrc1, slot) in [(vgpr(1), 4, "SRC0"), (vgpr(4), 1, "VSRC1")] { + let compare = [vopc(form.op, src0, vsrc1)]; + let (code, offset) = program(&lane_pair(7), Some(0xffff_ff80), &compare, &[]); + let label = format!("{} v2 uninitialized in {slot}, no active lane", form.name); + assert_rejected_at(&label, &code, offset, input); + n += 1; + } + } + assert_eq!(n, F64 * 2); +} + #[test] fn neighbouring_compares_stay_unsupported() { // Table 61 neighbours of the 28 forms: 136 V_CMP_CLASS_F32, 143 From a3599560333305b815c9a9560fafba1024dff9d5 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:11:54 -0600 Subject: [PATCH 3/7] refactor(ref): keep Op::Compare for 32-bit forms, add Op::WideCompare (#215) The decoder's gfx1030 test builds Op::Compare from its own decode, so adding a width argument broke that crate's test build. Keep the 32-bit variant as it was and carry 16- and 64-bit forms in a new variant that shares the same evaluation. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- crates/ref/src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/ref/src/lib.rs b/crates/ref/src/lib.rs index 8bfdfab..7b1750d 100644 --- a/crates/ref/src/lib.rs +++ b/crates/ref/src/lib.rs @@ -55,10 +55,12 @@ pub enum Op { ScalarUnary(ScalarUnary, u8, Source), /// VOP1 move of a normal SGPR or `vcc_lo` (106) into a VGPR. MoveScalar(u8, u8), - /// VOPC 16-, 32- or 64-bit integer compare writing the Wave32 mask to + /// VOPC 32-bit integer compare writing the Wave32 mask to `vcc_lo`. + Compare(Comparison, CompareSource, u8), + /// VOPC 16- or 64-bit integer compare writing the Wave32 mask to /// `vcc_lo`. For 64-bit forms a `Vector` or `Scalar` source and VSRC1 name /// the low register of a pair. - Compare(Comparison, CompareWidth, CompareSource, u8), + WideCompare(Comparison, CompareWidth, CompareSource, u8), /// SOP2 operation writing an SGPR and SCC. Scalar(ScalarAlu, u8, Source, Source), /// SOPC `s_cmp_eq_{i32,u32}` (opcodes 0 and 6, identical per guide 12.4). @@ -651,7 +653,12 @@ pub fn decode(code: &[u8]) -> Result, ReferenceError> { } let (src0, length) = decode_vector_source(*lo, *packed, tail, offset, "unsupported VOPC source")?; - (Op::Compare(comparison, width, src0, vsrc1), length) + let op = if width == CompareWidth::Word { + Op::Compare(comparison, src0, vsrc1) + } else { + Op::WideCompare(comparison, width, src0, vsrc1) + }; + (op, length) } // VOP2 integer forms (bit 31 = 0): OP bits 25-30, VDST 17-24, // VSRC1 9-16, SRC0 0-8. @@ -1166,7 +1173,16 @@ fn execute_wave(program: &[DecodedInstruction], input: &[u32]) -> Result { + Op::Compare(..) | Op::WideCompare(..) => { + let (comparison, width, src0, src1) = match op { + Op::Compare(comparison, src0, src1) => { + (comparison, CompareWidth::Word, src0, src1) + } + Op::WideCompare(comparison, width, src0, src1) => { + (comparison, width, src0, src1) + } + _ => unreachable!(), + }; let mut mask = 0u32; if exec != 0 { // The registers each source occupies: a pair for 64-bit forms. From 06d895ec23fa14543ad509dd439010f400f4eafd Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:14:28 -0600 Subject: [PATCH 4/7] test(vopc): close review gaps in the 64-bit oracle (#215) The fresh review found the independent interpreter accepted v255 as a VSRC1 pair base when SRC0 was an SGPR pair (and overflowed at run time), and that four checks could pass with a wrong implementation: overlapping and highest pairs decided by the high dword alone, a second wave equal to the first, and a known-zero vccz branch over an unread SGPR. Fix the interpreter, add both rejections, and make each case observable. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- tests/validation/independent_ref.rs | 2 +- tests/validation/tests/vopc_wide.rs | 46 +++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/tests/validation/independent_ref.rs b/tests/validation/independent_ref.rs index 545bbee..e7c6e78 100644 --- a/tests/validation/independent_ref.rs +++ b/tests/validation/independent_ref.rs @@ -1088,7 +1088,7 @@ fn decode_one(code: &[u8], offset: usize) -> Result<(Insn, usize), String> { let allowed = match (bits, src0) { (16 | 64, CmpSrc::Imm(_)) if len == 8 => false, (64, CmpSrc::Vcc) => false, - (64, CmpSrc::Sgpr(reg)) => reg % 2 == 0, + (64, CmpSrc::Sgpr(reg)) => reg % 2 == 0 && src1 != 255, (64, CmpSrc::Vgpr(reg)) => reg != 255 && src1 != 255, (64, _) => src1 != 255, _ => true, diff --git a/tests/validation/tests/vopc_wide.rs b/tests/validation/tests/vopc_wide.rs index 283e50a..e294b85 100644 --- a/tests/validation/tests/vopc_wide.rs +++ b/tests/validation/tests/vopc_wide.rs @@ -256,9 +256,9 @@ const REJECTIONS16: usize = 4 + 2 + 2 + 9 + 9 + 1 + 4 + 1 + 1 + 1; /// 64-bit per form: 7 uninitialized pair halves, 2 pending loads on a high /// half, vcc (known and under empty EXEC), a literal with and without its /// dword, 2 odd SGPR bases and one under empty EXEC, v255 as either base and -/// under empty EXEC, then as for 16-bit: 9 float, 9 special, a float under +/// under empty EXEC, v255 as VSRC1 after an SGPR pair (also under empty EXEC), then as for 16-bit: 9 float, 9 special, a float under /// empty EXEC, SDWA/DPP 4, `_e64`, `v_cmpx`, vccz over a scalar instruction. -const REJECTIONS64: usize = 7 + 2 + 2 + 2 + 3 + 3 + 9 + 9 + 1 + 4 + 1 + 1 + 1; +const REJECTIONS64: usize = 7 + 2 + 2 + 2 + 3 + 3 + 2 + 9 + 9 + 1 + 4 + 1 + 1 + 1; const LITERAL: u32 = 255; const VCC_LO: u32 = 106; @@ -321,8 +321,11 @@ const LT_U32_V1_V3: u32 = 0x7d82_0701; const VCCZ_OVER_ONE: u32 = 0xbf86_0001; /// `v_xor_b32 v2, v1, v2`. const XOR_V2_V1: u32 = 29 << 25 | 2 << 17 | 2 << 9 | (256 + 1); -/// `s_mov_b32 s8, 0`. +/// `s_mov_b32 s8, 0` and `s_mov_b32 s8, 1`. const SMOV_S8_0: u32 = 0xbe88_0380; +const SMOV_S8_1: u32 = 0xbe88_0381; +/// `v_mov_b32 v2, s8`. +const V2_FROM_S8: u32 = 0x7e04_0208; const PREFIX: [u32; 3] = [0xe030_2000, 0x8000_0100, 0xbf8c_3f70]; const STORE_V2: [u32; 4] = [0xe070_2000, 0x8001_0200, 0xbbfd_0000, 0xbf81_0000]; @@ -588,7 +591,8 @@ fn cases64(form: Form, cases: &mut Vec) { ); } // Aliasing: the same pair twice, and the overlapping pairs v[4:5], v[5:6]. - let (high, c) = (0x8000_0000, 0x7fff_ffff); + // v[5:6] has the same high dword as v[4:5], so the low dwords decide. + let (high, c) = (0x8000_0000, 0x8000_0000); let mut setup = lane_pair(high); setup.extend(vmov(6, c)); setup.extend(vmov(7, 0x8000_0000)); @@ -648,17 +652,18 @@ fn cases64(form: Form, cases: &mut Vec) { ); } } - // v[254:255], the highest pair. - let b = 0x8000_0000_0000_0005; - let mut setup = lane_pair(0); + // v[254:255], the highest pair; its high dword equals the lane pair's, + // so v254 decides, and reading v255 as anything but 1 changes the result. + let b = join(0x8000_0000, 1); + let mut setup = lane_pair(1); setup.extend(vmov64(254, b)); for (src0, vsrc1, label) in [ - (254, 4, "v[254:255], (v1, 0)"), - (4, 254, "(v1, 0), v[254:255]"), + (254, 4, "v[254:255], (v1, 1)"), + (4, 254, "(v1, 1), v[254:255]"), (254, 254, "v[254:255], v[254:255]"), ] { let f = full(form, move |n| { - let pick = |top: bool| if top { b } else { v1(n) }; + let pick = |top: bool| if top { b } else { join(LANES[n % 32], 1) }; (pick(src0 == 254), pick(vsrc1 == 254)) }); push( @@ -715,7 +720,12 @@ fn common_tail( let op = form.op; let family = |name: &str| -> &'static str { Box::leak(format!("{width} {name}").into_boxed_str()) }; - let long: Vec = LANES.iter().chain(&LANES[..13]).copied().collect(); + // The second wave holds different values from the first one's lanes 0-12. + let long: Vec = LANES + .iter() + .chain(LANES.iter().rev().take(13)) + .copied() + .collect(); let short = LANES[..7].to_vec(); for (exec, input, label) in [ (u32::MAX, short, "7 lanes"), @@ -758,9 +768,10 @@ fn common_tail( setup, Some(0), &[vopc(op, src0, vsrc1)], - &[VCCZ_OVER_ONE, SMOV_S8_0], + &[SMOV_S8_0, VCCZ_OVER_ONE, SMOV_S8_1, V2_FROM_S8], ); - let label = "exec 0, then s_cbranch_vccz over s_mov".into(); + // Taken: s8 stays 0 and v2 = s8 = 0; falling through would give 1. + let label = "exec 0, then s_cbranch_vccz over s_mov, v2 = s8".into(); cases.push(case( form, family("vccz branch"), @@ -938,6 +949,15 @@ fn rejections() -> Vec<(String, Vec, usize)> { "v255 as a base under empty exec".into(), program(&top, Some(0), &[vopc(op, vgpr(255), other)], &[]), ); + let pair = with(&smov64(8, 1)); + reject( + "s[8:9], v255 as VSRC1 base".into(), + program(&pair, None, &[vopc(op, 8, 255)], &[]), + ); + reject( + "s[8:9], v255 as VSRC1 base under empty exec".into(), + program(&pair, Some(0), &[vopc(op, 8, 255)], &[]), + ); } for src0 in (240..=248).chain([107, 124, 125, 126, 127, 251, 252, 253, 254]) { reject( From 0b00fb378db68ba570d643358e21a233eb46f466 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:44:01 -0600 Subject: [PATCH 5/7] docs, validation: document the 16-bit and 64-bit compare oracle subset (#215) vector-compare.md gains section 2a for the subset the reference interpreters accept, and no longer says the reference rejects these forms. vopc_f_t.rs stops listing the 64-bit F/T opcodes as unsupported: the reference accepts them now, and the check passed only because v2 of the v[1:2] pair was uninitialized. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- CHANGELOG.md | 1 + docs/shaders/vector-compare.md | 36 ++++++++++++++++++++++++++++-- tests/validation/tests/vopc_f_t.rs | 10 ++++----- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 286bccf..bbaf8c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Added +- Added an independent guide-derived oracle for the Wave32 16-bit (`v_cmp_{lt..ge}_{i16,u16}_e32`) and 64-bit (`v_cmp_{f..t}_{i64,u64}_e32`) integer compares in the reference and the second interpreter (6,212 lane cases, 1,192 rejections; contract in `docs/shaders/vector-compare.md` section 2a). The decoder and translator still reject these forms, and native Metal agreement is not yet shown. Part of #215. - Added `python3 -m qa rdna2 rank` to the read-only RDNA2 planner: it groups undeclared gfx1030 forms by the piece of work that would unlock them (an encoding variant of a declared form, a new operand rule, or a new primitive, from XML identity and operand shape only) and ranks the groups by forms per estimated cost, excluding claimed scopes, with `--no-float` to leave out floating-point groups until the FP contract exists (part of #215). - Added authoritative read-only x86-64 CPU inventory CLI (`python3 -m qa cpu-inventory`) over `crates/loader/x86/instructions.toml` via `generate_x86.load`, with opcode-map/prefix/operand summaries, native vs shared-semantic operation partitions from `dbt.rs`, fixture mnemonic evidence and a separate unsupported catalog. Does not count candidates as implemented. Covered by `tests/qa/test_cpu_inventory.py`. - Added typed runtime inventory CLI (`python3 -m qa runtime-inventory`) over `crates/loader/runtime/services.txt`, reporting implemented/partial/stub services separately from API-set aliases and leaving independently-verified empty unless recorded. Covered by `tests/qa/test_runtime_inventory.py`. diff --git a/docs/shaders/vector-compare.md b/docs/shaders/vector-compare.md index 0244694..116b675 100644 --- a/docs/shaders/vector-compare.md +++ b/docs/shaders/vector-compare.md @@ -75,6 +75,29 @@ stream carries no wave-size declaration (the VOPC word has no wave-size field), so every caller of the translator and the reference establishes Wave32 by using this ABI; there is no Wave64 entry point to reject. +### 2a. 16-bit and 64-bit compares (reference interpreters only) + +The reference (`crates/ref`) and the second interpreter +(`tests/validation/independent_ref.rs`) also accept the `_e32` forms +`v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}` (opcodes 137-142, 169-174) and +`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i64,u64}` (160-167, 224-231). The decoder and +the translator still reject them (section 4); these forms have an oracle +but no translation yet. + +- 16-bit forms read bits 15:0 of each 32-bit source and ignore bits 31:16 + (an inference from guide 6.2, 6.2.5 and 6.8: only SDWA selects a half). + There is no literal source. +- 64-bit forms read a pair: v[n:n+1] with n <= 254, an even-aligned + s[2k:2k+1] with k <= 52 (3.6.3), or an inline integer, which is + sign-extended to 64 bits (-1 is all ones, also for `_u64`; a reading of + Table 20). VSRC1 is v[m:m+1] with m <= 254. Literals and VCC as a 64-bit + source are rejected. +- The result, EXEC masking, VCCZ and the empty-EXEC rule are those of + sections 3 and 5. + +The expectations and the guide readings are in +`tests/validation/tests/vopc_wide.rs`. + ## 3. Semantics of one supported compare, Wave32 Let E = EXEC_LO before the instruction and A = the set of lanes that exist in @@ -100,7 +123,9 @@ this wave (all 32 except in a final partial wave, see 7). As an unsigned - V_CMPX_* (writes EXEC: divergent execution; needs its own contract). - The VOP3 encoding of any compare (arbitrary SGPR destination, modifiers). -- SDWA and DPP forms; 16-bit, 64-bit and float compares; v_cmp_class. +- SDWA and DPP forms; float compares; v_cmp_class. +- 16-bit and 64-bit compares in the decoder and translator (the reference + interpreters accept the subset in 2a). - SRC0 inline float constants (240-248), SGPRs other than s0-s106, TTMPs, M0, EXEC_LO/HI, VCC_HI, SCC/VCCZ/EXECZ selectors, LDS direct. - A normal-SGPR SRC0 without a known value, when EXEC_LO != 0. @@ -226,6 +251,11 @@ programs, plus 2 mask-chain programs (252 in all), and the F/T fixture (253) mat (`vector_compare_native_metal_known_answers`, run by `scripts/test-gpu.sh`). The F/T oracle's own native Metal cases run in `vopc_f_t_match_the_guide_on_native_metal`. +The 16-bit and 64-bit oracle (`tests/validation/tests/vopc_wide.rs`) checks +6,212 lane cases on both reference interpreters, plus 1,192 rejections and the +13 neighbouring opcodes. Its native Metal test stays ignored until the +translator implements these forms. + A deliberately broken copy of each implementation fails these tests: the reference with unsigned-only compares or with the EXEC test removed; the second interpreter with unsigned-only compares or with no empty-EXEC rule; @@ -238,7 +268,9 @@ locally on Apple M5 Pro. It is not agreement with RDNA 2 or Xbox hardware. ## 10. Limitations -- Wave32 only. No `v_cmpx`, VOP3, SDWA, DPP, 16-bit, 64-bit or float compares. +- Wave32 only. No `v_cmpx`, VOP3, SDWA, DPP or float compares. 16-bit and + 64-bit compares exist only in the reference interpreters (2a); native Metal + agreement for them is not shown. - A lane-derived VCC_LO cannot reach EXEC or any scalar instruction, so there is no divergent execution. That needs its own contract. - The skipped region of a runtime `s_cbranch_vccz` holds only integer vector diff --git a/tests/validation/tests/vopc_f_t.rs b/tests/validation/tests/vopc_f_t.rs index 522ec48..c45b0ff 100644 --- a/tests/validation/tests/vopc_f_t.rs +++ b/tests/validation/tests/vopc_f_t.rs @@ -666,16 +666,14 @@ fn vopc_f_t_outside_the_contract_are_rejected() { #[test] fn other_constant_compares_stay_unsupported() { - // F and T/TRU of the float, 64-bit and v_cmpx groups (Table 61): + // F and T/TRU of the float and v_cmpx groups (Table 61): // V_CMP_F_F32 0, V_CMP_TRU_F32 15, V_CMPX_F_F32 16, V_CMPX_TRU_F32 31, - // V_CMP_F_I64 160, V_CMP_T_I64 167, V_CMPX_F_I64 176, V_CMPX_T_I64 183, - // V_CMP_F_U64 224, V_CMP_T_U64 231, V_CMPX_F_U64 240, V_CMPX_T_U64 247. + // V_CMPX_F_I64 176, V_CMPX_T_I64 183, V_CMPX_F_U64 240, V_CMPX_T_U64 247. + // The 64-bit V_CMP_F/T (160, 167, 224, 231) are covered by vopc_wide.rs. let setup = vmov(3, 7); // Also the neighbours of the two 32-bit integer groups: 127 and 191 (not // in Table 61), 136 V_CMP_CLASS_F32 and 200 V_CMP_F_F16. - for op in [ - 0, 15, 16, 31, 127, 136, 160, 167, 176, 183, 191, 200, 224, 231, 240, 247, - ] { + for op in [0, 15, 16, 31, 127, 136, 176, 183, 191, 200, 240, 247] { let (code, offset) = program(&setup, None, &[vopc(op, vgpr(1), 3)], &[]); assert_rejected_at(&format!("opcode {op}"), &code, offset, &lanes()); } From 74af0d8eb9ca37e76a7929f1b9d23a37e697d102 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:46:52 -0600 Subject: [PATCH 6/7] scripts: skip the wide-compare Metal oracle in the GPU gate until the translator lands (#215) The ignored native Metal test fails today because the translator rejects the 16- and 64-bit compares; scripts/test-gpu.sh runs every ignored semantic-review test, so it skips this one by name. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- scripts/test-gpu.sh | 4 +++- tests/validation/tests/vopc_wide.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/test-gpu.sh b/scripts/test-gpu.sh index 9627db8..15230f7 100755 --- a/scripts/test-gpu.sh +++ b/scripts/test-gpu.sh @@ -53,4 +53,6 @@ timeout 30s cargo test --locked -p xenora-ffi --features gpu-tests --test audio_ # Native Metal known answers of the independent semantic review, including SOP2 scalar ALU # and VOPC compares. cargo test --locked -p xenora-semantic-review --no-run -timeout 120s cargo test --locked -p xenora-semantic-review -- --ignored --nocapture --test-threads=1 +# Oracles whose translator side has not landed are skipped by name until it does. +timeout 120s cargo test --locked -p xenora-semantic-review -- --ignored --nocapture --test-threads=1 \ + --skip vopc_wide_match_the_guide_on_native_metal diff --git a/tests/validation/tests/vopc_wide.rs b/tests/validation/tests/vopc_wide.rs index e294b85..81eced6 100644 --- a/tests/validation/tests/vopc_wide.rs +++ b/tests/validation/tests/vopc_wide.rs @@ -1198,7 +1198,7 @@ fn neighbouring_compares_stay_unsupported() { } #[test] -#[ignore = "requires a native Metal device with API and shader validation, and the translator's 16- and 64-bit forms"] +#[ignore = "requires a native Metal device with API and shader validation, and the translator's 16- and 64-bit forms (skipped by scripts/test-gpu.sh until then)"] fn vopc_wide_match_the_guide_on_native_metal() { assert_eq!(std::env::var("MTL_DEBUG_LAYER").as_deref(), Ok("1")); assert_eq!(std::env::var("MTL_SHADER_VALIDATION").as_deref(), Ok("1")); From 1b4911ffea7e3a33cf7ea7990df7b811f4a4d191 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:50:10 -0600 Subject: [PATCH 7/7] validation, docs: close the wide-compare review gaps (#215) A CPU test checks that the production compiler still rejects all 28 forms, and each 64-bit form gains a rejection for v255 as VSRC1 after an inline integer (dropping that check in the second interpreter now fails the rejection test). Docs and counts follow. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- CHANGELOG.md | 2 +- docs/shaders/vector-compare.md | 13 +++++++--- tests/validation/independent_ref.rs | 6 +++-- tests/validation/tests/vopc_wide.rs | 39 +++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbaf8c3..a7e4744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Added -- Added an independent guide-derived oracle for the Wave32 16-bit (`v_cmp_{lt..ge}_{i16,u16}_e32`) and 64-bit (`v_cmp_{f..t}_{i64,u64}_e32`) integer compares in the reference and the second interpreter (6,212 lane cases, 1,192 rejections; contract in `docs/shaders/vector-compare.md` section 2a). The decoder and translator still reject these forms, and native Metal agreement is not yet shown. Part of #215. +- Added an independent guide-derived oracle for the Wave32 16-bit (`v_cmp_{lt..ge}_{i16,u16}_e32`) and 64-bit (`v_cmp_{f..t}_{i64,u64}_e32`) integer compares in the reference and the second interpreter (6,212 lane cases, 1,208 rejections; contract in `docs/shaders/vector-compare.md` section 2a). The decoder and translator still reject these forms, and native Metal agreement is not yet shown. Part of #215. - Added `python3 -m qa rdna2 rank` to the read-only RDNA2 planner: it groups undeclared gfx1030 forms by the piece of work that would unlock them (an encoding variant of a declared form, a new operand rule, or a new primitive, from XML identity and operand shape only) and ranks the groups by forms per estimated cost, excluding claimed scopes, with `--no-float` to leave out floating-point groups until the FP contract exists (part of #215). - Added authoritative read-only x86-64 CPU inventory CLI (`python3 -m qa cpu-inventory`) over `crates/loader/x86/instructions.toml` via `generate_x86.load`, with opcode-map/prefix/operand summaries, native vs shared-semantic operation partitions from `dbt.rs`, fixture mnemonic evidence and a separate unsupported catalog. Does not count candidates as implemented. Covered by `tests/qa/test_cpu_inventory.py`. - Added typed runtime inventory CLI (`python3 -m qa runtime-inventory`) over `crates/loader/runtime/services.txt`, reporting implemented/partial/stub services separately from API-set aliases and leaving independently-verified empty unless recorded. Covered by `tests/qa/test_runtime_inventory.py`. diff --git a/docs/shaders/vector-compare.md b/docs/shaders/vector-compare.md index 116b675..486d221 100644 --- a/docs/shaders/vector-compare.md +++ b/docs/shaders/vector-compare.md @@ -252,11 +252,16 @@ programs, plus 2 mask-chain programs (252 in all), and the F/T fixture (253) mat run in `vopc_f_t_match_the_guide_on_native_metal`. The 16-bit and 64-bit oracle (`tests/validation/tests/vopc_wide.rs`) checks -6,212 lane cases on both reference interpreters, plus 1,192 rejections and the -13 neighbouring opcodes. Its native Metal test stays ignored until the -translator implements these forms. +6,212 lane cases on both reference interpreters, plus 1,208 rejections and the +13 neighbouring opcodes, and that the production compiler still rejects all 28 +forms. Its native Metal test stays ignored (and skipped by `scripts/test-gpu.sh`) +until the translator implements these forms. Deliberately broken copies of both +interpreters (16-bit reads of 32 bits, swapped pair halves, zero-extended +inline integers, flipped signedness, reads under empty EXEC and others) each +fail it. -A deliberately broken copy of each implementation fails these tests: the +For the 32-bit forms, a deliberately broken copy of each implementation fails +the tests above: the reference with unsigned-only compares or with the EXEC test removed; the second interpreter with unsigned-only compares or with no empty-EXEC rule; the translator with a stale VCC under empty EXEC (CPU), and, on Metal, with diff --git a/tests/validation/independent_ref.rs b/tests/validation/independent_ref.rs index e7c6e78..69114ba 100644 --- a/tests/validation/independent_ref.rs +++ b/tests/validation/independent_ref.rs @@ -27,8 +27,10 @@ //! production operator. SOP2 arithmetic runs through a bit-serial ripple //! adder (subtraction as addition of the complement), not host integer ops. //! -//! Wave32 VOPC integer compares (`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32,i64,u64}_e32` -//! and `v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_e32`, contract `docs/shaders/vector-compare.md`) need all lanes of a wave, so execution +//! Wave32 VOPC integer compares +//! (`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32,i64,u64}_e32` and +//! `v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_e32`, contract +//! `docs/shaders/vector-compare.md`) need all lanes of a wave, so execution //! runs lockstep waves of 32 consecutive dispatch indices with wave-uniform //! scalar state and per-lane vector state. A lane-local failure stops only that //! lane and the lowest-index error is reported, so programs without compares diff --git a/tests/validation/tests/vopc_wide.rs b/tests/validation/tests/vopc_wide.rs index 81eced6..0df572b 100644 --- a/tests/validation/tests/vopc_wide.rs +++ b/tests/validation/tests/vopc_wide.rs @@ -11,8 +11,9 @@ //! 6.2 (Table 20 and 6.2.1), 6.2.5, 6.8 and 9.1. Nothing here comes from the //! ISA crate, the semantic registry, the decoder or the shader compiler. Every //! case runs on the reference interpreter `xenora_ref` and on the second -//! interpreter `independent_ref`; the ignored test also runs the translated MSL -//! on native Metal. +//! interpreter `independent_ref`. The ignored native Metal test needs the +//! translator's 16- and 64-bit forms; until then a CPU test checks that the +//! production compiler rejects all 28. //! //! Readings of the guide (not hardware evidence): //! - Table 59: offsets F 0, LT 1, EQ 2, LE 3, GT 4, LG/NE 5, GE 6, TRU 7 with @@ -256,9 +257,11 @@ const REJECTIONS16: usize = 4 + 2 + 2 + 9 + 9 + 1 + 4 + 1 + 1 + 1; /// 64-bit per form: 7 uninitialized pair halves, 2 pending loads on a high /// half, vcc (known and under empty EXEC), a literal with and without its /// dword, 2 odd SGPR bases and one under empty EXEC, v255 as either base and -/// under empty EXEC, v255 as VSRC1 after an SGPR pair (also under empty EXEC), then as for 16-bit: 9 float, 9 special, a float under -/// empty EXEC, SDWA/DPP 4, `_e64`, `v_cmpx`, vccz over a scalar instruction. -const REJECTIONS64: usize = 7 + 2 + 2 + 2 + 3 + 3 + 2 + 9 + 9 + 1 + 4 + 1 + 1 + 1; +/// under empty EXEC, v255 as VSRC1 after an SGPR pair (also under empty +/// EXEC) and after an inline integer, then as for 16-bit: 9 float, 9 special, +/// a float under empty EXEC, SDWA/DPP 4, `_e64`, `v_cmpx`, vccz over a scalar +/// instruction. +const REJECTIONS64: usize = 7 + 2 + 2 + 2 + 3 + 3 + 3 + 9 + 9 + 1 + 4 + 1 + 1 + 1; const LITERAL: u32 = 255; const VCC_LO: u32 = 106; @@ -958,6 +961,10 @@ fn rejections() -> Vec<(String, Vec, usize)> { "s[8:9], v255 as VSRC1 base under empty exec".into(), program(&pair, Some(0), &[vopc(op, 8, 255)], &[]), ); + reject( + "inline 1, v255 as VSRC1 base".into(), + program(&setup, None, &[vopc(op, 129, 255)], &[]), + ); } for src0 in (240..=248).chain([107, 124, 125, 126, 127, 251, 252, 253, 254]) { reject( @@ -1161,6 +1168,28 @@ fn vopc_wide_outside_the_contract_are_rejected() { eprintln!("vopc_wide: {} rejections", rejections.len()); } +#[test] +fn production_compiler_still_rejects_the_wide_compares() { + // docs/shaders/vector-compare.md 2a: the oracle runs ahead of the + // translator, which rejects all 28 forms at the compare until it + // implements them. This test fails once it does; then the Metal test + // below takes over. + for form in forms() { + let (setup, other) = if form.bits == 64 { + (lane_pair(7), 4) + } else { + (vmov(3, 7).to_vec(), 3) + }; + let (code, offset) = program(&setup, None, &[vopc(form.op, vgpr(1), other)], &[]); + let at = match compile(Architecture::Gfx1030, &code) { + Err(CompileError::Decode(error)) => error.offset, + Err(CompileError::Verify { offset, .. }) => offset, + other => panic!("compiler accepted {}: {other:?}", form.name), + }; + assert_eq!(at, offset, "compiler rejected {} elsewhere", form.name); + } +} + #[test] fn pair_halves_are_checked_without_active_lanes() { // Contract section 5: with EXEC_LO != 0 every source register is checked