From 425327a0f2f79e6a2bbbd43c19dd3238bc142f45 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:46:56 -0600 Subject: [PATCH] shader: add s_add_i32 and s_sub_i32 with signed-overflow SCC #185 left both forms unsupported because the pinned XML says only "carry-out" for them. The AMD RDNA 2 ISA reference guide (GPUOpen, section 12.1 SOP2 Instructions, p. 101-102, and section 5.3 Scalar Condition Code, p. 33) defines SCC for both as signed overflow: add overflows when the operands share a sign that the result lacks; subtract when the operands' signs differ and the result's sign differs from S0's. The guide also confirms the unsigned rules used in #185. Adds both forms to the generated opcodes, the decoder, the reference (exact signed result must fit in i32), the independent interpreter (the guide's sign-bit rule over its ripple adder) and the translator, plus 400 boundary programs, eight hand-worked overflow and no-overflow cases (-1 + 1 and 0 - 1 carry or borrow but do not overflow) and two signed overflows in the scalar ALU fixture. Inverting the overflow rule in any one implementation fails three tests. s_min_i32 and the other SOP2 forms stay unsupported. Refs #185 Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- CHANGELOG.md | 3 +- crates/decoder/src/lib.rs | 19 ++--- crates/decoder/tests/gfx1030.rs | 12 ++-- crates/isa/generate_rdna2.py | 2 + crates/isa/src/lib.rs | 29 ++++++-- crates/isa/src/rdna2.rs | 10 +++ crates/ref/src/lib.rs | 19 ++++- docs/assets/xenora-progress.svg | 8 +-- docs/shaders/instruction-coverage.md | 8 ++- docs/shaders/reference-interpreter.md | 2 +- docs/shaders/scalar-alu.md | 29 +++++--- docs/status.toml | 24 +++++++ fixtures/golden/scalar-alu.ir | 14 ++-- fixtures/golden/scalar-alu.metal | 6 +- fixtures/rdna2/scalar-alu.bin | Bin 96 -> 112 bytes fixtures/rdna2/scalar-alu.s | 7 +- tests/validation/independent_ref.rs | 23 ++++-- tests/validation/known_answers.rs | 96 +++++++++++++++++++++++--- 18 files changed, 248 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edda0e6..bf0a768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/crates/decoder/src/lib.rs b/crates/decoder/src/lib.rs index 0eaf77c..6517516 100644 --- a/crates/decoder/src/lib.rs +++ b/crates/decoder/src/lib.rs @@ -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, diff --git a/crates/decoder/tests/gfx1030.rs b/crates/decoder/tests/gfx1030.rs index 0b3af3c..6da7805 100644 --- a/crates/decoder/tests/gfx1030.rs +++ b/crates/decoder/tests/gfx1030.rs @@ -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 { 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(), diff --git a/crates/isa/generate_rdna2.py b/crates/isa/generate_rdna2.py index 319ac7c..7cb8690 100644 --- a/crates/isa/generate_rdna2.py +++ b/crates/isa/generate_rdna2.py @@ -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"), diff --git a/crates/isa/src/lib.rs b/crates/isa/src/lib.rs index bd7d041..a2e5c9d 100644 --- a/crates/isa/src/lib.rs +++ b/crates/isa/src/lib.rs @@ -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), diff --git a/crates/isa/src/rdna2.rs b/crates/isa/src/rdna2.rs index 28026d2..33571b4 100644 --- a/crates/isa/src/rdna2.rs +++ b/crates/isa/src/rdna2.rs @@ -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, diff --git a/crates/ref/src/lib.rs b/crates/ref/src/lib.rs index 83c694a..d187730 100644 --- a/crates/ref/src/lib.rs +++ b/crates/ref/src/lib.rs @@ -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, 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, diff --git a/docs/assets/xenora-progress.svg b/docs/assets/xenora-progress.svg index d9538fc..831b1dc 100644 --- a/docs/assets/xenora-progress.svg +++ b/docs/assets/xenora-progress.svg @@ -1,17 +1,17 @@ Xenora · recorded progress -Decoded gfx1030 forms: 28 of 28. Accepted Metal forms · Apple M5: 18 of 28. Accepted guest milestones · X1–X4: 4 of 4 +Decoded gfx1030 forms: 30 of 30. Accepted Metal forms · Apple M5: 18 of 30. Accepted guest milestones · X1–X4: 4 of 4 RECORDED FIXTURE PROGRESS Decoded gfx1030 forms -28 / 28 +30 / 30 Accepted Metal forms · Apple M5 -18 / 28 +18 / 30 - + Accepted guest milestones · X1–X4 4 / 4 diff --git a/docs/shaders/instruction-coverage.md b/docs/shaders/instruction-coverage.md index 9511314..ad421c2 100644 --- a/docs/shaders/instruction-coverage.md +++ b/docs/shaders/instruction-coverage.md @@ -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. diff --git a/docs/shaders/reference-interpreter.md b/docs/shaders/reference-interpreter.md index 1e1ec8e..cb88e33 100644 --- a/docs/shaders/reference-interpreter.md +++ b/docs/shaders/reference-interpreter.md @@ -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. | diff --git a/docs/shaders/scalar-alu.md b/docs/shaders/scalar-alu.md index 6f90425..c9e3233 100644 --- a/docs/shaders/scalar-alu.md +++ b/docs/shaders/scalar-alu.md @@ -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. diff --git a/docs/status.toml b/docs/status.toml index 8a4e2a0..e4be518 100644 --- a/docs/status.toml +++ b/docs/status.toml @@ -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" diff --git a/fixtures/golden/scalar-alu.ir b/fixtures/golden/scalar-alu.ir index b41b9f9..c416873 100644 --- a/fixtures/golden/scalar-alu.ir +++ b/fixtures/golden/scalar-alu.ir @@ -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 diff --git a/fixtures/golden/scalar-alu.metal b/fixtures/golden/scalar-alu.metal index 0072fb2..ef611db 100644 --- a/fixtures/golden/scalar-alu.metal +++ b/fixtures/golden/scalar-alu.metal @@ -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; } diff --git a/fixtures/rdna2/scalar-alu.bin b/fixtures/rdna2/scalar-alu.bin index e8cf87909d24a779abfe51ca66ba57c62f73f5fb..6493e0ced0f9494130b20a606105891b33934468 100644 GIT binary patch delta 69 zcmYc&m|)<*c~GEH;NZkYzJC68CWe;%4NNR`Yz$5Nxq&nv6I&eV@OL)@ HL*sq`WP%MF diff --git a/fixtures/rdna2/scalar-alu.s b/fixtures/rdna2/scalar-alu.s index 7a99535..5f46966 100644 --- a/fixtures/rdna2/scalar-alu.s +++ b/fixtures/rdna2/scalar-alu.s @@ -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 diff --git a/tests/validation/independent_ref.rs b/tests/validation/independent_ref.rs index 93cb167..5753bf0 100644 --- a/tests/validation/independent_ref.rs +++ b/tests/validation/independent_ref.rs @@ -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 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")); diff --git a/tests/validation/known_answers.rs b/tests/validation/known_answers.rs index c728900..2e8e845 100644 --- a/tests/validation/known_answers.rs +++ b/tests/validation/known_answers.rs @@ -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, [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 = (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); }