* 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> * isa: split the semantic registry into one file per encoding family (#215) crates/isa/semantics/rdna2.toml keeps the schema, architecture, snapshot hash, cited documents and field meanings, and lists the family files under `include`: rdna2/{sop2,sopc,sop1,vop2,vopc}.toml, each holding only the operand rules and forms of the family it is named after. The generator merges them in include order, and rejects a form or rule of another family, a rule declared twice, root keys in a family file and a missing or repeated include. The merged registry equals the former single file, and semantics_gen.rs is byte-identical (generate_semantics.py --check passes unchanged). The coverage report and the RDNA2 planner read the merged registry; their output is unchanged. Parallel work on different families no longer edits the same file. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, decoder, shader: registry encoding variants; v_add_nc_u32_e64 as the first (#215) An [[encoding_variant]] re-encodes a declared form in another family: it names the form, the family, its opcode there and that encoding's operand rule, and inherits the rest. The XML check runs on the inherited declaration, and the generator rejects variants of undeclared forms, in the form's own family, duplicates, rules that cannot encode the form and forms that use a variant-only rule. It emits from_<family>_word and <family>_assembly_name on the base enum and one FORMS row per variant. rdna2/vop3.toml declares the vop3_vgpr2 rule (the subset the hand-written decoder accepted: VGPR sources, no modifiers, unused SRC2) and the VOP3 encoding of V_ADD_NC_U32. The decoder dispatches it through VectorAluOp::from_vop3_word into Instruction::VectorAluVop3, which replaces AddNoCarryU32; the shader lowers it through the VOP2 form's template and still admits only v_add_nc_u32_e64 inside a runtime s_cbranch_vccz region. Goldens, known answers and the LLVM oracle are unchanged. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, scripts: review fixes for the registry split and encoding variants (#215) - Includes must be `<root stem>/<family>.toml`, so a family file cannot live outside the registry directory or be aliased past the repeat guard. The coverage report (and through it the RDNA2 planner) now uses the generator's loader, so the family-file guards apply to every consumer. - A variant's explicit XML operand formats, destination included, must equal its base encoding's; malformed variant fields are diagnosed instead of raising. - Variants get their own generated enum (VectorAluVop3Op: from_word, base, assembly_name, semantics), so Instruction::VectorAluVop3 cannot name an undeclared encoding and formatting cannot panic. - The planner no longer counts a variant-only operand rule as a rule that can host new forms of its family (V_FMA_F32 was shown as reuse-ready). - The isa test checks that no first word of another 64-bit family (VOP3B) selects a VOP3 variant; docs describe the variant files. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, scripts: second round of registry review fixes (#215) - A family file must be a regular file directly in the registry directory: links (and so second names for one file) are rejected. - Variant operand formats are compared by role (destination, then the sources in rule order), not by XML row order. - Malformed spec and verify values are diagnosed instead of raising. - vop3_vgpr2 declares variant_only = true in the data; the planner reads that flag instead of inferring it from which entries use the rule. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa: third round of registry review fixes (#215) - The family directory must not be a link, and two includes must not be hard links to one file. - The role-based format comparison skips malformed rules (they keep their rule diagnostic instead of raising), and its test now reproduces the row-order case the old comparison got wrong. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa: fourth round of registry review fixes (#215) A self-referential family-directory link is reported instead of raising, a malformed rule destination field is a rule diagnostic in the XML check too, and the row-order test asserts a fully valid registry so it catches the old row-order comparison. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, scripts: registry loader and variant validation give diagnostics, not tracebacks (#215) - A standalone registry (no include) no longer needs a family directory. - A variant of an incomplete form reports the form instead of a KeyError. - A non-array encoding_variant, or a non-table entry, is a diagnostic in the loader, the validator and the coverage report. Each case has a test that fails on the previous code. The split-registry test now compares the whole merged result with a hand concatenation, and vop3.toml cites guide 12.7.1 for the VOP3 opcode offset. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> * isa, scripts: two more registry crash paths become diagnostics (#215) - A variant whose first duplicate base form is incomplete: the completeness check and the XML check now use the same base row (no KeyError). - qa rdna2 inventory and rank report a malformed encoding_variant (missing 'of', non-string operands) as an error with exit 2, not a traceback; the coverage reader checks the field types. Each has a test that fails on the previous code. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --------- Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
14 KiB
Execution contract: bounded Wave32 VOPC integer compares (issue #186)
Status: v2, after independent review (codex-contract.log); findings 1-7 addressed. Scope is the Xenora compute-fixture ABI
(one input word and one output word per dispatched thread; dispatch index i
is wave i / 32, lane i % 32; every wave starts with EXEC_LO = 0xffffffff;
scalar state is wave-uniform).
1. Sources (public only)
- AMD "RDNA 2" Instruction Set Architecture Reference Guide, 30 Nov 2020
(public PDF):
- S1 §2.1 "Wave32 and Wave64", printed p. 7-8 (PDF p. 15-16): "The upper half of EXEC and VCC are ignored for wave32 waves."
- S2 §3.9 "Vector Compares: VCC and VCCZ", printed p. 19-20 (PDF p. 27-28): compares set VCC (1 = pass, 0 = fail); "VCCZ is updated every time VCC is updated: vector compares and scalar writes to VCC"; "V_CMP_* => VCC[n] = EXEC[n] & (test passed for thread[n])"; "VCC is fully written; there are no partial mask updates"; in wave32 "only the lower 32 bits of VCC contribute to the value of VCCZ".
- S3 §4.5 "Manually Inserted Wait States", printed p. 30 (PDF p. 38): "Inserting S_NOP is not required to achieve correct operation."
- S3b §3.3 "EXECute Mask", printed p. 13 (PDF p. 21-22): EXEC "does not affect scalar (ALU or memory) execution or branches"; in wave32 EXECZ reflects only the lower 32 bits.
- S4 §6.2.2 "Instruction Outputs", printed p. 43 (PDF p. 51): a thread writes a VGPR result only if its EXEC bit is set; V_CMPX writes EXEC.
- S4b §6.2 Table 20 (operand selectors), PDF p. 49-50: 106 = VCC_LO.
- S5 §12.9 "VOPC Instructions", printed p. 150-152 (PDF p. 158-160): field map SRC0 / VSRC1 / OP; "producing a 1 bit result per lane into VCC or EXEC"; Table 59: integer compare offsets LT 1, EQ 2, LE 3, GT 4, LG/NE 5, GE 6. Table 61 "VOPC Compare Opcodes", printed p. 155 and 159 (PDF p. 163, 167): 128-135 V_CMP_{F,LT,EQ,LE,GT,NE,GE,T}_I32 and 192-199 the _U32 forms, "D = VCC in VOPC encoding". The XML is authoritative for encodings.
- AMD RDNA 2 machine-readable ISA XML (pinned SHA-256
d671ecbc..., used bycrates/isa/generate_rdna2.py; seedocs/research/rdna2.md): ENC_VOPC opcodes V_CMP_{LT,EQ,LE,GT,NE,GE}I32 = 129-134 and V_CMP{LT,EQ,LE,GT,NE,GE}_U32 = 193-198; descriptions "Set the per-lane condition code to 1 iff the first input is the second input"; SRC0 and VSRC1 data format FMT_NUM_I32 for the _I32 forms and FMT_NUM_U32 for the _U32 forms; destination OPR_VCC. - LLVM 23.1.6 AMDGPU (
llvm-mc, gfx1030,+wavefrontsize32) as an encoding oracle only:compares_fixture_instructions_against_llvmchecks every form with every source class against the decoder and the reference.
2. Supported forms
The 32-bit VOPC encoding (bits 31:25 = 0b0111110), no SDWA or DPP:
| Form | per-lane test, S0 = SRC0, S1 = VSRC1 |
|---|---|
| v_cmp_lt_i32 / v_cmp_lt_u32 | S0 < S1 |
| v_cmp_eq_i32 / v_cmp_eq_u32 | S0 == S1 |
| v_cmp_le_i32 / v_cmp_le_u32 | S0 <= S1 |
| v_cmp_gt_i32 / v_cmp_gt_u32 | S0 > S1 |
| v_cmp_ne_i32 / v_cmp_ne_u32 | S0 != S1 |
| v_cmp_ge_i32 / v_cmp_ge_u32 | S0 >= S1 |
| v_cmp_f_i32 / v_cmp_f_u32 | false (Table 59 offset 0) |
| v_cmp_t_i32 / v_cmp_t_u32 | true (offset 7) |
_i32 compares the 32-bit two's-complement values, _u32 the unsigned values.
The forms are declared in the semantic registry
(crates/isa/semantics/rdna2/vopc.toml, operand rule vopc_vcc); the decoder
dispatches them from the generated VectorCompareOp.
Operand order matters (S0 is SRC0): lt with swapped operands is gt.
SRC0 selectors, exhaustively: 0-105 (normal SGPR), 106 (VCC_LO, see 5), 128-208 (inline integers 0..64 and -1..-16), 255 (one 32-bit literal, which must be present in full after the word), 256-511 (VGPR). Every other selector is rejected at decode, independent of EXEC: TTMPs, M0, NULL, EXEC_LO/HI, VCC_HI, apertures, wave-ID, reserved values, inline floats 240-248, SDWA 249, DPP8 233/234, DPP16 250, VCCZ/EXECZ/SCC 251-253, LDS direct 254. VSRC1 is a VGPR.
Wave size: the compute fixture ABI is defined as Wave32. A raw instruction stream carries no wave-size declaration (the VOPC word has no wave-size field), so every caller of the translator and the reference establishes Wave32 by using this ABI; there is no Wave64 entry point to reject.
3. Semantics of one supported compare, Wave32
Let E = EXEC_LO before the instruction and A = the set of lanes that exist in this wave (all 32 except in a final partial wave, see 7). As an unsigned 32-bit value,
VCC_LO := OR over 0 <= n < 32 of ( [n in A] & E[n] & test(S0[n], S1[n]) ) << n
- VCC_LO is fully written, every time, including bits of inactive or absent lanes, which become 0 (S2). With E = 0 the result is VCC_LO = 0 even if VCC held a nonzero value before.
- Inactive lanes (E[n] = 0) read no VGPR source; their bit is 0 regardless of their data.
- VCC_HI: S1 says it is ignored in wave32; whether hardware writes it is not settled by the sources. After any supported compare, VCC_HI has no known value. (No supported instruction can read vcc_hi today; this keeps any future reader honest.)
- EXEC, SCC, every VGPR and every other SGPR are unchanged.
- VCCZ, as read by
s_cbranch_vccz, is (VCC_LO == 0) (S2, wave32 sentence). - No wait states are needed between the compare and a reader of VCC (S3).
4. Rejected (controlled translation/reference error, never a silent no-op)
- V_CMPX_* (writes EXEC: divergent execution; needs its own contract).
- The VOP3 encoding of any compare (arbitrary SGPR destination, modifiers).
- SDWA and DPP forms; 16-bit, 64-bit and float compares; v_cmp_class.
- SRC0 inline float constants (240-248), SGPRs other than s0-s106, TTMPs, M0, EXEC_LO/HI, VCC_HI, SCC/VCCZ/EXECZ selectors, LDS direct.
- A normal-SGPR SRC0 without a known value, when EXEC_LO != 0.
- Wave64 in any form.
5. VCC_LO state and where a compare result may flow
Both the translator and the reference track VCC_LO in one of three static states, decided by the instruction path alone, never by lane data:
- uninitialized (program start);
- known: written by
s_mov_b32 vcc_lo, <known value>, or by a compare executed while EXEC_LO is statically 0 (result: known 0); - lane-derived: written by a compare executed while EXEC_LO != 0. It stays lane-derived even when a particular wave's result happens to be 0 or equal to a constant; acceptance never depends on that value.
Acceptance rules for one compare:
- EXEC_LO = 0 (EXEC is always a known value in this ABI): the encoding is validated, no source is read or checked (not VGPR readiness, SGPR knowledge or VCC state), and VCC_LO becomes known 0.
- EXEC_LO != 0: VSRC1 and a VGPR SRC0 must be initialized and not pending a
load; a normal-SGPR SRC0 must be known; a
vcc_loSRC0 must be known or lane-derived (uninitialized is rejected). VCC_LO becomes lane-derived.
While VCC_LO is lane-derived it may be read only by:
s_cbranch_vccz(6);v_mov_b32_e32 vD, vcc_lo, which writes the 32-bit mask into vD of active lanes (inactive lanes keep vD, as for any partial-EXEC move);- SRC0 of a supported compare (
v_cmp_* vcc_lo, vcc_lo, vN), which reads the old VCC_LO in every active lane before the new value is written. - SRC0 of a registry VOP2 integer form (for example
v_add_nc_u32_e32 vD, vcc_lo, vN), which reads the 32-bit mask in every active lane, asv_mov_b32_e32does.
v_mov_b32_e32 vD, vcc_lo and the VOP2 forms follow the usual EXEC rules: with EXEC_LO = 0 it
is skipped without reading VCC; otherwise VCC_LO must be known or
lane-derived. Every other read of vcc_lo is rejected in any state: the
supported scalar instructions (s_mov_b32, SOP2, s_cmp_eq_u32) do not
accept vcc_lo as a source at decode, so s_mov_b32 exec_lo, vcc_lo cannot
create divergent EXEC. s_mov_b32 vcc_lo, <known> makes VCC_LO known again.
6. Branches
s_cbranch_scc0: unaffected (compares do not touch SCC).s_cbranch_execz: unaffected (compares do not touch EXEC).s_cbranch_vcczwith a known VCC_LO: resolved during translation, as today.s_cbranch_vcczwith a lane-derived VCC_LO (a runtime branch): the whole wave takes it iff VCC_LO == 0. Control flow stays wave-uniform; no lane diverges. Accepted only if the skipped region R (from the end of the branch to the target, exclusive) satisfies all of:- the target is a forward instruction boundary (existing rule);
- no vector load is outstanding at the branch;
- R contains only these integer vector forms:
v_mov_b32_e32(from a VGPR, an immediate (inline integer, the inline +1.0f bit pattern or a literal; the move copies bits), a known normal SGPR, or vcc_lo known or lane-derived),v_add_nc_u32_e64,v_lshlrev_b32_e32,v_lshrrev_b32_e32,v_ashrrev_i32_e32,v_and_b32_e32,v_or_b32_e32,v_xor_b32_e32. Nothing else: no FP32 form, scalar instruction, compare, branch, wait, memory access ors_endpgm; - every VGPR written in R was initialized before the branch;
- every source read in R is ready where it is read (the normal rules, as on the fall-through path). These rules are checked on R's instructions whether the branch is taken or not, and whatever EXEC_LO is, so acceptance does not depend on lane data. After R, each VGPR written in R holds its value from R if the branch was not taken, else its value from before the branch; scalar state (SGPRs, SCC, EXEC, VCC state) is the same on both paths. The translator's known-bit facts for a joined VGPR keep only bits known and equal on both inputs, and the union of the candidate values when it has at most two (so an exact FP32 operation after the join is still accepted when both edges allow it). An outstanding store at the branch is allowed: the store already read its data.
7. Wave formation and absent lanes
Waves are formed from consecutive dispatch indices: wave w is indices 32w..32w+31. A final partial wave (input length not a multiple of 32) has absent lanes; they never execute and contribute 0 to every compare result. (The fixture's EXEC_LO still reads 0xffffffff in such a wave; that existing ABI simplification is unchanged and documented as a limitation.)
8. Mapping to Metal (no SIMD-group assumption)
Metal SIMD groups, threadgroups and RDNA 2 waves are not assumed to coincide.
The Metal kernel makes no cross-thread call: no simd_ballot, no threadgroup
memory, no barrier. For each compare, every thread recomputes the compare's
per-lane operand values for each of the 32 lanes of its own RDNA wave
(dispatch indices (index & ~31) + n, bounded by the grid size from
[[threads_per_grid]]), from those lanes' input words, and assembles VCC_LO
itself. All values are pure functions of input words and translation-time
constants, so every thread of a wave computes the same VCC_LO. A runtime
branch becomes a per-register select on (VCC_LO != 0) at the join; region
instructions are pure 32-bit ALU and are evaluated unconditionally.
9. Verification
fixtures/rdna2/vector-compare.s is an original program, assembled by LLVM.
It uses the twelve relational forms with VGPR, SGPR, inline, literal and vcc_lo
sources, a compare and a mask move under partial EXEC, a compare under empty
EXEC that must clear a stale VCC, SCC kept across compares, and a runtime
s_cbranch_vccz. Its expected output is written in
tests/validation/known_answers.rs from the listing, with an explicit
relation for each row. The test input has a wave whose branch is taken, one
whose branch is not, and a final partial wave of 9 lanes.
fixtures/rdna2/vector-compare-ft.s does the same for the four F and T
forms: an F that clears a stale VCC, T and F under partial EXEC, a T reading
vcc_lo, and runtime s_cbranch_vccz on a T result (never taken) and an F
result (always taken). The independent oracle for F and T is
tests/validation/tests/vopc_f_t.rs.
tests/validation/known_answers.rs runs each relational form against 10 boundary values (0, 1, 0x7ffffffe,
0x7fffffff, 0x80000000, 0x80000001, 0xfffffff0, 0xfffffffe, 0xffffffff,
0x0f0f0f0f), in both operand orders. That is 240 programs, checked against
exact i128 differences. It also covers partial and empty EXEC with seeded
destinations, both runtime branch edges in one dispatch, and 32 rejections
pinned to their byte offset. The translator, the reference (lockstep waves,
compares on widened i64 values) and the second interpreter (sign-bit flip,
then an unsigned compare) must all agree. The 9 ordered compare slots in the
relational fixture each change the result when their signedness is flipped. The same
programs, plus 2 mask-chain programs (252 in all), and the F/T fixture (253) match on native Metal with API and shader validation
(vector_compare_native_metal_known_answers, run by scripts/test-gpu.sh). The F/T oracle's own native Metal cases
run in vopc_f_t_match_the_guide_on_native_metal.
A deliberately broken copy of each implementation fails these tests: the reference with unsigned-only compares or with the EXEC test removed; the second interpreter with unsigned-only compares or with no empty-EXEC rule; the translator with a stale VCC under empty EXEC (CPU), and, on Metal, with swapped signedness, without the EXEC mask, without the absent-lane bound (shader validation aborts on the out-of-range read) or with an inverted join.
Evidence class: agreement with a specification-derived reference, run locally on Apple M5 Pro. It is not agreement with RDNA 2 or Xbox hardware.
10. Limitations
- Wave32 only. No
v_cmpx, VOP3, SDWA, DPP, 16-bit, 64-bit or float compares. - A lane-derived VCC_LO cannot reach EXEC or any scalar instruction, so there is no divergent execution. That needs its own contract.
- The skipped region of a runtime
s_cbranch_vcczholds only integer vector ALU instructions. Loops, nested runtime branches and scalar state that differs between the two edges are not supported. - Each thread recomputes up to 32 lanes per compare. The cost grows with the compare's dependency chain. That is fine for fixtures, and no performance is claimed.