Skip to content

rdna2spirv: v_mad_u32_u16 / v_mad_i32_i16 op_sel on S2 and D, and clamp, measured on RDNA2 - #2494

Open
evilclanker9000 wants to merge 5 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/hw-mad-u32-u16
Open

evilclanker9000 wants to merge 5 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/hw-mad-u32-u16

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-HW-MAD-U32-U16, #2078 (comment)

What and why

The translator refused v_mad_u32_u16 / v_mad_i32_i16 with op_sel on S2 or D and with clamp (Unsupported: op_sel/clamp), because S2 and D are whole dwords and the result was unknown. I measured it natively on an AMD Radeon iGPU (gfx1036, Windows driver 26.10.1) with raw kernels through tools/hw_oracle, 11 kernels of four words each, wave32 and wave64 giving the same words:

  • op_sel bit 2 adds S2[31:16] zero-extended, in the i16 op too: v_mad_i32_i16 d, v(3), v(3), 0xffff8000 op_sel:[0,0,1,0] gives 0x00010008 (9 + 0xffff), and S2 = 0x8003fffe adds 0x8003. An inline float constant there is its f32 pattern (1.0 adds 0x3f80).
  • op_sel bit 3 writes the 32-bit result shifted left by 16: low half 0, the old D not kept (0x1239566e is written as 0x566e0000).
  • clamp saturates the 32-bit sum to [0, 0xffffffff] (u16) or [0x80000000, 0x7fffffff] (i16), before the bit 3 shift (0xfffe * 0xfffe + 0xfffffff0 gives 0xffffffff; with bit 3 set, 0xffff0000). Underflow gives 0x80000000.

The translator now does exactly this; with the change all 44 measured words match it (spirv_mini_interp, one lane).

Changes:

  • tools/rdna2spirv.py: the v_mad_u32_u16 / v_mad_i32_i16 branch reads S2's high half for op_sel bit 2, saturates on clamp (unsigned: sum below the product; signed: product and addend of one sign, sum of the other), and shifts for op_sel bit 3.
  • runtime/test/shaders/ps_valu16_mad_sel.asm: S2 high half with bit 15 set in the i16 op, the destination shift, unsigned overflow and signed underflow under clamp, each word derived in the header; registered in check.py and scripts/run_gpu_tests.py.
  • The same shader as a semantic fixture, with hand constants and a Python reference in tests/semantic/test_value_semantics.py. tests/semantic/test_alu_mad_32_16.py asserted the refusal; it now checks the measured words and 40 seeded cases with every op_sel bit and clamp against its reference.
  • docs/design.md: a contract row with the measurement.

Tested

  • python -I scripts/run_gpu_tests.py --filter ps_valu16_mad: ps_valu16_mad and the new ps_valu16_mad_sel PASS 4096/4096 pixels exact on NVIDIA GeForce RTX 5080 (driver 616.92) and on the AMD Radeon iGPU (gfx1036, driver 26.10.1), selected with LEONIDA_VK_DEVICE. The fixture's body also ran natively on gfx1036 (tools/hw_oracle) and gave 0x00008003, 0xdb880000, 0xffffffff, 0x80000000 in wave32 and wave64.
  • Red on main: the shader does not translate with main (missing .spv (translation failed)) on either GPU.
  • Negative control: sign-extending S2's high half (OpShiftRightArithmetic) fails on both GPUs, 4096/4096 mismatches (got 0xffff8003 for 0x00008003). Reverted.
  • python -m unittest discover -s tests/semantic: OK. python tests/semantic/regen_fixtures.py --check: clean. python -I tests/test_shader_checks.py: OK.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

These forms were refused. Measured on RDNA2 (gfx1036, raw kernels through
tools/hw_oracle, wave32 and wave64): op_sel bit 2 adds S2[31:16]
zero-extended (in the i16 op too), op_sel bit 3 writes the result shifted
left by 16, and clamp saturates the sum to u32 / i32 before that shift.

GPU test ps_valu16_mad_sel (4096/4096 on RTX 5080 and the gfx1036 iGPU), the
same shader as a semantic fixture, test_alu_mad_32_16 checks the measured
rule instead of the refusal, and a docs/design.md row (EGEN-HW-MAD-U32-U16).

