* isa, decoder, shader: move the twelve Wave32 VOPC compares into the semantic registry (#215) The v_cmp_{lt,eq,le,gt,ne,ge}_{i32,u32}_e32 forms from #239 are now registry data under a new vopc_vcc operand rule (SRC0 a vector_source, VSRC1 a VGPR, destination implicitly vcc_lo). The decoder dispatches them through the generated VectorCompareOp instead of a hand-written opcode table, and the hand-written VectorCompareOp struct in xenora-isa is gone. No behaviour change: goldens, known answers and the LLVM oracle are unchanged. The vocabulary gains vcc = "lane_mask", allowed exactly for the VOPC rule and required to account for the XML's implicit VCC output, and wave = "wave32", now allowed exactly for that rule (its one entry point is the Wave32 compute fixture ABI) and still rejected elsewhere. exec must stay unchanged, so v_cmpx cannot be declared. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * test(vopc): independent oracle for v_cmp_{f,t}_{i32,u32}_e32 (#215) The registry stage adds the four constant 32-bit integer VOPC compares, which nothing could check: the reference and the second interpreter both rejected them. Derive them from the RDNA 2 guide only (12.9 Table 61: D = 0 / D = 1; 3.9: VCC[n] = EXEC[n] & test, fully written) under the #239 Wave32 contract, so T writes EXEC_LO masked to existing lanes, F writes 0, and sources keep the allowlist and initialization rules. vopc_f_t.rs covers every SRC0 class, aliasing, full, partial and empty EXEC with a stale VCC_LO, a partial final wave, s_cbranch_vccz on both edges and the rejections; the native Metal part is ignored until the translator side lands. known_answers no longer expects F and T to be rejected. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, decoder, shader: v_cmp_{f,t}_{i32,u32} through the semantic registry (#215) Adds the four constant Wave32 VOPC compares as registry forms with the relations f and t (guide Table 59 offsets 0 and 7), valid only for the vopc_vcc rule, backed by false_b32/true_b32 primitives. They keep the #239 contract: sources are decoded and checked as for every compare, VCC_LO := EXEC & existing lanes & test, fully written, and the mask is lane-derived. Their MSL wave mask reads no operand bits. A new LLVM-assembled fixture covers F clearing a stale VCC, T and F under partial EXEC, T reading vcc_lo and runtime s_cbranch_vccz on both results; the oracle merged from task/215-vopc-oracle supplies the reference side. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * test(vopc): cover EXEC preservation, known-0 branch, pending loads (#215) Review found four ways F/T could be wrong and still pass: clearing EXEC under partial EXEC, treating the empty-EXEC result as lane-derived, ignoring an outstanding load on a source, and decoding opcode 200. Add a case or rejection for each; each was shown failing with that break. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa: check VOPC relations against the Table 59 opcode layout and the VCC write direction (#215) Review fixes. A VOPC compare's opcode must equal its group base (128 signed, 192 unsigned) plus its relation's Table 59 offset, so a row such as V_CMP_T_I32 with relation f is rejected instead of lowering to false. A lane_mask form now needs exactly one fieldless XML VCC operand that is written and not read. The generator tests render the real F/T rows, and the vector-compare doc names the file each count comes from. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --------- Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
15 KiB
Adding an instruction through the semantic registry
The semantic registry is a data file that declares what each supported gfx1030 instruction form does. A generator checks it and turns it into Rust tables that the decoder uses. This page explains where entries live, what each field means, how to check and regenerate the tables, and what the generator does not do for you.
The registry covers the nine SOP2 forms listed in SOP2 scalar ALU.
Files
| File | Role |
|---|---|
crates/isa/semantics/rdna2.toml |
The registry. Data only: every value comes from a closed vocabulary. |
crates/isa/generate_semantics.py |
Validates the registry and writes the Rust tables. Python 3.11 or newer, standard library only. |
crates/isa/data/rdna2.jsonl |
Pinned snapshot of the AMD machine-readable ISA XML, used to cross-check identity and operands. |
crates/isa/src/semantics_gen.rs |
Generated output. Never edit it by hand. |
crates/isa/src/semantics.rs |
The Rust types the generated tables use (FormSemantics, Operation, SccEffect and so on). |
crates/isa/src/primitive.rs |
The shared integer primitives that the generated evaluate calls. |
tests/semantics_generator/ |
Unit tests for the generator. |
Fields
The top of the file holds:
schema: must be1.architecture: must be"gfx1030".source_sha256: must equal thesha256on thesourceline ofcrates/isa/data/rdna2.jsonl, so the registry is always checked against the snapshot it was written for.[documents.<name>]: the public documents that spec references cite. Onlyguide(the AMD RDNA 2 ISA reference guide) andxml(the AMD machine-readable ISA) are allowed, and each must match the title and file pinned in the generator, so a citation cannot point at another file.[operand_rules.<name>]: an operand rule shared by a whole family. The decoder implements each rule once, and the generator only accepts a rule whose declaration matches the decoder's implementation exactly. Today there is one rule,sop2_sgpr: an SGPR destinations0-s105, two scalar sources (an SGPR, an inline integer -16 to 64, or a literal word) and at most one literal. Itsunsupportedlist names the encodings the rule rejects, which stay unsupported instead of being guessed. The list must name exactly the encodings the decoder rejects.
Each [[form]] entry has exactly these fields. A missing or unknown field is an
error.
| Field | Meaning | Allowed values |
|---|---|---|
name |
XML instruction name, the form's stable identity | upper-case, e.g. S_ADD_U32 |
family |
Encoding family; must match the XML base encoding | e.g. SOP2 |
opcode |
Opcode; must match the XML | non-negative integer |
variant |
Rust enum variant of the typed operation | CamelCase, e.g. AddU32 |
operation |
The primitive the form uses | add, sub, and, or, xor, andn2, orn2, nand, nor, xnor, lshl, lshr, ashr, min, max, cselect, bfm, mul, mul_hi, bfe, absdiff, lshl_add, mul24, mul_hi24 (vector only), compare and the SOP1 unaries |
signedness |
How operands are read | unsigned, signed, bitwise (always bitwise for the logical operations, cselect and bfm; never for the arithmetic ones, add, sub, min, max, mul, mul_hi, bfe, absdiff and lshl_add; shifts take their primitive's signedness) |
carry_in |
Whether the form reads SCC: carry-in, borrow-in or the select condition | none, scc (scc only for unsigned add and sub, and required for cselect) |
width |
Operand and result width in bits | 32 |
source_bits |
Sign extension only: the low bits of S0 that are read. The XML must list S0 with that size and format (FMT_NUM_I8 or FMT_NUM_I16) |
8, 16; required for sext, rejected elsewhere |
relation |
Compares only: the relation tested with S0 on the left | eq, lg (not equal), gt, ge, lt, le; required for compare, rejected elsewhere |
source_order |
Vector forms only: reversed when the guide computes op(S1, S0) (the XML records no operand order); absent otherwise |
reversed |
shift_amount |
lshl_add only: the fixed left shift of S0 before the add |
1 to 4; required for lshl_add, 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, relation (SOPC compares only), selected_src0 (min and max: S0 won the strict compare, so a tie gives 0), unchanged |
vcc, exec |
Effect on VCC and EXEC, stated rather than implied | unchanged; vcc is lane_mask exactly for the VOPC compares (VCC_LO bit n = EXEC[n] & test, fully written; VCC_HI unknown afterwards, see vector-compare.md); exec must be unchanged (no EXEC-writing contract yet) |
wave |
Wave sizes the form is valid in | wave32 exactly for the VOPC compares (their one entry point is the Wave32 compute fixture ABI, vector-compare.md section 2), any for everything else; the decoder has no wave-size input, so no other form may claim wave32 |
operands |
Name of an [operand_rules] entry for the same family |
e.g. sop2_sgpr |
spec |
References { document, section, page } |
at least one guide reference with a page above 0 |
verify |
Evidence the form needs before it counts as verified | reference, independent, metal, llvm_encoding |
The operation and signedness pair picks one primitive, and that primitive
fixes the scc value:
operation / signedness |
Required scc |
Primitive | Reads carry-in |
|---|---|---|---|
add / unsigned |
carry_out |
add_carry_u32 |
allowed |
sub / unsigned |
borrow |
sub_borrow_u32 |
allowed |
add / signed |
signed_overflow |
add_overflow_i32 |
no |
sub / signed |
signed_overflow |
sub_overflow_i32 |
no |
and, or, xor, andn2, orn2, nand, nor, xnor / bitwise |
nonzero |
and_nonzero_b32, or_nonzero_b32, xor_nonzero_b32, andn2_nonzero_b32, orn2_nonzero_b32, nand_nonzero_b32, nor_nonzero_b32, xnor_nonzero_b32 |
no |
lshl / bitwise |
nonzero |
lshl_nonzero_b32 |
no |
lshr / unsigned |
nonzero |
lshr_nonzero_b32 |
no |
ashr / signed |
nonzero |
ashr_nonzero_i32 |
no |
not, bcnt0, bcnt1 / bitwise |
nonzero |
not_nonzero_b32, bcnt0_nonzero_b32, bcnt1_nonzero_b32 |
no |
brev, ff0, ff1, flbit / bitwise |
unchanged |
brev_b32, ff0_b32, ff1_b32, flbit_b32 |
no |
flbit, sext / signed |
unchanged |
flbit_i32, sext_i32 |
no |
abs / signed |
nonzero |
abs_nonzero_i32 |
no |
min, max / signed or unsigned |
selected_src0 |
min_i32/min_u32, max_i32/max_u32 |
no |
cselect / bitwise |
unchanged |
cselect_b32 |
required |
bfm / bitwise |
unchanged |
bfm_b32 |
no |
mul / signed |
unchanged |
mul_lo_i32 |
no |
mul_hi / unsigned or signed |
unchanged |
mul_hi_u32, mul_hi_i32 |
no |
bfe / unsigned or signed |
nonzero |
bfe_nonzero_u32, bfe_nonzero_i32 |
no |
absdiff / signed |
nonzero |
absdiff_nonzero_i32 |
no |
lshl_add / unsigned |
carry_out |
lshl_add_carry_u32 (with shift_amount) |
no |
compare / signed or unsigned |
relation |
eq_b32, ne_b32 (for both signednesses), gt_i32/gt_u32, ge_*, lt_*, le_* |
no |
Vector forms (operand rule vop2_vgpr, kind vector) use a separate table:
the pair picks the per-lane VectorBinary the MSL lowering uses, and scc
must be unchanged (VOP2 integer forms write no SCC, VCC or EXEC):
operation / signedness |
VectorBinary |
|---|---|
add / unsigned, sub / unsigned |
Add, Sub |
xnor / bitwise |
Xnor |
min, max / signed or unsigned |
MinSigned, MinUnsigned, MaxSigned, MaxUnsigned |
mul24, mul_hi24 / signed or unsigned |
Mul24Signed, Mul24Unsigned, MulHi24Signed, MulHi24Unsigned |
VOPC compares (operand rule vopc_vcc, kind vector_compare: SRC0 a
vector_source as for VOP2, VSRC1 a VGPR, destination implicitly vcc_lo)
take the same relation and primitive as SOPC compares, but declare
scc = "unchanged", vcc = "lane_mask" and wave = "wave32". The XML's implicit
64-bit VCC output must be present and is accounted for by lane_mask. Signedness
still matters for eq and lg, because the MSL lowering casts signed operands.
v_cmpx_* (EXEC writes), the VOP3 encoding and Wave64 stay unsupported.
A SOPC compare must use an operand rule of the compare kind (sopc_scalar: two
sources, no destination), a unary operation one of the unary kind (sop1_sgpr:
one source and an SGPR destination), and a two-source ALU operation
sop2_sgpr.
Behaviour is chosen only from these declared fields, never from the mnemonic.
What the validator checks
validate reports every error it finds, not only the first (a form with a missing field is not checked further). Besides the
vocabularies above, it rejects:
- contradictions: a declared
sccorcarry_inthat the primitive does not have,bitwiseon an arithmetic operation or the reverse; - VOPC compares whose opcode is not the group base (128 for
signed, 192 forunsigned) plus the relation's offset in guide Table 59 (F 0, LT 1, EQ 2, LE 3, GT 4, LG/NE 5, GE 6, T 7), and alane_maskform whose XML does not have exactly one implicit VCC operand that is written and not read; - duplicates: the same
name, the same(family, opcode), or the samevarianttwice in one generated enum; - XML disagreements: a family with no XML encoding, a form with no default XML
variant, a different opcode, an SCC input or output the XML does not show (or
that the declaration omits), an operand width other than
width, a signed declaration on unsigned XML formats or the reverse (a shift count or thebfefield descriptor in S1 must beFMT_NUM_U32whatever the signedness), an operand direction that differs, and XML operands the operand rule does not cover; - references: a spec citing an undeclared document, a bad section or page, or no guide page at all;
- operand rules the decoder does not implement, or whose declaration differs from the implemented one;
- wrong types: a float width, a boolean schema, an opcode or page outside
0-65535, or a reserved variant name such as
Self.
CI runs generate_semantics.py --check, scripts/audit/semantic_coverage.py --check and both Python test suites next to the existing
generate_rdna2_meta.py --check. Both scripts stop with a clear message on
Python older than 3.11.
What the generator does not do
- The XML gives identity and encodings only. The snapshot confirms the
name, family, opcode, operand fields, widths, number formats and whether SCC
is read or written. It says nothing about the arithmetic. Behaviour comes from
the AMD guide, cited in
spec, through the closed primitive table. - It does not invent behaviour. A form whose operation, signedness or
operand rule is not already implemented is rejected. Adding a new operation
means hand-writing a primitive in
primitive.rs, a variant insemantics.rsand a row in the generator's vocabulary tables, reviewed like any other code. - Undeclared forms stay unsupported. The generated
from_wordreturnsNonefor every opcode not in the registry, and the decoder reportsUnsupportedOpcode. Nothing falls back to a guess or a no-op. - It does not write tests or reference semantics. The reference
interpreter (
crates/ref) and the independent interpreter (tests/validation/independent_ref.rs) never read the registry or the generated code, and their known-answer tests are written by hand from the guide. That independence is what lets them catch a wrong registry entry; keep it when you add a form. Agreement with these references is agreement with a reading of the public AMD guide, not with Xbox hardware.
Commands
Run these from the repository root. python3 must be 3.11 or newer.
# Check the registry only.
python3 crates/isa/generate_semantics.py validate
# Validate, then rewrite crates/isa/src/semantics_gen.rs.
python3 crates/isa/generate_semantics.py generate
# Validate and regenerate in memory; exit 1 if the committed file is stale. Never writes.
python3 crates/isa/generate_semantics.py --check
# Generator unit tests.
python3 -m unittest discover -s tests/semantics_generator
# The XML metadata tables the registry is cross-checked against are still current.
python3 crates/isa/generate_rdna2_meta.py --check
# Rust tests of the generated tables and the decoder that uses them.
cargo test --locked -p xenora-isa -p xenora-decoder
# Independent CPU validation.
cargo test --locked -p xenora-semantic-review
# Native GPU correctness on Apple silicon.
scripts/test-gpu.sh
The LLVM encoding corpus check needs an LLVM build with the AMDGPU target:
python3 crates/isa/tests/llvm/corpus.py --llvm-bin /path/to/llvm/bin --check
Worked example: s_addc_u32
s_addc_u32 adds two 32-bit sources plus SCC and writes the carry-out of
bit 31 to SCC (guide section 12.1, page 102; SCC rules in section 5.3, page 33).
Its registry entry is:
[[form]]
name = "S_ADDC_U32"
family = "SOP2"
opcode = 4
variant = "AddcU32"
operation = "add"
signedness = "unsigned"
carry_in = "scc"
width = 32
scc = "carry_out"
vcc = "unchanged"
exec = "unchanged"
wave = "any"
operands = "sop2_sgpr"
spec = [
{ document = "guide", section = "12.1", page = 102 },
{ document = "guide", section = "5.3", page = 33 },
]
verify = ["reference", "independent", "metal", "llvm_encoding"]
How each field is checked:
- The XML snapshot has a default
ENC_SOP2variant namedS_ADDC_U32with opcode 4, soname,familyandopcodeagree. - That XML variant reads and writes SCC, which matches
carry_in = "scc"and ansccother thanunchanged. add/unsignedselectsadd_carry_u32, which requiresscc = "carry_out"and accepts a carry-in.- The XML
SDST,SSRC0andSSRC1operands are 32-bitFMT_NUM_U32fields, which matcheswidth = 32,unsignedand thesop2_sgprrule.
From this entry the generator emits, in semantics_gen.rs, the AddcU32
variant of ScalarAluOp, its from_word arm (0x8200_0000), the mnemonic
s_addc_u32, reads_scc() == true, an evaluate arm that calls
primitive::add_carry_u32(src0, src1, carry_in), and a FormSemantics row.
To add a form the same way:
- Confirm its behaviour in the AMD guide and note the section and page.
- Check that an existing primitive and operand rule cover it. If not, the form needs hand-written code first (see above).
- Add a
[[form]]entry and runvalidateuntil it reports no errors. - Run
generateand commit the registry andsemantics_gen.rstogether. - Add known-answer tests to the reference and independent interpreters by hand, then run every command in Commands.