fix(shader): handle ordered NaN comparisons #3541

Merged
MarcZX10 merged 2 commits from upstream-pr/3541 into main 2026-10-11 21:43:11 +00:00
MarcZX10 commented 2026-10-11 12:44:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T12:44:48Z.


What

Fixes #3536. The AMD Radeon 860M Windows driver returns true for ordered less-than or greater-than when one operand is NaN and the other is +infinity supplied as a shader constant. The existing agc_driver_f32_compare_constant test covers both operations.

FPOrdLessThan32 and FPOrdGreaterThan32 now AND their native ordered comparison with an integer bit check that both operands are ordered. This returns false for every NaN input while preserving results for all ordered values. The same bit check is shared with FPOrdNotEqual32, covering the earlier AMD ordered not-equal issue (#825). This handles both NaN < +infinity and +infinity > NaN without a constant-specific specialization.

Tested

  • Linux, GCC 13.3: built agc_driver_f32_compare_constant_tests.
  • Offline Recompile of the existing register-operand comparison shader confirmed the +infinity comparison path; the integrated checks also cover the symmetric greater-than case.
  • CTest agc_driver_f32_compare_constant was skipped locally because this container has no Vulkan device. The #3595 author reported this test passing 3/3 on AMD Windows with the shared ordered NaN check; issue #3536 records the failing baseline.
  • python3 tools/check_conventions.py --base upstream/main passed.
  • git diff --check upstream/main...HEAD passed.

Checklist

  • Based on current main; no other open PR implements the same behavior
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are listed in TechnicalDebt
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behavior, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819139204 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3541). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T12:44:48Z. --- ### What Fixes #3536. The AMD Radeon 860M Windows driver returns true for ordered less-than or greater-than when one operand is NaN and the other is `+infinity` supplied as a shader constant. The existing `agc_driver_f32_compare_constant` test covers both operations. `FPOrdLessThan32` and `FPOrdGreaterThan32` now AND their native ordered comparison with an integer bit check that both operands are ordered. This returns false for every NaN input while preserving results for all ordered values. The same bit check is shared with `FPOrdNotEqual32`, covering the earlier AMD ordered not-equal issue (#825). This handles both `NaN < +infinity` and `+infinity > NaN` without a constant-specific specialization. ### Tested - Linux, GCC 13.3: built `agc_driver_f32_compare_constant_tests`. - Offline `Recompile` of the existing register-operand comparison shader confirmed the `+infinity` comparison path; the integrated checks also cover the symmetric greater-than case. - CTest `agc_driver_f32_compare_constant` was skipped locally because this container has no Vulkan device. The #3595 author reported this test passing 3/3 on AMD Windows with the shared ordered NaN check; issue #3536 records the failing baseline. - `python3 tools/check_conventions.py --base upstream/main` passed. - `git diff --check upstream/main...HEAD` passed. ### Checklist - [x] Based on current `main`; no other open PR implements the same behavior - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behavior, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-11 12:46:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T12:46:58Z.


I also compiled the existing register-operand comparison shader offline through Recompile and confirmed that its +infinity RHS reaches this specialization. The CTest runtime still skips here without a Vulkan device, so the AMD Windows result remains unverified on my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109174053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3541#issuecomment-6109174053). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T12:46:58Z. --- I also compiled the existing register-operand comparison shader offline through `Recompile` and confirmed that its `+infinity` RHS reaches this specialization. The CTest runtime still skips here without a Vulkan device, so the AMD Windows result remains unverified on my side.
klosixgaming-arch commented 2026-10-11 15:17:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-11T15:17:19Z.


Cross-GPU verification on Windows (this is the data your container couldn't produce — no Vulkan device there).

Setup: Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, Release; NVIDIA RTX 5090 and AMD Radeon iGPU; ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1. Base main a11c8f7 (same base as this PR). For the "with this PR" runs I applied your exact SpirvAluEmitterMath.cpp change onto that base.

On main (no fix):

  • agc_driver_f32_compare_constant: passes on both GPUs. The #3536 miscompile (v_cmp_lt_f32 NaN, +inf-from-register → 1) does not reproduce here — my AMD iGPU driver differs from the reported Radeon 860M / 24.30.58.08, so I can't confirm the crash-side fix on this hardware, only that it doesn't regress.

With this PR:

  • agc_driver_f32_compare_constant: passes on both GPUs.
  • agc_driver_vopc_compare (all ordered/unordered f32/f16 compare predicates): passes on both GPUs.

So the added BothOrderedBits32 AND on FPOrdLessThan32 / FPOrdGreaterThan32 / FPOrdNotEqual32 shows no regression to any compare predicate on either NVIDIA or AMD here. I did not observe a case where it changes a result that was already correct, consistent with the "same for every input" claim.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110495189 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3541#issuecomment-6110495189). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-11T15:17:19Z. --- Cross-GPU verification on Windows (this is the data your container couldn't produce — no Vulkan device there). **Setup:** Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, Release; NVIDIA RTX 5090 and AMD Radeon iGPU; `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`. Base `main` a11c8f7 (same base as this PR). For the "with this PR" runs I applied your exact `SpirvAluEmitterMath.cpp` change onto that base. **On `main` (no fix):** - `agc_driver_f32_compare_constant`: **passes on both GPUs.** The #3536 miscompile (`v_cmp_lt_f32 NaN, +inf-from-register → 1`) does **not** reproduce here — my AMD iGPU driver differs from the reported Radeon 860M / 24.30.58.08, so I can't confirm the crash-side fix on this hardware, only that it doesn't regress. **With this PR:** - `agc_driver_f32_compare_constant`: **passes on both GPUs.** - `agc_driver_vopc_compare` (all ordered/unordered f32/f16 compare predicates): **passes on both GPUs.** So the added `BothOrderedBits32` AND on `FPOrdLessThan32` / `FPOrdGreaterThan32` / `FPOrdNotEqual32` shows no regression to any compare predicate on either NVIDIA or AMD here. I did not observe a case where it changes a result that was already correct, consistent with the "same for every input" claim. Written with AI assistance.
LeandroLP23 commented 2026-10-11 17:11:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T17:11:09Z.


Thanks @MarcZX100. The final tree of this head (d668b7c2b) is identical to #3595's, so the Radeon 860M results posted there apply to it as is. Those results are from Windows 11 (26300), AMD driver 32.0.13058.8001, Release, on main a11c8f7f7, with the AMD Vulkan cache and shader_cache emptied before each run:

  • agc_driver_f32_compare_constant passes 3/3. It fails on main, and it fails at v_cmp_gt_f32 0x7f800000, 0x7fc00000 with only less-than guarded.
  • ctest -R agc_ passes 374/374.

Closing #3595 in favour of this one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6111550402 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3541#issuecomment-6111550402). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-11T17:11:09Z. --- Thanks @MarcZX100. The final tree of this head (`d668b7c2b`) is identical to #3595's, so the Radeon 860M results posted there apply to it as is. Those results are from Windows 11 (26300), AMD driver 32.0.13058.8001, Release, on `main` `a11c8f7f7`, with the AMD Vulkan cache and `shader_cache` emptied before each run: - `agc_driver_f32_compare_constant` passes 3/3. It fails on `main`, and it fails at `v_cmp_gt_f32 0x7f800000, 0x7fc00000` with only less-than guarded. - `ctest -R agc_` passes 374/374. Closing #3595 in favour of this one.
adriadam10 commented 2026-10-11 21:43:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:43:06Z.


Checked: the change is an AND of the native ordered compare with a bit-level "both operands ordered" check, so results are identical for every input; agc_driver_f32_compare_constant, agc_driver_vopc_compare and agc_driver_vopc_compare_table pass on Linux, and the Radeon 860M run by @LeandroLP23 (3/3, fails on main) plus @klosixgaming-arch's NVIDIA/AMD run show no regression. TechnicalDebt entry matches the code. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114108707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3541#issuecomment-6114108707). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:43:06Z. --- Checked: the change is an AND of the native ordered compare with a bit-level "both operands ordered" check, so results are identical for every input; `agc_driver_f32_compare_constant`, `agc_driver_vopc_compare` and `agc_driver_vopc_compare_table` pass on Linux, and the Radeon 860M run by @LeandroLP23 (3/3, fails on main) plus @klosixgaming-arch's NVIDIA/AMD run show no regression. TechnicalDebt entry matches the code. Merging.
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!3541
No description provided.