Skip to content

rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) - #3040

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
whoalin1:shader-526-f16-denorm
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
whoalin1:shader-526-f16-denorm

Conversation

@whoalin1

@whoalin1 whoalin1 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claim: SHADER-526 — claimed in the coordination issue #2891, from @Rainmaker33's finding.

Root cause

Since #2209, ps_sdwa_f16_const reads lane y of mrt0 as 0xaaaa0000 instead of 0xaaaa1c00 on some NVIDIA builds: 4096/4096 pixels wrong, the other three lanes right.

The instruction is v_mul_f16_sdwa v3, 2.0, v1 src0_sel:BYTE_1 src1_sel:WORD_1. 2.0 as an f16 inline constant is 0x00004000, so BYTE_1 selects 0x0040, the f16 denormal 2^-18; times 1024.0 that is 2^-8 = 0x1c00. The SDWA select itself is right — the same lanes match on AMD RDNA2 and on NVIDIA builds that do not flush — so the value reaching the multiply is the only thing that differs.

The translator models f16 arithmetic in f32 (Gen.f16_bits: the halves are unpacked with UnpackHalf2x16, the op runs in f32, and the result is narrowed with integer shifts and adds), so no f32 denormal is involved anywhere in the module. What the driver decides is the float-controls mode of the 16-bit conversion it emits for the unpack. Every module declared SignedZeroInfNanPreserve 32 only; with no 16-bit denormal mode, NVIDIA flushed that conversion to +0 and the multiply returned +0. DenormPreserve 16 is the mode that governs it.

Assembling the same translated module with only the two DenormPreserve lines removed puts 0xaaaa0000 back, so it is the mode the driver honours, not anything in the generated code (see Testing).

Changes

  • tools/rdna2spirv.py: the float-controls block now adds OpCapability DenormPreserve next to SignedZeroInfNanPreserve and emits OpExecutionMode %main DenormPreserve 16 next to SignedZeroInfNanPreserve 32. Both are core since SPIR-V 1.4, so no extension is needed at --target-env vulkan1.2. The index entry's float_controls list gains "shaderDenormPreserveFloat16", the device property the mode needs. --no-float-controls still drops both, and its help text now says so.
  • runtime/test/vk_pipeline_check.cpp: the property loop knows shaderDenormPreserveFloat16, so a device that reports it false skips the shader (rather than failing pipeline creation) exactly as it does for shaderSignedZeroInfNanPreserveFloat32. Unknown names still fail.
  • make_game.py: the --no-float-controls help text names both properties.
  • tests/test_translate.py: the declared-modes test expects both capability/mode pairs and both index properties (ps and cs).
  • tests/semantic/test_f16_denorm_preserve.py (new, standard library only): pins the capability and the execution mode, the index property, the --no-float-controls fallback, that the mode changes nothing but the header, the four ps_sdwa_f16_const words through the interpreter, and that SPIRV-Tools accepts the module at vulkan1.2.
  • docs/design.md: a "Float controls (f16 denormals) (SHADER-526)" contract row with the measurement, the fallback, and what was verified on which device.

No new .asm fixture: the rule is a module-header rule, and the value side already has the GPU fixture it protects (runtime/test/shaders/ps_sdwa_f16_const.asm, measured on gfx1036 and already a tests/semantic fixture). The new test module pins mode and words together instead.

Fallback where the device cannot preserve: vk_pipeline_check skips such a shader, and --no-float-controls translates without either mode. The module then has no f16 denormal guarantee, so an f16 denormal read can come back +0 — which is exactly the lane above on the NVIDIA builds that flush, and it is a difference in the shader's values, not something the translator can repair.

Not every NVIDIA build flushes (so the test passing on main is not a contradiction): on main without the mode, an RTX 4060 Ti (621.24), an RTX 5070 Ti (620.73.04, Linux) and an RTX 5080 (616.92) pass the test (data points from @airp1ane and @Marukooh). This change does not make those worse: they already keep the denormal, and the mode is a no-op there.

Testing

Hardware: NVIDIA GeForce RTX 4070 SUPER, driver 610.88, Windows 11, apiVersion 1.4.341 (Vulkan SDK 1.4.363); AMD Radeon iGPU (Ryzen 5 7600, RDNA2), Windows driver 24.10.24, apiVersion 1.3.280.

export VK_LOADER_DRIVERS_SELECT='*nv-vk*' LEONIDA_VK_DEVICE=4070 LEONIDA_VK_VALIDATION=1
export LLVM_MC='wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc'
py -3.14 -I scripts/run_gpu_tests.py --filter ps_sdwa_f16_const --work <dir>

ps_sdwa_f16_const:

tree result
origin/main translator, before the change FAIL 4096/4096 mismatches; lane y 0xaaaa0000, want 0xaaaa1c00
this branch PASS 4096/4096 pixels exact, also with LEONIDA_VK_VALIDATION=1 (no validation errors)

Isolation, on the same module and the same harness (the three variants differ only in which float-controls lines the header carries): no modes at all → lane y 0x0000; SignedZeroInfNanPreserve 32 only (what main emits) → 0x0000; both modes → 0x1c00. So the 16-bit mode is the operative part.

Full GPU suite on the RTX 4070 SUPER with validation on: 143/189 passed, 46 skipped, 0 failed with the change (on the same build and GPU with the origin/main (1628210) translator: 142/189 passed, 46 skipped (the one failure being ps_sdwa_f16_const), so the change moves exactly one test from fail to pass and changes nothing else).

py -3.14 -m unittest discover -s tests/semantic

The new module alone, python -I -m unittest discover -s tests/semantic -p test_f16_denorm_preserve.py: Ran 6 tests ... OK. The whole tests/semantic discovery run did not finish before this PR was opened: the machine was shared with other agents' builds and it was still running after 25 minutes, so it is not claimed here.

py -3.14 -m pytest tests/test_translate.py -q -p no:cacheprovider -k float_controls

3 passed, 542 deselected — test_float_controls_declared[ps], [cs] and test_no_float_controls_leaves_them_out.

vk_pipeline_check on the AMD Radeon iGPU (which accepts the new module and pipeline even though its denormBehaviorIndependence is 32_BIT_ONLY):

vulkaninfo: shaderDenormPreserveFloat16 = true, shaderDenormFlushToZeroFloat16 = true
device: AMD Radeon(TM) Graphics (subgroup 32..64)
ps: 1 modules ok, 0 failed
ps pipelines: 1 ok, 0 failed, 0 skipped

The GPU test itself skips on that iGPU (it needs 144 push-constant bytes; the device has 128), so no pixel result is claimed from AMD hardware. spirv-val --target-env vulkan1.2 passes on the translated module, which the new semantic test checks.

Not verified: an NVIDIA build that actually flushes is only available to me as the RTX 4070 SUPER (which does flush); I could not run an RTX 3090, 4060 Ti or 5070 Ti myself.

🤖 Generated with Capy (DeepSeek V4.1 Flash)

… (SHADER-526)

ps_sdwa_f16_const read mrt0 lane y as 0x0000 instead of 0x1c00 on NVIDIA: the
_sdwa src0_sel:BYTE_1 selects the f16 denormal 0x0040 (2^-18), and the driver
flushed it on the way into f32 because the module declared no 16-bit denormal
mode. The translator models f16 arithmetic in f32 with integer narrowing, so no
f32 denormal is involved and that conversion is what the 16-bit mode governs.

Every module now also emits OpExecutionMode %main DenormPreserve 16 with the
matching capability (core since SPIR-V 1.4, so no extension), and the index
lists shaderDenormPreserveFloat16, which vk_pipeline_check requires and
--no-float-controls still drops along with the 32-bit mode.

Tested on an RTX 4070 SUPER (driver 610.88, validation layers on): the GPU test
goes from 4096/4096 mismatches to 4096/4096 pixels exact, and the full GPU suite
from 142/189 to 143/189 passed with 0 failures and the same 46 skips.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@tylerstraub

Copy link
Copy Markdown
Contributor

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

Independent Linux Vulkan check of a094bd2d on a GeForce RTX 5090, driver 595.99.02, with 32-wide subgroups. The stock ps_sdwa_f16_const fixture produced aaaa3800 aaaa1c00 aaaa3e00 aaaa73f0 at all 4,096 pixels. Reassembling the same fragment module after removing only OpCapability DenormPreserve and OpExecutionMode ... DenormPreserve 16 changed the second word to aaaa0000 at all 4,096 pixels. The other three words were identical. Both variants used the same vertex module.

Both fragment modules passed spirv-val --target-env vulkan1.2. The stock vertex and fragment pipelines passed vk_pipeline_check (2/2, no skips), and the focused CPU suite passed 6/6. This confirms the effect of the mode on this Linux driver for the synthetic fixture. AI-assisted.

