Skip to content

rdna2spirv: v_dot2_f32_f16, v_dot2c_f32_f16 and v_trig_preop_f64 (SHADER-449) - #2537

Open
IAmNotSiob wants to merge 8 commits into
ItsNotRexed:mainfrom
IAmNotSiob:isa-dot2-trig-preop
Open

IAmNotSiob wants to merge 8 commits into
ItsNotRexed:mainfrom
IAmNotSiob:isa-dot2-trig-preop

Conversation

@IAmNotSiob

Copy link
Copy Markdown
Contributor

SHADER-449 (claim in #2300). three RDNA2 ops the translator refused: v_dot2_f32_f16, v_dot2c_f32_f16, v_trig_preop_f64. isa_coverage exact 498 -> 501.

what it does

  • v_dot2_f32_f16 (VOP3P) / v_dot2c_f32_f16 (VOP2, accumulates into D): each f16·f16 product is exact in f32, so it's two f32 Fmas, low half first: fma(S0.hi, S1.hi, fma(S0.lo, S1.lo, S2)). op_sel/op_sel_hi/neg_lo/neg_hi on S0/S1, neg_lo[2] on S2, clamp. refused: abs, DPP/SDWA, VOP3 neg, and S2 half-selects (S2 is f32).
  • v_trig_preop_f64: the 53-bit segment of 2/π starting at fraction bit shift = (S1&31)*53 + max(exp-1077, 0), scaled by 2^(-53-shift) (+128 when exp >= 1968). NaN -> quieted NaN, a segment past the 37-word table -> +0. this matches LLVM's amdgcn_trig_preop constant fold ("return 0 for outbound segment (hardware behavior)"). SPIR-V side: Private table array, 64-bit funnel shift on 32-bit words (no Int64 needed), and the scale split into two Ldexps so no step goes below the double range.
  • design.md: two rows next to the signed-byte dot products.

testing

  • tests/semantic/test_dot2_trig_preop.py: dot2/dot2c value tests in the mini interp against a python model (random, modifiers, clamp, inf/NaN, a single-rounding case), table == 2/π computed from Machin, the reference model vs LLVM's fold values, spirv-val of all three ops, refusals. (the mini interp has no f64, so trig_preop values are checked on the GPU)
  • python model vs LLVM 21's opt -passes=instcombine fold: 891 swept (exponent, segment) pairs incl. the exponent 1077/1968 edges and the table end: 0 mismatches.
  • GPU fixture cs_w32_trig_preop (seg 0, exp >= 1968, a denormal result, a funnel shift): PASS bit-exact on RTX 6000 Ada (Linux, NVIDIA), native and LEONIDA_W64_EMULATE=1. plus 480 random inputs through the same fixture vs the model: 480/480 bit-exact.
  • cs3: tests/semantic + test_rdna2spirv* + isa_coverage + wave64 tests: 2692 passed, 3 skipped (test_fma_khr spirv-tools check deselected, old SPIRV-Tools on that box; fails on main too).
  • test_docs_design_rows.py fails the same 3 ways as on main (PROC-97/106), nothing new.

unverified on AMD: dot2's add order (two roundings vs one, last bit only) and f16 denormals; trig_preop on real RDNA2 (follows LLVM). a device that flushes f64 denormals would fail the denormal word of the fixture.

overlaps: none in code. check.py/run_gpu_tests.py lists touch the same lines as #1976 (mine), trivial rebase whichever lands second.

AI-assisted (Claude Code).

TL;DR: adds 3 missing ops (two f16 dot products into f32 + the f64 2/π lookup used by big-argument sin/cos). CPU value tests, matches LLVM's reference on 891 cases, bit-exact on an NVIDIA GPU on 484 inputs.

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent GPU coverage of the two dot instructions at 1f8e711 on an RTX 5070 (Windows 11, driver 621.24, native wave32). I used 512 synthetic input triples with seven instruction recipes, including both opcodes, half selection, sign modifiers, clamp, and DOT2C with D aliasing a packed input. I compared the readbacks to two calls to the Windows CRT fmaf, in low-product-first order. Both generated modules passed Vulkan 1.2 SPIR-V validation, and all 11 PR tests passed.

GLSL Fma and --fma khr each matched 3,570 of 3,581 checked results, with matching NaN classification where applicable. This includes 45 unmodified cases where reversing the two products changes the reference result and 33 cases where flushing FP16 inputs changes it. I excluded three clamped NaN outputs.

All 11 differences in each mode came from FP32 subnormal accumulators. For example, with both packed inputs zero and the FP32 accumulator word 0x00000001, both translated ops wrote zero. The device advertises shaderDenormPreserveFloat32 = false. This establishes the NVIDIA behavior of the lowered shaders. The rounding order and mode-specific denormal behavior of the native RDNA2 instructions still need an AMD oracle.

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

ty for the 5070 run, 512 triples incl. D aliasing is way more than i covered :D

@Rainmaker33

Copy link
Copy Markdown

Tested 1f8e711 on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass.

The PR's diff applied cleanly to main 4947579. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8; harness programs from a CMake build of that main; overlay layers off.

  • scripts/run_gpu_tests.py: 140/186 pass, 46 skipped (wave64), no failures (main: 139/185). cs_w32_trig_preop: 1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470.
  • tests/semantic/test_dot2_trig_preop.py: OK.

@Marukooh

Copy link
Copy Markdown
Contributor

Tested 1f8e711 on NVIDIA GeForce RTX 5070 Ti (Blackwell), NVIDIA 620.73.04, CachyOS Linux. Built the harness from this PR and ran scripts/run_gpu_tests.py --filter cs_w32_trig_preop with LEONIDA_W64_EMULATE=0 and =1. The synthetic module passed SPIR-V validation and all eight expected words matched in both runs. The 11 test_dot2_trig_preop.py tests passed. GPU coverage here is for v_trig_preop_f64; the dot-product cases were checked by the semantic suite. AI-assisted.

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

ty for testing ^^

@Pierre-Kookis

Copy link
Copy Markdown

Native RDNA2 run of v_dot2_f32_f16 / v_dot2c_f32_f16: the hardware differs from this PR's model in three ways. The PR's tests (11/11) and the NVIDIA runs above check the translation against the model, so they cannot see this.

Setup: AMD Radeon RDNA2 iGPU (Ryzen 7 7800X3D, gfx1036), Windows 11, driver 32.0.21043.5001, tools/hw_oracle, raw wave32 kernels, MODE ieee=0 dx10_clamp=1, f16 denormals kept, f32 denormals flushed and kept. Inputs as (S0, S1, S2) words:

case gfx1036 (both MODEs) PR model / translation at 1f8e7110
1·1 + 1·1 + 2^24: 3c003c00, 3c003c00, 4b800000 4b800001 (dot2 and dot2c) 4b800000
65504² − 65504² + 1: 7bff7bff, 7bfffbff, 3f800000 3f800000 (dot2 and dot2c) 00000000
40003c00, 44004200, 3f000000 with clamp 41380000 (11.5, same as without clamp) 3f800000
c0803e00, 42003600, bf000000 with clamp c0d60000 (−6.6875) 00000000
0·0 + 00000001 00000000 (dot2 and dot2c) 00000001
0·0 + 007fffff 00000000 (dot2 and dot2c) 007fffff
  1. One rounding, not two FMAs. The products and S2 are added exactly and rounded once. Two chained FMAs round the intermediate sum, which loses the +2 in the first row and the +1 in the second.
  2. The clamp bit does nothing on gfx1036. llvm-mc 23.1.6 encodes it ([0x08,0xc0,0x13,0xcc,...], bit 15 set), and the result is the unclamped sum, positive and negative alike. clamp on v_dot2c_f32_f16 does not assemble at all.
  3. A denormal f32 accumulator reads as zero, in both f32 denormal MODEs. f16 denormal inputs are kept.

A wider sweep had a second model write and run a probe on the same GPU: 26,305 words, with seeded random inputs, half selection, neg_lo/neg_hi, NaN/inf and the clamp form. It found mismatches in all three classes above, plus NaN results whose payload or sign differs from the canonical NaN the translation gives. I re-ran the table above myself with a separate kernel.

(AI-assisted: the wider sweep by a second model on this machine; the table re-measured by me. Hand-written kernels, no game files.)

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (MSVC 14.51, llvm-mc 23.1.6). cs_w32_trig_preop passes on the RTX 4080 SUPER (1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470). On the Intel UHD 770 it hangs and is killed after 120 s (vk_compute_test.exe was killed (a GPU hang?)). v_trig_preop_f64 emits Float64, and on main vk_compute_test neither enables shaderFloat64 nor skips devices without it (only vk_harness checks). #2395 adds exactly that check, return 77 when the device lacks shaderFloat64, so with it this fixture should become a skip on such devices instead of a hang. It might be worth landing after #2395, or noting the dependency.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 1f8e711 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: cs_w32_trig_preop PASS.
  • tests/semantic/test_dot2_trig_preop.py: ran 11 tests in 0.555s; OK (skipped=1).

AI-assisted (Claude Code).

@zneel

zneel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 776e091c 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: cs_w32_trig_preop fails on the denormal word, which is the case you flagged as unverified on AMD. Everything else passes.

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) 175/190, 14 skipped, 1 failed: cs_w32_trig_preop
same with LEONIDA_W64_EMULATE=1 same, 1 failed: cs_w32_trig_preop
pytest tests/test_translate.py tests/test_shader_checks.py tests/test_run_gpu_tests.py 648 passed, 4 skipped
unittest discover -s tests/semantic -p test_dot2_trig_preop.py 11 OK

out[0..7] = 1841940610 1071931184 1381846399 107934026 0 0 501947569 682761470. Word 4 is 0, where 0x07DE294A was expected: that's the third case (exponent 1117, segment 19, result 2^-1100-ish). The other seven words match.

What I found by editing the translated module (spirv-dis → edit → spirv-as, spirv-val clean, run with vk_compute_test ... 1 dump):

module word 4
as translated 0
plus OpCapability DenormPreserve / OpExecutionMode DenormPreserve 64 (the device reports shaderDenormPreserveFloat64 = true) 0
the last Ldexp of that case (Ldexp %514 %513, here by -447) replaced with OpFMul by the constant 2^-447 (bits 0x24000000_00000000) 0x07DE294A, correct
same OpFMul change, without DenormPreserve 0x07DE294A, correct

So on this driver, GLSL.std.450 Ldexp on a double returns 0 when the result is denormal, even with DenormPreserve 64, while an f64 multiply produces the denormal correctly. The first two steps of the chain (ldexp(v, -53), then ldexp(.., max(-shift, -600))) stay normal, so only the last step matters.

A possible fix that doesn't depend on the driver's Ldexp: do the last step as an OpFMul by 2^r built from bits ((1023 + r) << 20 in the high word). r can fall below -1022 for large shifts, so it needs a clamp or a split there, but those results underflow to 0 anyway. That's a single rounding, as with Ldexp. Whether RDNA2 behaves the same through RADV or AMD's Windows driver, I can't check here.

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

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

thx @zneel, great digging :D pushed 3f1784f: the last scale step is now two exact f64 multiplies by 2^k built from bits (first by 2^max(k,-300) so the value stays normal and exact, then by 2^max(rest,-1022), anything below that underflows to 0 anyway), so there's still a single rounding and no dependence on the driver's Ldexp. cs_w32_trig_preop passes on RTX 6000 Ada (linux) with word 4 = 0x07DE294A, semantic tests ok. a rerun on your 9070 XT would close it out ^^ AI-assisted (Claude Code).

@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 3c20f6d on NVIDIA RTX 4080, driver 610.88, Windows 11: pass

This head merges main 03b09a08; the last check was at 776e091c (AMD). Ran (MSVC 19.44 / VS 2022 BuildTools, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, native llvm-mc 23.1.6; test programs built from this branch):

  • python -I scripts/run_gpu_tests.py --filter trig_preop, three runs: 1/1 each, cs_w32_trig_preop PASS with the same eight words every time. This includes the 3f1784fd change: the last scale step is now exact f64 multiplies instead of Ldexp.
  • Full run_gpu_tests.py: 165/211 passed, 46 skipped, 0 failed.
  • tests/semantic/test_dot2_trig_preop.py (run from tests/semantic): OK.
  • Corpus: no shader in my 1.0.2.1 carve (25,734 disassembled) uses v_dot2_f32_f16, v_dot2c_f32_f16 or v_trig_preop_f64, so this changes no game shader's translation.

