Merge remote-tracking branch 'origin/main' into task/209-llvm-corpus

This commit is contained in:
elforeign committed 2026-11-24 02:06:03 -06:00
commit fa4d4b9d71
19 files changed
+702 -63

No files matched your search

+2 -1
View File
@@ -11,7 +11,8 @@ remains unsupported.
### Added
- Added the gfx1030 SOP2 scalar forms `s_add_u32`, `s_sub_u32`, `s_addc_u32`, `s_subb_u32`, `s_and_b32`, `s_or_b32` and `s_xor_b32`. They are decoded, executed by the reference and independent interpreters, and resolved by the translator, with SCC as carry, borrow or nonzero result. `v_mov_b32_e32` can now read a normal SGPR. Verified on 1,400 original operand vectors plus an LLVM-assembled fixture, on CPU and on native Metal with validation. `s_add_i32`, `s_sub_i32` and the other SOP2 forms stay unsupported. See `docs/shaders/scalar-alu.md`.
- Added the gfx1030 SOP2 forms `s_add_i32` and `s_sub_i32`, with SCC as signed overflow as defined in the AMD RDNA 2 ISA guide (sections 5.3 and 12.1). They are checked on CPU and on native Metal with validation on 400 boundary programs, four immediate-operand overflows and the scalar ALU fixture, plus eight hand-worked overflow and no-overflow cases on CPU.
- Added the gfx1030 SOP2 scalar forms `s_add_u32`, `s_sub_u32`, `s_addc_u32`, `s_subb_u32`, `s_and_b32`, `s_or_b32` and `s_xor_b32`. They are decoded, executed by the reference and independent interpreters, and resolved by the translator, with SCC as carry, borrow or nonzero result. `v_mov_b32_e32` can now read a normal SGPR. Verified on 1,400 original operand vectors plus an LLVM-assembled fixture, on CPU and on native Metal with validation. The other SOP2 forms stay unsupported. See `docs/shaders/scalar-alu.md`.
- Added `scripts/audit/test_inventory.py`, which lists the Cargo test inventory of two commits in temporary detached worktrees and reports removed, added, newly ignored and newly un-ignored tests, failing on any loss not explained in an `--allow` file. Run against the old PR #166 head 5450612 and main 951189c, it reports the 12 BC texture tests that head was missing (exit 1); the same commit against itself reports no change. Default features only; see `docs/testing/test-inventory.md`.
- Added complete gfx1030 instruction metadata generated from AMD's public RDNA 2 XML: 1,465 base forms across 21 encoding families with fields, operands, flags and allowed extension words. A committed, normalised snapshot lets `crates/isa/generate_rdna2_meta.py --check` verify the generated table offline; CI runs it.
- Added `xenora_isa::meta::identify`, which finds any gfx1030 instruction's form and exact length, including literal, SDWA, DPP and NSA words, and rejects truncated, unassigned and disallowed-extension encodings. It agrees with every LLVM-assembled fixture and with LLVM 23.1.6 on a probe set covering each family and extension word. Identification does not make an instruction executable: 25 forms have semantic decoding.
+11 -8
View File
@@ -7,11 +7,11 @@ pub use xenora_isa::{
VectorSource,
};
use xenora_isa::{
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_ADD_U32, S_ADDC_U32, S_AND_B32,
S_BRANCH, S_CBRANCH_EXECZ, S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM, S_MOV_B32,
S_OR_B32, S_SUB_U32, S_SUBB_U32, S_WAITCNT, S_WAITCNT_VSCNT, S_XOR_B32, 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,
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_ADD_I32, S_ADD_U32, S_ADDC_U32,
S_AND_B32, S_BRANCH, S_CBRANCH_EXECZ, S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM,
S_MOV_B32, S_OR_B32, S_SUB_I32, S_SUB_U32, S_SUBB_U32, S_WAITCNT, S_WAITCNT_VSCNT, S_XOR_B32,
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;
@@ -19,11 +19,12 @@ 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); 32] = [
pub const SEMANTIC_FORMS: [(&str, EncodingFamily); 34] = [
("BUFFER_LOAD_DWORD", EncodingFamily::Mubuf),
("BUFFER_STORE_DWORD", EncodingFamily::Mubuf),
("EXP", EncodingFamily::Exp),
("IMAGE_SAMPLE_LZ", EncodingFamily::Mimg),
("S_ADD_I32", EncodingFamily::Sop2),
("S_ADD_U32", EncodingFamily::Sop2),
("S_ADDC_U32", EncodingFamily::Sop2),
("S_AND_B32", EncodingFamily::Sop2),
@@ -35,6 +36,7 @@ pub const SEMANTIC_FORMS: [(&str, EncodingFamily); 32] = [
("S_ENDPGM", EncodingFamily::Sopp),
("S_MOV_B32", EncodingFamily::Sop1),
("S_OR_B32", EncodingFamily::Sop2),
("S_SUB_I32", EncodingFamily::Sop2),
("S_SUB_U32", EncodingFamily::Sop2),
("S_SUBB_U32", EncodingFamily::Sop2),
("S_WAITCNT", EncodingFamily::Sopp),
@@ -350,6 +352,8 @@ fn decode_gfx1030(bytes: &[u8], offset: usize) -> Result<(Instruction, u8), Deco
for (opcode, op) in [
(S_ADD_U32, ScalarAluOp::AddU32),
(S_SUB_U32, ScalarAluOp::SubU32),
(S_ADD_I32, ScalarAluOp::AddI32),
(S_SUB_I32, ScalarAluOp::SubI32),
(S_ADDC_U32, ScalarAluOp::AddcU32),
(S_SUBB_U32, ScalarAluOp::SubbU32),
(S_AND_B32, ScalarAluOp::AndB32),
@@ -469,8 +473,7 @@ fn decode_gfx1030(bytes: &[u8], offset: usize) -> Result<(Instruction, u8), Deco
}
/// SOP2: SSRC0 in bits 0-7, SSRC1 in bits 8-15, SDST in bits 16-22.
/// Other SOP2 opcodes, including S_ADD_I32 and S_SUB_I32 whose SCC rule is
/// not pinned by the sources in docs/shaders/scalar-alu.md, stay unsupported.
/// Other SOP2 opcodes stay unsupported.
fn scalar_alu(
op: ScalarAluOp,
first: u32,
+7 -5
View File
@@ -328,6 +328,8 @@ fn reference_op(instruction: Instruction) -> Op {
match op {
ScalarAluOp::AddU32 => ScalarAlu::Add,
ScalarAluOp::SubU32 => ScalarAlu::Subtract,
ScalarAluOp::AddI32 => ScalarAlu::AddSigned,
ScalarAluOp::SubI32 => ScalarAlu::SubtractSigned,
ScalarAluOp::AddcU32 => ScalarAlu::AddWithCarry,
ScalarAluOp::SubbU32 => ScalarAlu::SubtractWithBorrow,
ScalarAluOp::AndB32 => ScalarAlu::And,
@@ -413,7 +415,7 @@ fn compares_fixture_instructions_against_llvm() {
] {
count += compare_against_llvm(fixture, &mut forms);
}
assert_eq!(forms.len(), 28, "forms compared: {forms:?}");
assert_eq!(forms.len(), 30, "forms compared: {forms:?}");
eprintln!(
"{count} three-way instruction comparisons across {} forms",
forms.len()
@@ -464,6 +466,8 @@ fn assemble_matrix() -> Vec<u8> {
s_sub_u32 s9, 0xdeadbeef, -16
s_addc_u32 s104, 0xdeadbeef, s105
s_subb_u32 s0, s0, s0
s_add_i32 s101, 0x7fffffff, s100
s_sub_i32 s100, -16, 0x80000000
s_and_b32 s1, -1, s2
s_or_b32 s103, s3, 0x80000000
s_xor_b32 s102, s104, -16
@@ -602,8 +606,6 @@ fn distinguishes_llvm_invalid_encodings_from_xenora_subset_rejections() {
];
let outside_subset: &[&[u32]] = &[
&[0x7e04_026a], // VOP1 VCC_LO source
&[0x8108_0908], // s_add_i32: SCC rule not pinned
&[0x8188_0908], // s_sub_i32: SCC rule not pinned
&[0x8788_0908], // s_and_b64
&[0x8008_ffff, 0x1234_5678], // SOP2 with two literal sources
&[0x806c_0908], // SOP2 TTMP destination
@@ -697,8 +699,8 @@ fn normalize_llvm_assembly(assembly: &str, next_offset: usize) -> String {
};
format!("{mnemonic} {}, {}", scalar(left), scalar(right))
}
"s_add_u32" | "s_sub_u32" | "s_addc_u32" | "s_subb_u32" | "s_and_b32" | "s_or_b32"
| "s_xor_b32" => {
"s_add_u32" | "s_sub_u32" | "s_add_i32" | "s_sub_i32" | "s_addc_u32" | "s_subb_u32"
| "s_and_b32" | "s_or_b32" | "s_xor_b32" => {
let scalar = |operand: &str| match literal_bits(operand) {
Some(bits) => match bits as i32 {
inline @ -16..=64 => inline.to_string(),
+2
View File
@@ -20,6 +20,8 @@ VARIANTS = (
("S_CMP_EQ_U32", "ENC_SOPC"),
("S_ADD_U32", "ENC_SOP2"),
("S_SUB_U32", "ENC_SOP2"),
("S_ADD_I32", "ENC_SOP2"),
("S_SUB_I32", "ENC_SOP2"),
("S_ADDC_U32", "ENC_SOP2"),
("S_SUBB_U32", "ENC_SOP2"),
("S_AND_B32", "ENC_SOP2"),
+22 -7
View File
@@ -7,11 +7,11 @@ mod rdna2;
#[rustfmt::skip]
mod rdna2_meta;
pub use rdna2::{
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_ADD_U32, S_ADDC_U32, S_AND_B32,
S_BRANCH, S_CBRANCH_EXECZ, S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM, S_MOV_B32,
S_OR_B32, S_SUB_U32, S_SUBB_U32, S_WAITCNT, S_WAITCNT_VSCNT, S_XOR_B32, 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,
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_ADD_I32, S_ADD_U32, S_ADDC_U32,
S_AND_B32, S_BRANCH, S_CBRANCH_EXECZ, S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM,
S_MOV_B32, S_OR_B32, S_SUB_I32, S_SUB_U32, S_SUBB_U32, S_WAITCNT, S_WAITCNT_VSCNT, S_XOR_B32,
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,
};
/// Selects the instruction encoding independently of the container and execution ABI.
@@ -87,14 +87,19 @@ impl fmt::Display for ScalarSource {
///
/// SCC behaviour follows the pinned AMD RDNA 2 XML descriptions: additions
/// store the carry-out, subtractions the borrow, ADDC and SUBB also consume
/// SCC as carry-in or borrow-in, and bitwise operations set SCC iff the result
/// is nonzero. See docs/shaders/scalar-alu.md for the sources.
/// SCC as carry-in or borrow-in, signed forms store signed overflow (RDNA 2 ISA
/// guide 12.1), and bitwise operations set SCC iff the result is nonzero. See
/// docs/shaders/scalar-alu.md for the sources.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ScalarAluOp {
/// `s_add_u32`: D = S0 + S1, SCC = carry-out.
AddU32,
/// `s_sub_u32`: D = S0 - S1, SCC = borrow (S1 > S0).
SubU32,
/// `s_add_i32`: D = S0 + S1, SCC = signed overflow.
AddI32,
/// `s_sub_i32`: D = S0 - S1, SCC = signed overflow.
SubI32,
/// `s_addc_u32`: D = S0 + S1 + SCC, SCC = carry-out.
AddcU32,
/// `s_subb_u32`: D = S0 - S1 - SCC, SCC = borrow.
@@ -113,6 +118,8 @@ impl ScalarAluOp {
match self {
Self::AddU32 => "s_add_u32",
Self::SubU32 => "s_sub_u32",
Self::AddI32 => "s_add_i32",
Self::SubI32 => "s_sub_i32",
Self::AddcU32 => "s_addc_u32",
Self::SubbU32 => "s_subb_u32",
Self::AndB32 => "s_and_b32",
@@ -136,6 +143,14 @@ impl ScalarAluOp {
let (value, second) = partial.overflowing_sub(carry);
(value, first || second)
}
Self::AddI32 => {
let (value, overflow) = (src0 as i32).overflowing_add(src1 as i32);
(value as u32, overflow)
}
Self::SubI32 => {
let (value, overflow) = (src0 as i32).overflowing_sub(src1 as i32);
(value as u32, overflow)
}
Self::AndB32 => (src0 & src1, src0 & src1 != 0),
Self::OrB32 => (src0 | src1, src0 | src1 != 0),
Self::XorB32 => (src0 ^ src1, src0 ^ src1 != 0),
+10
View File
@@ -60,6 +60,16 @@ pub const S_SUB_U32: Opcode = Opcode {
bits: 0x80800000,
bytes: 4,
};
pub const S_ADD_I32: Opcode = Opcode {
mask: 0xff800000,
bits: 0x81000000,
bytes: 4,
};
pub const S_SUB_I32: Opcode = Opcode {
mask: 0xff800000,
bits: 0x81800000,
bytes: 4,
};
pub const S_ADDC_U32: Opcode = Opcode {
mask: 0xff800000,
bits: 0x82000000,
+454
View File
@@ -0,0 +1,454 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! gfx1030 extension-word negative tests (issue #172): truncated and invalid
//! literal, SDWA, DPP16, DPP8 and MIMG NSA trailers.
//!
//! Every source instruction is original and was assembled by llvm-mc 23.1.6
//! (`source_encodings_match_llvm` re-checks the bytes). Expected results come
//! from LLVM's disassembly of the cut or altered bytes and from the field
//! layouts in AMD's public "RDNA 2 Instruction Set Architecture" reference
//! (microcode formats: SDWA, DPP16, DPP8, MIMG NSA), never from Xenora's
//! decoder. `identify` recognises lengths and allowed extensions; it does not
//! validate control-word values (see `reserved_control_values_keep_their_trailer_lengths`).
use std::io::Write;
use std::process::{Command, Stdio};
use xenora_isa::meta::{
IdentifyError, SRC_DPP8, SRC_DPP16, SRC_LITERAL, SRC_SDWA, Trailer, VectorControl, identify,
};
struct Fixture {
source: &'static str,
bytes: &'static [u8],
}
const SOP2_LITERAL: Fixture = Fixture {
source: "s_add_u32 s9, 0x12345679, s11",
bytes: &[0xff, 0x0b, 0x09, 0x80, 0x79, 0x56, 0x34, 0x12],
};
const SOPK_IMMEDIATE: Fixture = Fixture {
source: "s_setreg_imm32_b32 hwreg(HW_REG_MODE), 0x125678ab",
bytes: &[0x01, 0xf8, 0x80, 0xba, 0xab, 0x78, 0x56, 0x12],
};
const VOP1_LITERAL: Fixture = Fixture {
source: "v_mov_b32_e32 v7, 0x12345679",
bytes: &[0xff, 0x02, 0x0e, 0x7e, 0x79, 0x56, 0x34, 0x12],
};
const VOP2_LITERAL: Fixture = Fixture {
source: "v_add_f32_e32 v7, 0x3f800003, v8",
bytes: &[0xff, 0x10, 0x0e, 0x06, 0x03, 0x00, 0x80, 0x3f],
};
const FMAMK: Fixture = Fixture {
source: "v_fmamk_f32 v7, v8, 0x40000001, v9",
bytes: &[0x08, 0x13, 0x0e, 0x58, 0x01, 0x00, 0x00, 0x40],
};
const FMAAK: Fixture = Fixture {
source: "v_fmaak_f32 v7, v8, v9, 0x40000002",
bytes: &[0x08, 0x13, 0x0e, 0x5a, 0x02, 0x00, 0x00, 0x40],
};
const VOPC_LITERAL: Fixture = Fixture {
source: "v_cmp_eq_u32_e32 vcc_lo, 0x1235, v9",
bytes: &[0xff, 0x12, 0x84, 0x7d, 0x35, 0x12, 0x00, 0x00],
};
const VOP3_LITERAL: Fixture = Fixture {
source: "v_fma_f32 v7, v8, v9, 0x3f800003",
bytes: &[
0x07, 0x00, 0x4b, 0xd5, 0x08, 0x13, 0xfe, 0x03, 0x03, 0x00, 0x80, 0x3f,
],
};
const VOP3P_LITERAL: Fixture = Fixture {
source: "v_pk_fma_f16 v7, v8, v9, 0x3c02",
bytes: &[
0x07, 0x40, 0x0e, 0xcc, 0x08, 0x13, 0xfe, 0x1b, 0x02, 0x3c, 0x00, 0x00,
],
};
const VOP1_SDWA: Fixture = Fixture {
source: "v_mov_b32_sdwa v7, v8 dst_sel:BYTE_1 dst_unused:UNUSED_PAD src0_sel:DWORD",
bytes: &[0xf9, 0x02, 0x0e, 0x7e, 0x08, 0x01, 0x06, 0x00],
};
const VOP2_SDWA: Fixture = Fixture {
source: "v_add_f32_sdwa v7, v8, v9 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD",
bytes: &[0xf9, 0x12, 0x0e, 0x06, 0x08, 0x06, 0x04, 0x06],
};
const VOPC_SDWA: Fixture = Fixture {
source: "v_cmp_eq_u32_sdwa s4, v8, v9 src0_sel:DWORD src1_sel:DWORD",
bytes: &[0xf9, 0x12, 0x84, 0x7d, 0x08, 0x84, 0x06, 0x06],
};
const DPP16: Fixture = Fixture {
source: "v_add_f32_dpp v7, v8, v9 row_shl:2 row_mask:0xf bank_mask:0xf",
bytes: &[0xfa, 0x12, 0x0e, 0x06, 0x08, 0x02, 0x01, 0xff],
};
const DPP8: Fixture = Fixture {
source: "v_mov_b32_dpp v7, v8 dpp8:[2,3,0,1,6,7,4,5]",
bytes: &[0xe9, 0x02, 0x0e, 0x7e, 0x08, 0x1a, 0xe2, 0xb3],
};
const DPP8_FI: Fixture = Fixture {
source: "v_mov_b32_dpp v7, v8 dpp8:[2,3,0,1,6,7,4,5] fi:1",
bytes: &[0xea, 0x02, 0x0e, 0x7e, 0x08, 0x1a, 0xe2, 0xb3],
};
const NSA1: Fixture = Fixture {
source: "image_sample v[4:7], [v8, v10], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D",
bytes: &[
0x0a, 0x0f, 0x80, 0xf0, 0x08, 0x04, 0x40, 0x00, 0x0a, 0x00, 0x00, 0x00,
],
};
const NSA2: Fixture = Fixture {
source: "image_sample_d v[4:7], [v8, v10, v12, v14, v16, v18], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D",
bytes: &[
0x0c, 0x0f, 0x88, 0xf0, 0x08, 0x04, 0x40, 0x00, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x00, 0x00,
0x00,
],
};
const NSA3: Fixture = Fixture {
source: "image_sample_c_d_cl v[4:7], [v8, v10, v12, v14, v16, v18, v20, v22, v24, v26, v28], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_3D",
bytes: &[
0x16, 0x0f, 0xac, 0xf0, 0x08, 0x04, 0x40, 0x00, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
0x18, 0x1a, 0x1c, 0x00, 0x00,
],
};
const FRACT_F64: Fixture = Fixture {
source: "v_fract_f64_e32 v[6:7], v[8:9]",
bytes: &[0x08, 0x7d, 0x0c, 0x7e],
};
const READFIRSTLANE: Fixture = Fixture {
source: "v_readfirstlane_b32 s7, v8",
bytes: &[0x08, 0x05, 0x0e, 0x7e],
};
const FMAC: Fixture = Fixture {
source: "v_fmac_f32_e32 v7, v8, v9",
bytes: &[0x08, 0x13, 0x0e, 0x56],
};
const READLANE: Fixture = Fixture {
source: "v_readlane_b32 s7, v8, s9",
bytes: &[0x07, 0x00, 0x60, 0xd7, 0x08, 0x13, 0x00, 0x02],
};
const NOP: Fixture = Fixture {
source: "s_nop 7",
bytes: &[0x07, 0x00, 0x80, 0xbf],
};
const VOPC_REGISTER: Fixture = Fixture {
source: "v_cmp_eq_u32_e32 vcc_lo, v8, v9",
bytes: &[0x08, 0x13, 0x84, 0x7d],
};
#[test]
fn proper_prefixes_report_exact_required_lengths() {
// Each row cuts at every dword boundary and one byte before the end.
// LLVM emits no original instruction for any prefix. Below, "silent"
// means no diagnostic, and "invalid" means LLVM's invalid encoding warning.
// Empty input is silent in all rows. The expected values are the next
// base boundary (4 or 8) until the base is present, then the LLVM length.
let cases: &[(&Fixture, &[usize])] = &[
// silent, SSRC0 selects the literal dword.
(&SOP2_LITERAL, &[4, 8, 8]),
// invalid, mandatory SOPK immediate.
(&SOPK_IMMEDIATE, &[4, 8, 8]),
// silent, literal selector.
(&VOP1_LITERAL, &[4, 8, 8]),
// silent, literal selector.
(&VOP2_LITERAL, &[4, 8, 8]),
// invalid, embedded multiplier dword.
(&FMAMK, &[4, 8, 8]),
// invalid, embedded addend dword.
(&FMAAK, &[4, 8, 8]),
// silent, literal selector.
(&VOPC_LITERAL, &[4, 8, 8]),
// invalid at 4 then silent, 8-byte base and literal.
(&VOP3_LITERAL, &[4, 8, 12, 12]),
// invalid at 4 then silent, 8-byte base and literal.
(&VOP3P_LITERAL, &[4, 8, 12, 12]),
// invalid, SDWA control dword.
(&VOP1_SDWA, &[4, 8, 8]),
// invalid, SDWA control dword.
(&VOP2_SDWA, &[4, 8, 8]),
// invalid, SDWAB control dword.
(&VOPC_SDWA, &[4, 8, 8]),
// invalid, DPP16 control dword.
(&DPP16, &[4, 8, 8]),
// invalid, DPP8 control dword (fi:0).
(&DPP8, &[4, 8, 8]),
// invalid, DPP8 control dword (fi:1).
(&DPP8_FI, &[4, 8, 8]),
// invalid, 8-byte base and one NSA dword.
(&NSA1, &[4, 8, 12, 12]),
// invalid, 8-byte base and two NSA dwords.
// LLVM also prints a stray v_mul_f32 from the address bytes.
(&NSA2, &[4, 8, 16, 16, 16]),
// invalid, 8-byte base and three NSA dwords.
// LLVM also prints stray v_mul_f32 and v_mul_hi_u32_u24 from the address bytes.
(&NSA3, &[4, 8, 20, 20, 20, 20]),
];
for &(fixture, expected) in cases {
let len = fixture.bytes.len();
let prefixes: Vec<_> = (0..len).step_by(4).chain([len - 1]).collect();
assert_eq!(prefixes.len(), expected.len(), "{}", fixture.source);
for (prefix, &needed) in prefixes.into_iter().zip(expected) {
assert_eq!(
identify(&fixture.bytes[..prefix]),
Err(IdentifyError::Truncated { needed }),
"{} at {prefix}/{len} bytes",
fixture.source,
);
}
}
}
#[test]
fn disallowed_vector_selectors_and_literal_are_rejected() {
// Replace the nine-bit SRC0 field of each assembled e32 instruction
// and append a known control or literal dword.
let cases = [
// LLVM rejects SDWA on v_fract_f64. Expect InvalidExtension("sdwa").
(&FRACT_F64, SRC_SDWA, &VOP1_SDWA, "sdwa"),
// LLVM rejects DPP16 on v_fract_f64. Expect InvalidExtension("dpp16").
(&FRACT_F64, SRC_DPP16, &DPP16, "dpp16"),
// LLVM rejects DPP8 on v_fract_f64 and prints the trailer as s_cmpk_lt_i32.
// Expect InvalidExtension("dpp8") for the rejected first instruction.
(&FRACT_F64, SRC_DPP8, &DPP8, "dpp8"),
// LLVM rejects SDWA on v_fmac_f32. Expect InvalidExtension("sdwa").
(&FMAC, SRC_SDWA, &VOP1_SDWA, "sdwa"),
// LLVM rejects DPP16 on this v_cmp_eq_u32. Expect InvalidExtension("dpp16").
(&VOPC_REGISTER, SRC_DPP16, &DPP16, "dpp16"),
// LLVM prints v_readfirstlane_b32 with "Invalid immediate" because
// this source must be a VGPR. Expect InvalidExtension("literal").
(&READFIRSTLANE, SRC_LITERAL, &VOP1_LITERAL, "literal"),
];
for (fixture, selector, trailer, reason) in cases {
let mut bytes = fixture.bytes.to_vec();
let word = u32::from_le_bytes(fixture.bytes.try_into().unwrap());
bytes.copy_from_slice(&((word & !0x1ff) | selector).to_le_bytes());
bytes.extend_from_slice(&trailer.bytes[4..]);
assert_eq!(
identify(&bytes),
Err(IdentifyError::InvalidExtension(reason)),
"{} with SRC0={selector:#x}: {bytes:02x?}",
fixture.source,
);
}
}
#[test]
fn literal_for_vop3_readlane_is_rejected() {
// v_readlane_b32: change SRC0 in the second base dword at bits 8:0
// and append the v_mov_b32 literal. LLVM prints "Invalid immediate"
// for the VGPR-only source. Expect the form to reject the literal.
let mut bytes = READLANE.bytes.to_vec();
let word = u32::from_le_bytes(bytes[4..8].try_into().unwrap());
bytes[4..8].copy_from_slice(&((word & !0x1ff) | SRC_LITERAL).to_le_bytes());
bytes.extend_from_slice(&VOP1_LITERAL.bytes[4..]);
assert_eq!(
identify(&bytes),
Err(IdentifyError::InvalidExtension("literal"))
);
}
/// Identification recognizes length independently of control-word validation.
#[test]
fn reserved_control_values_keep_their_trailer_lengths() {
let cases = [
// v_add_f32_dpp: replace DPP_CTRL 0x102 with 0x100, which LLVM reports as reserved.
// LLVM prints "Invalid dpp_ctrl value". Expect 8 bytes and DPP16.
(&DPP16, 5, 0, "V_ADD_F32", VectorControl::Dpp16),
// v_mov_b32_sdwa: change SRC0_SEL from 6 to 7, which LLVM treats as reserved.
// LLVM prints DWORD and reassembles it as 6. Expect 8 bytes and SDWA.
(&VOP1_SDWA, 6, 7, "V_MOV_B32", VectorControl::Sdwa),
];
for (fixture, offset, value, name, control) in cases {
let mut bytes = fixture.bytes.to_vec();
bytes[offset] = value;
let found = identify(&bytes).unwrap();
assert_eq!(found.form.name, name);
assert_eq!(found.len, 8);
assert_eq!(
found.trailer,
Trailer {
vector_control: Some(control),
..Trailer::default()
},
);
}
}
#[test]
fn next_instruction_starts_after_the_trailer() {
// Append s_nop 7 to each original instruction. LLVM disassembles the
// two instructions with lengths (offset, 4). Expect that boundary from
// identify and the trailer selected by SRC0, SSRC0, or MIMG.NSA.
let cases = [
// LLVM emits s_nop at byte 8. Expect a scalar literal dword.
(
&SOP2_LITERAL,
"S_ADD_U32",
8,
Trailer {
literal: true,
..Trailer::default()
},
),
// LLVM emits s_nop at byte 8. Expect an SDWA control dword.
(
&VOP1_SDWA,
"V_MOV_B32",
8,
Trailer {
vector_control: Some(VectorControl::Sdwa),
..Trailer::default()
},
),
// LLVM emits s_nop at byte 8. Expect a DPP16 control dword.
(
&DPP16,
"V_ADD_F32",
8,
Trailer {
vector_control: Some(VectorControl::Dpp16),
..Trailer::default()
},
),
// LLVM emits s_nop at byte 8. Expect DPP8 with fi:0.
(
&DPP8,
"V_MOV_B32",
8,
Trailer {
vector_control: Some(VectorControl::Dpp8 {
fetch_inactive: false,
}),
..Trailer::default()
},
),
// LLVM emits s_nop at byte 8. Expect DPP8 with fi:1.
(
&DPP8_FI,
"V_MOV_B32",
8,
Trailer {
vector_control: Some(VectorControl::Dpp8 {
fetch_inactive: true,
}),
..Trailer::default()
},
),
// LLVM emits s_nop at byte 12. Expect one NSA dword.
(
&NSA1,
"IMAGE_SAMPLE",
12,
Trailer {
nsa_dwords: 1,
..Trailer::default()
},
),
// LLVM emits s_nop at byte 16. Expect two NSA dwords.
(
&NSA2,
"IMAGE_SAMPLE_D",
16,
Trailer {
nsa_dwords: 2,
..Trailer::default()
},
),
// LLVM emits s_nop at byte 20. Expect three NSA dwords.
(
&NSA3,
"IMAGE_SAMPLE_C_D_CL",
20,
Trailer {
nsa_dwords: 3,
..Trailer::default()
},
),
];
for (fixture, name, offset, trailer) in cases {
let bytes = [fixture.bytes, NOP.bytes].concat();
let first = identify(&bytes).unwrap();
assert_eq!(
(first.form.name, first.len, first.trailer),
(name, offset, trailer),
"{} followed by {}",
fixture.source,
NOP.source,
);
let second = identify(&bytes[first.len..]).unwrap();
assert_eq!(
(second.form.name, second.len, second.trailer),
("S_NOP", 4, Trailer::default()),
);
assert_eq!(first.len + second.len, bytes.len());
}
}
#[test]
#[ignore = "requires LLVM with the gfx1030 AMDGPU target"]
fn source_encodings_match_llvm() {
let fixtures = [
&SOP2_LITERAL,
&SOPK_IMMEDIATE,
&VOP1_LITERAL,
&VOP2_LITERAL,
&FMAMK,
&FMAAK,
&VOPC_LITERAL,
&VOP3_LITERAL,
&VOP3P_LITERAL,
&VOP1_SDWA,
&VOP2_SDWA,
&VOPC_SDWA,
&DPP16,
&DPP8,
&DPP8_FI,
&NSA1,
&NSA2,
&NSA3,
&FRACT_F64,
&READFIRSTLANE,
&FMAC,
&READLANE,
&NOP,
&VOPC_REGISTER,
];
let executable = std::env::var_os("LLVM_BIN").map_or_else(
|| "llvm-mc".into(),
|directory| std::path::PathBuf::from(directory).join("llvm-mc"),
);
let mut llvm = Command::new(executable)
.args([
"-triple=amdgcn-amd-amdhsa",
"-mcpu=gfx1030",
"-mattr=+wavefrontsize32",
"-show-encoding",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("launch llvm-mc (provide its directory in LLVM_BIN or put it on PATH)");
{
let mut input = llvm.stdin.take().unwrap();
for fixture in fixtures {
writeln!(input, "{}", fixture.source).unwrap();
}
}
let output = llvm.wait_with_output().unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr),
);
let assembly = String::from_utf8(output.stdout).unwrap();
let encodings: Vec<_> = assembly
.lines()
.filter_map(|line| line.split_once("; encoding: [").map(|(_, bytes)| bytes))
.collect();
assert_eq!(encodings.len(), fixtures.len());
for (fixture, encoding) in fixtures.into_iter().zip(encodings) {
let bytes: Vec<_> = encoding
.strip_suffix(']')
.unwrap()
.split(',')
.map(|byte| u8::from_str_radix(byte.trim().strip_prefix("0x").unwrap(), 16).unwrap())
.collect();
assert_eq!(bytes, fixture.bytes, "{}", fixture.source);
}
}
+17 -2
View File
@@ -85,6 +85,9 @@ pub enum ScalarAlu {
Subtract,
AddWithCarry,
SubtractWithBorrow,
/// SCC = signed overflow (RDNA 2 ISA guide 12.1).
AddSigned,
SubtractSigned,
And,
Or,
Xor,
@@ -231,15 +234,17 @@ pub fn decode(code: &[u8]) -> Result<Vec<DecodedInstruction>, ReferenceError> {
left,
right,
low,
high @ (0x80 | 0x82 | 0x87 | 0x88 | 0x89),
high @ (0x80..=0x82 | 0x87 | 0x88 | 0x89),
tail @ ..,
] if (*left, *right) != (255, 255)
&& low & 0x7f <= 105
&& (low & 0x80 == 0 || matches!(high, 0x80 | 0x82)) =>
&& (low & 0x80 == 0 || matches!(high, 0x80..=0x82)) =>
{
let kind = match (high, low >> 7) {
(0x80, 0) => ScalarAlu::Add,
(0x80, _) => ScalarAlu::Subtract,
(0x81, 0) => ScalarAlu::AddSigned,
(0x81, _) => ScalarAlu::SubtractSigned,
(0x82, 0) => ScalarAlu::AddWithCarry,
(0x82, _) => ScalarAlu::SubtractWithBorrow,
(0x87, _) => ScalarAlu::And,
@@ -546,6 +551,16 @@ fn execute_lane(
let difference = lhs - rhs - carry;
(difference, difference < 0)
}
// The exact signed result must fit in i32, else SCC = overflow.
ScalarAlu::AddSigned | ScalarAlu::SubtractSigned => {
let signed = |value: i64| i64::from(value as u32 as i32);
let exact = if kind == ScalarAlu::AddSigned {
signed(lhs) + signed(rhs)
} else {
signed(lhs) - signed(rhs)
};
(exact, i32::try_from(exact).is_err())
}
ScalarAlu::And | ScalarAlu::Or | ScalarAlu::Xor => {
let bits = match kind {
ScalarAlu::And => lhs & rhs,
+4 -4
View File
@@ -1,17 +1,17 @@
<svg xmlns="http://www.w3.org/2000/svg" width="530" height="185" viewBox="0 0 530 185" role="img" aria-labelledby="title description">
<title id="title">Xenora · recorded progress</title>
<desc id="description">Decoded gfx1030 forms: 28 of 28. Accepted Metal forms · Apple M5: 18 of 28. Accepted guest milestones · X1–X4: 4 of 4</desc>
<desc id="description">Decoded gfx1030 forms: 30 of 30. Accepted Metal forms · Apple M5: 18 of 30. Accepted guest milestones · X1–X4: 4 of 4</desc>
<rect x="1" y="1" width="528" height="183" rx="10" fill="#18212a" stroke="#516171"/>
<g font-family="system-ui,sans-serif" fill="#eaf0f5">
<text x="20" y="29" font-size="13" font-weight="700">RECORDED FIXTURE PROGRESS</text>
<text x="20" y="58" font-size="13">Decoded gfx1030 forms</text>
<text x="510" y="58" font-size="13" text-anchor="end">28 / 28</text>
<text x="510" y="58" font-size="13" text-anchor="end">30 / 30</text>
<rect x="20" y="65" width="490" height="4" rx="2" fill="#3c4a58"/>
<rect x="20" y="65" width="490.00" height="4" rx="2" fill="#88c6bd"/>
<text x="20" y="96" font-size="13">Accepted Metal forms · Apple M5</text>
<text x="510" y="96" font-size="13" text-anchor="end">18 / 28</text>
<text x="510" y="96" font-size="13" text-anchor="end">18 / 30</text>
<rect x="20" y="103" width="490" height="4" rx="2" fill="#3c4a58"/>
<rect x="20" y="103" width="315.00" height="4" rx="2" fill="#88c6bd"/>
<rect x="20" y="103" width="294.00" height="4" rx="2" fill="#88c6bd"/>
<text x="20" y="134" font-size="13">Accepted guest milestones · X1–X4</text>
<text x="510" y="134" font-size="13" text-anchor="end">4 / 4</text>
<rect x="20" y="141" width="490" height="4" rx="2" fill="#3c4a58"/>

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

+5 -3
View File
@@ -24,6 +24,8 @@ The wider LLVM operand matrix exercises decoding only. ABI means application bin
| `s_cmp_eq_u32` | Normal SGPR or integer operands, with at most one source selecting a literal | `BF068708` | yes | yes | three-way passed | resolved SCC | implemented | fixture verified | fixture verified |
| `s_add_u32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = carry-out | `80088308` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_sub_u32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = borrow | `808A8180` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_add_i32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = signed overflow | `8110C109` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_sub_i32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = signed overflow | `8191C110` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_addc_u32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; adds SCC; SCC = carry-out | `820980FF 7FFFFFFF` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_subb_u32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; subtracts SCC; SCC = borrow | `828B0809` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
| `s_and_b32` | SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = result != 0 | `870D090C` | yes | yes | three-way passed | resolved scalar state and SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending |
@@ -46,11 +48,11 @@ The wider LLVM operand matrix exercises decoding only. ABI means application bin
## Automated three-way comparison
LLVM 23.1.6, the production decoder and the independent reference decoder
matched 452 instructions across nine streams: the seven synthetic fixtures,
matched 458 instructions across nine streams: the seven synthetic fixtures,
a high-register sequence and an operand matrix assembled by LLVM.
The harness checks byte offsets, widths, encoded bytes from LLVM, mnemonic
and operands, and the typed reference operation against the production
operation. It covers all 28 forms in this document.
operation. It covers all 30 forms in this document.
The matrix checks the 81 inline integer selectors in VOP1, SOP1, SOPC and
SOP2, forced literals that could otherwise be inline, literal selection in
@@ -59,7 +61,7 @@ operands, and zero and nonzero forward branch displacements. The decoder
trace exposes MUBUF operands independently of the synthetic execution ABI,
which still requires the original index and resource bindings.
Five malformed LLVM probes produced diagnostics, and 27 valid encodings
Five malformed LLVM probes produced diagnostics, and 25 valid encodings
or disassemblable branch sequences were deliberately rejected by both Xenora
decoders. Both Xenora decoders also reject all 37 interior truncation offsets
in the add fixture and every case in the unsupported-encoding list.
+1 -1
View File
@@ -9,7 +9,7 @@
| VOP1 `V_MOV_B32` | Read a register, inline integer or literal. Copy all 32 bits. |
| VOP3 `V_ADD_NC_U32` | Read two vector registers and add modulo 2^32. No implicit carry or flag update. |
| SOP1 `S_MOV_B32`, SOPC `S_CMP_EQ_U32` | Copy 32 bits into a scalar register or compare two operands and set SCC. Descriptor SGPRs are reserved. |
| SOP2 `S_ADD_U32`, `S_SUB_U32`, `S_ADDC_U32`, `S_SUBB_U32`, `S_AND_B32`, `S_OR_B32`, `S_XOR_B32` | Exact integer result reduced modulo 2^32; SCC is the carry-out, the borrow, or a nonzero bitwise result, and ADDC/SUBB consume SCC ([SOP2 scalar ALU](scalar-alu.md)). Descriptor SGPRs are reserved. |
| SOP2 `S_ADD_U32`, `S_SUB_U32`, `S_ADD_I32`, `S_SUB_I32`, `S_ADDC_U32`, `S_SUBB_U32`, `S_AND_B32`, `S_OR_B32`, `S_XOR_B32` | Exact integer result reduced modulo 2^32; SCC is the carry-out, the borrow, signed overflow (the exact signed result does not fit in i32), or a nonzero bitwise result, and ADDC/SUBB consume SCC ([SOP2 scalar ALU](scalar-alu.md)). Descriptor SGPRs are reserved. |
| SOPP `S_BRANCH`, `S_CBRANCH_SCC0` | Use a signed dword displacement from the next instruction (CLAIM-RDNA2-SOPP-BRANCH-DISPLACEMENT). Require a nonnegative word displacement to a decoded instruction boundary, including for untaken edges. |
| VOP2 `V_AND_B32`, `V_XOR_B32` | Apply the bitwise operator to two VGPRs. |
| VOP2 `V_ADD_F32`, `V_MUL_F32` | Require exact binary32 results by checking the inputs and the wider intermediate. Multiplication of the significands fits in binary64. Addition must also recover the smaller operand by subtracting the larger. |
+20 -9
View File
@@ -1,12 +1,14 @@
# SOP2 scalar ALU subset
Xenora decodes and executes seven gfx1030 SOP2 forms. Each takes 32-bit
Xenora decodes and executes nine gfx1030 SOP2 forms. Each takes 32-bit
operands, writes a normal SGPR (s0-s105) and sets SCC.
| Form | D | SCC |
| --- | --- | --- |
| `s_add_u32` | S0 + S1 | carry-out of bit 31 |
| `s_sub_u32` | S0 - S1 | borrow (S1 > S0) |
| `s_add_i32` | S0 + S1 | signed overflow: S0 and S1 have the same sign and D's sign differs |
| `s_sub_i32` | S0 - S1 | signed overflow: S0 and S1 have opposite signs and D's sign differs from S0's |
| `s_addc_u32` | S0 + S1 + SCC | carry-out of bit 31 |
| `s_subb_u32` | S0 - S1 - SCC | borrow (S1 + SCC > S0) |
| `s_and_b32` | S0 & S1 | D != 0 |
@@ -18,6 +20,14 @@ Sources are normal SGPRs, inline integers (-16 to 64) or one literal word.
## Sources
- The AMD "RDNA 2" Instruction Set Architecture reference guide (GPUOpen,
`rdna2-shader-instruction-set-architecture.pdf`, 291 pages) gives the
pseudocode for every form in section 12.1, "SOP2 Instructions" (pages
101-102). Section 5.3, "Scalar Condition Code" (page 33), defines SCC as
signed overflow for signed add and subtract and as carry-out for the
unsigned forms. The unsigned rules below were derived before the guide was
available and agree with it.
- Opcodes, operand fields and which forms read and write SCC come from the
pinned AMD RDNA 2 XML (`crates/isa/generate_rdna2.py`). Its descriptions say
that additions "store the carry-out bit into SCC", ADDC adds "a carry-in bit
@@ -33,11 +43,6 @@ Sources are normal SGPRs, inline integers (-16 to 64) or one literal word.
## Not supported
- `s_add_i32` and `s_sub_i32`. The XML also says they store "the carry-out
bit", which for signed operands could mean the carry or signed overflow.
The RDNA 2 ISA guide's SOP2 pseudocode is understood to define signed
overflow. That has not been checked against the guide here, so both stay
rejected until a follow-up confirms it and adds overflow vectors.
- Every other SOP2 form, including the 64-bit bitwise forms.
- Destinations other than s0-s105 (VCC, EXEC, TTMP and M0).
- Sources other than normal SGPRs, inline integers and one literal.
@@ -55,15 +60,21 @@ IR and MSL contain the results, not scalar instructions.
## Verification
`signed_add_and_subtract_set_scc_on_overflow_not_carry` checks eight signed
cases worked by hand, including -1 + 1 and 0 - 1, which carry or borrow but do
not overflow. The reference checks that the exact signed result fits in i32.
The independent interpreter applies the guide's sign-bit rule.
`fixtures/rdna2/scalar-alu.s` is an original program, assembled by LLVM. It
chains a carry through `s_add_u32`/`s_addc_u32` and a borrow through
`s_sub_u32`/`s_subb_u32`. It also covers aliased destinations, inline and
literal operands, bitwise results that set and clear SCC, and an
`s_cbranch_scc0` that depends on them. Its expected output, input ^ 0x8000000e,
`s_cbranch_scc0` that depends on them. It also includes two signed overflows.
Its expected output, input ^ 0x0000000e,
was derived by hand.
`tests/validation/known_answers.rs` runs every supported form over 10 boundary
operands squared, with SCC clear and set. That is 1,400 programs. The same file
operands squared, with SCC clear and set. That is 1,800 programs. The same file
covers aliasing, inline and literal operands, and rejected forms. Expected
values come from exact integer definitions in the test. Three implementations
must agree with them:
@@ -71,7 +82,7 @@ must agree with them:
- the reference interpreter (exact integers, reduced modulo 2^32);
- the independent interpreter (a bit-serial ripple-carry adder).
`scalar_alu_native_metal_known_answers` runs the fixture, the 1,400 programs
`scalar_alu_native_metal_known_answers` runs the fixture, the 1,800 programs
and the aliasing and immediate cases on native Metal with API and shader
validation. `scripts/test-gpu.sh` runs it. This is local evidence from one
Apple M5 Pro; independent acceptance is pending.
+24
View File
@@ -171,6 +171,30 @@ recognized = true
decoded = true
llvm = "three-way passed"
[[instruction]]
form = "s_add_i32"
encoding = "SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = signed overflow"
representation = "`8110C109`"
lowering = "resolved scalar state and SCC"
reference = "implemented"
cpu = "local known-answer test passed"
gpu = "local Apple M5 readback passed; acceptance pending"
recognized = true
decoded = true
llvm = "three-way passed"
[[instruction]]
form = "s_sub_i32"
encoding = "SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; SCC = signed overflow"
representation = "`8191C110`"
lowering = "resolved scalar state and SCC"
reference = "implemented"
cpu = "local known-answer test passed"
gpu = "local Apple M5 readback passed; acceptance pending"
recognized = true
decoded = true
llvm = "three-way passed"
[[instruction]]
form = "s_addc_u32"
encoding = "SOP2, normal SGPR destination; normal SGPR, inline integer or literal sources with at most one literal; adds SCC; SCC = carry-out"
+8 -6
View File
@@ -1,7 +1,9 @@
0000: t0 = load.u32 input[index]
0034: branch.uniform 0x0040 taken=true
0040: t2 = const.u32 0x7ffffffe
0044: t3 = const.u32 0xfffffff0
0048: t4 = xor.b32 t2, t3
004c: t5 = xor.b32 t0, t4
0050: store.u32 output[index], t5
003c: branch.uniform 0x0048 taken=true
0048: t2 = const.u32 0x7ffffffe
004c: t3 = const.u32 0xfffffff0
0050: t4 = xor.b32 t2, t3
0054: t5 = const.u32 0x80000000
0058: t6 = xor.b32 t4, t5
005c: t7 = xor.b32 t0, t6
0060: store.u32 output[index], t7
+4 -2
View File
@@ -8,6 +8,8 @@ kernel void xenora_compute(
uint t2 = 0x7ffffffeu;
uint t3 = 0xfffffff0u;
uint t4 = t2 ^ t3;
uint t5 = t0 ^ t4;
output[index] = t5;
uint t5 = 0x80000000u;
uint t6 = t4 ^ t5;
uint t7 = t0 ^ t6;
output[index] = t7;
}
Binary file not shown.
+6 -1
View File
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Original SOP2 vectors: a carry chain through s_add_u32/s_addc_u32, a borrow
// chain through s_sub_u32/s_subb_u32, bitwise results feeding SCC, an SCC
// branch, and SGPR results moved into VGPRs. Expected: input ^ 0x8000000e.
// branch, signed overflow, and SGPR results moved into VGPRs.
// Expected: input ^ 0x0000000e.
.text
buffer_load_dword v1, v0, s[0:3], 0 idxen
s_waitcnt vmcnt(0)
@@ -13,6 +14,8 @@ s_subb_u32 s11, s9, s8 // s11 = 0x7ffffffe, SCC = 0
s_xor_b32 s12, s11, s10 // s12 = 0x80000001, SCC = 1
s_and_b32 s13, s12, s9 // s13 = 0x80000000, SCC = 1
s_or_b32 s14, s13, -16 // s14 = 0xfffffff0, SCC = 1
s_add_i32 s16, s9, -1 // s16 = 0x7fffffff, SCC = 1 (signed overflow)
s_sub_i32 s17, s16, -1 // s17 = 0x80000000, SCC = 1 (signed overflow)
s_and_b32 s15, s14, 15 // s15 = 0, SCC = 0
s_cbranch_scc0 .Lzero
v_mov_b32_e32 v2, 0
@@ -21,6 +24,8 @@ s_branch .Ldone
v_mov_b32_e32 v2, s11
v_mov_b32_e32 v3, s14
v_xor_b32_e32 v2, v2, v3
v_mov_b32_e32 v4, s17
v_xor_b32_e32 v2, v2, v4
v_xor_b32_e32 v2, v1, v2
.Ldone:
buffer_store_dword v2, v0, s[4:7], 0 idxen
+19 -4
View File
@@ -8,8 +8,9 @@
//! field layout and executes them with an explicit program-counter machine, so
//! a defect in the production decoder or reference cannot cancel out here.
//!
//! Scope matches the demo fixtures: scalar move/compare/branch, SOP2 add,
//! subtract, add-with-carry, subtract-with-borrow, AND, OR and XOR, VOP1 move
//! Scope matches the demo fixtures: scalar move/compare/branch, SOP2 unsigned
//! and signed add and subtract, add-with-carry, subtract-with-borrow, AND, OR
//! and XOR, VOP1 move
//! (including from an SGPR),
//! VOP2 bitwise, shift, arithmetic-shift and float, VOP3 add-no-carry, MUBUF
//! load/store and the two waits plus `s_endpgm`. Anything else is a controlled
@@ -30,7 +31,7 @@ enum Src {
#[derive(Clone, Copy, Debug)]
enum Insn {
SMov(u8, Src),
/// SOP2 opcode 0, 1, 4, 5, 14, 16 or 18.
/// SOP2 opcode 0-5, 14, 16 or 18.
SAlu(u32, u8, Src, Src),
SCmpEq(Src, Src),
BranchScc0(usize),
@@ -241,6 +242,20 @@ fn run_lane(program: &[(usize, Insn)], lane_input: u32, lane: usize) -> Result<u
let carry_in = || scc.ok_or_else(|| format!("{offset:#x}: uninitialized SCC"));
let (value, flag) = match opcode {
0 => ripple(a, b, false),
// RDNA 2 ISA guide 12.1: signed overflow from the sign bits,
// add: S0[31] == S1[31] != D[31]; subtract: S0[31] != S1[31] != D[31].
2 | 3 => {
let value = if opcode == 2 {
ripple(a, b, false).0
} else {
ripple(a, !b, true).0
};
let sign = |word: u32| word & 0x8000_0000 != 0;
let same = sign(a) == sign(b);
let overflow =
(if opcode == 2 { same } else { !same }) && sign(a) != sign(value);
(value, overflow)
}
4 => ripple(a, b, carry_in()?),
// a - b - borrow = a + !b + (1 - borrow); borrow-out = !carry-out.
1 | 5 => {
@@ -439,7 +454,7 @@ fn decode_one(code: &[u8], offset: usize) -> Result<(Insn, usize), String> {
return Ok((Insn::SMov(dst, src), len));
}
// SOP2: ENCODING 0b10 in bits 30-31, OP in bits 23-29, SDST in 16-22.
if first >> 30 == 0b10 && matches!((first >> 23) & 0x7f, 0 | 1 | 4 | 5 | 14 | 16 | 18) {
if first >> 30 == 0b10 && matches!((first >> 23) & 0x7f, 0..=5 | 14 | 16 | 18) {
let dst = ((first >> 16) & 0x7f) as u8;
if dst > 105 {
return Err(fail("unsupported SOP2 destination"));
+86 -10
View File
@@ -769,7 +769,7 @@ mod review {
const SCALAR_ALU_FIXTURE: &[u8] = include_bytes!("../../fixtures/rdna2/scalar-alu.bin");
// SOP2 opcodes of the supported subset (RDNA 2 XML opcode table).
const SOP2_OPCODES: [u32; 7] = [0, 1, 4, 5, 14, 16, 18];
const SOP2_OPCODES: [u32; 9] = [0, 1, 2, 3, 4, 5, 14, 16, 18];
const SCALAR_VALUES: [u32; 10] = [
0,
1,
@@ -796,6 +796,18 @@ mod review {
let (exact, scc) = match opcode {
0 | 4 => (a + b + c, a + b + c >= 1 << 32),
1 | 5 => (((1 << 32) + a - b - c) % (1 << 32), b + c > a),
// Signed: the exact result of the sign-extended operands must fit in
// [-2^31, 2^31), else SCC = overflow (RDNA 2 ISA guide 5.3 and 12.1).
2 | 3 => {
let signed = |value: u64| i64::from(value as u32 as i32);
let exact = if opcode == 2 {
signed(a) + signed(b)
} else {
signed(a) - signed(b)
};
let overflow = !(-(1_i64 << 31)..(1_i64 << 31)).contains(&exact);
(exact.rem_euclid(1 << 32) as u64, overflow)
}
14 => (a & b, a & b != 0),
16 => (a | b, a | b != 0),
_ => (a ^ b, a ^ b != 0),
@@ -842,8 +854,8 @@ mod review {
#[test]
fn scalar_alu_fixture_chains_carry_borrow_and_bitwise_scc() {
let input = [0, 1, u32::MAX, 0x8000_000e, 0x1234_5678];
let expected: Vec<_> = input.iter().map(|word| word ^ 0x8000_000e).collect();
let input = [0, 1, u32::MAX, 0x0000_000e, 0x1234_5678];
let expected: Vec<_> = input.iter().map(|word| word ^ 0x0000_000e).collect();
verify(SCALAR_ALU_FIXTURE, &input, Some(&expected));
let compiled = compile(Architecture::Gfx1030, SCALAR_ALU_FIXTURE).unwrap();
// The scalar results reach the kernel as constants; the branch is resolved.
@@ -852,8 +864,8 @@ mod review {
// Clearing the final AND's SCC-zero edge (s15 = s14 & 15 = 0) is observable:
// with 31 instead of 15 the result is 0x10, SCC = 1, and v2 = 0.
let mut nonzero = SCALAR_ALU_FIXTURE.to_vec();
assert_eq!(nonzero[0x31], 0x8f);
nonzero[0x31] = 0x9f;
assert_eq!(nonzero[0x39], 0x8f);
nonzero[0x39] = 0x9f;
verify(&nonzero, &input, Some(&[0; 5]));
}
@@ -904,17 +916,80 @@ mod review {
#[test]
fn scalar_alu_matches_definitions_on_boundary_operands() {
let vectors = sop2_vectors();
assert_eq!(vectors.len(), 7 * 10 * 10 * 2);
assert_eq!(vectors.len(), 9 * 10 * 10 * 2);
for (code, expected) in &vectors {
verify(code, &[0, u32::MAX], Some(expected));
assert_translated(code, expected);
}
}
#[test]
fn signed_add_and_subtract_set_scc_on_overflow_not_carry() {
// (opcode, S0, S1, D, SCC), worked by hand from RDNA 2 ISA guide 12.1.
for (opcode, a, b, value, scc) in [
(2, 0x7fff_ffff, 1, 0x8000_0000, true), // MAX + 1: overflow, no carry
(2, u32::MAX, 1, 0, false), // -1 + 1: carry, no overflow
(2, 0x8000_0000, u32::MAX, 0x7fff_ffff, true), // MIN + -1: overflow
(2, 0x8000_0000, 0x7fff_ffff, u32::MAX, false), // MIN + MAX = -1
(3, 0x8000_0000, 1, 0x7fff_ffff, true), // MIN - 1: overflow
(3, 0, 1, u32::MAX, false), // 0 - 1: borrow, no overflow
(3, 0x7fff_ffff, u32::MAX, 0x8000_0000, true), // MAX - -1: overflow
(3, 0x8000_0000, 0x8000_0000, 0, false), // MIN - MIN = 0
] {
let code = sop2_program(&[sop2(opcode, 10, 8, 9)], 10, a, b, Some(false));
let expected = lanes(value, scc);
assert_eq!(sop2_expected(opcode, a, b, false), (value, scc));
verify(&code, &[0, u32::MAX], Some(&expected));
assert_translated(&code, &expected);
}
for (code, expected) in signed_immediate_overflows() {
verify(&code, &[0, u32::MAX], Some(&expected));
assert_translated(&code, &expected);
}
}
/// Overflow through an inline and a literal source, observed by the branch.
fn signed_immediate_overflows() -> Vec<(Vec<u8>, [u32; 2])> {
vec![
// s_add_i32 s10, s8, 1 with s8 = MAX: 0x80000000, overflow.
(
sop2_program(&[sop2(2, 10, 8, 0x81)], 10, 0x7fff_ffff, 0, Some(false)),
lanes(0x8000_0000, true),
),
// s_sub_i32 s10, s8, 0x12345678 with s8 = MIN: 0x6dcba988, overflow.
(
sop2_program(
&[sop2(3, 10, 8, 255), 0x1234_5678],
10,
0x8000_0000,
0,
Some(false),
),
lanes(0x6dcb_a988, true),
),
// s_add_i32 s10, 0x40000000, s8 with s8 = 0x40000000: 0x80000000, overflow.
(
sop2_program(
&[sop2(2, 10, 255, 8), 0x4000_0000],
10,
0x4000_0000,
0,
Some(false),
),
lanes(0x8000_0000, true),
),
// s_sub_i32 s10, -16, s8 with s8 = MAX: 0x7ffffff1, overflow.
(
sop2_program(&[sop2(3, 10, 0xd0, 8)], 10, 0x7fff_ffff, 0, Some(false)),
lanes(0x7fff_fff1, true),
),
]
}
#[test]
fn scalar_alu_aliasing_inline_and_literal_operands() {
let cases = sop2_operand_cases();
assert_eq!(cases.len(), 7 * 10);
assert_eq!(cases.len(), 9 * 10);
for (code, expected) in &cases {
verify(code, &[0, u32::MAX], Some(expected));
assert_translated(code, expected);
@@ -945,8 +1020,8 @@ mod review {
#[test]
fn scalar_alu_rejects_forms_outside_the_subset() {
// load 8, wait 4, two literal moves 8 each, s_cmp 4: the SOP2 word is at 0x20.
for opcode in [2, 3, 15, 17, 19] {
// s_add_i32 and s_sub_i32 (SCC rule not confirmed), 64-bit bitwise forms.
for opcode in [6, 15, 17, 19] {
// s_min_i32 and the 64-bit bitwise forms.
rejected_at(
&sop2_program(&[sop2(opcode, 10, 8, 9)], 10, 1, 2, Some(false)),
0x20,
@@ -1012,10 +1087,11 @@ mod review {
.unwrap()
};
let input: Vec<u32> = (0..64).map(|lane| lane * 0x0101_0101).collect();
let expected: Vec<_> = input.iter().map(|word| word ^ 0x8000_000e).collect();
let expected: Vec<_> = input.iter().map(|word| word ^ 0x0000_000e).collect();
assert_eq!(run(SCALAR_ALU_FIXTURE, &input), expected);
let mut vectors = sop2_vectors();
vectors.extend(sop2_operand_cases());
vectors.extend(signed_immediate_overflows());
for (code, expected) in &vectors {
assert_eq!(&run(code, &[0, u32::MAX]), expected);
}