Skip to content

rdna2spirv: v_cubeid/sc/tc/ma_f32 with NaN, infinite and denormal operands match RDNA2 - #5589

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/fz-cube-nonfinite
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/fz-cube-nonfinite

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-CUBE-NONFINITE, #5032 (comment) (my finding there: #5032 (comment))

What and why

v_cubeid/sc/tc/ma_f32 with a NaN, infinite or denormal operand differed from RDNA2 when the translated shader ran on a real GPU (found with f32_fuzz --oracle hw,vk, #5351). I measured the four ops on the gfx1036 in game MODE with raw kernels over 4 x 13^3 = 8,788 operand triples (qNaN, sNaN, ±inf, ±0, ±denormal, normals). The rules:

  • the major-axis compares are float compares that a NaN fails and that read a denormal as zero, so with a NaN operand x is the major axis unless the NaN-free compares pick another (v_cubeid_f32(NaN, NaN, NaN) is 0);
  • "less than zero" is false for a NaN, a -0 and a denormal;
  • v_cubesc_f32 / v_cubetc_f32 pass the raw source word through (a NaN or denormal survives; neg moves the sign bit);
  • v_cubema_f32 is 2 * the major coordinate, +0 for a flushed zero.

The translator used float compares: on the AMD Windows driver (26.10.1) the NaN took the Z-major branch (cubeid(NaN, NaN, NaN) = 5 instead of 0), and NVIDIA kept denormals. The compares and selects are now integer operations on the magnitude bits, so no driver rule is involved. f32_fuzz.py's cube() model has the same rules and no longer declines a NaN (model vs hardware: 0 mismatches over 1,951 programs, seed 9).

Tested

  • All 8,788 measured triples against the translated shader on the AMD Radeon iGPU (gfx1036, driver 26.10.1) and on an NVIDIA GeForce RTX 5080 (driver 616.92): 0 differences with the change (any NaN matches any NaN); 1,197 / 1,068 / 411 / 1,039 differences per op on the Radeon before.
  • New GPU test cs_cube_nonfinite (eight words, derivations in the .asm header, words measured natively), plus the same shader as a semantic fixture (regen_fixtures.py, asserted in tests/semantic/test_cube_nonfinite.py). python -I scripts/run_gpu_tests.py --filter cube: pass on both GPUs; on main it fails on both (Radeon 0x40a00000 0x40800000 0xffc00000 ..., RTX 5080 0x0 0x3f800000 0x40000000 0x80000000 0xffc00000 0x80000000 0x7f800000 0x8000000a).
  • Negative control: the denormal flush threshold set to 0 in magnitude() fails the semantic test.
  • python -m unittest discover -s tests/semantic: 6246 tests, only the existing test_loop_ballot_barrier *_emulate* failures (tests/semantic: run test_loop_ballot_barrier's emulated cases through the structured form #5024), as on unmodified main. regen_fixtures.py --check clean, tests/test_shader_checks.py and tests/test_f32_fuzz.py OK, test_docs_* OK.
  • docs/design.md: the cube row gains the NaN/infinite/denormal rules and how they were measured.
  • This touches the same lists as rdna2spirv: v_mul_legacy_f32 zero override on the Radeon driver, and omod keeps a subnormal result's sign #5399 (check.py, run_gpu_tests.py, test_shader_checks.py): whichever lands second needs a trivial merge.

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.

…rands match RDNA2

f32_fuzz --oracle hw,vk showed the cube ops differing from the hardware on a NaN or
infinite operand on the Radeon. Measured on gfx1036 in game MODE over 4 x 13^3 operand
triples (qNaN, sNaN, +-inf, +-0, +-denormal, normals): the major-axis compares are
float compares that a NaN fails and that read a denormal as zero, "less than zero" is
false for a NaN, -0 and a denormal, sc and tc pass the raw source words through and ma
is 2 * the major coordinate, +0 for a flushed zero.

The translator did the compares as float compares: the AMD Windows driver (26.10.1)
took the NaN-major branch (cubeid(NaN, NaN, NaN) was 5 where the hardware gives 0) and
NVIDIA kept denormals. They are now integer compares on the magnitude bits, so no
driver rule is involved. f32_fuzz's cube() model has the same rules and no longer
declines a NaN.

New GPU test cs_cube_nonfinite. On main it fails on both GPUs; with the change it
passes on both, and all 8,788 measured triples agree with the translated shaders on
the Radeon and the RTX 5080.

AI-assisted.
@tylerstraub

Copy link
Copy Markdown
Contributor

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

On a Linux RTX 5090 at 79fb309e, the stock cs_cube_nonfinite shader passed SPIR-V validation and matched all eight expected words. I also reran the four-case synthetic v_cubema_f32 probe from the #5566 review on the same GPU. Inputs and outputs below are raw words for the subnormal cases.

Input (X, Y, Z) #5566 at 1744ed1 This head
(00000001, 0, 0) 00000002 00000000
(80000001, 0, 0) 80000002 00000000
(+2, −2, 0) −4 −4
(+2, −2, +2) +4 +4

Both signs of the smallest subnormal now produce positive zero, matching the game-MODE result reported for native RDNA2. The finite tie cases still select Y over X and Z over Y.

AI-assisted verification.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 79fb309e28 (base 3faac557b). git diff --check clean.

CPU: python tests/semantic/test_cube_nonfinite.py -> 1 test OK; test_valu_cube.py -> 12 OK; tests/test_f32_fuzz.py -> 28 passed, 1 skipped; tests/test_shader_checks.py -> 83 passed, 266 subtests.
Negative control with main's tools/rdna2spirv.py: test_cube_nonfinite.py FAILED (2 assertions; e.g. out word 0 vs 0x8000000a), so the fixture pins the change.
GPU (RTX 5070 Ti): python scripts/run_gpu_tests.py --filter cube -> 2/2 passed: cs_cube_nonfinite 0x0 0x40800000 0x40000000 0x80000005 0xffc00000 0x80000000 0x7f800000 0x0 and ps_cube_sample. Doing the compares on magnitude bits is what avoids the driver's ordered-compare-of-NaN behaviour the docs describe.

Blocker: conflicts with current main (561b65a83) in tests/semantic/regen_fixtures.py and tests/test_shader_checks.py. Rebase needed.

Verdict: change is correct; request rebase.

tests/semantic/regen_fixtures.py
tests/test_shader_checks.py

AI-assisted.
@valentin-reboli

Copy link
Copy Markdown

Tested 79fb309 on AMD Radeon RX 7700 XT (RDNA3, gfx1101), AMD Windows driver 32.0.32029.2010, Windows 11 Pro 26300: pass. One caveat: on this driver the new test does not tell the old translation from the new one.

Setup: Vulkan SDK 1.4.363.0, llvm-mc 23.1.6 (conda-forge llvm-tools). Test programs built with llvm-mingw 23.1.6 from main 561b65a8; the PR changes no C++ under test, but its branch is based on 3faac557.

  • python -I scripts/run_gpu_tests.py --filter cube: 2/2 pass. cs_cube_nonfinite = 0x0 0x40800000 0x40000000 0x80000005 0xffc00000 0x80000000 0x7f800000 0x0 (all eight expected words), and ps_cube_sample passes.
  • python -m unittest discover -s tests/semantic -p test_cube_nonfinite.py: OK.

Before the change: with only tools/rdna2spirv.py reverted to 3faac557 (everything else at the PR head), cs_cube_nonfinite also passes on this GPU, with the same eight words. I checked that the shaders differ: the pre-PR SPIR-V has 50 float compares (OpFOrd*/OpFUnord*), the PR's has none. I also ran both directly with vk_compute_test <spv> 1 dump, natively and with VKC_SUBGROUP_SIZE=32 as the runner sets it, and got the same words both ways.

So the float-compare translation that the description measured wrong on gfx1036 (26.10.1, cubeid(NaN, NaN, NaN) = 5) and on the RTX 5080 gives the right answers for these eight inputs on gfx1101 with 32.0.32029. That fits the description's point that the old result depended on driver rules, and it argues for the integer form. It also means cs_cube_nonfinite is no regression check on this driver. I didn't run the 8,788-triple sweep, so I can't say whether the old translation differs on some other input here.

No game files used.

@valentin-reboli

Copy link
Copy Markdown

Re-tested on the new head a090c13 (main 561b65a8 merged in), same GPU and driver as above, with test programs built from that same main: same result.

  • python -I scripts/run_gpu_tests.py --filter cube: 2/2 pass. cs_cube_nonfinite gives the same eight expected words; test_cube_nonfinite.py is OK.
  • With tools/rdna2spirv.py from main 561b65a8, cs_cube_nonfinite also passes here. On this driver the test still can't tell the two translations apart.

@bunbeans2-arch

Copy link
Copy Markdown

An RDNA4 data point on the driver version from the description. AMD Radeon RX 9070 XT (gfx1201), AMD 26.10.1, Windows 11, wave32.

Sweep. I compared main d974c88a's translation with this head's: 13 x 13 x 13 operand triples (qNaN of both signs, ±inf, ±0, ±denormal, ±1, ±2, 0.5), operands built at run time, all four cube ops. Results:

  • 0 of 2197 words differ between the two translations, for each op.
  • Both give the words of the gfx1036-measured model in tools/f32_fuzz.py cube() for all 8,788, including cubeid(NaN, NaN, NaN) = 0.

Fixture. cs_cube_nonfinite passes with both translations, with literal and with run-time operands.

So on RDNA4 the old float compares already match RDNA2 on 26.10.1. The cubeid = 5 on the gfx1036 iGPU looks like it comes from that architecture's path in AMD's compiler, not from the driver version as such (a guess from one RDNA4 card; RDNA3 on 26.10.1 not tested). For RDNA4, this answers @valentin-reboli's question: the architecture matters, not only the newer driver. The fixture would then tell the two translations apart only on RDNA2-class parts and NVIDIA.

This is not a positive control: the card shows no difference. spirv-val isn't installed here, so the modules were assembled with each tree's tools/spirv_asm.py.

Synthetic operands only. AI-assisted (Claude Code). No game files.

@sennecools

Copy link
Copy Markdown

Tested a090c13 (on main 561b65a), Windows 11 Pro 26300, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8. This is the RDNA2 run asked for in #5934. On this driver it still can't tell the two translations apart.

python -I scripts/run_gpu_tests.py --filter cube, 3 runs per row, the PR's fixture and check.py in every row:

GPU driver translator cs_cube_nonfinite ps_cube_sample
Radeon iGPU, 9800X3D (gfx1036) 32.0.21045 PR PASS 3/3 PASS
Radeon iGPU, 9800X3D (gfx1036) 32.0.21045 main 561b65a PASS 3/3 PASS
RX 9070 XT (gfx1201) 32.0.32029.2010 PR PASS 3/3 PASS
RX 9070 XT (gfx1201) 32.0.32029.2010 main 561b65a PASS 3/3 PASS

All 12 runs return 0x0 0x40800000 0x40000000 0x80000005 0xffc00000 0x80000000 0x7f800000 0x0. With main's float compares, the gfx1036 here gives cubeid(NaN, NaN, NaN) = 0, not 5. So on this hardware the old result depends on the driver version, not only the architecture: the description measured it on 26.10.1, which is newer than this 32.0.21045. That supports the integer form, but cs_cube_nonfinite is not a regression check on this driver either. I didn't run the 8,788-triple sweep.

test_cube_nonfinite.py: OK. tests/test_shader_checks.py: 82 OK.

No game files used. AI-assisted.

tests/semantic/regen_fixtures.py

AI-assisted.
@Mortify7612

Copy link
Copy Markdown

Tested 4bdc34c1 merged onto main 1cc6e3c1 (clean) on AMD Radeon RX 9060 XT (RDNA4, GFX1200, subgroup 32..64), RADV Mesa 25.0.7, Debian 12 Linux, llvm-mc 23.1.6: pass. run_gpu_tests.py --filter cs_cube_nonfinite: PASS cs_cube_nonfinite 0x0 0x40800000 0x40000000 0x80000005 0xffc00000 0x80000000 0x7f800000 0x0 (fixture only, not the full suite). AI-assisted (Claude Code).

@jordi-vancuijlenborg-vinci

Copy link
Copy Markdown

Tested 4bdc34c1 against main at 1cc6e3c1 on Windows 11 build 26200 with an NVIDIA GeForce RTX 3080 (driver 621.01, Vulkan 1.4.357, subgroup size 32).

I translated the same cs_cube_nonfinite fixture with both revisions and ran both modules through the same vk_compute_test binary. Across three runs per revision, main matched 5/8 expected words and this PR matched 8/8. Both modules passed spirv-val. The focused semantic test also passed on the PR. Synthetic fixture only. AI-assisted.

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.

8 participants