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

+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"