AI-assisted.
@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent GPU check of c921b24 on Windows 11 build 26300, RTX 5070, driver 621.24. A hand-written compute probe loaded 4,096 operand triples from GPU memory (a boundary grid plus seeded random words). For every triple it ran both opcodes, all 16 op_sel combinations and both clamp settings. Destinations started at 0xAAAA5555. A Python reference used unbounded integer arithmetic before applying the specified saturation/wrap and final destination shift.

All 262,144 words matched, including 1,122 positive signed saturations, 1,080 negative signed saturations and 7,934 unsigned saturations. The module passed Vulkan 1.2 spirv-val, the input heap stayed byte-identical, and all six tests in test_alu_mad_32_16.py passed. This checks the register-source translation against the documented model. Synthetic inputs only. AI-assisted review.

@TurboSosiska304

Copy link
Copy Markdown

Ran c921b24 on AMD RX 570 (Polaris 10, gfx803), RADV Mesa 26.2.6-arch3.1, Vulkan 1.4.354, CachyOS — a GCN4 part on Mesa, not one of the RDNA2 cards in the description:

python3 -I scripts/run_gpu_tests.py --build runtime/build/linux --filter ps_valu16_mad
  PASS  ps_valu16_mad         4096/4096 pixels exact
  PASS  ps_valu16_mad_sel     4096/4096 pixels exact
2/2 passed

tests/semantic/test_alu_mad_32_16.py: 6 tests OK.

Independent negative control on the GPU path: with main's tools/rdna2spirv.py restored, the new fixture does not translate at all (FAIL ps_valu16_mad_sel: missing .spv (translation failed)) — so the fixture is new coverage, not a re-check of a form that already compiled. Restoring the branch file makes it pass again.

The op_sel/clamp words match here too, so the rule holds on a third AMD generation and a different compiler stack (RADV, not LLPC/Windows).

@Marukooh

Copy link
Copy Markdown
Contributor

Tested c921b24 on NVIDIA GeForce RTX 5070 Ti (Blackwell), NVIDIA 620.73.04, CachyOS Linux. Built the Vulkan harness from the PR and ran scripts/run_gpu_tests.py --filter ps_valu16_mad_sel with LEONIDA_W64_EMULATE=0 and =1. Both runs passed SPIR-V validation and matched all 4,096 pixels. The six test_alu_mad_32_16.py semantic tests also passed. AI-assisted.

@dac0129

dac0129 commented Nov 22, 2026

Copy link
Copy Markdown

Tested commit c921b244 on real dual-GPU silicon: pass.

Environment:

  • CPU: AMD Ryzen 7 7800X3D (8C/16T)
  • OS: Windows 11 Build 26200.0 (x64)
  • dGPU: NVIDIA GeForce RTX 4080 (Driver 617.42, Vulkan 1.4.351)
  • iGPU: AMD Radeon(TM) Graphics (RDNA2, Driver 32.0.21045.29003, Vulkan 1.4.315)

Test verification:

  • python -m compileall -q make_game.py tools tests: pass (0 errors)
  • tests/semantic/test_alu_mad_32_16.py: 6 passed (OK)
    • Verified op_sel on S2 (zero-extended high half on u16 and i16), op_sel on D (<< 16), and saturation clamp across reference edges and seeded values.
  • tests/semantic/test_value_semantics.py: 32 passed (OK)
    • test_mad_u32_u16_i32_i16_op_sel_s2_d_and_clamp passed cleanly.

AI-assisted.

@throny

throny commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Real-corpus incidence for c921b244 (my own Xbox Series 1.0.2.1 copy, all 26,503 disassembled shaders, gfx1013; Windows 11, Python 3.13, CPU only). Counts only.

v_mad_u32_u16 and v_mad_i32_i16 occur 0 times in the corpus, with or without op_sel or clamp. So the refusal this PR replaces never fired on this game's shaders, and merging it changes no corpus SPIR-V.

  • tests/semantic/test_alu_mad_32_16.py at the PR head: 6 tests, OK.
  • python -m unittest discover -s tests/semantic: 2,569 tests, OK.

