Skip to content

rdna2spirv: v_med3_i16 / v_med3_u16 as the sum minus the smallest and the largest (AMD driver returns a bound) - #6484

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/fz-med3-i16
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/fz-med3-i16

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-MED3-I16, #5032 (comment)

What and why

pk16_diff --oracle hw,vk (#6306) ran the translated programs on a real GPU and found v_med3_i16 with a constant operand giving a bound instead of the median on the AMD Windows driver (Radeon iGPU gfx1036, driver 26.10.1, wave32 and wave64): v_med3_i16 v7, s4, v12, v3 op_sel:[1,0,0,0] with s4.hi = 17, v12 = -32768, v3 = 29 gave -32768 (hardware: 17). It was 5 of 8 mismatches of a 500-program wave32 run and 7 of 8 of a wave64 run. The translated SClamp(z, min(x,y), max(x,y)) did it, and so did a min/max composition and a select composition of the same median; the RTX 5080 and spirv_mini_interp are right with all of them. The median is now the sum minus the smallest and the largest (Min/Max of three, then x + y + z - min - max; 16-bit values fit 32 bits), which the Radeon computes correctly, also for v_med3_u16.

Tested

  • New GPU test cs_med3_i16 (four medians with an SGPR constant, an inline constant and a literal, one with the destination high half; the other two operands come from the buffer fill at run time; words measured natively on gfx1036 in game MODE: 0xe78a0011 0x12340018 0x1234001d 0xc000abcd). Passes on the AMD Radeon iGPU (26.10.1) and the NVIDIA RTX 5080 (616.92) with the change. On main: the Radeon fails (0xe78a001d 0x1234001d 0x1234001d 0xc000abcd), the RTX 5080 passes (the problem is the AMD driver's).
  • tests/semantic/test_med3_i16.py runs the same shader through the interpreter and asserts the words and that no Clamp is left in the SPIR-V (red on main for that assertion, green now). The values themselves cannot go red in the interpreter.
  • pk16_diff --oracle hw,vk before/after on the Radeon, 500 programs per wave size: all v_med3_i16 mismatches gone in wave32 (seed 201: 8 to 1; the one left is a v_pk_mul_lo_u16 that is not this) and in wave64 (seed 202: 8 to 0).
  • python -m unittest discover -s tests/semantic (6,151 tests, OK), regen_fixtures.py --check, tests/test_shader_checks.py (fixture count and canned dump updated), test_docs_*.
  • docs/design.md: the v_min3/v_max3/v_med3 16-bit row gains the median form and why.

AI-assisted: yes.

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.

… the largest

pk16_diff --oracle hw,vk (ItsNotRexed#6306) found v_med3_i16 with a constant operand coming out as
a bound of the three values on the AMD Windows driver (Radeon iGPU 26.10.1, wave32 and
wave64): 17, -32768 and 29 gave -32768. The translated SClamp, and min/max and select
forms of the same median, all did it; the sum minus Min3 and Max3 is right there, on
the RTX 5080 and in the interpreter. 16-bit values fit 32 bits, so the sum cannot
overflow.

New GPU test cs_med3_i16 (four medians with an SGPR, an inline and a literal constant,
words measured on gfx1036). It fails on main on the Radeon and passes with the change on
both GPUs; the semantic test checks the value and that no clamp is left.

AI-assisted.
@hashdev512

Copy link
Copy Markdown
Contributor

This is a second AMD data point, from a newer GPU on a newer driver. The PR passes. The driver bug does not reproduce here: the SClamp form that main emits gives the right medians on this card.

Tested d948a6b on its parent, main 561b65a. Hardware and software: AMD Radeon RX 9070 XT (RDNA4), Adrenalin 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11 build 26300, llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. The runtime was built at 561b65a with LLVM_MC set.

cs_med3_i16 with main's translator. I ran it on this branch's tree with tools/rdna2spirv.py reverted to 561b65a. The module has the 4 SClamp, SHA-256 bc11f494…. The test passed in 3 of 3 runs native and 3 of 3 with LEONIDA_W64_EMULATE=1, every time with the expected words:

0xe78a0011 0x12340018 0x1234001d 0xc000abcd

There was no 0xe78a001d 0x1234001d … as on the gfx1036 with 26.10.1. So either the miscompile is specific to that GPU or driver, or 26.11.2 fixed it. I can't tell which from here.

With this PR (no Clamp left, 8 SMin/UMin, SHA-256 89db40dc…), the same words come out native and emulated, so the new form is also right on RDNA4.

Check main 561b65a this PR
unittest discover -s tests/semantic 6150 run, OK 6151 run, OK
scripts/run_gpu_tests.py 216/232 passed, 16 skipped 216/233 passed, 16 skipped, plus cs_med3_i16 passing
same with LEONIDA_W64_EMULATE=1 216/232 passed 217/233 passed

All 252 other SPIR-V modules of the suite are byte-identical to main, native and emulated, and all 239 translated fixtures pass spirv-val.

The only other difference from main is ms_wave_info_s3, which failed in this branch's native run. Its module is byte-identical to main's. It is the known intermittent failure (#6172, #5741), and on this card main's native module passes 5 of 20 runs.

(AI-assisted test run.)

Keep both the cs_med3_i16 and the new cs_pk_mul_lo_const entries in the GPU runner, check.py, regen_fixtures and the shader-check mock.

AI-assisted.
@AnaNazzDev

Copy link
Copy Markdown

Tested 680783d39 on NVIDIA GeForce GTX 1080 (Pascal, 32-wide), driver 582.66, Windows 11 (10.0.26200): pass.

MSVC (VS 18) Vulkan test programs built from main (this PR doesn't change them), llvm-mc 21.1.8 (scripts/build_llvm_mc.ps1), SPIRV-Tools from Vulkan SDK 1.4.363.0.

run_gpu_tests.py --filter <fixture>, each fixture on its own, natively and with LEONIDA_W64_EMULATE=1:

fixture native LEONIDA_W64_EMULATE=1
cs_med3_i16 PASS cs_med3_i16 0xe78a0011 0x12340018 0x1234001d 0xc000abcd 0xe78a0011 0x12340018 0x1234001d 0xc000abcd PASS cs_med3_i16 0xe78a0011 0x12340018 0x1234001d 0xc000abcd 0xe78a0011 0x12340018 0x1234001d 0xc000abcd

Every module translated and passed spirv-val.

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

@gurvancampion

Copy link
Copy Markdown
Contributor

Tested 680783d39 on Linux 7.2.15 (Arch) on two GPUs, gfx1036 on RADV (Mesa 26.2.5, ACO) and an RTX 5090 (615.78.08): pass. The controls are main f283b1378 and the base e0816d6af, each with only the PR's test files added (fixture, check.py/run_gpu_tests.py wiring, test_med3_i16.py), so they translate with the old SMin/SMax + SClamp form (4 Clamps in the module; head: 0 Clamps, 4× min/max + sum minus both).

cs_med3_i16, --filter cs_med3 --require-pass head base main
gfx1036, RADV 26.2.5, native wave64 1/1 passed 1/1 passed 1/1 passed
RTX 5090, 615.78.08, LEONIDA_W64_EMULATE=1 1/1 passed 1/1 passed 1/1 passed
RTX 5090, native 1/1 passed 1/1 passed 1/1 passed
tests/semantic/test_med3_i16.py 1/1 ok 1 failed 1 failed
tests/test_shader_checks.py 88 run, 15 errors in 7 tests same same
  • The bug does not reproduce on Mesa RADV 26.2.5. On the old clamp form, both lanes read back E78A0011 12340018 1234001D C000ABCD, with MESA_SHADER_CACHE_DISABLE=true. Case 0 gives 17, not the bound -32768. The 5090 gives the same. Here it looks specific to AMD's Windows driver.
  • The expected words are the true medians, not values fitted to the bug. I checked all 4 cases by hand from the 0xEE fill. So the GPU fixture is right, but on these two drivers it can't tell the old form from the new one.
  • Only the structural check in test_med3_i16.py (no Clamp, at least 2 OpISub) separates the head from the controls. The control failure is 'Clamp' unexpectedly found, which stops the test before its value check.
  • The 15 test_shader_checks.py errors are the same on all three trees: harness() got an unexpected keyword argument 'cwd', from run_readback(..., cwd=) (Exercise translated pixel image_load through a matched integer descriptor #6833). They are not caused by this PR, but they stop the test that would use the PR's new cs_med3_i16 stub, so that stub was not exercised.

Commands (each tree built with cmake -S runtime -B runtime/build/linux -G Ninja -DVulkan_INCLUDE_DIR=<Vulkan-Headers>/include and cmake --build runtime/build/linux --parallel; llvm-mc 23.1.3, Python 3.14.8):

MESA_SHADER_CACHE_DISABLE=true LEONIDA_VK_DEVICE=RADV python -I scripts/run_gpu_tests.py --build runtime/build/linux --filter cs_med3 --require-pass
LEONIDA_VK_DEVICE=5090 LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --filter cs_med3 --require-pass
LEONIDA_VK_DEVICE=5090 python -I scripts/run_gpu_tests.py --build runtime/build/linux --filter cs_med3 --require-pass
(cd tests/semantic && python -m unittest -v test_med3_i16); python -m unittest tests.test_shader_checks

No game files. AI-assisted.

@ibressan

Copy link
Copy Markdown

Independent Windows 11 check at 680783d3 on an AMD Radeon RX 7800 XT (RDNA3), driver 26.11.2 (LLPC), Vulkan 1.4.359. Same-head Release Vulkan harness built with LLVM-mingw Clang 23.1.6, using llvm-mc 23.1.6 and SPIRV-Tools 2026.4.

run_gpu_tests.py --filter cs_med3_i16 --require-pass passed with wave64 emulation both off and on. I then replaced only tools/rdna2spirv.py with the version from the upstream merge parent e0816d6a, keeping the same shader fixture and harness. Both runs also passed with that translator. All four runs passed spirv-val and returned e78a0011 12340018 1234001d c000abcd in each output group. I confirmed the generated SPIR-V has three SClamp and one UClamp in the baseline and none on the head.

The incorrect median reported on the older AMD configuration did not reproduce on this RDNA3 card and driver. Synthetic inputs only. AI-assisted validation.

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.

5 participants