AI-assisted verification.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Tested a094bd2d merged onto main 16282101 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5, subgroup 32..64), Mesa 25.2.8, Ubuntu 24.04, LLVM 23.1.6, SPIRV-Tools 2026.4: pass.

  • Radeon (shaderDenormPreserveFloat16 = true), runtime rebuilt from this branch: 172/189 passed, 15 skipped, identical per test to plain main (the same two known Mesa 25.2 failures, GPU test results (one comment per GPU and driver) #166).
  • Mesa llvmpipe 25.2.8 (LLVM 20), which reports shaderDenormPreserveFloat16 = false, the CI device: 120/189 passed, 69 skipped on both main and this branch, identical per test; ps_sdwa_f16_const passes on llvmpipe with this branch. So the CI llvmpipe job loses no coverage.
  • tests/semantic/test_f16_denorm_preserve.py and tests/test_translate.py: pass (541 passed, 4 skipped for test_translate.py).
  • Question: vk_pipeline_check now skips a module on a device without shaderDenormPreserveFloat16, but vk_compute_test/vk_harness don't check the property, so on llvmpipe the GPU suite creates pipelines from modules that declare a mode the device says it doesn't support (a SPIR-V/Vulkan valid-usage rule, if I read the float-controls VUIDs right). llvmpipe ignores it, so it's harmless in practice; I couldn't run the validation layer here to confirm whether it flags it.

gfx1151 is RDNA 3.5, not RDNA2: this shows the translated SPIR-V gives the modelled words on a real driver, not that RDNA2 hardware does. AI-assisted (Claude Code).

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Independent Windows/NVIDIA confirmation of SHADER-526 on a GeForce RTX 4060 Ti, driver 610.88: on upstream main 16282101, ps_sdwa_f16_const had 4096/4096 mismatched pixels (low-half denormal 0x0040 became zero). I independently implemented an alternative fallback that avoids relying on driver f16 denorm support for the scalar paths: decode signed exponent-zero f16 halves by integer mantissa widening to f32 (mantissa * 2^-24), preserving both signs of zero; retain ordinary unpack for normal/Inf/NaN.

Reference-only fork branch: https://github.com/pmilewsskiedu-gif/Leonida/tree/alternative/f16-subnormal-integer-widening (single commit cd3c908e, 2 source/test files). I am not opening a competing upstream PR because SHADER-526 is already claimed here. The fallback may be useful to complement your DenormPreserve 16 approach on devices without shaderDenormPreserveFloat16: this integer path needs no extra float-control property for scalar f16 source widening.

On RTX 4060 Ti: 144 targeted CPU tests pass; 10 focused Vulkan fixtures match 40,960/40,960 pixels; complete synthetic suite 194/194 SPIR-V modules validated, 143 GPU pass, 46 skip, 0 fail, validation on. Independent review exhaustively checked all 2,048 signed exponent-zero half values. Limit: this alternative only changes f16_in and v_cvt_f32_f16, not packed f16 or other direct unpacks. AI-assisted; synthetic-only, no game files.

@Rainmaker33

Copy link
Copy Markdown

Tested a094bd2 on the machine from the finding: NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300. pass: it fixes ps_sdwa_f16_const here.

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

  • scripts/run_gpu_tests.py: 143/189 pass, 46 skipped (wave64), 0 failures. On main the same GPU has 1 failure, ps_sdwa_f16_const, with lane y 0xaaaa0000.
  • ps_sdwa_f16_const: 4096/4096 pixels exact, so lane y now gives 0xaaaa1c00.
  • spirv-dis of the translated ps_sdwa_f16_const.spv now has OpCapability DenormPreserve and OpExecutionMode ... DenormPreserve 16, next to the existing SignedZeroInfNanPreserve 32. This driver reports shaderDenormPreserveFloat16 = true.
  • No f16 regressions: valu_fuzz --family f16 --seed 1 --programs 100 matches 100/100 that ran natively and 200/200 with --emulate, with 0 mismatches.
  • tests/semantic/test_f16_denorm_preserve.py: OK. I didn't run tests/test_translate.py (it needs pytest, which isn't installed here).

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested a094bd2d on NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass — on exactly the hardware family the bug showed on.

  • ps_sdwa_f16_const 4096/4096 pixels exact — lane y of mrt0 keeps the f16 denormal (0xaaaa1c00-shaped results, not the flushed 0xaaaa0000), so the DenormPreserve 16 declaration works through the NVIDIA driver on Ada. This is the fixture whose lane-y regression since rdna2spirv: apply the SDWA src_sel to an f16 inline float constant #2209 the PR fixes.
  • run_gpu_tests.py: 143/189 passed, 46 skipped (native wave64, expected on this 32-lane GPU) — no regressions from the new execution-mode declaration
  • Semantic suite: 4148 OK (Python 3.12.10), including the updated SDWA f16 constant coverage

The root-cause reasoning holds on this machine: without the declaration the driver flushes the f16 denormal on the load path, with it the bits survive end to end.

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@skipper-hack skipper-hack left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested a094bd2 on an RTX 3090 with NVIDIA driver 595.99.02 on Linux. ps_sdwa_f16_const failed on baseline 1628210 and passed on the PR with all 4,096 pixels matching. Full GPU suite on the PR had 143 passed, 46 skipped, and 0 failed.

@steamaze0

Copy link
Copy Markdown