AI-assisted (Claude Code).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested c921b24 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 140/186 passed, 46 skipped, 0 failed. main 4947579 gives 139/185, 46 skipped, 0 failed. All 191 test shaders translate and pass spirv-val.
  • New GPU tests: ps_valu16_mad_sel PASS.
  • tests/semantic/test_alu_mad_32_16.py: ran 6 tests in 1.751s; OK.
  • tests/semantic/test_value_semantics.py: ran 32 tests in 2.063s; OK.

AI-assisted (Claude Code).

@zneel

zneel commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested c921b244 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: pass.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 171/186, 15 skipped, 0 failed (main 49475796: 170/185)
same with LEONIDA_W64_EMULATE=1 171/186, 15 skipped, 0 failed
pytest tests/test_translate.py tests/test_shader_checks.py tests/test_run_gpu_tests.py 619 passed, 2 skipped, 232 subtests passed in 63.49s (0:01:03)
unittest discover -s tests/semantic -p test_alu_mad_32_16.py 6 OK
unittest discover -s tests/semantic -p test_value_semantics.py 32 OK
tests/semantic/regen_fixtures.py --check clean

This PR's shaders:

  • native: PASS ps_valu16_mad_sel 4096/4096 pixels exact; emulated: PASS ps_valu16_mad_sel 4096/4096 pixels exact

The only other module that changes compared with main 49475796 is ps_valu16_mad, which still passes both native and emulated. First RDNA4 run, after the RDNA2 measurements.

AI-assisted (Claude Code); I checked the results.

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Rechecked in real wave64 with the hw_oracle fix in #2961: same words.

AI-assisted.

Conflicts:
	docs/design.md
	tests/semantic/test_value_semantics.py

AI-assisted.
docs/design.md
tests/semantic/test_value_semantics.py

AI-assisted.
docs/design.md
tests/semantic/regen_fixtures.py

AI-assisted.
@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6da1c717 (the rebased head) on NVIDIA GeForce RTX 3060 Ti (subgroup 32..32), driver 580.173.02, Linux (kernel 6.18), Python 3.14: pass.

  • scripts/run_gpu_tests.py: 165/211 passed, 46 skipped, 0 failed. ps_valu16_mad_sel: 4096/4096 pixels exact.
  • ctest: 47/47 passed.
  • pytest tests/semantic/test_alu_mad_32_16.py tests/semantic/test_value_semantics.py: 46 passed, 454 subtests passed. With main's tools/ (aa2988c): 36 failed, 45 passed.

No game files. AI-assisted (Claude Code).

@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6da1c71 on NVIDIA RTX 4080, driver 610.88, Windows 11: pass

This is the head rebased on aa2988cc. Ran (MSVC 19.44 / VS 2022 BuildTools, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, native llvm-mc 23.1.6; this PR has no runtime C++ change, so the test programs were built from the aa2988cc tree):

  • python -I scripts/run_gpu_tests.py --build <build>: 165/211 passed, 46 skipped, 0 failed. ps_valu16_mad_sel 4096/4096 pixels exact.
  • test_alu_mad_32_16 and test_value_semantics (run from tests/semantic): 46 tests OK.

AI-assisted.

@amarovndres

Copy link
Copy Markdown

Tested 6da1c717 on AMD Radeon RX 9060 XT (RDNA4, discrete, subgroup 32..64), AMD 26.10.1 (32.0.32015.7029, LLPC, Vulkan 1.4.359), Windows 11 Pro (build 26200): pass.

  • scripts/run_gpu_tests.py (full suite; test programs built from this head with llvm-mingw, llvm-mc 23.1.6): 197/211 passed, 14 skipped, 0 failed. main d0835dd9 on the same machine: 196/210 passed, 14 skipped.
    • PASS ps_valu16_mad_sel 4096/4096 pixels exact
  • tests/semantic/test_alu_mad_32_16.py: 6 tests, OK
  • tests/semantic/test_value_semantics.py: 40 tests, OK

AI-assisted (Claude Code). Synthetic shaders only, no game files.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 / MoltenVK validation of head a0239846 (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup 4..32, llvm-mc 23.1.6. Python/translator-only change; one harness built from base 3eb4410b and shared for the A/B.

  • New GPU fixture ps_valu16_mad_sel PASS: ps_valu16_mad_sel 4096/4096 pixels exact
  • Full suite: 217 shaders translated, 217 pass spirv-val; 153/218 passed, 58 skipped, 7 failed (known MoltenVK expected set), versus base 3eb4410b at 152/217, 58 skipped, 7 failed. No common test changed status.

No code changes, no game data; synthetic fixture and counts only.

AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested a023984617b5ba4a06e59618b9b726f3b5e14d93 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

The stock synthetic fragment fixture ps_valu16_mad_sel passed with 4096/4096 pixels exact. I also enabled destination-high selection (op_sel:[0,0,0,1]) on both clamped MAD instructions in a temporary copy. The unsigned overflow produced 0xffff0000 and the signed underflow produced 0x00000000, as expected when saturation precedes the 16-bit shift. The other two channels retained their expected values. The variant passed with 4096/4096 pixels exact. Both shaders passed spirv-val. These were filtered fixture runs.

AI-assisted verification.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a023984 (merge base 7b7d73f) on NVIDIA GeForce RTX 5080, Linux 580.65.06: pass

Tested the PR head merged with current main 7b7d73f8 (clean merge, not pushed). Runtime harnesses built from main 7b7d73f8, llvm-mc 21, Python 3.13, Vulkan SDK 1.4.363, Linux native.

Check main 7b7d73f8 this PR
run_gpu_tests.py native 170/217 passed, 47 skipped 171/218 passed, 47 skipped
run_gpu_tests.py with LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped 202/218 passed, 16 skipped
failed (native / emulated) 0 / 0 0 / 0
tests/semantic/regen_fixtures.py --check ok ok
pytest tests/semantic/test_alu_mad_32_16.py tests/semantic/test_value_semantics.py tests/test_find_hash_lookup.py tests/test_first_look_shim_report.py tests/test_shader_checks.py tests/test_run_gpu_tests.py tests/test_cf_diff.py 162 passed, 315 subtests passed (3 shared files) 212 passed, 1 skipped, 770 subtests passed in 11.89s

Line-by-line against my main run (results that differ from main):

  • native:
    • ps_valu16_mad_sel main: - pr: PASS 4096/4096 pixels exact
  • emulated:
    • ps_valu16_mad_sel main: - pr: PASS 4096/4096 pixels exact

Not run: the full unittest discover -s tests/semantic (about 6 minutes, and main alone has failures there); only the test files this PR touches plus the shared checker tests ran. No other GPU vendor, no Windows. No game files.

AI-assisted (Claude Code).

@junaydirfan

Copy link
Copy Markdown

Tested a0239846 merged into main 98fa503f on Linux (CachyOS, kernel 7.3.5), llvm-mc 23.1.5, LEONIDA_W64_EMULATE=1, test programs built from main (this PR changes no C++): pass.

GPU driver run_gpu_tests.py main
Intel Raptor Lake-S iGPU (Xe-LP), subgroup 8..32 Mesa ANV 26.3.1 187/218 passed, 31 skipped, 0 failed 186/217
RTX 4080 Laptop GPU (Ada), subgroup 32 NVIDIA 620.73.04 202/218 passed, 16 skipped, 0 failed 201/217

New in this PR:

  • ps_valu16_mad_sel: Intel PASS, 4096/4096 pixels exact; RTX 4080 PASS, 4096/4096 pixels exact

Every other test has the same result as on main on both GPUs.

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a0239846 (merge base 3eb4410b) on an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56, Windows 11 10.0.26300. Translator and fixtures came from this head; runtime test programs were built from main 98fa503f (MSVC 19.44, Vulkan-Headers fetched by CMake, loader from the NVIDIA driver); spirv-as/spirv-val and llvm-mc 21 came from WSL Ubuntu. Result: pass.

Not run: LEONIDA_W64_EMULATE=1, other vendors, Linux. No game files.

AI-assisted (Claude Code).

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.