From abbcb31e54fb64b9397f9afd32d8d90789e547b3 Mon Sep 17 00:00:00 2001 From: Chad Date: Sun, 4 Oct 2026 18:09:01 -0500 Subject: [PATCH] feat(shader): implement v_dot2_f32_f16 VOP3P 0x13 threw "VOP3P opcode is not implemented". It computes src0.lo * src1.lo + src0.hi * src1.hi + src2 like v_dot2c_f32_f16, with the VOP3P op_sel/op_sel_hi and neg_lo/neg_hi modifiers on the packed f16 sources, neg on the f32 accumulator and clamp. op_sel and neg_hi on the accumulator throw. Co-Authored-By: Claude Opus 5.5 --- .../tests/execution/Float16Dot.cpp | 161 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 1 + .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 6 +- .../Translation/TranslationContext.hpp | 1 + .../Translation/src/FloatInstructions.cpp | 12 ++ .../Translation/src/VectorInstructions.cpp | 2 + 7 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/Float16Dot.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Float16Dot.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Float16Dot.cpp new file mode 100644 index 000000000..56d54ed41 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Float16Dot.cpp @@ -0,0 +1,161 @@ +#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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 64; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 8; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array DotCode{ + 0x34020084, 0x34060085, 0xe0301000, 0x80000401, 0xe0301004, 0x80000501, 0xe0301008, 0x80000601, + 0xbf8c3f70, 0xcc13400a, 0x1c1a0b04, 0xcc13480b, 0x141a0b04, 0xcc13420c, 0xbc1a0b04, 0xcc13c00d, + 0x1c1a0b04, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, + 0x80010d03, 0xbf810000, +}; + +constexpr std::array, 8> Edges{{ + {0x3c003c00u, 0x3c003c00u, 0x00000000u}, {0x7c000000u, 0x3c000000u, 0x3f800000u}, {0x7c003c00u, 0x00003c00u, 0x3f800000u}, + {0xfc007c00u, 0x3c003c00u, 0x40000000u}, {0x80000000u, 0x00008000u, 0x80000000u}, {0x7bff7bffu, 0x7bff7bffu, 0xcf800000u}, + {0x38003800u, 0xb800b800u, 0x3e800000u}, {0x7e003c00u, 0x3c003c00u, 0x00000000u}, +}}; + +float Half(std::uint32_t bits) { + const std::uint32_t sign = (bits & 0x8000u) << 16u; + const std::uint32_t exponent = (bits >> 10u) & 0x1fu; + const std::uint32_t mantissa = bits & 0x3ffu; + if (exponent == 0x1fu) return std::bit_cast(sign | 0x7f800000u | (mantissa << 13u)); + if (exponent == 0u) return std::bit_cast(sign) + (sign != 0u ? -1.0f : 1.0f) * std::ldexp(static_cast(mantissa), -24); + return std::bit_cast(sign | ((exponent + 112u) << 23u) | (mantissa << 13u)); +} + +std::uint32_t RandomHalf(std::uint64_t& state) { + state = state * 6364136223846793005ull + 1442695040888963407ull; + const auto bits = static_cast(state >> 33u); + return (bits & 0x8000u) | ((8u + (bits >> 16u) % 15u) << 10u) | (bits & 0x3ffu); +} + +void FillInput() { + std::uint64_t state = 0x9e3779b97f4a7c15ull; + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + auto* words = &Input[tid * Inputs]; + if (tid < Edges.size()) { + std::copy(Edges[tid].begin(), Edges[tid].end(), words); + continue; + } + words[0] = RandomHalf(state) | (RandomHalf(state) << 16u); + words[1] = RandomHalf(state) | (RandomHalf(state) << 16u); + words[2] = std::bit_cast(Half(RandomHalf(state)) * Half(RandomHalf(state))); + } +} + +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, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +auto Compile(AgcDriver::VulkanDevice& device, std::span code) { + 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::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {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; + return ShaderRecompiler::Recompile(request); +} + +void Run(AgcDriver::VulkanDevice& device) { + Output.fill(0xdeadbeefu); + const std::span code(DotCode); + const auto result = Compile(device, code); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void CheckRefused(AgcDriver::VulkanDevice& device, std::uint32_t word0, const std::string& what) { + alignas(256) const std::array code{word0, 0x1c1a0b04u, 0xbf810000u}; + std::string refusal; + try { + static_cast(Compile(device, code)); + } catch (const std::exception& error) { + refusal = error.what(); + } + Require(refusal.find("v_dot2_f32_f16 accumulator op_sel and neg_hi are not implemented") != std::string::npos, what + " was not refused"); +} + +float Dot(float aLow, float aHigh, float bLow, float bHigh, float c) { + return std::fma(aHigh, bHigh, std::fma(aLow, bLow, c)); +} + +void Expect(std::uint32_t tid, std::uint32_t actual, float expected, const char* name) { + const std::uint32_t bits = std::bit_cast(expected); + const bool nan = std::isnan(expected) && std::isnan(std::bit_cast(actual)); + Require(nan || actual == bits, std::string("v_dot2_f32_f16: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(bits)); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const auto* in = &Input[tid * Inputs]; + const auto* out = &Output[tid * Results]; + const float aLow = Half(in[0] & 0xffffu); + const float aHigh = Half(in[0] >> 16u); + const float bLow = Half(in[1] & 0xffffu); + const float bHigh = Half(in[1] >> 16u); + const float c = std::bit_cast(in[2]); + const float plain = Dot(aLow, aHigh, bLow, bHigh, c); + Expect(tid, out[0], plain, "plain"); + Expect(tid, out[1], Dot(aHigh, aLow, bLow, bHigh, c), "op_sel swapping the halves of src0"); + Expect(tid, out[2], Dot(-aLow, aHigh, bLow, -bHigh, -c), "neg_lo src0, neg_hi src1 and neg src2"); + if (!std::isnan(plain)) Expect(tid, out[3], plain > 0.0f ? std::min(plain, 1.0f) : 0.0f, "clamp"); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + FillInput(); + Run(*device); + Check(); + CheckRefused(*device, 0xcc13600au, "op_sel on the accumulator"); + CheckRefused(*device, 0xcc13440au, "neg_hi on the accumulator"); + std::puts("v_dot2_f32_f16 tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 5ed43f202..8870b4e4a 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -279,6 +279,7 @@ enum class RdnaOpcode : std::uint16_t { VCubetcF32, VCubemaF32, VDot2cF32F16, + VDot2F32F16, VDot4cI32I8, VDot2I32I16, VDot2U32U16, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index 4b0069c92..b490dc22f 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -228,6 +228,7 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::VCubetcF32: case RdnaOpcode::VCubemaF32: case RdnaOpcode::VDot2cF32F16: + case RdnaOpcode::VDot2F32F16: case RdnaOpcode::VDot4cI32I8: case RdnaOpcode::VDot2I32I16: case RdnaOpcode::VDot2U32U16: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index 7a09fc34e..cb44b5325 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -590,6 +590,7 @@ constexpr VectorOpcodeInfo vop3pOpcodes[] = { {0x10u, RdnaOpcode::VPkMulF16}, {0x11u, RdnaOpcode::VPkMinF16}, {0x12u, RdnaOpcode::VPkMaxF16}, + {0x13u, RdnaOpcode::VDot2F32F16}, {0x14u, RdnaOpcode::VDot2I32I16}, {0x15u, RdnaOpcode::VDot2U32U16}, {0x16u, RdnaOpcode::VDot4I32I8}, @@ -2199,7 +2200,10 @@ RdnaInstruction DecodeRdnaVop3p(std::uint32_t programCounter, std::span 2u) { instruction.source2 = DecodeRdnaScalarSource(src2, programCounter); } - if (instruction.op == RdnaOpcode::VFmaF32 || isMadMixF16(instruction.op) || isPackedVop3p(instruction.op) || isVop3pIntegerSaturate(instruction.op)) { + if (instruction.op == RdnaOpcode::VDot2F32F16 && (((opSel | negHi) >> 2u) & 1u) != 0u) { + throw std::invalid_argument("VOP3P v_dot2_f32_f16 accumulator op_sel and neg_hi are not implemented"); + } + if (instruction.op == RdnaOpcode::VFmaF32 || instruction.op == RdnaOpcode::VDot2F32F16 || isMadMixF16(instruction.op) || isPackedVop3p(instruction.op) || isVop3pIntegerSaturate(instruction.op)) { instruction.destination.clamp = clamp != 0u; } else if (clamp != 0u && instruction.op != RdnaOpcode::VPkMulLoU16) { throw std::invalid_argument("VOP3P integer clamp is not implemented"); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 8622e85b1..9fb00946a 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -171,6 +171,7 @@ private: void emitFloat16ClassCompare(const RdnaInstruction& inst, bool cmpx); bool float64Operation(const RdnaInstruction& inst, IrOpcode opcode); bool vDot2cF32F16(const RdnaInstruction& inst); + bool vDot2F32F16(const RdnaInstruction& inst); bool vCubeidF32(const RdnaInstruction& inst); bool vCubescF32(const RdnaInstruction& inst); bool vCubetcF32(const RdnaInstruction& inst); diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index 4307ca9c9..b11dbe80d 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -433,6 +433,18 @@ bool TranslationContext::vDot2cF32F16(const RdnaInstruction& inst) { return true; } +bool TranslationContext::vDot2F32F16(const RdnaInstruction& inst) { + const IrF32 aLow = readF16LaneAsF32(sourceAt(inst, 0u), false, true); + const IrF32 aHigh = readF16LaneAsF32(sourceAt(inst, 0u), true, true); + const IrF32 bLow = readF16LaneAsF32(sourceAt(inst, 1u), false, true); + const IrF32 bHigh = readF16LaneAsF32(sourceAt(inst, 1u), true, true); + IrValue* accumulator = readOperand(sourceAt(inst, 2u), IrType::F32); + const IrF32 low(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aLow.Value(), &bLow.Value(), accumulator})); + const IrF32 result(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aHigh.Value(), &bHigh.Value(), &low.Value()})); + writeOperand(inst.destination, &result.Value()); + return true; +} + bool TranslationContext::vCubeidF32(const RdnaInstruction& inst) { return floatCube(inst, 0u); } diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 9e2d42450..75ba73621 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -950,6 +950,8 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { return floatTernary(inst, IrOpcode::FPMedTri32, false, false); case RdnaOpcode::VDot2cF32F16: return vDot2cF32F16(inst); + case RdnaOpcode::VDot2F32F16: + return vDot2F32F16(inst); case RdnaOpcode::VDot4cI32I8: return integerDot(inst, 8u, true, true); case RdnaOpcode::VDot2I32I16: