fix(shader): mark the fused exact f32 operations NoContraction #16967

Merged
sherySJ merged 2 commits from upstream-pr/16967 into main 2026-11-15 19:52:51 +00:00
sherySJ commented 2026-11-08 02:24:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sherySJ on 2026-11-08T02:24:14Z.


Cause and design

Not an ANV float-controls deviation and not a test problem: the fused lowering lost the exactness marker its unfused sibling has.

  • fusedExactArithmetic lowers the f32 multiply, add and subtract to Fma(x, y, -0.0), Fma(1.0, x, y) and Fma(-1.0, y, x). Production sets it for MoltenVK only (VulkanDevice.cpp:1592); F32DenormalFlush.cpp:524-526 forces it on every device, which is how ANV reaches it. The unfused branch of each emitter is OpFMul/OpFAdd/OpFSub plus NoContraction; the fused Fma carried no decoration.
  • flushTinyProduct (OperandAccess.cpp:159, line 169) decides "tiny after rounding to 24 bits with an unbounded exponent" from (lhs * 2^64) * rhs. Mesa 26.2.5 rewrites an undecorated Fma(x, y, -0.0) to fmul (nir_opt_algebraic.py:311), then nir_opt_algebraic_before_ffma (brw_nir.c:3241) moves the constant to the outer multiply (nir_opt_algebraic.py:3607-3609). Those rules are inexact (~) and skip an instruction flagged exact (nir_search.c:332; NoContraction becomes nir_fp_exact, vtn_alu.c:466).
  • Lane 4 (0x3f7fffff * 0x00800000, exact value 2^-126 - 2^-150) was therefore judged on the rounded product: the tie rounds to 0x00800000, times 2^64 is 0x20800000, not tiny, so 0x00800000 was kept where the console flushes to zero. INTEL_DEBUG=cs on the HD 520, before: fmul(fmul(lhs, rhs), 2^64) with no exact flag; after: fmul(lhs, 2^64) // exact then fmul(.., rhs) // exact, the authored order.
  • ANV honours the declared controls (the module declares DenormPreserve 32 and SignedZeroInfNanPreserve 32; the [float-controls] hardware flush probe reports 0 of 5472 results differing; the unfused variants pass), so no probe is needed. The fix is the decoration, as EmitFPFma32 and the Exact(Fma) polynomials already do. Any NIR driver that runs the fused lowering is exposed the same way; only the HD 520 and llvmpipe were run.

What

Marks every fused-exact f32 Fma NoContraction, like the unfused operation it replaces:

  • core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp: ExactMul (152), ExactSub (157), EmitFPAdd32 (1931), EmitFPSub32 (1936), EmitFPMul32 (1941).
  • core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp: the offset fold in FoldedOffsetCoord (528) and texelFloor in the packed horizontal gather (1508); the same pattern, no test samples through the fused lowering on this host.
  • core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp: verifyFusedExactArithmetic (7055) now requires every Fma of the plain and the fused module to be decorated. agc_driver_f32_denormal_flush passes on llvmpipe before and after the change, so this device-independent assertion is what keeps CI from losing the decoration.

No float semantics change on a driver that did not reassociate, and none for the console measurements in the debt files. No shader cache version edit: ShaderCacheVersion.cmake hashes the recompiler sources. v_fma_f32 already carried the decoration (SpirvAluEmitterMath.cpp:2109), so MoltenVK sees nothing new in kind.

Refs: #15817 (four failing tests; this change covers agc_driver_f32_denormal_flush; the f64 frexp one is https://github.com/boykopovar/AnyPS5/pull/16930)

Tested

Linux 7.2.8 x86_64 (Arch), GCC 16.2.1 Release, Mesa 26.2.5. Windows and macOS (MoltenVK) not tested. No title run.

Same machine, test binaries rebuilt for each commit, ctest --test-dir build -R '^agc_driver_f32_denormal_flush$' -V with ANYPS5_GPU=<gpu> ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The layer is vulkan-validation-layers 1.4.363.0 from the Arch extra mirror, SHA-256 checked against extra.db, extracted without root; VK_LOADER_DEBUG=layer shows it loaded, and no log has was not found but was requested by env var VK_INSTANCE_LAYERS.

