Merge pull request #529 from DotDebian/fix/shader-decode-past-endpgm

fix(shader): keep decoding past an s_endpgm while a branch targets later code
This commit is contained in:
Dean
2026-10-05 10:10:29 +13:00
committed by GitHub
3 changed files with 147 additions and 7 deletions
@@ -617,6 +617,45 @@ early_exit:
{0x7e020280u, 0xbf06c002u, 0xbf850003u, 0x4a020081u, 0xbf068003u, 0xbf850006u, 0x7d880088u, 0xbf870001u, 0x4a020282u, 0xe0700000u,
0x80000100u, 0xbf810000u, 0xbefe0480u, 0xbf810000u},
Split::None},
{"return block entered from the return block after it", R"(
v_mov_b32 v1, 0
s_cmp_eq_u32 s2, 64
s_cbranch_scc0 later
v_add_nc_u32 v1, 1, v0
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
middle:
v_add_nc_u32 v1, 2, v0
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
later:
s_cmp_eq_u32 s3, 0
s_cbranch_scc1 middle
v_add_nc_u32 v1, 3, v0
buffer_store_dword v1, off, s[0:3], 0
s_endpgm)",
{0x7e020280u, 0xbf06c002u, 0xbf840008u, 0x4a020081u, 0xe0700000u, 0x80000100u, 0xbf810000u, 0x4a020082u, 0xe0700000u, 0x80000100u,
0xbf810000u, 0xbf068003u, 0xbf85fffau, 0x4a020083u, 0xe0700000u, 0x80000100u, 0xbf810000u},
Split::None},
{"loop body between an early return and the loop test", R"(
s_mov_b32 s8, 0
v_mov_b32 v1, 0
s_cmp_eq_u32 s2, 64
s_cbranch_scc0 test
v_add_nc_u32 v1, 1, v0
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
body:
v_add_nc_u32 v1, 2, v1
s_add_u32 s8, s8, 16
test:
s_cmp_lt_u32 s8, 64
s_cbranch_scc1 body
buffer_store_dword v1, off, s[0:3], 0
s_endpgm)",
{0xbe880380u, 0x7e020280u, 0xbf06c002u, 0xbf840006u, 0x4a020081u, 0xe0700000u, 0x80000100u, 0xbf810000u, 0x4a020282u, 0x80089008u,
0xbf0ac008u, 0xbf85fffcu, 0xe0700000u, 0x80000100u, 0xbf810000u},
Split::None},
};
int failures = 0;
for (const auto& program : programs) {
@@ -0,0 +1,103 @@
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "Recompiler.hpp"
#include "VulkanTestDevice.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>
namespace {
using AgcDriver::Graphics::Require;
using ShaderRecompiler::ShaderStage;
constexpr std::uint32_t Threads = 64;
constexpr std::uint32_t Stride = 4;
constexpr std::uint32_t SelectorRegister = 8;
alignas(256) std::array<std::uint32_t, Threads * Stride> Input{};
alignas(256) std::array<std::uint32_t, Threads * Stride> Output{};
alignas(256) constexpr std::array<std::uint32_t, 20> TailBlockCode{
0x34020082, 0xe0302000, 0x80000401, 0xbf8c3f70, 0xbf068008, 0xbf840008, 0x4a080881, 0xe0702000, 0x80010401, 0xbf810000,
0x4a080882, 0xe0702000, 0x80010401, 0xbf810000, 0xbf068108, 0xbf85fffa, 0x4a080883, 0xe0702000, 0x80010401, 0xbf810000,
};
alignas(256) constexpr std::array<std::uint32_t, 18> LoopBodyCode{
0x34020082, 0xe0302000, 0x80000401, 0xbf8c3f70, 0xbe890380, 0xbf068008, 0xbf840006, 0x4a0808c0, 0xe0702000,
0x80010401, 0xbf810000, 0x4a080881, 0x80098109, 0xbf0a0809, 0xbf85fffc, 0xe0702000, 0x80010401, 0xbf810000,
};
struct Case {
const char* name;
std::span<const std::uint32_t> code;
std::uint32_t selector;
std::uint32_t added;
};
const std::array<Case, 6> Cases{{
{"return before the tail blocks", TailBlockCode, 0u, 1u},
{"tail block entered from the tail block after it", TailBlockCode, 1u, 2u},
{"last tail block", TailBlockCode, 2u, 3u},
{"return before the loop", LoopBodyCode, 0u, 64u},
{"loop body entered once from the test after it", LoopBodyCode, 1u, 1u},
{"loop body entered three times from the test after it", LoopBodyCode, 3u, 3u},
}};
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t count) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu) | (4u << 16u), count, 0x01016facu};
}
void Run(AgcDriver::VulkanDevice& device, const Case& test) {
for (std::uint32_t tid = 0; tid < Threads; ++tid) Input[tid * Stride] = tid * 0x01010101u + 7u;
Output.fill(0xdeadbeefu);
std::vector<std::uint32_t> userData(SelectorRegister + 1u, 0u);
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size()));
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size()));
std::copy(input.begin(), input.end(), userData.begin());
std::copy(output.begin(), output.end(), userData.begin() + 4);
userData[SelectorRegister] = test.selector;
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(test.code.data()), std::as_bytes(test.code)}}};
const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1};
ShaderRecompiler::RecompileRequest request{
{ShaderStage::Compute, reinterpret_cast<std::uintptr_t>(test.code.data()), test.code, 0, {}},
{32, 0, userData, compute, std::nullopt, std::nullopt, memory},
device.Target(),
{0, 0, 0, 128}
};
request.useCache = false;
const auto result = ShaderRecompiler::Recompile(request);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(test.code.data()));
device.WaitIdle();
}
void Check(const Case& test) {
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
const auto expected = Input[tid * Stride] + test.added;
const auto actual = Output[tid * Stride];
Require(actual == expected, std::string("branch past endpgm: ") + test.name + ": thread " + std::to_string(tid) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected));
}
}
}
int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
for (const auto& test : Cases) {
Run(*device, test);
Check(test);
}
std::puts("branch past endpgm tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
@@ -5,6 +5,7 @@
#include "RdnaDecoder/RdnaOpcode.hpp"
#include "RdnaDecoder/RdnaScalarOpDecoder.hpp"
#include "RdnaDecoder/RdnaVectorOpDecoder.hpp"
#include <algorithm>
#include <bit>
#include <cstdio>
#include <stdexcept>
@@ -151,7 +152,7 @@ void DecodeRdnaProgram(std::span<const std::uint32_t> code, RdnaProgram& program
program.instructions.reserve(code.size());
program.code = code;
std::vector<bool> branchTargets;
std::uint32_t furthestBranchTarget = 0;
for (std::uint32_t wordIndex = 0; wordIndex < code.size();) {
const std::uint32_t programCounter = wordIndex * static_cast<std::uint32_t>(sizeof(std::uint32_t));
program.instructions.push_back(DecodeRdnaInstruction(programCounter, code, wordIndex));
@@ -161,16 +162,13 @@ void DecodeRdnaProgram(std::span<const std::uint32_t> code, RdnaProgram& program
if (IsDirectBranchOpcode(instruction.op)) {
const std::uint32_t targetIndex = instruction.branchTarget / static_cast<std::uint32_t>(sizeof(std::uint32_t));
if (branchTargets.empty()) {
branchTargets.resize(code.size());
}
if (targetIndex >= branchTargets.size()) {
if (targetIndex >= code.size()) {
throw std::out_of_range("branch target is out of the code span bounds");
}
branchTargets[targetIndex] = true;
furthestBranchTarget = std::max(furthestBranchTarget, targetIndex);
}
if (instruction.op == RdnaOpcode::SEndpgm && (wordIndex >= code.size() || branchTargets.empty() || !branchTargets[wordIndex])) {
if (instruction.op == RdnaOpcode::SEndpgm && furthestBranchTarget < wordIndex) {
return;
}
}