agc_driver_f32_arithmetic_nan fails on NVIDIA (Windows) since #2279: v_fmamk_f32 returns a quiet NaN where the signalling one is expected #2581

Closed
opened 2026-10-10 06:56:04 +00:00 by Yuva1024 · 1 comment
Yuva1024 commented 2026-10-10 06:56:04 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Yuva1024 on 2026-10-10T06:56:04Z.


agc_driver_f32_arithmetic_nan fails on every run on main 6f262eb0:

AGC graphics: f32 arithmetic nan: lane 1 no float mode v_fmamk_f32 is 0x7fc12345, expected 0x7f812345

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4060 (driver 617.42), ctest -R ^agc_driver_f32_arithmetic_nan$, 5 runs each:

Build Pass
main 530b6a42 0/5
main 530b6a42 with #2279 reverted (git revert -m 1 c94cbb7a) 5/5
main 6f262eb0 0/5

The last full ctest on this machine without it was bc40a6b5 (606/606), before #2279 merged at 06:14 UTC. The rest of the suite passes, including f32_denormal_flush, f32_output_modifier and vector_fma_nan_result.

#2279 records Linux results (RADV wave64, lavapipe); CI's Windows job has no Vulkan driver, so the AGC tests skip there. I have not analysed why the NVIDIA result differs or tried other GPUs.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5788966979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2581). Originally posted by [Yuva1024](https://github.com/Yuva1024) on 2026-10-10T06:56:04Z. --- `agc_driver_f32_arithmetic_nan` fails on every run on `main` `6f262eb0`: ``` AGC graphics: f32 arithmetic nan: lane 1 no float mode v_fmamk_f32 is 0x7fc12345, expected 0x7f812345 ``` Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4060 (driver 617.42), `ctest -R ^agc_driver_f32_arithmetic_nan$`, 5 runs each: | Build | Pass | |---|---| | `main` `530b6a42` | 0/5 | | `main` `530b6a42` with #2279 reverted (`git revert -m 1 c94cbb7a`) | 5/5 | | `main` `6f262eb0` | 0/5 | The last full `ctest` on this machine without it was `bc40a6b5` (606/606), before #2279 merged at 06:14 UTC. The rest of the suite passes, including `f32_denormal_flush`, `f32_output_modifier` and `vector_fma_nan_result`. #2279 records Linux results (RADV wave64, lavapipe); CI's Windows job has no Vulkan driver, so the AGC tests skip there. I have not analysed why the NVIDIA result differs or tried other GPUs. AI-assisted: yes (Claude Code).
Yuva1024 commented 2026-10-10 07:08:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Yuva1024 on 2026-10-10T07:08:20Z.


Root cause, and a fix that keeps #2279's selection, in case a re-land is preferred to the revert in #2584. Same setup as the report: Windows 11 (26200), WinLibs GCC 15.2.0 r7, RTX 4060 (driver 617.42), main 6f262eb0.

Cause. EmitFPNanResultFma32 turns each source into integer bits with OpBitcast. For v_fmamk_f32 the literal is a float OpConstant 0x7f812345 (a signalling NaN), so the module contains OpBitcast %uint applied directly to a NaN constant. I dumped the SPIR-V the test compiles: it has that float constant and four direct bitcasts of it. The only other NaN constant, 0xffc12345 (the v_fmaak_f32 literal), is already quiet.

That fits what the test shows. With the test changed to list every mismatch instead of stopping at the first, there are 44, all v_fmamk_f32 (22 lanes in each of the no-float-mode and IEEE=0 runs, none in IEEE=1), and each is the expected payload with the quiet bit set (0x7fc12345). This is consistent with the driver folding the bitcast of a constant and quieting the NaN; I have not looked inside the driver.

The IR does not hit this: ConstantFolder.cpp (foldBitCast) turns a bitcast of an F32 immediate into a U32 immediate before emission. The new op does the bitcast inside the emitter, which bypasses that.

Fix (one function, plus the <bit> include): use the immediate's bits as an integer constant when a source is an F32 immediate.

Diff
diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
index 42055c2e..a086d281 100644
--- a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
+++ b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
@@ -5,6 +5,7 @@
 #include <spirv/unified1/spirv.hpp>
 #include <algorithm>
 #include <array>
+#include <bit>
 #include <initializer_list>
 #include <optional>
 #include <string>
@@ -1280,18 +1281,26 @@ std::uint32_t EmitFPMad32(SpirvEmitterState& state, std::uint32_t arg0, std::uin
 std::uint32_t EmitFPNanResultFma32(SpirvEmitterState& state, const IrValue& inst, std::uint32_t result, std::uint32_t lhs, std::uint32_t rhs, std::uint32_t addend) {
     const auto u32 = TypeU32(state);
     const auto boolean = TypeBool(state);
-    const auto bits = [&](std::uint32_t value) { return Unary(state, spv::OpBitcast, u32, value); };
-    const auto magnitude = [&](std::uint32_t value) { return Binary(state, spv::OpBitwiseAnd, u32, bits(value), ConstantU32(state, 0x7fffffffu)); };
-    const auto isNan = [&](std::uint32_t value) { return Binary(state, spv::OpUGreaterThan, boolean, magnitude(value), ConstantU32(state, 0x7f800000u)); };
+    const auto bitsOf = [&](std::size_t argument, std::uint32_t value) {
+        const IrValue* source = inst.Argument(argument)->Resolve();
+        if (source->HasImmediate() && source->Type() == IrType::F32) return ConstantU32(state, std::bit_cast<std::uint32_t>(source->ImmediateF32()));
+        return Unary(state, spv::OpBitcast, u32, value);
+    };
+    const auto resultBits = bitsOf(0u, result);
+    const auto lhsBits = bitsOf(1u, lhs);
+    const auto rhsBits = bitsOf(2u, rhs);
+    const auto addendBits = bitsOf(3u, addend);
+    const auto magnitude = [&](std::uint32_t bits) { return Binary(state, spv::OpBitwiseAnd, u32, bits, ConstantU32(state, 0x7fffffffu)); };
+    const auto isNan = [&](std::uint32_t bits) { return Binary(state, spv::OpUGreaterThan, boolean, magnitude(bits), ConstantU32(state, 0x7f800000u)); };
     const bool quiet = (inst.Flags<std::uint64_t>() & 1u) != 0u;
-    return EmitValueOrDefaultIfCondition(state, isNan(result), TypeF32(state), result, [&] {
+    return EmitValueOrDefaultIfCondition(state, isNan(resultBits), TypeF32(state), result, [&] {
         const auto equals = [&](std::uint32_t value, std::uint32_t constant) { return Binary(state, spv::OpIEqual, boolean, value, ConstantU32(state, constant)); };
         const auto infZero = [&](std::uint32_t inf, std::uint32_t zero) { return Binary(state, spv::OpLogicalAnd, boolean, equals(magnitude(inf), 0x7f800000u), equals(magnitude(zero), 0u)); };
-        const auto invalidProduct = Binary(state, spv::OpLogicalOr, boolean, infZero(lhs, rhs), infZero(rhs, lhs));
-        const auto quieted = [&](std::uint32_t value) { return quiet ? Binary(state, spv::OpBitwiseOr, u32, bits(value), ConstantU32(state, 0x00400000u)) : bits(value); };
-        auto chosen = Select(state, u32, Binary(state, spv::OpLogicalAnd, boolean, isNan(addend), Unary(state, spv::OpLogicalNot, boolean, invalidProduct)), quieted(addend), ConstantU32(state, 0xffc00000u));
-        chosen = Select(state, u32, isNan(rhs), quieted(rhs), chosen);
-        chosen = Select(state, u32, isNan(lhs), quieted(lhs), chosen);
+        const auto invalidProduct = Binary(state, spv::OpLogicalOr, boolean, infZero(lhsBits, rhsBits), infZero(rhsBits, lhsBits));
+        const auto quieted = [&](std::uint32_t bits) { return quiet ? Binary(state, spv::OpBitwiseOr, u32, bits, ConstantU32(state, 0x00400000u)) : bits; };
+        auto chosen = Select(state, u32, Binary(state, spv::OpLogicalAnd, boolean, isNan(addendBits), Unary(state, spv::OpLogicalNot, boolean, invalidProduct)), quieted(addendBits), ConstantU32(state, 0xffc00000u));
+        chosen = Select(state, u32, isNan(rhsBits), quieted(rhsBits), chosen);
+        chosen = Select(state, u32, isNan(lhsBits), quieted(lhsBits), chosen);
         return Unary(state, spv::OpBitcast, TypeF32(state), chosen);
     });
 }

With it, agc_driver_f32_arithmetic_nan has 0 mismatches in all three runs, and the compiled SPIR-V no longer contains a bitcast of a NaN constant. Not tested: Linux or other GPUs. The rest of the suite is still running with this change; I will only follow up if something fails.

Whether to revert or fix is the maintainers' call; this does not change #2584.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094966499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2581#issuecomment-6094966499). Originally posted by [Yuva1024](https://github.com/Yuva1024) on 2026-10-10T07:08:20Z. --- Root cause, and a fix that keeps #2279's selection, in case a re-land is preferred to the revert in #2584. Same setup as the report: Windows 11 (26200), WinLibs GCC 15.2.0 r7, RTX 4060 (driver 617.42), `main` `6f262eb0`. **Cause.** `EmitFPNanResultFma32` turns each source into integer bits with `OpBitcast`. For `v_fmamk_f32` the literal is a float `OpConstant 0x7f812345` (a signalling NaN), so the module contains `OpBitcast %uint` applied directly to a NaN constant. I dumped the SPIR-V the test compiles: it has that float constant and four direct bitcasts of it. The only other NaN constant, `0xffc12345` (the `v_fmaak_f32` literal), is already quiet. That fits what the test shows. With the test changed to list every mismatch instead of stopping at the first, there are 44, all `v_fmamk_f32` (22 lanes in each of the no-float-mode and `IEEE=0` runs, none in `IEEE=1`), and each is the expected payload with the quiet bit set (`0x7fc12345`). This is consistent with the driver folding the bitcast of a constant and quieting the NaN; I have not looked inside the driver. The IR does not hit this: `ConstantFolder.cpp` (`foldBitCast`) turns a bitcast of an `F32` immediate into a `U32` immediate before emission. The new op does the bitcast inside the emitter, which bypasses that. **Fix** (one function, plus the `<bit>` include): use the immediate's bits as an integer constant when a source is an `F32` immediate. <details><summary>Diff</summary> ```diff diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp index 42055c2e..a086d281 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp @@ -5,6 +5,7 @@ #include <spirv/unified1/spirv.hpp> #include <algorithm> #include <array> +#include <bit> #include <initializer_list> #include <optional> #include <string> @@ -1280,18 +1281,26 @@ std::uint32_t EmitFPMad32(SpirvEmitterState& state, std::uint32_t arg0, std::uin std::uint32_t EmitFPNanResultFma32(SpirvEmitterState& state, const IrValue& inst, std::uint32_t result, std::uint32_t lhs, std::uint32_t rhs, std::uint32_t addend) { const auto u32 = TypeU32(state); const auto boolean = TypeBool(state); - const auto bits = [&](std::uint32_t value) { return Unary(state, spv::OpBitcast, u32, value); }; - const auto magnitude = [&](std::uint32_t value) { return Binary(state, spv::OpBitwiseAnd, u32, bits(value), ConstantU32(state, 0x7fffffffu)); }; - const auto isNan = [&](std::uint32_t value) { return Binary(state, spv::OpUGreaterThan, boolean, magnitude(value), ConstantU32(state, 0x7f800000u)); }; + const auto bitsOf = [&](std::size_t argument, std::uint32_t value) { + const IrValue* source = inst.Argument(argument)->Resolve(); + if (source->HasImmediate() && source->Type() == IrType::F32) return ConstantU32(state, std::bit_cast<std::uint32_t>(source->ImmediateF32())); + return Unary(state, spv::OpBitcast, u32, value); + }; + const auto resultBits = bitsOf(0u, result); + const auto lhsBits = bitsOf(1u, lhs); + const auto rhsBits = bitsOf(2u, rhs); + const auto addendBits = bitsOf(3u, addend); + const auto magnitude = [&](std::uint32_t bits) { return Binary(state, spv::OpBitwiseAnd, u32, bits, ConstantU32(state, 0x7fffffffu)); }; + const auto isNan = [&](std::uint32_t bits) { return Binary(state, spv::OpUGreaterThan, boolean, magnitude(bits), ConstantU32(state, 0x7f800000u)); }; const bool quiet = (inst.Flags<std::uint64_t>() & 1u) != 0u; - return EmitValueOrDefaultIfCondition(state, isNan(result), TypeF32(state), result, [&] { + return EmitValueOrDefaultIfCondition(state, isNan(resultBits), TypeF32(state), result, [&] { const auto equals = [&](std::uint32_t value, std::uint32_t constant) { return Binary(state, spv::OpIEqual, boolean, value, ConstantU32(state, constant)); }; const auto infZero = [&](std::uint32_t inf, std::uint32_t zero) { return Binary(state, spv::OpLogicalAnd, boolean, equals(magnitude(inf), 0x7f800000u), equals(magnitude(zero), 0u)); }; - const auto invalidProduct = Binary(state, spv::OpLogicalOr, boolean, infZero(lhs, rhs), infZero(rhs, lhs)); - const auto quieted = [&](std::uint32_t value) { return quiet ? Binary(state, spv::OpBitwiseOr, u32, bits(value), ConstantU32(state, 0x00400000u)) : bits(value); }; - auto chosen = Select(state, u32, Binary(state, spv::OpLogicalAnd, boolean, isNan(addend), Unary(state, spv::OpLogicalNot, boolean, invalidProduct)), quieted(addend), ConstantU32(state, 0xffc00000u)); - chosen = Select(state, u32, isNan(rhs), quieted(rhs), chosen); - chosen = Select(state, u32, isNan(lhs), quieted(lhs), chosen); + const auto invalidProduct = Binary(state, spv::OpLogicalOr, boolean, infZero(lhsBits, rhsBits), infZero(rhsBits, lhsBits)); + const auto quieted = [&](std::uint32_t bits) { return quiet ? Binary(state, spv::OpBitwiseOr, u32, bits, ConstantU32(state, 0x00400000u)) : bits; }; + auto chosen = Select(state, u32, Binary(state, spv::OpLogicalAnd, boolean, isNan(addendBits), Unary(state, spv::OpLogicalNot, boolean, invalidProduct)), quieted(addendBits), ConstantU32(state, 0xffc00000u)); + chosen = Select(state, u32, isNan(rhsBits), quieted(rhsBits), chosen); + chosen = Select(state, u32, isNan(lhsBits), quieted(lhsBits), chosen); return Unary(state, spv::OpBitcast, TypeF32(state), chosen); }); } ``` </details> With it, `agc_driver_f32_arithmetic_nan` has 0 mismatches in all three runs, and the compiled SPIR-V no longer contains a bitcast of a NaN constant. Not tested: Linux or other GPUs. The rest of the suite is still running with this change; I will only follow up if something fails. Whether to revert or fix is the maintainers' call; this does not change #2584. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
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#2581
No description provided.