GPU main 210676ad86 this change
Intel HD Graphics 520 (Skylake GT2, Mesa ANV) fails: fused lane 4 no float mode v_mul_f32 v12, v4, v5 is 0x00800000, expected 0x00000000 f32 denormal flush tests passed
llvmpipe (LLVM 23.1.3) passes passes

The main and change logs differ only in that result line and the host memory budget figures; no VUID- or validation line in either, on either GPU.

The table ran on the change rebased onto main 210676ad86; the float, shader and full-sweep runs below ran on it rebased onto 6daf9a1cb6, and the focused test and agc_shader_memory were run again on the head rebased onto 4c4ff42a6c with the same results. Between 210676ad86 and this head the only change under core/shader or core/libs/prx/libSceAgcDriver is core/libs/prx/libSceAgcDriver/tests/TextureTiling.cpp.

  • Negative control for the new assertion: agc_shader_memory with the test change and main's emitters fails with fused exact arithmetic: a fused multiply-add is not marked NoContraction ...; with this change it passes.
  • 75 float tests (every agc_driver_f32_*, agc_driver_f16_*, f64, fma, ldexp, mullit, vcmp and other denormal, rounding, NaN and float-mode test), with the layer. HD 520: main fails f32_denormal_flush and f64_rounding_denormal_modes; this change fails only f64_rounding_denormal_modes (the frexp mantissa of https://github.com/boykopovar/AnyPS5/pull/16930, out of scope). llvmpipe: f64_div_scale_denormal_modes and f64_fma_denormal_modes fail on main and with this change alike (not touched here). Zero VUID- lines in all four runs.
  • 44 agc_shader_* tests, with the layer: all pass (agc_shader_disk_cache_large_file skips).
  • Full ctest -R '^agc_driver_' --timeout 90 on the HD 520, without the layer (1026 registered, 984 passed, 33 skipped, 9 failed): f64_rounding_denormal_modes, image_sample_lod_clamp (T# MIN_LOD), sample_multisample_target, the four native_color_initial_nonuniform* and packed_float_blend (missing capabilities) with the same messages as on main 411718d8d9, and guest_memory_lifetime_converted (loader error 127, libGuestMemoryFixture.prx not found, before any shader runs; not re-run on main). f32_denormal_flush is the only test that changed from fail to pass.
  • python3 tools/check_conventions.py --base origin/main passes.

Overlap

Base bbb638e808. claims.py check-branch HEAD: no open PR overlaps a changed block (58, 9 and 41 open PRs touch ShaderMemory.cpp, SpirvAluEmitterMath.cpp and SpirvImageEmitter.cpp elsewhere). tools/pr_overlap.py --branch: 73 open PRs share a file with the branch, all 73 merge into the base and none reports shared lines or exports; for 33 of them that conflict with main already, the shared files were not compared, so I read their hunks in the two emitters: https://github.com/boykopovar/AnyPS5/pull/12324 (EntryLaneWalk and wave64 analysis), https://github.com/boykopovar/AnyPS5/pull/16321 (QuantizeStoreTexel), https://github.com/boykopovar/AnyPS5/pull/16260 (ExpandFmaskTexel, PackedImageTexel, EmitImageMode), https://github.com/boykopovar/AnyPS5/pull/10652 (EmitAtomicOp) and https://github.com/boykopovar/AnyPS5/pull/14853 (EmitTableWord) edit other functions than the ones here.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (Refs: #15817)
  • Unblocks: none
  • GPUs tested: Intel HD Graphics 520 (Skylake GT2), Mesa 26.2.5 ANV, Linux 7.2.8; llvmpipe (LLVM 23.1.3), Mesa 26.2.5, Linux 7.2.8; validation layer 1.4.363.0 on both; Windows not tested
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5207908975 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16967). Originally posted by [sherySJ](https://github.com/sherySJ) on 2026-11-08T02:24:14Z. --- **Cause and design** Not an ANV float-controls deviation and not a test problem: the fused lowering lost the exactness marker its unfused sibling has. - `fusedExactArithmetic` lowers the f32 multiply, add and subtract to `Fma(x, y, -0.0)`, `Fma(1.0, x, y)` and `Fma(-1.0, y, x)`. Production sets it for MoltenVK only (`VulkanDevice.cpp:1592`); `F32DenormalFlush.cpp:524-526` forces it on every device, which is how ANV reaches it. The unfused branch of each emitter is `OpFMul`/`OpFAdd`/`OpFSub` plus `NoContraction`; the fused `Fma` carried no decoration. - `flushTinyProduct` (`OperandAccess.cpp:159`, line 169) decides "tiny after rounding to 24 bits with an unbounded exponent" from `(lhs * 2^64) * rhs`. Mesa 26.2.5 rewrites an undecorated `Fma(x, y, -0.0)` to `fmul` (`nir_opt_algebraic.py:311`), then `nir_opt_algebraic_before_ffma` (`brw_nir.c:3241`) moves the constant to the outer multiply (`nir_opt_algebraic.py:3607-3609`). Those rules are inexact (`~`) and skip an instruction flagged exact (`nir_search.c:332`; `NoContraction` becomes `nir_fp_exact`, `vtn_alu.c:466`). - Lane 4 (`0x3f7fffff * 0x00800000`, exact value 2^-126 - 2^-150) was therefore judged on the rounded product: the tie rounds to `0x00800000`, times 2^64 is `0x20800000`, not tiny, so `0x00800000` was kept where the console flushes to zero. `INTEL_DEBUG=cs` on the HD 520, before: `fmul(fmul(lhs, rhs), 2^64)` with no exact flag; after: `fmul(lhs, 2^64) // exact` then `fmul(.., rhs) // exact`, the authored order. - ANV honours the declared controls (the module declares `DenormPreserve 32` and `SignedZeroInfNanPreserve 32`; the `[float-controls]` hardware flush probe reports 0 of 5472 results differing; the unfused variants pass), so no probe is needed. The fix is the decoration, as `EmitFPFma32` and the `Exact(Fma)` polynomials already do. Any NIR driver that runs the fused lowering is exposed the same way; only the HD 520 and llvmpipe were run. ### What Marks every fused-exact f32 `Fma` `NoContraction`, like the unfused operation it replaces: - `core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp`: `ExactMul` (152), `ExactSub` (157), `EmitFPAdd32` (1931), `EmitFPSub32` (1936), `EmitFPMul32` (1941). - `core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp`: the offset fold in `FoldedOffsetCoord` (528) and `texelFloor` in the packed horizontal gather (1508); the same pattern, no test samples through the fused lowering on this host. - `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp`: `verifyFusedExactArithmetic` (7055) now requires every `Fma` of the plain and the fused module to be decorated. `agc_driver_f32_denormal_flush` passes on llvmpipe before and after the change, so this device-independent assertion is what keeps CI from losing the decoration. No float semantics change on a driver that did not reassociate, and none for the console measurements in the debt files. No shader cache version edit: `ShaderCacheVersion.cmake` hashes the recompiler sources. `v_fma_f32` already carried the decoration (`SpirvAluEmitterMath.cpp:2109`), so MoltenVK sees nothing new in kind. Refs: #15817 (four failing tests; this change covers `agc_driver_f32_denormal_flush`; the f64 frexp one is https://github.com/boykopovar/AnyPS5/pull/16930) ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux 7.2.8 x86_64 (Arch), GCC 16.2.1 Release, Mesa 26.2.5. Windows and macOS (MoltenVK) not tested. No title run. Same machine, test binaries rebuilt for each commit, `ctest --test-dir build -R '^agc_driver_f32_denormal_flush$' -V` with `ANYPS5_GPU=<gpu> ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. The layer is `vulkan-validation-layers` 1.4.363.0 from the Arch `extra` mirror, SHA-256 checked against `extra.db`, extracted without root; `VK_LOADER_DEBUG=layer` shows it loaded, and no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`. | GPU | main `210676ad86` | this change | |---|---|---| | Intel HD Graphics 520 (Skylake GT2, Mesa ANV) | fails: `fused lane 4 no float mode v_mul_f32 v12, v4, v5 is 0x00800000, expected 0x00000000` | `f32 denormal flush tests passed` | | llvmpipe (LLVM 23.1.3) | passes | passes | The main and change logs differ only in that result line and the host memory budget figures; no `VUID-` or validation line in either, on either GPU. The table ran on the change rebased onto `main` `210676ad86`; the float, shader and full-sweep runs below ran on it rebased onto `6daf9a1cb6`, and the focused test and `agc_shader_memory` were run again on the head rebased onto `4c4ff42a6c` with the same results. Between `210676ad86` and this head the only change under `core/shader` or `core/libs/prx/libSceAgcDriver` is `core/libs/prx/libSceAgcDriver/tests/TextureTiling.cpp`. - Negative control for the new assertion: `agc_shader_memory` with the test change and main's emitters fails with `fused exact arithmetic: a fused multiply-add is not marked NoContraction ...`; with this change it passes. - 75 float tests (every `agc_driver_f32_*`, `agc_driver_f16_*`, `f64`, `fma`, `ldexp`, `mullit`, `vcmp` and other denormal, rounding, NaN and float-mode test), with the layer. HD 520: main fails `f32_denormal_flush` and `f64_rounding_denormal_modes`; this change fails only `f64_rounding_denormal_modes` (the frexp mantissa of https://github.com/boykopovar/AnyPS5/pull/16930, out of scope). llvmpipe: `f64_div_scale_denormal_modes` and `f64_fma_denormal_modes` fail on main and with this change alike (not touched here). Zero `VUID-` lines in all four runs. - 44 `agc_shader_*` tests, with the layer: all pass (`agc_shader_disk_cache_large_file` skips). - Full `ctest -R '^agc_driver_' --timeout 90` on the HD 520, without the layer (1026 registered, 984 passed, 33 skipped, 9 failed): `f64_rounding_denormal_modes`, `image_sample_lod_clamp` (T# MIN_LOD), `sample_multisample_target`, the four `native_color_initial_nonuniform*` and `packed_float_blend` (missing capabilities) with the same messages as on main `411718d8d9`, and `guest_memory_lifetime_converted` (loader error 127, `libGuestMemoryFixture.prx` not found, before any shader runs; not re-run on main). `f32_denormal_flush` is the only test that changed from fail to pass. - `python3 tools/check_conventions.py --base origin/main` passes. ### Overlap Base `bbb638e808`. `claims.py check-branch HEAD`: no open PR overlaps a changed block (58, 9 and 41 open PRs touch `ShaderMemory.cpp`, `SpirvAluEmitterMath.cpp` and `SpirvImageEmitter.cpp` elsewhere). `tools/pr_overlap.py --branch`: 73 open PRs share a file with the branch, all 73 merge into the base and none reports shared lines or exports; for 33 of them that conflict with `main` already, the shared files were not compared, so I read their hunks in the two emitters: https://github.com/boykopovar/AnyPS5/pull/12324 (`EntryLaneWalk` and wave64 analysis), https://github.com/boykopovar/AnyPS5/pull/16321 (`QuantizeStoreTexel`), https://github.com/boykopovar/AnyPS5/pull/16260 (`ExpandFmaskTexel`, `PackedImageTexel`, `EmitImageMode`), https://github.com/boykopovar/AnyPS5/pull/10652 (`EmitAtomicOp`) and https://github.com/boykopovar/AnyPS5/pull/14853 (`EmitTableWord`) edit other functions than the ones here. - https://github.com/boykopovar/AnyPS5/pull/16440 (f32 fma flush in mode 1): other files (`VectorInstructions.cpp`, `debt/shader.md`, a new test), merges cleanly with this head by `git merge-tree`. Built on `main` it leaves this failure unchanged on the HD 520 (same lane 4 line), so it is not a substitute. - https://github.com/boykopovar/AnyPS5/pull/15343 (`OpFmaKHR` for f32 fma on RADV): edits `EmitFPFma32` in the same file, a different function; it also wraps its `Fma` in `Exact`. Its `ShaderMemory.cpp` conflict exists against `main` without this change. - https://github.com/boykopovar/AnyPS5/pull/13929 and https://github.com/boykopovar/AnyPS5/pull/15548: the `ShaderMemory.cpp` conflict of #13929 exists against `main` already; #15548 merges cleanly. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none (Refs: #15817) - [x] Unblocks: none - [x] GPUs tested: Intel HD Graphics 520 (Skylake GT2), Mesa 26.2.5 ANV, Linux 7.2.8; llvmpipe (LLVM 23.1.3), Mesa 26.2.5, Linux 7.2.8; validation layer 1.4.363.0 on both; Windows not tested - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!16967
No description provided.