AI-assisted.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 3c20f6d4 (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. cs_w32_trig_preop passes (1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470), so this device keeps the f64 denormal of the fixture.
  • ctest: 48/48 passed.
  • pytest tests/semantic/test_dot2_trig_preop.py: 11 passed. With the base's tools/ (03b09a0): 8 failed, 3 passed.

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

@amarovndres

Copy link
Copy Markdown

Tested 3c20f6d4 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 cs_w32_trig_preop 1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470
  • tests/semantic/test_dot2_trig_preop.py: 11 tests, OK

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

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

nice thx @amarovndres, that closes the AMD windows denormal case \o/

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

merged main: main now has its own v_dot2_f32_f16 / v_dot2c_f32_f16 (68804c6), so this pr is down to v_trig_preop_f64 only. heads up for whoever owns that: my old dot2 test expected one rounding of the full f32 sum and main's gives 1 ulp different on some inputs (e.g. a=0xc7cac6f4 b=0xc3b34566 c=0xc2548905 -> main 0xc2730168 vs 0xc2730169). not measured which one rdna2 does, just flagging :)

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent Apple Silicon / MoltenVK check of head 7d3623ea (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup range 4..32, llvm-mc 23.1.6. The C++ harness was built once from base 3eb4410b and shared with base for a clean A/B (this PR changes only Python/translator files).

  • python -m unittest discover -s tests/semantic -p test_dot2_trig_preop.py: 5/5 OK.
  • python -I scripts/run_gpu_tests.py --build <shared harness>: 152/218 passed, 58 skipped, 8 failed. Base 3eb4410b with the identical harness: 152/217, 58 skipped, 7 failed. The only difference is the new fixture; no common test changed status.
  • The new cs_w32_trig_preop fixture cannot run on this device: the harness exits unsupported: ... uses Float64 and the device has no shaderFloat64. So I cannot confirm the f64 words here — the NVIDIA bit-exact results in the PR body stand.

Actionable harness gap found: that capability failure is counted as a FAIL, not a SKIP. judge_cs_dump() in scripts/run_gpu_tests.py maps exit 77 to a skip only when wave64=True; cs_w32_trig_preop is in the non-wave64 list (line ~1099), so it falls through to (False, msg). cs_f64_add (line ~946) already handles rc == 77 with SKIP (no shaderFloat64 on this device). A comparable guard for this fixture would make the Apple result a correct skip. (Same applies to check.py if it gates f64 fixtures similarly.) I did not change code; just reporting the observation.

No game data; synthetic fixtures and counts only.

AI-assisted.

@IAmNotSiob

Copy link
Copy Markdown
Contributor Author

thx @bobcatchris, good catch :) judge_cs_dump now treats exit 77 + 'no shaderFloat64' as a skip for any compute dump (pushed here and to #1976, which has cs_w32_f64_ops with the same issue), plus a unit test. AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown

Tested 959f00c (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_dot2_trig_preop.py tests/test_find_hash_lookup.py tests/test_first_look_shim_report.py tests/test_run_gpu_tests.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) 172 passed, 1 skipped, 315 subtests passed in 3.92s

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

  • native:
    • cs_w32_trig_preop main: - pr: PASS 1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470
  • emulated:
    • cs_w32_trig_preop main: - pr: PASS 1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470

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 959f00c2 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 186/218 passed, 32 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:

  • cs_w32_trig_preop: Intel SKIP (no shaderFloat64 on this device); RTX 4080 PASS, 1841940610 1071931184 1381846399 107934026 132000074 0 501947569 682761470

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

@kapitche

Copy link
Copy Markdown
Contributor

Tested 959f00c merged with main 98fa503 on Linux (Python 3.12.16) and Windows (RTX 4080 SUPER, UHD 770): pass. test_dot2_trig_preop and test_run_gpu_tests 2/2 ok. run_gpu_tests.py cs_w32_trig_preop 1/1 on RTX 4080 SUPER; skipped on UHD 770. Negative control with main's tools/rdna2spirv.py: test_dot2_trig_preop errors (2 errors) and the GPU case fails with a translation failure (no .spv).

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 959f00c2 (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.