diff --git a/CHANGELOG.md b/CHANGELOG.md index 9448fa5..c025351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ remains unsupported. ### Added -- Added the gfx1030 SOPC compares `s_cmp_{eq,lg,gt,ge,lt,le}_{i32,u32}` through the semantic registry, which replaces the hand-written `s_cmp_eq_u32` path. Each sets only SCC to whether the relation holds with SSRC0 on the left (AMD guide section 12.4); the registry gained a `compare` operation with a declared `relation` and a `sopc_scalar` operand rule. `Instruction::ScalarCompareEqU32` is now `Instruction::ScalarCompare { op: ScalarCompareOp, .. }`; `s_cmp_eq_u32` behaves as before. The reference and independent semantic-review interpreters, written separately from the guide, evaluate all twelve, checked against 7,824 guide-derived cases and 348 rejections on CPU and native Metal with validation, plus a new LLVM-assembled fixture. Two literal selectors stay rejected. Part of #215, stage D. +- Added the gfx1030 SOPC compares `s_cmp_{eq,lg,gt,ge,lt,le}_{i32,u32}` through the semantic registry, which replaces the hand-written `s_cmp_eq_u32` path. Each sets only SCC to whether the relation holds with SSRC0 on the left (AMD guide section 12.4); the registry gained a `compare` operation with a declared `relation` and a `sopc_scalar` operand rule. `Instruction::ScalarCompareEqU32` is now `Instruction::ScalarCompare { op: ScalarCompareOp, .. }`; `s_cmp_eq_u32` behaves as before. The reference and independent semantic-review interpreters, written separately from the guide, evaluate all twelve, checked against 7,824 guide-derived cases on both CPU interpreters and on native Metal with validation; 348 rejected programs are refused by both interpreters and by the compiler. A new LLVM-assembled fixture covers all twelve. Two literal selectors stay rejected. Part of #215, stage D. - Added eight gfx1030 SOP2 forms through the semantic registry alone: `s_andn2_b32`, `s_orn2_b32`, `s_nand_b32`, `s_nor_b32`, `s_xnor_b32`, `s_lshl_b32`, `s_lshr_b32` and `s_ashr_i32`, each with SCC = result != 0. Each is one registry entry with no per-form Rust; the registry gained these primitives and a declared shift-count width (`shift_count_bits = 5`, so shifts use S1[4:0] as in the AMD guide, section 12.1), which the generator applies as a mask and checks against the XML's unsigned count operand. The reference interpreter and the independent semantic-review interpreter, written separately from the guide, now evaluate the same eight forms and are checked against 6,896 guide-derived cases; a new LLVM-assembled fixture (`fixtures/rdna2/scalar-logic.s`) runs on CPU and native Metal with validation. A NULL destination (s125) stays rejected. Part of #215, stage C. - Added an independent operand-domain verification harness for the 9 supported SOP2 scalar ALU forms and 12 VOPC integer compares (`tests/validation/tests/operand_domains.rs`, part of #215 stage E): 4,788 generated cases (boundary values, 64-bit carry and borrow chains, aliased registers, every integer inline constant, literals, s105 and v255, partial and zero EXEC, VCCZ) and 498 rejections (missing literal words, and encodings outside Xenora's supported subset) run on `xenora_ref` and, as an ignored test picked up by `scripts/test-gpu.sh`, on native Metal. Expected values come from the public AMD RDNA 2 guide's pseudocode, written out in the test without Xenora code; this is agreement with a spec-derived reference, not with Xbox hardware. Local verification only: 32 deliberate code breaks were each caught; the guide leaves the width of `S1 + SCC` in `s_subb_u32` open, and those 8 cases are kept in their own family. - Added a declarative semantic registry for gfx1030 instruction forms (`crates/isa/semantics/rdna2.toml`, schema 1) and its generator, `crates/isa/generate_semantics.py`, with `validate`, `generate` and `--check` modes. The generator checks every declaration against a closed vocabulary and the pinned AMD XML snapshot (opcode, family, SCC input and output, operand widths and signed formats), rejects contradictory, incomplete or duplicate definitions, and emits static Rust tables marked as generated. The nine SOP2 forms moved onto it with no change in behaviour: the decoder's SOP2 dispatch and `ScalarAluOp`'s mnemonic, SCC read and evaluation are now generated, and evaluation goes through shared primitives instead of per-opcode code. All existing SOP2 tests, including the native Metal known answers and the LLVM comparison, pass unchanged. Decoding speed is unchanged (10.2 ns before, 10.1 ns after, per SOP2 instruction, release build on an Apple M5 Pro). Also added `scripts/audit/semantic_coverage.py`, which generates `docs/shaders/semantic-coverage.md`: per-family counts of identified, declared, decoded, lowered, reference-verified and Metal-verified forms, taking evidence only from `docs/status.toml`. Part of #215, stages A and B. diff --git a/crates/isa/generate_semantics.py b/crates/isa/generate_semantics.py index a7158a0..4c46e75 100755 --- a/crates/isa/generate_semantics.py +++ b/crates/isa/generate_semantics.py @@ -262,9 +262,14 @@ def _check_xml(form, rule, snapshot, errors): errors.append(f"{label}: scc {form['scc']} contradicts the XML SCC output") if rule is None: return + if not isinstance(rule.get("src"), list) or not all( + isinstance(src, dict) and isinstance(src.get("field"), str) for src in rule["src"] + ) or ("dst" in rule and not (isinstance(rule["dst"], dict) and "field" in rule["dst"])): + return # the rule check already reported the malformed shape fields = [(rule["dst"]["field"], False)] if "dst" in rule else [] fields += [(src["field"], True) for src in rule["src"]] - count_field = rule["src"][1]["field"] if form["operation"] in SHIFTS else None + shift = form["operation"] in SHIFTS and len(rule["src"]) > 1 + count_field = rule["src"][1]["field"] if shift else None for field, is_source in fields: if field not in operands: errors.append(f"{label}: XML has no operand {field}") @@ -374,7 +379,7 @@ def validate(registry, snapshot): errors.append(f"{label}: {SHIFT_KEY} on a non-shift operation") if form["operation"] == "compare": relation = form.get(RELATION_KEY) - if relation not in RELATIONS: + if not isinstance(relation, str) or relation not in RELATIONS: errors.append(f"{label}: unknown or missing {RELATION_KEY} {relation!r}") elif (relation, form["signedness"]) not in COMPARE_PRIMITIVES: errors.append(f"{label}: no primitive implements {relation}/{form['signedness']}") diff --git a/docs/shaders/instruction-coverage.md b/docs/shaders/instruction-coverage.md index a9d39c2..12ae750 100644 --- a/docs/shaders/instruction-coverage.md +++ b/docs/shaders/instruction-coverage.md @@ -28,7 +28,7 @@ The wider LLVM operand matrix exercises decoding only. ABI means application bin | `s_cmp_ge_i32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 >= S1 (signed) | `BF030909` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | | `s_cmp_lt_i32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 < S1 (signed) | `BF040908` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | | `s_cmp_le_i32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 <= S1 (signed) | `BF050809` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | -| `s_cmp_lg_u32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 != S1 (unsigned) | `BF07C108` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | +| `s_cmp_lg_u32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 != S1 (unsigned) | `BF07FF0A 89ABCDEF` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | | `s_cmp_gt_u32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 > S1 (unsigned) | `BF080908` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | | `s_cmp_ge_u32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 >= S1 (unsigned) | `BF090809` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | | `s_cmp_lt_u32` | SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 < S1 (unsigned) | `BF0A0809` | yes | yes | three-way passed | resolved SCC | implemented | local known-answer test passed | local Apple M5 readback passed; acceptance pending | diff --git a/docs/shaders/reference-interpreter.md b/docs/shaders/reference-interpreter.md index 3fcbe41..7af039c 100644 --- a/docs/shaders/reference-interpreter.md +++ b/docs/shaders/reference-interpreter.md @@ -8,8 +8,8 @@ | SOPP `S_WAITCNT`, SOPK `S_WAITCNT_VSCNT` | A zero vector-load or vector-store threshold completes the associated transfer. The model permits one pending operation per counter (CLAIM-RDNA2-TRANSFER-WAITS). | | 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_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. | +| SOP1 `S_MOV_B32`, SOPC `S_CMP_{EQ,LG,GT,GE,LT,LE}_{I32,U32}` | Copy 32 bits into a scalar register, or compare SSRC0 with SSRC1 (signed for `_I32` ordering, unsigned for `_U32`; EQ and LG on the raw bits) and set only SCC. 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`, `S_ANDN2_B32`, `S_ORN2_B32`, `S_NAND_B32`, `S_NOR_B32`, `S_XNOR_B32`, `S_LSHL_B32`, `S_LSHR_B32`, `S_ASHR_I32` | Exact integer result reduced modulo 2^32; shifts use S1[4:0]; SCC is the carry-out, the borrow, signed overflow (the exact signed result does not fit in i32), or a nonzero logical or shift result, and ADDC/SUBB consume SCC ([SOP2 scalar ALU](scalar-alu.md)). Descriptor SGPRs are reserved. | | VOPC `V_CMP_{LT,EQ,LE,GT,NE,GE}_{I32,U32}` | Compare widened 64-bit values per active lane. VCC_LO gets EXEC & the test results and is fully written, so empty EXEC gives 0. A later `S_CBRANCH_VCCZ` on such a mask checks the skipped code statically ([VOPC integer compares](vector-compare.md)). | | 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. | diff --git a/docs/shaders/scalar-alu.md b/docs/shaders/scalar-alu.md index 7db07b1..8331f43 100644 --- a/docs/shaders/scalar-alu.md +++ b/docs/shaders/scalar-alu.md @@ -131,7 +131,9 @@ literal. Both sources selecting the literal encoding (255) is rejected for SOPC as for SOP2; whether the guide allows one shared literal there was not settled. `tests/validation/tests/sopc_stage_d.rs` checks 7,824 cases, with expected -values derived from the guide separately from the registry, plus 348 rejected -programs, on both CPU interpreters and, through the translator, on native -Metal. `fixtures/rdna2/scalar-compare.s` (LLVM-assembled, expected input ^ +values derived from the guide separately from the registry, on both CPU +interpreters and, through the translator, on native Metal (compiled per case +and read back from nine distinct generated kernels). Its 348 rejected programs +are checked on both CPU interpreters and against the compiler, which rejects +them before any Metal dispatch. `fixtures/rdna2/scalar-compare.s` (LLVM-assembled, expected input ^ 0x00000d5b) records every compare's SCC through an `s_cbranch_scc0` per form. diff --git a/docs/shaders/semantic-registry.md b/docs/shaders/semantic-registry.md index a12ab9b..c5ec0f1 100644 --- a/docs/shaders/semantic-registry.md +++ b/docs/shaders/semantic-registry.md @@ -57,7 +57,7 @@ error. | `width` | Operand and result width in bits | `32` | | `relation` | Compares only: the relation tested with S0 on the left | `eq`, `lg` (not equal), `gt`, `ge`, `lt`, `le`; required for `compare`, rejected elsewhere | | `shift_count_bits` | Shifts only: the low bits of S1 used as the count. The generator masks S1 with it, and the XML must list S1 as unsigned | `5` (log2 of `width`); required for shifts, rejected elsewhere | -| `scc` | What the form writes to SCC | `carry_out`, `borrow`, `signed_overflow`, `nonzero`, `unchanged` | +| `scc` | What the form writes to SCC | `carry_out`, `borrow`, `signed_overflow`, `nonzero`, `relation` (compares only), `unchanged` | | `vcc`, `exec` | Effect on VCC and EXEC, stated rather than implied | `unchanged` | | `wave` | Wave sizes the form is valid in | `any` (`wave32` is in the vocabulary but rejected until the decoder can enforce it) | | `operands` | Name of an `[operand_rules]` entry for the same family | e.g. `sop2_sgpr` | diff --git a/docs/status.toml b/docs/status.toml index 5a1621c..29e8359 100644 --- a/docs/status.toml +++ b/docs/status.toml @@ -222,7 +222,7 @@ llvm = "three-way passed" [[instruction]] form = "s_cmp_lg_u32" encoding = "SOPC; normal SGPR, inline integer or literal sources with at most one literal; SCC = S0 != S1 (unsigned)" -representation = "`BF07C108`" +representation = "`BF07FF0A 89ABCDEF`" lowering = "resolved SCC" reference = "implemented" cpu = "local known-answer test passed" diff --git a/fixtures/golden/scalar-compare.ir b/fixtures/golden/scalar-compare.ir index 860cea4..5a96ab4 100644 --- a/fixtures/golden/scalar-compare.ir +++ b/fixtures/golden/scalar-compare.ir @@ -6,11 +6,11 @@ 004c: branch.uniform 0x0054 taken=false 0058: branch.uniform 0x0060 taken=true 0064: branch.uniform 0x006c taken=false -0070: branch.uniform 0x007c taken=true -0080: branch.uniform 0x008c taken=false -0090: branch.uniform 0x009c taken=true -00a0: branch.uniform 0x00ac taken=false -00b0: branch.uniform 0x00bc taken=false -00bc: t13 = const.u32 0x00000d5b -00c0: t14 = xor.b32 t0, t13 -00c4: store.u32 output[index], t14 +007c: branch.uniform 0x0088 taken=true +008c: branch.uniform 0x0098 taken=false +009c: branch.uniform 0x00a8 taken=true +00ac: branch.uniform 0x00b8 taken=false +00bc: branch.uniform 0x00c8 taken=false +00c8: t13 = const.u32 0x00000d5b +00cc: t14 = xor.b32 t0, t13 +00d0: store.u32 output[index], t14 diff --git a/fixtures/rdna2/scalar-compare.bin b/fixtures/rdna2/scalar-compare.bin index fe70a6a..e4e1f1f 100644 Binary files a/fixtures/rdna2/scalar-compare.bin and b/fixtures/rdna2/scalar-compare.bin differ diff --git a/fixtures/rdna2/scalar-compare.s b/fixtures/rdna2/scalar-compare.s index 45f0de5..1b6950b 100644 --- a/fixtures/rdna2/scalar-compare.s +++ b/fixtures/rdna2/scalar-compare.s @@ -38,7 +38,8 @@ s_cmp_eq_u32 s9, 1 // 1 == 1: SCC = 1 s_cbranch_scc0 .Lskip6 s_or_b32 s20, s20, 64 .Lskip6: -s_cmp_lg_u32 s8, 0xffffffff // 0xffffffff != 0xffffffff: SCC = 0 +s_mov_b32 s10, 0x89abcdef +s_cmp_lg_u32 s10, 0x89abcdef // 0x89abcdef != 0x89abcdef: SCC = 0 (literal) s_cbranch_scc0 .Lskip7 s_or_b32 s20, s20, 0x80 .Lskip7: diff --git a/tests/semantics_generator/test_generate_semantics.py b/tests/semantics_generator/test_generate_semantics.py index 5c3ffe6..4eb154c 100644 --- a/tests/semantics_generator/test_generate_semantics.py +++ b/tests/semantics_generator/test_generate_semantics.py @@ -392,6 +392,16 @@ class Validate(unittest.TestCase): reg["form"][-1]["relation"] = "ne" self.rejects(reg, "S_CMP_LT_U32: unknown or missing relation 'ne'") + def test_rejects_a_non_string_relation(self): + reg = compares() + reg["form"][-1]["relation"] = ["lt"] + self.rejects(reg, "S_CMP_LT_U32: unknown or missing relation ['lt']") + + def test_rejects_a_rule_without_sources(self): + reg = compares() + del reg["operand_rules"]["sopc_scalar"]["src"] + self.rejects(reg, "operand rule sopc_scalar: declaration differs from the decoder's implemented rule") + def test_rejects_a_relation_on_a_non_compare(self): reg = compares() reg["form"][0]["relation"] = "lt"