fix(shader): avoid NVIDIA signed folding on unsigned int-to-float conversions #21159

Merged
gurtgg merged 2 commits from upstream-pr/21159 into main 2026-11-27 22:02:11 +00:00
gurtgg commented 2026-11-15 21:44:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-15T21:44:45Z.


What

Avoid emitting OpConvertUToF for sign-extended 32-bit integers in the SPIR-V ALU emitter, where NVIDIA's compiler can fold the conversion with the preceding sign extension as signed int-to-float and produce negative results that clamp to zero on unsigned sub-dword SDWA writes.

  • Added SignExtendedU32(const IrValue& root) in core/shader/recompiler/SpirvBackend (SpirvEmitterHelpers.hpp / SpirvAluEmitterMath.cpp) to detect values originating from BitFieldSExtract or ShiftRightArithmetic32 across phis, selects, lane/DPP/permute operations, bitcasts, and narrowing conversions.
  • Updated EmitConvertF32U32 to convert sign-extended inputs via non-negative 31-bit sticky-half OpConvertSToF followed by ExactMul(..., 2.0f).
  • Updated EmitConvertF64U32 to convert sign-extended inputs via OpConvertSToF followed by a conditional exact 2^32 adjustment.
  • Expanded core/libs/prx/libSceAgcDriver/tests/execution/SdwaDestination.cpp to test WORD_0/WORD_1 with SEXT preserving bit 15 in RunConversions and added RunSignExtendedUnsignedConversions covering all six sub-dword SDWA selectors (BYTE_0..BYTE_3, WORD_0, WORD_1) across ZEXT/SEXT as well as v_readfirstlane_b32 + s_sext_i32_i8/s_sext_i32_i16 feeding v_cvt_f32_u32_e32.
  • Removed [Unsigned conversion of sign-extended SDWA sources on NVIDIA] from docs/dev/debt/shader.md via docs/dev/debt/changes/fix-shader-nvidia-sext-utof.md.

Tested

  • Focused Vulkan validation comparison (ANYPS5_GPU="NVIDIA GeForce GTX 1660 SUPER" ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R "^(agc_driver_sdwa_destination.*|agc_driver_sdwa_cvt_f32_ubyte.*|agc_redundant_value_elimination|agc_denormal_flush_elimination|agc_shader_memory)$" -V):
    • main (e9cfe038c1): 9/9 passed (4.39 sec), 6 Insert instance layer "VK_LAYER_KHRONOS_validation" insertions, 0 was not found warnings, 0 VUID- errors.
    • Negative control (expanded tests without shader fix): agc_driver_sdwa_destination and agc_driver_sdwa_destination_wave64 failed on lane 2 u32 wave32/wave64 source 4 sext 1 destination 0 unused 0 is 0x00000000, expected 0x000000ff.
    • This branch (fix-shader-nvidia-sext-utof): 9/9 passed (4.05 sec), 6 Insert instance layer "VK_LAYER_KHRONOS_validation" insertions, 0 was not found warnings, 0 VUID- errors (sdwa integer-to-f32 u32 wave32/64: 252 combinations passed, sdwa sign-extended u32-to-f32 wave32/64: 896 results passed).
  • Broader SDWA and shader test sweep (ctest --test-dir build -R "^(agc_driver_sdwa.*|agc_driver_f16_to_f32.*|shader.*)$" -j 4): 42/42 passed (10.01 sec).
  • python3 tools/debt_fold.py --check passed.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce GTX 1660 SUPER, proprietary 595.99.02, Vulkan 1.4.329 (validation layer 1.4.341), Linux (Ubuntu 26.04.1 LTS)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5327519357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21159). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-15T21:44:45Z. --- ### What Avoid emitting `OpConvertUToF` for sign-extended 32-bit integers in the SPIR-V ALU emitter, where NVIDIA's compiler can fold the conversion with the preceding sign extension as signed int-to-float and produce negative results that clamp to zero on unsigned sub-dword SDWA writes. - Added `SignExtendedU32(const IrValue& root)` in `core/shader/recompiler/SpirvBackend` (`SpirvEmitterHelpers.hpp` / `SpirvAluEmitterMath.cpp`) to detect values originating from `BitFieldSExtract` or `ShiftRightArithmetic32` across phis, selects, lane/DPP/permute operations, bitcasts, and narrowing conversions. - Updated `EmitConvertF32U32` to convert sign-extended inputs via non-negative 31-bit sticky-half `OpConvertSToF` followed by `ExactMul(..., 2.0f)`. - Updated `EmitConvertF64U32` to convert sign-extended inputs via `OpConvertSToF` followed by a conditional exact `2^32` adjustment. - Expanded `core/libs/prx/libSceAgcDriver/tests/execution/SdwaDestination.cpp` to test `WORD_0`/`WORD_1` with `SEXT` preserving bit 15 in `RunConversions` and added `RunSignExtendedUnsignedConversions` covering all six sub-dword SDWA selectors (`BYTE_0..BYTE_3`, `WORD_0`, `WORD_1`) across `ZEXT`/`SEXT` as well as `v_readfirstlane_b32` + `s_sext_i32_i8`/`s_sext_i32_i16` feeding `v_cvt_f32_u32_e32`. - Removed `[Unsigned conversion of sign-extended SDWA sources on NVIDIA]` from `docs/dev/debt/shader.md` via `docs/dev/debt/changes/fix-shader-nvidia-sext-utof.md`. ### 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. --> - Focused Vulkan validation comparison (`ANYPS5_GPU="NVIDIA GeForce GTX 1660 SUPER" ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R "^(agc_driver_sdwa_destination.*|agc_driver_sdwa_cvt_f32_ubyte.*|agc_redundant_value_elimination|agc_denormal_flush_elimination|agc_shader_memory)$" -V`): - `main` (`e9cfe038c1`): 9/9 passed (4.39 sec), 6 `Insert instance layer "VK_LAYER_KHRONOS_validation"` insertions, 0 `was not found` warnings, 0 `VUID-` errors. - Negative control (expanded tests without shader fix): `agc_driver_sdwa_destination` and `agc_driver_sdwa_destination_wave64` failed on `lane 2 u32 wave32/wave64 source 4 sext 1 destination 0 unused 0 is 0x00000000, expected 0x000000ff`. - This branch (`fix-shader-nvidia-sext-utof`): 9/9 passed (4.05 sec), 6 `Insert instance layer "VK_LAYER_KHRONOS_validation"` insertions, 0 `was not found` warnings, 0 `VUID-` errors (`sdwa integer-to-f32 u32 wave32/64: 252 combinations passed`, `sdwa sign-extended u32-to-f32 wave32/64: 896 results passed`). - Broader SDWA and shader test sweep (`ctest --test-dir build -R "^(agc_driver_sdwa.*|agc_driver_f16_to_f32.*|shader.*)$" -j 4`): 42/42 passed (10.01 sec). - `python3 tools/debt_fold.py --check` passed. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce GTX 1660 SUPER, proprietary 595.99.02, Vulkan 1.4.329 (validation layer 1.4.341), Linux (Ubuntu 26.04.1 LTS) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-16 06:02:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T06:02:33Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the checks reported by the author are encouraging, but I did not run them, so approval is not justified.

