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>
This commit is contained in:
elforeign committed 2026-11-24 01:48:52 -06:00
1 parent 1212280ab8
commit 425327a0f2
18 files changed
+248 -63

No files matched your search

+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(),