Tested a094bd2 on NVIDIA RTX 5060 Ti (Blackwell), subgroup 32..32, NVIDIA 621.01 (Vulkan 1.4.357), Windows 11: pass.

  • ps_sdwa_f16_const: PASS, 4096/4096 pixels exact (the fixture this rule protects). This 621.01 Blackwell build keeps the f16 denormal (does not flush), so DenormPreserve 16 is a no-op here — another non-flushing NVIDIA data point, and the mode does not regress it.
  • run_gpu_tests.py (LEONIDA_W64_EMULATE=1): 174/189 passed, 15 skipped, 0 failed. build.py: 194 shaders, 194 translated, 194 pass spirv-val.
  • CPU: test_f16_denorm_preserve.py 6/6 OK.

Tools: llvm-mc 23.1.6 (conda-forge, native Windows), Vulkan SDK 1.4.363.0 (spirv-as/spirv-val), clang 23.1.6 (llvm-mingw; standalone Vulkan tools with -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, no MSVC).

AI-assisted (Qoder): I built the tools, ran the GPU suite and the CPU test.

@gustavorafaelschmidt

Copy link
Copy Markdown

I confirmed this on a driver that flushes: GTX 1050 Ti (Pascal), NVIDIA 582.78, Windows 11. The driver reports shaderDenormPreserveFloat16 = true.

main 1628210 this PR at a094bd2d
ps_sdwa_f16_const FAIL (lane y 0xaaaa0000, want 0xaaaa1c00) PASS (4096/4096 exact); also passes with LEONIDA_VK_VALIDATION=1, with no validation errors
run_gpu_tests.py, native 133/189 passed, 55 skipped, 1 failed 134/189 passed, 55 skipped, 0 failed
run_gpu_tests.py, W64/bary/mesh/texoff fallbacks on 172/189 passed, 16 skipped, 1 failed 173/189 passed, 16 skipped, 0 failed

In both runs, ps_sdwa_f16_const is the only test whose result changes. Full entry in #166: #166 (comment)

Synthetic shaders only. AI-assisted (Claude Code).

@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.

Verified on the PR head: tests/semantic/test_f16_denorm_preserve.py -> 6/6 passed; test_translate.py -k float_controls -> 3 passed, 542 deselected. On current main the new test fails 3/6 (capability + mode, index property, mode + words). The assembled binary carries OpCapability DenormPreserve and OpExecutionMode %main DenormPreserve with the single literal 0x10 = 16, no 32/64 preserve, and the existing SignedZeroInfNanPreserve 32 is unchanged; spirv-val --target-env vulkan1.2 passes. Across 33 translated fixtures, deleting only the two *DenormPreserve* lines leaves the output byte-identical, so f32/f64 behavior is unchanged. No game data or absolute paths. Approve.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested a094bd2d (base 16282101) on NVIDIA GeForce RTX 3060 Ti (Ampere, subgroup 32..32), driver 580.173.02 on Linux (Vulkan 1.4.312; Ubuntu 26.04 container on a TrueNAS 26.0 host): pass. The other confirmations here are Windows drivers, so this adds the Linux one, plus wave64 emulation.

The device reports shaderDenormPreserveFloat16 = true, shaderDenormFlushToZeroFloat16 = false, denormBehaviorIndependence = ALL. scripts/run_gpu_tests.py, whole suite, fresh --work each run:

base 16282101 this head
native wave32 142/189 passed, 46 skipped, ps_sdwa_f16_const FAIL 143/189 passed, 46 skipped, 0 failed
LEONIDA_W64_EMULATE=1 173/189 passed, 15 skipped, ps_sdwa_f16_const FAIL 174/189 passed, 15 skipped, 0 failed

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

@ttd0x

ttd0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested a094bd2d merged onto main f847748c on NVIDIA GeForce RTX 2080 Ti (Turing, subgroup 32..32, driver 610.88), Windows 11: pass.

ps_sdwa_f16_const fails on main on this card (an f16 denormal operand is read as 0: word y 0x0000 instead of 0x1c00) and passes with this PR, 4096/4096 pixels exact.

  • tests/semantic/test_f16_denorm_preserve.py: 6 tests OK.
  • scripts/run_gpu_tests.py (default, no emulation): 204 shaders translated, all pass spirv-val, 153/199 passed, 46 skipped (native wave64, expected on a 32-wide device), 0 failed.
  • Not run: the runtime/test/vk_pipeline_check.cpp change (harness not rebuilt for it) and tests/test_translate.py (needs pytest, which is not installed here).
  • The branch is based on 16282101, and docs/design.md conflicts with main; I took main's version of that file for the test (the other files merged cleanly).

Synthetic fixtures only, no game data. AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit d0b036f into ItsNotRexed:main Nov 22, 2026
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.