Skip to content

Shaders: an audit of which opcodes lower, and 100 more that do - #163

Closed
r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:shader/opcode-coverage
Closed

r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:shader/opcode-coverage

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Measured on the title's shaders (by a tester)

EduAzevedo ran victory translate --jobs 8 --list over the retail shaders (v1.0.2.1, 26,148 programs) on main d2a4bae and on this head ce1f138, compared program by program (result): 0 programs gained, 0 programs lost. The only change is the refusal reason of the 4 programs that use v_cvt_f32_f64: they now stop at floating-point arithmetic in f64 is not implemented by the DXIL emitter.

So this PR does not change what translates in this title today. It adds coverage and is tested on synthetic programs only; it can wait behind PRs that unblock programs the title uses.

Rebased onto main 7c8cecb (2026-11-24)

#117 (s_bitcmp*) and #140 (v_cmp_class_f16) merged and conflicted with this branch in emit/lower.rs. As promised under Overlap, this branch now keeps their lowering and drops its own for those six instructions (s_bitcmp{0,1}_b{32,64}, v_cmp_class_f16, v_cmpx_class_f16), so it adds 100 instructions instead of 106. This branch's GPU tests for those six still run, now against the merged code, and pass on the RTX 5070. Head 6035910; the measurement above was taken on the earlier head.

What

One instruction the translator refuses fails its whole shader, and nobody had a list of which instructions those are. Two commits:

1. An audit. For every entry of the opcode table, a test assembles one compute fixture containing that instruction, runs it through ir::build → resource → io → structure → lower, and records whether it lowers or which pass refuses it and why. The audit runs in a child test process with a per-case timeout, so a panic, stack overflow or hang names its opcode. It found none.

  • cargo test -p victory-shader opcode_coverage_table -- --ignored --nocapture prints the table grouped by encoding and reason.
  • refused_opcodes (a regular test) compares against opcode_refusals.txt. An opcode that stops lowering fails it. One that starts lowering only makes its line stale and prints a note, so other PRs that lower opcodes need not touch the list.
  • Not audited: the image family, generic FLAT and scratch instructions (185 entries), which need resources a minimal fixture cannot express.
  • For the audit, an instruction's refusal is reported even when none of its results are used (a test-only thread-local, no change outside tests).

2. 100 more instructions lower (the list goes from 500 to 400 entries on main 7c8cecb):

  • Scalar: s_absdiff_i32, s_bcnt0_i32_b{32,64}, s_bfe_{i,u}64, s_bfm_b64, s_bitreplicate_b64_b32, s_bitset{0,1}_b64, s_brev_b64, s_ff0_i32_b{32,64}, s_flbit_i32_{b64,i64}, s_pack_{ll,lh,hh}_b32_b16, s_sext_i32_i{8,16}.
  • Vector integer: v_mul_hi_{i32_i24,u32_u24}, v_mad_{i16,u16,i32_i16,u32_u16,i64_i32,u64_u32}, v_{min3,max3,med3}_{i16,u16}, v_sad_{u8,hi_u8,u16,u32}, v_msad_u8, v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, v_mqsad_u32_u8, v_lerp_u8.
  • Dots and conversions: v_dot2_{i32_i16,u32_u16}, v_dot4_{i32_i8,u32_u8}, v_dot4c_i32_i8, v_dot8_u32_u4, v_cvt_off_f32_i4, v_cvt_pk_{i16_i32,u16_u32}, v_sat_pk_u8_i16.
  • Float: frexp mantissa and exponent for f16/f32/f64, class compares for f64, the 16 f64 comparison predicates, and the always-false/true i64/u64 compares (v_cmp and v_cmpx), all on IEEE bits so NaNs, signed zeros and denormals survive.

Left refused with a reason, because rounding, exceptional values or carries could differ from the hardware: ldexp, the normalized conversions, half FMA and half dot products, v_mullit_f32, general f64 arithmetic.

Not verified

No game files were used. Semantics are from AMD's RDNA 2 ISA reference and LLVM's AMDGPU backend; the GPU tests compare against CPU references written from the same documents, so they catch translation mistakes, not a misreading of the documents.

Overlap

Tested

Windows 11, NVIDIA RTX 5070, synthetic only:

  • 11 new GPU tests (hand-assembled compute programs, results against CPU references, dxil.dll validation).
  • cargo test -p victory-shader at 6035910: 89 passed, 3 ignored. The audit test takes about 40 s.
  • cargo clippy -p victory-shader --all-targets: nothing new. cargo xtask policy: ok.

The title was not used in these tests. AI-assisted.

🤖 Generated with Claude Code

@DaviTostes

Copy link
Copy Markdown

Tested ce1f138 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 57 passed, 0 failed, 2 ignored (2 test binaries), including the 14 tests this PR adds.
  • The two ignored tests are emit::opcode_audit::audit_worker and opcode_coverage_table (the audit helpers).
  • The full workspace still needs Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

No game data used.

@DaviTostes

Copy link
Copy Markdown

Coordination note after testing the queue: this branch already lowers instructions that other open PRs cover, in lower.rs:

The branch passes on Linux (cargo test -p victory-shader: 57 passed, 2 ignored — the audit helpers), including its 14 new tests. Could you state in the PR which of those it supersedes, so the coordinator can close the redundant ones instead of reviewers checking each separately? It does not touch v_mad_u64 (#171), tbuffer_load_format_d16 (#169) or image atomics (#173), so those remain separate.

No game data used.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for testing, and for the coordination note. I went through the open single-opcode PRs and compared each diff with this branch. Of the 106 instructions here, 38 are also in another open PR and 68 are only here.

PR Instructions it shares with this one Compared with this branch
#117, #142 s_bitcmp{0,1}_b{32,64}; #142 also s_sext_i32_i{8,16} same result and SCC
#120 s_absdiff_i32, s_sext_i32_i{8,16}, s_pack_{ll,lh,hh}_b32_b16 same
#140 v_cmp_class_f16, v_cmpx_class_f16 same classes (this branch also does f64)
#148, #165 s_bcnt0_i32_b{32,64}, s_ff0_i32_b{32,64}; #148 also s_flbit_i32_{b64,i64} same
#156, #157, #162 v_mul_hi_{u32_u24,i32_i24} same
#157 v_cvt_pk_{u16_u32,i16_i32}, v_dot2_{i32_i16,u32_u16}, v_dot4_{i32_i8,u32_u8}, v_dot8_u32_u4 same; #157 also lowers v_ldexp_f32, which this branch leaves refused
#162 v_sad_{u8,hi_u8,u16,u32}, v_msad_u8 same
#166 s_bitset{0,1}_b64, s_brev_b64, s_bitreplicate_b64_b32 same
#171 v_mad_u64_u32, v_mad_i64_i32 unsigned: same. Signed carry differs, see below

So this branch does cover #171 too (the note above lists it as separate). #169 and #173 are indeed untouched.

One real disagreement, with #171 on the carry-out of v_mad_i64_i32. This branch takes the ISA's {carry, D} as the 65-bit sign-extended result, so the carry is bit 64 (unsigned carry out of bit 63, xor the two operands' signs). #171 computes signed overflow of the 64-bit add. They differ whenever both addends are negative without overflowing, e.g. (-1) * 1 + (-1): bit 64 is 1, signed overflow is 0. I have not checked this on hardware, so one of the two is wrong and whoever has a console reference could settle it with that input. The 64-bit result itself is the same in both.

I would rather not call any of those PRs redundant myself, since that is the maintainers' call and the small ones are easier to review. Either order works for me:

  • if the small PRs land first, I rebase and drop those instructions here;
  • if you would rather take them from here, say so and I can also cut this PR down to the audit (first commit, independent of all of them) plus the 68 instructions nobody else covers.

No game data used.

@Sunkiller-qc

Copy link
Copy Markdown

The audit is useful. A few overlaps beyond the ones listed in the description, from PRs opened since: #165 (s_bcnt0/s_ff0), #166 (s_bitset*_b64, s_brev_b64, s_bitreplicate_b64_b32), #171 (v_mad_u64_u32/v_mad_i64_i32), #162 (v_sad*, v_msad_u8) and #157 (24-bit mul_hi, cvt_pk, dot, ldexp). On v_mad_i64_i32, wide_mad (lower.rs:5043) matches the ISA's 65-bit definition of the carry and #171 doesn't, so I'd keep this one there.

Separately, #168 fixes SCC for s_min/s_max and s_lshl1-4_add_u32. Main sets it to "result != 0", and this branch keeps that (lower.rs:2782-2797). If #168 lands first, the rebase should keep its version.

Static review, not built or run.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the v_mad_i64_i32 carry: I looked it up in AMD's ISA references, and this branch's reading holds.

  • RDNA 3 ISA (Feb 2023), V_MAD_I64_I32: { D1.i1, D0.i64 } = 65'B(65'I(S0.i) * 65'I(S1.i) + 65'I(S2.i64)), and for V_MAD_U64_U32: { D1.u1, D0.u64 } = 65'B(65'U(S0.u) * 65'U(S1.u) + 65'U(S2.u64)).
  • RDNA 2 ISA, opcode 375: {vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64.

So the flag is bit 64 of the sum of the operands widened to 65 bits (sign-extended in the signed form), not signed overflow of the 64-bit add. For (-1) * 1 + (-1) that is 1. I left a note on #171.

While checking the other overlaps against the same documents I also found a mistake of my own in #160 (a16 does not make derivatives 16-bit on GFX10.3) and fixed it there. Nothing else in this PR changed.

No game data used.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Sunkiller-qc a late answer to your second point: agreed. A trial merge (git merge-tree) of #168's head ae8967c into this branch (ce1f138) is clean, so its SCC for s_min, s_max and s_lshl1-4_add_u32 comes through whichever lands first, and I will check those lines when I rebase.

Three more overlaps from PRs opened since, now in the description:

Trial merges only; nothing was built or run for this note. AI-assisted.

Synthesize one compute fixture per opcode table entry and run the translation passes. Diagnose refused instructions even when their results are unused, and identify opcode panics or timeouts in an isolated test process.

List the refused set and provide an opt-in report grouped by encoding and reason. An opcode that stops lowering fails the regular test; one that starts lowering only makes its line stale, so changes that lower opcodes need not touch the list. The baseline covers 1,233 entries without game data.
Add scalar bit counts, searches, field and packing operations, and vector integer multiply-adds, SADs, dot products and saturating conversions. Use IEEE bits for frexp, class and double compares so NaNs, signed zeros and denormals survive.

Add synthetic compute tests with live DXIL results compared against CPU references. Validate the shaders with the Windows SDK validator and run them on the RTX 5070. The opcode audit moves 106 mnemonics from refused to lowered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants