From f132f16155ba1137d9af0cbbc626650b84c205e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Thu, 8 Oct 2026 23:59:11 +0200 Subject: [PATCH] feat(shader): flush f32 denormals in the float modes that flush them Co-Authored-By: Claude --- .../tests/execution/F32DenormalFlush.cpp | 211 ++++++++++++++++++ core/libs/tests/VectorFusedMultiplyAdd.cpp | 1 + .../Translation/TranslationContext.hpp | 4 + .../Translation/src/ConvertInstructions.cpp | 4 +- .../Translation/src/DispatchInstructions.cpp | 1 + .../Translation/src/FloatInstructions.cpp | 16 +- .../src/TranslationContext/OperandAccess.cpp | 25 ++- .../Translation/src/VectorInstructions.cpp | 74 ++++++ docs/dev/TechnicalDebt.md | 1 + 9 files changed, 327 insertions(+), 10 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp new file mode 100644 index 000000000..37427d269 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp @@ -0,0 +1,211 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084u, 0x34060086u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x06140b04u, 0x08160905u, 0x10180b04u, + 0x0e1a0b04u, 0xd54b000eu, 0x041a0b04u, 0x7e1e0306u, 0x561e0b04u, 0x1e200b04u, 0xd5540011u, 0x041a0b04u, + 0xd5570012u, 0x041a0b04u, 0x7e264904u, 0x7e284505u, 0x7e2a8104u, 0x7c020b04u, 0xd5010016u, 0x01a90280u, + 0x7e2e1b04u, 0x7e307f05u, 0xd5080119u, 0x42020b04u, 0xe0781000u, 0x80010a03u, 0xe0781010u, 0x80010e03u, + 0xe0781020u, 0x80011203u, 0xe0781030u, 0x80011603u, 0xbf810000u, +}; + +constexpr std::uint32_t Rows[32][4] = { + {0x00000001u, 0x3f800000u, 0x00000000u, 0x00000000u}, + {0x80400000u, 0x3f800000u, 0x00800000u, 0x00000000u}, + {0x3f800000u, 0x00012345u, 0x80000001u, 0x00000000u}, + {0x007fffffu, 0x007fffffu, 0x3f800000u, 0x00000000u}, + {0x3f7fffffu, 0x00800000u, 0x00000000u, 0x00000000u}, + {0xbf7fffffu, 0x00800000u, 0x80000000u, 0x00000000u}, + {0x3f7fffffu, 0x00800001u, 0x00000001u, 0x00000000u}, + {0x3f000000u, 0x01000000u, 0x80000001u, 0x00000000u}, + {0x00800000u, 0x80800001u, 0x00800000u, 0x00000000u}, + {0x00800001u, 0x00800000u, 0x3f800000u, 0x00000000u}, + {0x00000000u, 0x80000001u, 0x00000001u, 0x00000000u}, + {0x80000000u, 0x00000001u, 0x807fffffu, 0x00000000u}, + {0x7f800000u, 0x00000001u, 0x3f800000u, 0x00000000u}, + {0x00000001u, 0xff800000u, 0x00000000u, 0x00000000u}, + {0x7fc00000u, 0x00000001u, 0x00000001u, 0x00000000u}, + {0x40000000u, 0x00400000u, 0x80800000u, 0x00000000u}, + {0x3f800000u, 0x3f800000u, 0x80000001u, 0x00000000u}, + {0xbf800000u, 0x00000001u, 0x3f000000u, 0x00000000u}, + {0x34000000u, 0x34000000u, 0x00000000u, 0x00000000u}, + {0x1f800000u, 0x1f800000u, 0x00000000u, 0x00000000u}, + {0x1fffffffu, 0x1fffffffu, 0x00800000u, 0x00000000u}, + {0x20000000u, 0x1f000000u, 0x00000000u, 0x00000000u}, + {0x20000000u, 0x1f7fffffu, 0x00000000u, 0x00000000u}, + {0x20000001u, 0x1f7fffffu, 0x00000000u, 0x00000000u}, + {0xa0000000u, 0x1f7fffffu, 0x80000000u, 0x00000000u}, + {0x3f800000u, 0xbf800000u, 0x00000001u, 0x00000000u}, + {0x00800000u, 0x3f000000u, 0x00000000u, 0x00000000u}, + {0x00800000u, 0x3f7fffffu, 0x00000001u, 0x00000000u}, + {0x41200000u, 0x00000003u, 0x3dcccccdu, 0x00000000u}, + {0xc0490fdbu, 0x80000002u, 0x40490fdbu, 0x00000000u}, + {0x80000001u, 0x80000001u, 0x80000001u, 0x00000000u}, + {0x00000001u, 0x00000001u, 0x00000001u, 0x00000000u} +}; +constexpr std::uint32_t Expected[32][16] = { + {0x3f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x80000000u}, + {0x3f800000u, 0x3f800000u, 0x80000000u, 0x00000000u, 0x00800000u, 0x00800000u, 0x80000000u, 0x3f800000u, 0x00800000u, 0x80000000u, 0x3f800000u, 0x80000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x80000000u}, + {0x3f800000u, 0xbf800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x80000000u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u}, + {0x3f7fffffu, 0xbf7fffffu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00800000u, 0x3f7fffffu, 0x00800000u, 0x00000000u, 0x3f800000u, 0x3f7fffffu, 0x00000000u, 0x00000000u, 0xffffff83u, 0x80000000u}, + {0xbf7fffffu, 0x3f7fffffu, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0xbf7fffffu, 0x00800000u, 0x80000000u, 0xbf800000u, 0x3f800000u, 0xbf7fffffu, 0x00000001u, 0xffffffffu, 0xffffff83u, 0x80000000u}, + {0x3f7fffffu, 0xbf7fffffu, 0x00800000u, 0x00800000u, 0x00800000u, 0x00800000u, 0x00800001u, 0x3f7fffffu, 0x00800001u, 0x00000000u, 0x3f800000u, 0x3f7fffffu, 0x00000000u, 0x00000000u, 0xffffff83u, 0x80800000u}, + {0x3f000000u, 0xbf000000u, 0x00800000u, 0x00800000u, 0x00800000u, 0x00800000u, 0x01000000u, 0x3f000000u, 0x01000000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffff84u, 0x80800000u}, + {0x80000000u, 0x81000000u, 0x80000000u, 0x80000000u, 0x00800000u, 0x00800000u, 0x80800001u, 0x00800000u, 0x00800000u, 0x00000000u, 0x80000000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffff83u, 0x00000000u}, + {0x01000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x3f800000u, 0x00800000u, 0x3f800000u, 0x00800001u, 0x00000000u, 0x3f800000u, 0x3f000001u, 0x00000000u, 0x00000000u, 0xffffff83u, 0x80000000u}, + {0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u}, + {0x7f800000u, 0xff800000u, 0xffc00000u, 0x00000000u, 0xffc00000u, 0xffc00000u, 0x00000000u, 0x7f800000u, 0x3f800000u, 0x7f800000u, 0x00000000u, 0x7f800000u, 0x00000000u, 0x7fffffffu, 0x00000000u, 0xffc00000u}, + {0xff800000u, 0xff800000u, 0xffc00000u, 0x00000000u, 0xffc00000u, 0xffc00000u, 0xff800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xffc00000u}, + {0x7fc00000u, 0xffc00000u, 0x7fc00000u, 0x00000000u, 0x7fc00000u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x7fc00000u, 0x00000000u, 0x7fc00000u, 0x00000000u, 0x7fffffffu, 0x00000000u, 0x7fc00000u}, + {0x40000000u, 0xc0000000u, 0x00000000u, 0x00000000u, 0x80800000u, 0x80800000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x3f000000u, 0x00000000u, 0x00000002u, 0x00000000u, 0x80000000u}, + {0x40000000u, 0x00000000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000001u, 0x00000001u, 0xbf800000u}, + {0xbf800000u, 0x3f800000u, 0x80000000u, 0x00000000u, 0x3f000000u, 0x3f000000u, 0xbf800000u, 0x3f000000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0xbf000000u, 0x00000001u, 0xffffffffu, 0x00000000u, 0x80000000u}, + {0x34800000u, 0x00000000u, 0x28800000u, 0x28800000u, 0x28800000u, 0x28800000u, 0x34000000u, 0x34000000u, 0x34000000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffffeau, 0xa8800000u}, + {0x20000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x1f800000u, 0x1f800000u, 0x1f800000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffffc1u, 0x80000000u}, + {0x207fffffu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00ffffffu, 0x00ffffffu, 0x1fffffffu, 0x1fffffffu, 0x1fffffffu, 0x00000000u, 0x3f800000u, 0x3f7fffffu, 0x00000000u, 0x00000000u, 0xffffffc1u, 0x80000000u}, + {0x20200000u, 0x9fc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x1f000000u, 0x20000000u, 0x1f000000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffffc0u, 0x80000000u}, + {0x20400000u, 0x9f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x1f7fffffu, 0x20000000u, 0x1f7fffffu, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xffffffc0u, 0x80000000u}, + {0x20400001u, 0x9f800002u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x1f7fffffu, 0x20000001u, 0x1f7fffffu, 0x00000000u, 0x3f800000u, 0x3f000001u, 0x00000000u, 0x00000000u, 0xffffffc0u, 0x80000000u}, + {0x9f800000u, 0x20400000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0xa0000000u, 0x1f7fffffu, 0x80000000u, 0xbf800000u, 0x3f800000u, 0xbf000000u, 0x00000001u, 0xffffffffu, 0xffffffc0u, 0x80000000u}, + {0x00000000u, 0xc0000000u, 0xbf800000u, 0xbf800000u, 0xbf800000u, 0xbf800000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0xbf800000u, 0x3f000000u, 0x00000000u, 0x00000001u, 0x00000001u, 0x3f800000u}, + {0x3f000000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00800000u, 0x3f000000u, 0x00800000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x80000000u}, + {0x3f7fffffu, 0x3f7fffffu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00800000u, 0x3f7fffffu, 0x00800000u, 0x00000000u, 0x3f800000u, 0x3f000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x80000000u}, + {0x41200000u, 0xc1200000u, 0x00000000u, 0x00000000u, 0x3dcccccdu, 0x3dcccccdu, 0x00000000u, 0x41200000u, 0x3dcccccdu, 0x41200000u, 0x00000000u, 0x3f200000u, 0x00000000u, 0x0000000au, 0x00000000u, 0x80000000u}, + {0xc0490fdbu, 0x40490fdbu, 0x00000000u, 0x00000000u, 0x40490fdbu, 0x40490fdbu, 0xc0490fdbu, 0x40490fdbu, 0x80000000u, 0xc0800000u, 0x80000000u, 0xbf490fdbu, 0x00000001u, 0xfffffffcu, 0x00000000u, 0x00000000u}, + {0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u} +}; +constexpr const char* Names[16] = { + "v_add_f32 v10, v4, v5", + "v_sub_f32 v11, v5, v4", + "v_mul_f32 v12, v4, v5", + "v_mul_legacy_f32 v13, v4, v5", + "v_fma_f32 v14, v4, v5, v6", + "v_fmac_f32 v15, v4, v5", + "v_min_f32 v16, v4, v5", + "v_max3_f32 v17, v4, v5, v6", + "v_med3_f32 v18, v4, v5, v6", + "v_floor_f32 v19, v4", + "v_ceil_f32 v20, v5", + "v_frexp_mant_f32 v21, v4", + "v_cmp_lt_f32 v4, v5", + "v_cvt_flr_i32_f32 v23, v4", + "v_frexp_exp_i32_f32 v24, v5", + "v_mul_f32 v25, |v4|, -v5" +}; + +void Fill(std::uint32_t tid, std::uint32_t* words) { + std::copy(std::begin(Rows[tid]), std::end(Rows[tid]), words); +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x31016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) { + Require(actual == expected, std::string("f32 denormal flush: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +bool IsNan(std::uint32_t value) { + return (value & 0x7fffffffu) > 0x7f800000u; +} + +void Run(AgcDriver::VulkanDevice& device, const std::optional& floatMode) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + request.context.floatMode = floatMode; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check(const char* mode) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::uint32_t* out = &Output[tid * Results]; + for (std::uint32_t i = 0; i < 16; ++i) { + const bool floatResult = i < 12u || i == 15u; + if (floatResult && IsNan(Expected[tid][i])) { + Require(IsNan(out[i]), std::string("f32 denormal flush: lane ") + std::to_string(tid) + " " + mode + " " + Names[i] + " is " + Hex(out[i]) + ", expected a NaN"); + continue; + } + Expect(tid, out[i], Expected[tid][i], (std::string(mode) + " " + Names[i]).c_str()); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device, std::nullopt); + Check("no float mode"); + Run(*device, ShaderRecompiler::ShaderFloatMode{0xc0u, true, false, false}); + Check("IEEE=0 f32 denormals flushed"); + Run(*device, ShaderRecompiler::ShaderFloatMode{0x00u, false, true, false}); + Check("IEEE=1 all denormals flushed"); + for (const std::uint32_t mode : {0xd0u, 0xe0u}) { + bool refused = false; + try { + Run(*device, ShaderRecompiler::ShaderFloatMode{mode, true, false, false}); + } catch (const std::runtime_error& error) { + refused = std::string(error.what()).find("f32 denormal mode") != std::string::npos; + } + Require(refused, "f32 denormal flush: FLOAT_MODE " + Hex(mode) + " was not refused"); + } + std::puts("f32 denormal flush tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/tests/VectorFusedMultiplyAdd.cpp b/core/libs/tests/VectorFusedMultiplyAdd.cpp index e004d27b2..e8713719a 100644 --- a/core/libs/tests/VectorFusedMultiplyAdd.cpp +++ b/core/libs/tests/VectorFusedMultiplyAdd.cpp @@ -51,6 +51,7 @@ void Check(const std::string& name, std::uint32_t encoding, std::uint32_t wordCo auto& block = program.CreateBlock(); program.SetEntryBlock(block); TranslationContext context(program, block, 256); + context.SetFloatMode(ShaderFloatMode{0xf0u, false, false, false}); context.TranslateInstruction(instruction); const IrOpcode rejected = expected == IrOpcode::FPFma32 ? IrOpcode::FPMad32 : IrOpcode::FPFma32; diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index e07419e9c..2cd100f21 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -54,6 +54,8 @@ private: void write16Bits(const RdnaOperand& operand, IrU32 value); void writeF16(const RdnaOperand& operand, IrF32 value); IrU32 readU32(const RdnaOperand& operand); + IrU32 flushF32Denormal(IrU32 bits); + IrF32 flushTinyProduct(IrValue* lhs, IrValue* rhs, IrValue* product, IrValue* addend = nullptr); std::array readU32Pair(const RdnaOperand& operand); IrU64 readU64(const RdnaOperand& operand); std::array readF64Bits(const RdnaOperand& operand); @@ -306,6 +308,7 @@ private: void eXP(const RdnaInstruction& inst); bool emitScalar(const RdnaInstruction& inst); bool emitVector(const RdnaInstruction& inst); + std::uint32_t f32DenormalFlushFor(const RdnaInstruction& inst) const; bool emitInterpolation(const RdnaInstruction& inst); bool emitMemory(const RdnaInstruction& inst); @@ -318,6 +321,7 @@ private: IrBlock& block; IrU1 instructionBranchCondition; RdnaOpcode currentOpcode = RdnaOpcode::Unknown; + std::uint32_t f32DenormalFlush = 0u; std::uint32_t currentProgramCounter = 0; std::uint32_t currentVectorLimit = 1; }; diff --git a/core/shader/recompiler/Translation/src/ConvertInstructions.cpp b/core/shader/recompiler/Translation/src/ConvertInstructions.cpp index 91978c218..b0a367c0d 100644 --- a/core/shader/recompiler/Translation/src/ConvertInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ConvertInstructions.cpp @@ -163,8 +163,8 @@ void TranslationContext::vCvtPkrtzF16F32(const RdnaInstruction& inst) { } void TranslationContext::vCvtPknormF32(const RdnaInstruction& inst, bool signedValue) { - const IrU32 low = normF32(readU32(sourceAt(inst, 0u)), signedValue); - const IrU32 high = normF32(readU32(sourceAt(inst, 1u)), signedValue); + const IrU32 low = normF32(flushF32Denormal(readU32(sourceAt(inst, 0u))), signedValue); + const IrU32 high = normF32(flushF32Denormal(readU32(sourceAt(inst, 1u))), signedValue); const IrU32 result(ir.BitwiseOr(low.Value(), ir.ShiftLeftLogical(high.Value(), ir.Constant(16u)))); writeOperand(inst.destination, &result.Value()); } diff --git a/core/shader/recompiler/Translation/src/DispatchInstructions.cpp b/core/shader/recompiler/Translation/src/DispatchInstructions.cpp index c77fff4ad..e2c9f9f7b 100644 --- a/core/shader/recompiler/Translation/src/DispatchInstructions.cpp +++ b/core/shader/recompiler/Translation/src/DispatchInstructions.cpp @@ -24,6 +24,7 @@ void TranslationContext::TranslateInstruction(const RdnaInstruction& decoded) { if (instruction.op == RdnaOpcode::Unsupported) { throw std::runtime_error(instruction.unsupportedReason.empty() ? "unsupported decoded instruction at pc " + std::to_string(instruction.programCounter) : std::string(instruction.unsupportedReason)); } + f32DenormalFlush = f32DenormalFlushFor(instruction); bool translated = false; switch (instruction.family) { case RdnaInstructionFamily::SOP1: diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index 8249830f0..61d7db992 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -337,7 +337,7 @@ bool TranslationContext::vMulLegacyF32(const RdnaInstruction& inst, bool accumul IrValue* rhs = readOperand(sourceAt(inst, 1u), IrType::F32); const auto isZero = [&](IrValue* value) { return IrU1(ir.IEqual(ir.BitwiseAnd(ir.BitCastU32(*value), ir.Constant(0x7fffffffu)), ir.Constant(0u))); }; const IrU1 zero(ir.LogicalOr(isZero(lhs).Value(), isZero(rhs).Value())); - IrValue* result = &ir.Emit(IrOpcode::SelectF32, IrType::F32, {&zero.Value(), &ir.ConstantF32(0.0f), &ir.Emit(IrOpcode::FPMul32, IrType::F32, {lhs, rhs})}); + IrValue* result = &ir.Emit(IrOpcode::SelectF32, IrType::F32, {&zero.Value(), &ir.ConstantF32(0.0f), &flushTinyProduct(lhs, rhs, &ir.Emit(IrOpcode::FPMul32, IrType::F32, {lhs, rhs})).Value()}); if (accumulate) { IrValue* addend = readOperand(accumulatorOperand(inst), IrType::F32); result = &ir.Emit(IrOpcode::FPAdd32, IrType::F32, {result, addend}); @@ -491,7 +491,7 @@ bool TranslationContext::vDivFixupF32(const RdnaInstruction& inst) { } bool TranslationContext::floatUnary(const RdnaInstruction& inst, IrOpcode opcode) { - const IrU32 bits = readU32(sourceAt(inst, 0u)); + const IrU32 bits = flushF32Denormal(readU32(sourceAt(inst, 0u))); const IrF32 argument(ir.BitCastF32(bits.Value())); const auto [result, invalid] = unaryFloatSpecials(opcode, argument, IrF32(ir.Emit(opcode, IrType::F32, {&argument.Value()}))); const IrU1 nan(ir.UGreaterThan(ir.BitwiseAnd(bits.Value(), ir.Constant(0x7fffffffu)), ir.Constant(0x7f800000u))); @@ -507,8 +507,9 @@ bool TranslationContext::floatBinary(const RdnaInstruction& inst, IrOpcode opcod const RdnaOperand& operand = sourceAt(inst, reverse ? 1u - index : index); args[index] = readOperand(operand, IrOpcodeArgumentType(opcode, index)); } - IrValue& result = ir.Emit(opcode, IrOpcodeType(opcode), {args[0], args[1]}); - writeOperand(inst.destination, &result); + IrValue* result = &ir.Emit(opcode, IrOpcodeType(opcode), {args[0], args[1]}); + if (opcode == IrOpcode::FPMul32) result = &flushTinyProduct(args[0], args[1], result).Value(); + writeOperand(inst.destination, result); return true; } @@ -519,8 +520,9 @@ bool TranslationContext::floatTernary(const RdnaInstruction& inst, IrOpcode opco const IrType type = IrOpcodeArgumentType(opcode, index); args[index] = type == IrType::F32 && mix ? &readMixF32(operand).Value() : readOperand(operand, type); } - IrValue& result = ir.Emit(opcode, IrOpcodeType(opcode), {args[0], args[1], args[2]}); - writeOperand(inst.destination, &result); + IrValue* result = &ir.Emit(opcode, IrOpcodeType(opcode), {args[0], args[1], args[2]}); + if (opcode == IrOpcode::FPFma32) result = &flushTinyProduct(args[0], args[1], result, args[2]).Value(); + writeOperand(inst.destination, result); return true; } @@ -568,7 +570,7 @@ bool TranslationContext::vFmaLegacyF32(const RdnaInstruction& inst) { } bool TranslationContext::vFrexpMantF32(const RdnaInstruction& inst) { - const IrU32 bits = readU32(sourceAt(inst, 0u)); + const IrU32 bits = flushF32Denormal(readU32(sourceAt(inst, 0u))); const IrU32 exponent(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&bits.Value(), &ir.Constant(23u), &ir.Constant(8u)})); const IrU32 mantissa(ir.BitwiseAnd(bits.Value(), ir.Constant(0x007fffffu))); const IrU32 sign(ir.BitwiseAnd(bits.Value(), ir.Constant(0x80000000u))); diff --git a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp index 501def011..ed391b2d6 100644 --- a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp +++ b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp @@ -125,6 +125,28 @@ RdnaOperand TranslationContext::plainOperand(const RdnaOperand& operand) { return result; } +IrU32 TranslationContext::flushF32Denormal(IrU32 bits) { + if ((f32DenormalFlush & 1u) == 0u) return bits; + const IrU1 denormal(ir.IEqual(ir.BitwiseAnd(bits.Value(), ir.Constant(0x7f800000u)), ir.Constant(0u))); + return IrU32(ir.Select(denormal.Value(), ir.BitwiseAnd(bits.Value(), ir.Constant(0x80000000u)), bits.Value())); +} + +IrF32 TranslationContext::flushTinyProduct(IrValue* lhs, IrValue* rhs, IrValue* product, IrValue* addend) { + if ((f32DenormalFlush & 2u) == 0u) return IrF32(*product); + IrValue& lhsBits = ir.BitCastU32(*lhs); + IrValue& rhsBits = ir.BitCastU32(*rhs); + const auto exponent = [&](IrValue& bits) -> IrValue& { return ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&bits, &ir.Constant(23u), &ir.Constant(8u)}); }; + const auto significand = [&](IrValue& bits) -> IrValue& { return ir.BitwiseOr(ir.BitwiseAnd(bits, ir.Constant(0x007fffffu)), ir.Constant(0x00800000u)); }; + IrValue& lhsExponent = exponent(lhsBits); + IrValue& rhsExponent = exponent(rhsBits); + const auto finiteNonZero = [&](IrValue& value) -> IrValue& { return ir.LogicalAnd(ir.INotEqual(value, ir.Constant(0u)), ir.INotEqual(value, ir.Constant(0xffu))); }; + IrValue& carry = ir.Select(ir.UGreaterThan(ir.Emit(IrOpcode::UMulHi, IrType::U32, {&significand(lhsBits), &significand(rhsBits)}), ir.Constant(0x7fffu)), ir.Constant(1u), ir.Constant(0u)); + IrValue* tiny = &ir.LogicalAnd(ir.LogicalAnd(finiteNonZero(lhsExponent), finiteNonZero(rhsExponent)), ir.ULessThan(ir.IAdd(ir.IAdd(lhsExponent, rhsExponent), carry), ir.Constant(128u))); + if (addend != nullptr) tiny = &ir.LogicalAnd(*tiny, ir.IEqual(ir.BitwiseAnd(ir.BitCastU32(*addend), ir.Constant(0x7fffffffu)), ir.Constant(0u))); + IrValue& sign = ir.BitwiseAnd(ir.BitwiseXor(lhsBits, rhsBits), ir.Constant(0x80000000u)); + return IrF32(ir.Emit(IrOpcode::SelectF32, IrType::F32, {tiny, &ir.BitCastF32(sign), product})); +} + IrValue* TranslationContext::readOperand(const RdnaOperand& operand, IrType type) { if (type == IrType::U16) { return &ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&applyBitSourceModifiers(operand, readRawU32(operand)).Value()}); @@ -158,7 +180,7 @@ IrValue* TranslationContext::readOperand(const RdnaOperand& operand, IrType type bits = IrU32(ir.BitwiseXor(bits.Value(), ir.Constant(0x80000000u))); } if (TypesOverlap(type, IrType::F32) && !TypesOverlap(type, IrType::U32)) { - return &ir.BitCastF32(bits.Value()); + return &ir.BitCastF32(flushF32Denormal(bits).Value()); } if (!TypesOverlap(type, IrType::U32)) { throw std::runtime_error("TranslationContext::readOperand requested unsupported operand type"); @@ -172,6 +194,7 @@ void TranslationContext::writeOperand(const RdnaOperand& operand, IrValue* value } IrType type = value->Type(); if (type == IrType::F32) { + if ((f32DenormalFlush & 2u) != 0u) value = &ir.BitCastF32(flushF32Denormal(IrU32(ir.BitCastU32(*value))).Value()); value = &applyF32ResultModifiers(operand, IrF32(*value)).Value(); type = IrType::F32; } diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 7d232075b..e4376863d 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -20,6 +20,80 @@ bool roundsProductSeparately(const RdnaInstruction& inst) { } +std::uint32_t TranslationContext::f32DenormalFlushFor(const RdnaInstruction& inst) const { + std::uint32_t flush = 0u; + switch (inst.op) { + case RdnaOpcode::VAddF32: + case RdnaOpcode::VSubF32: + case RdnaOpcode::VSubrevF32: + case RdnaOpcode::VMulF32: + case RdnaOpcode::VMulLegacyF32: + case RdnaOpcode::VMinF32: + case RdnaOpcode::VMaxF32: + case RdnaOpcode::VMin3F32: + case RdnaOpcode::VMax3F32: + case RdnaOpcode::VMed3F32: + case RdnaOpcode::VFractF32: + case RdnaOpcode::VFloorF32: + case RdnaOpcode::VCeilF32: + case RdnaOpcode::VFrexpMantF32: + case RdnaOpcode::VSinF32: + case RdnaOpcode::VCosF32: + flush = 3u; + break; + case RdnaOpcode::VMacF32: + case RdnaOpcode::VMadmkF32: + case RdnaOpcode::VMadakF32: + case RdnaOpcode::VFmaF32: + flush = roundsProductSeparately(inst) ? 0u : 3u; + break; + case RdnaOpcode::VCmpEqF32: + case RdnaOpcode::VCmpGeF32: + case RdnaOpcode::VCmpGtF32: + case RdnaOpcode::VCmpLeF32: + case RdnaOpcode::VCmpLgF32: + case RdnaOpcode::VCmpLtF32: + case RdnaOpcode::VCmpNeqF32: + case RdnaOpcode::VCmpNgeF32: + case RdnaOpcode::VCmpNgtF32: + case RdnaOpcode::VCmpNleF32: + case RdnaOpcode::VCmpNlgF32: + case RdnaOpcode::VCmpNltF32: + case RdnaOpcode::VCmpxEqF32: + case RdnaOpcode::VCmpxGeF32: + case RdnaOpcode::VCmpxGtF32: + case RdnaOpcode::VCmpxLeF32: + case RdnaOpcode::VCmpxLgF32: + case RdnaOpcode::VCmpxLtF32: + case RdnaOpcode::VCmpxNeqF32: + case RdnaOpcode::VCmpxNgeF32: + case RdnaOpcode::VCmpxNgtF32: + case RdnaOpcode::VCmpxNleF32: + case RdnaOpcode::VCmpxNlgF32: + case RdnaOpcode::VCmpxNltF32: + case RdnaOpcode::VCvtI32F32: + case RdnaOpcode::VCvtU32F32: + case RdnaOpcode::VCvtRpiI32F32: + case RdnaOpcode::VCvtFlrI32F32: + case RdnaOpcode::VFrexpExpI32F32: + case RdnaOpcode::VCvtF64F32: + case RdnaOpcode::VCvtPknormI16F32: + case RdnaOpcode::VCvtPknormU16F32: + case RdnaOpcode::VCvtPkU8F32: + case RdnaOpcode::VCvtPkrtzF16F32: + case RdnaOpcode::VCvtF16F32: + flush = 1u; + break; + default: + return 0u; + } + const std::uint32_t denormals = floatMode.has_value() ? (floatMode->floatMode >> 4u) & 3u : 0u; + if (denormals == 1u || denormals == 2u) { + throw std::runtime_error("f32 denormal mode " + std::to_string(denormals) + " at pc " + std::to_string(inst.programCounter) + " is not implemented"); + } + return denormals == 0u ? flush : 0u; +} + bool TranslationContext::emitVector(const RdnaInstruction& inst) { switch (inst.op) { case RdnaOpcode::VNop: diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index fa7525661..aa1617d1f 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -291,6 +291,7 @@ Throughout the project, every function at every stage either **does exactly what - [v_fma_legacy_f32](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (VOP3 0x140) is assumed to be fused, which has not been measured on hardware. LLVM decodes this encoding as `v_mad_legacy_f32` for gfx1013, and `v_mac_legacy_f32` (VOP2 0x06) was measured to round its product before the add, so 0x140 may do the same. - [v_fma_f16, v_pk_fma_f16 and v_pk_fmac_f16](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) are an f32 FMA rounded to f16, which rounds twice: 0x62e9 * 0xd159 + 0x23ec gives 0xf89e, the hardware 0xf89f. - [Packed f16 NaN results](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (`v_pk_add/mul/fma/min/max_f16`) follow the hardware measured on RDNA2 (gfx1035) at IEEE=0 and IEEE=1 with f16 round-to-nearest-even and f16 denormals kept: with IEEE=1 a signaling NaN input is quieted and min/max return the other operand only for a quiet NaN; with IEEE=0 NaN inputs pass through unquieted and min/max ignore signaling. In `v_pk_fma_f16` an invalid product (inf * 0) gives the default NaN even when src2 is a NaN. A shader without a known float mode is translated as IEEE=0, the mode titles use; a mode with another f16 rounding or that flushes f16 denormals throws. +- [f32 denormals](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) follow the shader's `FLOAT_MODE` f32 denormal field (bits 5:4) when it flushes them (0, and shaders without a known float mode, as in the 0xc0 most titles use), measured on RDNA2 (gfx1035) over 1444 rows per instruction at IEEE=0 and IEEE=1. `v_add/sub/subrev/mul/mul_legacy/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32` flush their f32 inputs and their result to a zero of the same sign; the f32 compares, `v_cvt_i32/u32/rpi_i32/flr_i32_f32`, `v_frexp_exp_i32_f32`, `v_cvt_f64_f32`, `v_cvt_f16_f32`, `v_cvt_pkrtz_f16_f32`, `v_cvt_pknorm_i16/u16_f32` and `v_cvt_pk_u8_f32` flush their f32 inputs. Tininess is detected before rounding: a product whose exact value is below 2^-126 gives zero even when it rounds to 2^-126, computed exactly from the significands for `v_mul_f32`, `v_mul_legacy_f32`, and for `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32`, `v_fmaak_f32` and `v_fma_mix_f32` when the addend is zero; with a nonzero addend the fused result is kept, so an exact result just below 2^-126 that rounds to it is not flushed. `v_fma_mix_f32` was measured with f32 sources only; its f16 sources are not flushed. Denormal modes 1 and 2 throw. Not covered, and not measurable on RDNA2 (no such opcodes) or not measured: `v_mac_f32`, `v_mad_f32`, `v_madak_f32` and `v_madmk_f32` (rounded separately), `v_mac_legacy_f32`, `v_dot2_f32_f16`, the division steps (on hardware `v_div_fixup_f32` flushes only src1 and src2, `v_div_fmas_f32` only its result), f32 interpolation and LDS float atomics keep the previous lowering. With f32 denormals kept (3) the results depend on the driver's default denormal behaviour, as no `DenormPreserve` execution mode is requested, and `v_ldexp_f32` flushes in every mode. - The executable file that [relinker](../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens the console when launched, which is inconvenient for playability. - [PA_CL_CLIP_CNTL](../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - `DX_LINEAR_ATTR_CLIP_ENA` (bit 24) clear is drawn like bit 24 set, with Vulkan's clipping of `NoPerspective` outputs; what a clear bit changes on the console is undocumented. - [Per-stage descriptor count](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - a pixel shader's colour attachments are not counted against `maxPerStageResources`, so its buffers and images can fit the limit while the pipeline exceeds it