What I ran

Reviewed head 199111fe7bb3a407bb06f368576bc3a8efbc56ee using repo_read.py to inspect the prepared diff and pinned repository files. No commands or tests were run by me; test results below are author-reported.

Results

Check Result Note
Patch and changed code passed Reviewed all six changed files
Focused Vulkan checks not run Author reports 9/9 passed
Broader SDWA and shader sweep not run Author reports 42/42 passed
Debt check not run Author reports passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not execute the reported tests or independently validate the NVIDIA compiler behavior. Author-reported hardware, driver, Vulkan, and OS details are in the PR description. The prepared bundle lists all six changed files as included; I read every added line in those files. No excluded changed files were listed in the review folder.

To get this to a good state

Nothing is required. The reported test results are not independently verified, so this review does not approve the change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5915670398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21159#pullrequestreview-5915670398). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T06:02:33Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the checks reported by the author are encouraging, but I did not run them, so approval is not justified. ## What I ran Reviewed head `199111fe7bb3a407bb06f368576bc3a8efbc56ee` using `repo_read.py` to inspect the prepared diff and pinned repository files. No commands or tests were run by me; test results below are author-reported. ## Results | Check | Result | Note | |---|---|---| | Patch and changed code | passed | Reviewed all six changed files | | Focused Vulkan checks | not run | Author reports 9/9 passed | | Broader SDWA and shader sweep | not run | Author reports 42/42 passed | | Debt check | not run | Author reports passed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not execute the reported tests or independently validate the NVIDIA compiler behavior. Author-reported hardware, driver, Vulkan, and OS details are in the PR description. The prepared bundle lists all six changed files as included; I read every added line in those files. No excluded changed files were listed in the review folder. ## To get this to a good state Nothing is required. The reported test results are not independently verified, so this review does not approve the change.
MarcZX10 commented 2026-11-20 12:55:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T12:55:16Z.

Review: COMMENTED


Reviewed — no blocking findings

The shader conversion changes and focused test additions were reviewed at head ca2b6ba24247c31ecf0b8db9ad7e57a023099430; no tests were run during this review, so a formal approval is not justified.

What I ran

No commands or tests were run. I inspected the prepared diff and targeted source ranges using repo_read.py.

Results

Check Result Note
Diff and targeted source review passed Main conversion logic and test additions inspected
Tests not run Description reports author-run test results

Findings

Blocking

None identified in the inspected ranges.

Non-blocking

None.

Not verified

I read description.md, commits.md, manifest.json, and diff.patch. Of the six changed files listed in the manifest, I inspected targeted ranges in SdwaDestination.cpp, SpirvEmitterInstructions.hpp, SpirvAluEmitterMath.cpp, and SpirvAluEmitterFloat64.cpp; the full text of those files was not read. I did not read SpirvEmitterHelpers.hpp or fix-shader-nvidia-sext-utof.md. Test results in the PR description were not independently verified. No files were excluded by the prepared review manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5993013425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21159#pullrequestreview-5993013425). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T12:55:16Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The shader conversion changes and focused test additions were reviewed at head `ca2b6ba24247c31ecf0b8db9ad7e57a023099430`; no tests were run during this review, so a formal approval is not justified. ## What I ran No commands or tests were run. I inspected the prepared diff and targeted source ranges using `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Main conversion logic and test additions inspected | | Tests | not run | Description reports author-run test results | ## Findings ### Blocking None identified in the inspected ranges. ### Non-blocking None. ## Not verified I read `description.md`, `commits.md`, `manifest.json`, and `diff.patch`. Of the six changed files listed in the manifest, I inspected targeted ranges in `SdwaDestination.cpp`, `SpirvEmitterInstructions.hpp`, `SpirvAluEmitterMath.cpp`, and `SpirvAluEmitterFloat64.cpp`; the full text of those files was not read. I did not read `SpirvEmitterHelpers.hpp` or `fix-shader-nvidia-sext-utof.md`. Test results in the PR description were not independently verified. No files were excluded by the prepared review manifest. ## To get this to a good state Nothing is required.
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!21159
No description provided.