Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdio>
#include <exception>
#include <fstream>
#include <initializer_list>
#include <map>
#include <span>
#include <sstream>
Expand Down Expand Up @@ -208,6 +209,80 @@ void verifyProgram(const Program& program) {
}
}

const std::vector<std::uint32_t> DwordTableGetpcFirst{
0xbe8c1f00u, 0x800cff0cu, 0x00000058u, 0x820d800du, 0x83928202u, 0xd4c40016u, 0x02000500u, 0x8f128212u, 0xf4000386u, 0x24000000u,
0xbf8cc07fu, 0x7d820003u, 0x808c0e0cu, 0x828d800du, 0xbefd210cu, 0x7e020281u, 0xbf820003u, 0x7e020282u, 0xbf820001u, 0x7e020283u,
0xe0700000u, 0x80000100u, 0xbf810000u, 0x00000020u, 0x00000018u, 0x00000010u};
const std::vector<std::uint32_t> DwordTableIndexFirst{
0x816ac102u, 0x83ea826au, 0x7e020280u, 0x8f6a826au, 0xbe8e1f00u, 0x800ebc0eu, 0x820f800fu, 0xf4000487u, 0xd4000008u, 0x8aea167eu,
0xbf8cc07fu, 0x808e120eu, 0x828f800fu, 0xbefd210eu, 0x7e020281u, 0xbf820003u, 0x7e020282u, 0xbf820001u, 0x7e020283u, 0xe0700000u,
0x80000100u, 0xbf810000u, 0x00000018u, 0x00000010u, 0x00000008u};
const std::vector<std::uint32_t> LongBranchVcc{
0x7e020281u, 0xbeea1f00u, 0x806a906au, 0x826b806bu, 0xbefd216au, 0x7e020282u, 0xe0700000u, 0x80000100u, 0xbf810000u};
const std::vector<std::uint32_t> LongBranchBack{
0xbe880380u, 0x7e020280u, 0x4a020281u, 0x80089008u, 0xbf0ac008u, 0xbf840004u, 0xbe901f00u, 0x80909410u, 0x82918011u, 0xbefd2110u,
0xe0700000u, 0x80000100u, 0xbf810000u};

std::vector<std::uint32_t> patched(std::vector<std::uint32_t> code, std::size_t word, std::initializer_list<std::uint32_t> replacement) {
std::copy(replacement.begin(), replacement.end(), code.begin() + static_cast<std::ptrdiff_t>(word));
return code;
}

std::string refusal(std::span<const std::uint32_t> code) {
try {
static_cast<void>(GraphBuilder{}.Build(RdnaInstructionDecoder{}.Decode(code)));
} catch (const std::exception& error) {
return error.what();
}
return {};
}

void verifyJumpTable(const std::string& name, std::span<const std::uint32_t> code, std::uint32_t loadPc, const std::vector<std::uint64_t>& values, const std::vector<std::uint32_t>& targets) {
const auto graph = GraphBuilder{}.Build(RdnaInstructionDecoder{}.Decode(code));
require(graph.codeTableLoads.size() == 1u && graph.codeTableLoads.front().programCounter == loadPc && graph.codeTableLoads.front().values == values, name + ": wrong code table");
std::vector<std::uint32_t> lowered;
for (const auto& block : graph.blocks) {
if (block.terminator.indirectPcSgpr != InvalidControlFlowId) lowered.insert(lowered.end(), block.terminator.indirectTargetProgramCounters.begin(), block.terminator.indirectTargetProgramCounters.end());
}
std::sort(lowered.begin(), lowered.end());
require(lowered == targets, name + ": the jump does not reach exactly the table targets");
}

void verifyLongBranch(const std::string& name, std::span<const std::uint32_t> code, std::uint32_t branchPc, std::uint32_t target) {
const auto decoded = RdnaInstructionDecoder{}.Decode(code);
const auto graph = GraphBuilder{}.Build(decoded);
const auto source = std::find_if(graph.blocks.begin(), graph.blocks.end(), [&](const BasicBlock& block) { return block.instructionEnd != block.instructionBegin && decoded.instructions[block.instructionEnd - 1u].programCounter == branchPc; });
require(source != graph.blocks.end() && source->terminator.kind == TerminatorKind::Branch && graph.FindBlock(source->terminator.trueBlock).startProgramCounter == target, name + ": the jump does not branch to " + std::to_string(target));
}

void verifyNullSwappc() {
const std::array<std::uint32_t, 2> jump{0xbefd210cu, 0xbf810000u};
const auto swappc = RdnaInstructionDecoder{}.Decode(jump).instructions.front();
require(swappc.op == RdnaOpcode::SSetpcB64 && swappc.opcodeId == 0x21u && swappc.destination.kind == RdnaOperandKind::Null &&
swappc.source0.kind == RdnaOperandKind::ScalarRegister && swappc.source0.reg == 12u, "s_swappc_b64 null, s[12:13] does not decode as s_setpc_b64 s[12:13]");
const std::array<std::uint32_t, 3> front{0x7e020281u, 0xbefd2106u, 0xbf810000u};
require(DecodeRdnaFrontProgram(front).code.size() == 2u, "a front program ending in s_swappc_b64 null, s[6:7] does not end there");
verifyJumpTable("dword jump table after its base", DwordTableGetpcFirst, 0x20u, {0x20u, 0x18u, 0x10u}, {0x3cu, 0x44u, 0x4cu});
verifyJumpTable("dword jump table after its index", DwordTableIndexFirst, 0x1cu, {0x18u, 0x10u, 0x08u}, {0x38u, 0x40u, 0x48u});
verifyLongBranch("long branch through vcc", LongBranchVcc, 0x10u, 0x18u);
verifyLongBranch("backward long branch", LongBranchBack, 0x24u, 0x08u);
const std::array<std::uint32_t, 3> call{0xbe8c1f00u, 0xbe8e210cu, 0xbf810000u};
const std::vector<std::tuple<std::string_view, std::vector<std::uint32_t>, std::string_view>> refused{
{"s_swappc_b64 with a return address", {call.begin(), call.end()}, "unsupported SOP1 opcode 33"},
{"index rewritten after the bound", patched(DwordTableGetpcFirst, 5, {0xbe9203ffu, 0x00000040u}), "unsupported dynamic s_setpc_b64"},
{"index scaled for two-dword entries", patched(DwordTableGetpcFirst, 7, {0x8f128312u}), "unsupported dynamic s_setpc_b64"},
{"entry rewritten after the load", patched(DwordTableGetpcFirst, 11, {0xbe8e0380u}), "unsupported dynamic s_setpc_b64"},
{"base rewritten after the load", patched(DwordTableGetpcFirst, 11, {0xbe8d0380u}), "unsupported dynamic s_setpc_b64"},
{"borrow other than zero", patched(DwordTableGetpcFirst, 13, {0x828d810du}), "unsupported dynamic s_setpc_b64"},
{"long branch with a register high half", patched(LongBranchVcc, 3, {0x826b046bu}), "unsupported dynamic s_setpc_b64"},
{"branch into a long branch", patched(LongBranchVcc, 0, {0xbf850001u}), "does not start in its block"},
};
for (const auto& [name, code, message] : refused) {
const auto error = refusal(code);
require(error.find(message) != std::string::npos, std::string(name) + ": expected a refusal with '" + std::string(message) + "', got '" + error + "'");
}
}

}

int main(int argc, char** argv) {
Expand Down Expand Up @@ -656,6 +731,92 @@ int main(int argc, char** argv) {
{0xbe880380u, 0x7e020280u, 0xbf06c002u, 0xbf840006u, 0x4a020081u, 0xe0700000u, 0x80000100u, 0xbf810000u, 0x4a020282u, 0x80089008u,
0xbf0ac008u, 0xbf85fffcu, 0xe0700000u, 0x80000100u, 0xbf810000u},
Split::None},
{"dword jump table after its base", R"(
s_getpc_b64 s[12:13]
base:
s_add_u32 s12, s12, table - base
s_addc_u32 s13, s13, 0
s_min_u32 s18, s2, 2
v_cmp_gt_u32 s[22:23], v0, s2
s_lshl_b32 s18, s18, 2
s_load_dword s14, s[12:13], s18
s_waitcnt lgkmcnt(0)
v_cmp_lt_u32 vcc, s3, v0
s_sub_u32 s12, s12, s14
s_subb_u32 s13, s13, 0
s_swappc_b64 null, s[12:13]
case0:
v_mov_b32 v1, 1
s_branch done
case1:
v_mov_b32 v1, 2
s_branch done
case2:
v_mov_b32 v1, 3
done:
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
table:
.long table - case0, table - case1, table - case2)",
DwordTableGetpcFirst, Split::None},
{"dword jump table after its index", R"(
s_add_i32 vcc_lo, s2, -1
s_min_u32 vcc_lo, vcc_lo, 2
v_mov_b32 v1, 0
s_lshl_b32 vcc_lo, vcc_lo, 2
s_getpc_b64 s[14:15]
base:
s_add_u32 s14, s14, table - base - 8
s_addc_u32 s15, s15, 0
s_load_dword s18, s[14:15], vcc_lo offset:8
s_andn2_b64 vcc, exec, s[22:23]
s_waitcnt lgkmcnt(0)
s_sub_u32 s14, s14, s18
s_subb_u32 s15, s15, 0
s_swappc_b64 null, s[14:15]
case0:
v_mov_b32 v1, 1
s_branch done
case1:
v_mov_b32 v1, 2
s_branch done
case2:
v_mov_b32 v1, 3
done:
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
table:
.long table - 8 - case0, table - 8 - case1, table - 8 - case2)",
DwordTableIndexFirst, Split::None},
{"long branch through vcc", R"(
v_mov_b32 v1, 1
s_getpc_b64 vcc
base:
s_add_u32 vcc_lo, vcc_lo, target - base
s_addc_u32 vcc_hi, vcc_hi, 0
s_swappc_b64 null, vcc
v_mov_b32 v1, 2
target:
buffer_store_dword v1, off, s[0:3], 0
s_endpgm)",
LongBranchVcc, Split::None},
{"backward long branch", R"(
s_mov_b32 s8, 0
v_mov_b32 v1, 0
loop:
v_add_nc_u32 v1, 1, v1
s_add_u32 s8, s8, 16
s_cmp_lt_u32 s8, 64
s_cbranch_scc0 done
s_getpc_b64 s[16:17]
base:
s_sub_u32 s16, s16, base - loop
s_subb_u32 s17, s17, 0
s_swappc_b64 null, s[16:17]
done:
buffer_store_dword v1, off, s[0:3], 0
s_endpgm)",
LongBranchBack, Split::None},
};
int failures = 0;
for (const auto& program : programs) {
Expand All @@ -666,6 +827,12 @@ int main(int argc, char** argv) {
++failures;
}
}
try {
verifyNullSwappc();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
++failures;
}
if (failures != 0) return 1;
std::puts("Control flow structurization tests passed");
return 0;
Expand Down
143 changes: 143 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/execution/ScalarSwappc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#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 MaxThreads = 64;
constexpr std::uint32_t Inputs = 4;
constexpr std::uint32_t Results = 4;
alignas(256) std::array<std::uint32_t, MaxThreads * Inputs> Input{};
alignas(256) std::array<std::uint32_t, MaxThreads * Results> Output{};

alignas(256) constexpr std::array<std::uint32_t, 109> Wave32Code{
0x34020084u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x7e1402ffu, 0xdeadbeefu, 0x7e1602ffu, 0xdeadbeefu,
0x7e1802ffu, 0xdeadbeefu, 0x7e1a02ffu, 0xdeadbeefu, 0xbe940380u, 0xd7600008u, 0x02002904u, 0xd760000au,
0x02002905u, 0xbe8c1f00u, 0x800cff0cu, 0x0000014cu, 0x820d800du, 0xbe89030au, 0x83928308u, 0xd4c40016u,
0x02001104u, 0x8f128212u, 0xf4000386u, 0x24000000u, 0xbf8cc07fu, 0x7d820a0au, 0x808c0e0cu, 0x828d800du,
0xbefd210cu, 0x8009ff0au, 0x11110000u, 0xbf820008u, 0x8009ff0au, 0x22220000u, 0xbf820005u, 0x8009ff0au,
0x33330000u, 0xbf820002u, 0x8009ff0au, 0x44440000u, 0xd761000au, 0x02002809u, 0x816ac108u, 0x83ea836au,
0x8f6a826au, 0xbe8e1f00u, 0x800eff0eu, 0x000000d4u, 0x820f800fu, 0xf4000487u, 0xd4000008u, 0x8aea167eu,
0xbf8cc07fu, 0x808e120eu, 0x828f800fu, 0xbefd210eu, 0x8009ff0au, 0x0a000000u, 0xbf820008u, 0x8009ff0au,
0x0b000000u, 0xbf820005u, 0x8009ff0au, 0x0c000000u, 0xbf820002u, 0x8009ff0au, 0x0d000000u, 0xd761000bu,
0x02002809u, 0x8009ff0au, 0x600d0000u, 0xbeea1f00u, 0x806a946au, 0x826b806bu, 0xbefd216au, 0xbe8903ffu,
0x00000badu, 0xd761000cu, 0x02002809u, 0xbe890380u, 0xbe950383u, 0x80090a09u, 0x80958115u, 0xbf068015u,
0xbf850004u, 0xbe901f00u, 0x80909410u, 0x82918011u, 0xbefd2110u, 0xd761000du, 0x02002809u, 0x80148114u,
0xbf0aa014u, 0xbf85ffabu, 0xe0781000u, 0x80010a01u, 0xbf810000u, 0x00000110u, 0x00000104u, 0x000000f8u,
0x000000ecu, 0x000000acu, 0x000000a0u, 0x00000094u, 0x00000088u,
};

alignas(256) constexpr std::array<std::uint32_t, 109> Wave64Code{
0x34020084u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x7e1402ffu, 0xdeadbeefu, 0x7e1602ffu, 0xdeadbeefu,
0x7e1802ffu, 0xdeadbeefu, 0x7e1a02ffu, 0xdeadbeefu, 0xbe940380u, 0xd7600008u, 0x02002904u, 0xd760000au,
0x02002905u, 0xbe8c1f00u, 0x800cff0cu, 0x0000014cu, 0x820d800du, 0xbe89030au, 0x83928308u, 0xd4c40016u,
0x02001104u, 0x8f128212u, 0xf4000386u, 0x24000000u, 0xbf8cc07fu, 0x7d820a0au, 0x808c0e0cu, 0x828d800du,
0xbefd210cu, 0x8009ff0au, 0x11110000u, 0xbf820008u, 0x8009ff0au, 0x22220000u, 0xbf820005u, 0x8009ff0au,
0x33330000u, 0xbf820002u, 0x8009ff0au, 0x44440000u, 0xd761000au, 0x02002809u, 0x816ac108u, 0x83ea836au,
0x8f6a826au, 0xbe8e1f00u, 0x800eff0eu, 0x000000d4u, 0x820f800fu, 0xf4000487u, 0xd4000008u, 0x8aea167eu,
0xbf8cc07fu, 0x808e120eu, 0x828f800fu, 0xbefd210eu, 0x8009ff0au, 0x0a000000u, 0xbf820008u, 0x8009ff0au,
0x0b000000u, 0xbf820005u, 0x8009ff0au, 0x0c000000u, 0xbf820002u, 0x8009ff0au, 0x0d000000u, 0xd761000bu,
0x02002809u, 0x8009ff0au, 0x600d0000u, 0xbeea1f00u, 0x806a946au, 0x826b806bu, 0xbefd216au, 0xbe8903ffu,
0x00000badu, 0xd761000cu, 0x02002809u, 0xbe890380u, 0xbe950383u, 0x80090a09u, 0x80958115u, 0xbf068015u,
0xbf850004u, 0xbe901f00u, 0x80909410u, 0x82918011u, 0xbefd2110u, 0xd761000du, 0x02002809u, 0x80148114u,
0xbf0ac014u, 0xbf85ffabu, 0xe0781000u, 0x80010a01u, 0xbf810000u, 0x00000110u, 0x00000104u, 0x000000f8u,
0x000000ecu, 0x000000acu, 0x000000a0u, 0x00000094u, 0x00000088u,
};

constexpr std::array<std::uint32_t, 8> Selectors{0u, 1u, 2u, 3u, 4u, 7u, 0x80000000u, 0xffffffffu};
constexpr std::array<std::uint32_t, 4> GetpcFirstCases{0x11110000u, 0x22220000u, 0x33330000u, 0x44440000u};
constexpr std::array<std::uint32_t, 4> IndexFirstCases{0x0a000000u, 0x0b000000u, 0x0c000000u, 0x0d000000u};
constexpr const char* Names[Results] = {"dword_table_getpc_first", "dword_table_index_first", "long_branch_vcc", "long_branch_back"};

std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((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;
}

std::uint32_t Expected(std::uint32_t lane, std::uint32_t result) {
const auto selector = Input[lane * Inputs];
const auto value = Input[lane * Inputs + 1u];
switch (result) {
case 0: return value + GetpcFirstCases[std::min(selector, 3u)];
case 1: return value + IndexFirstCases[std::min(selector - 1u, 3u)];
case 2: return value + 0x600d0000u;
default: return value * 3u;
}
}

ShaderRecompiler::RecompileResult Compile(std::span<const std::uint32_t> code, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target) {
std::vector<std::uint32_t> userData(8, 0u);
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u));
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
std::copy(input.begin(), input.end(), userData.begin());
std::copy(output.begin(), output.end(), userData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
const ShaderRecompiler::ShaderComputeStageInfo compute{{waveSize, 1, 1}, 0u, {false, false, false}, false, 1};
ShaderRecompiler::RecompileRequest request{
{ShaderStage::Compute, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}},
{waveSize, 0, userData, compute, std::nullopt, std::nullopt, memory},
target,
{0, 0, 0, 128}
};
request.useCache = false;
return ShaderRecompiler::Recompile(request);
}

void Run(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target) {
for (std::uint32_t tid = 0; tid < MaxThreads; ++tid) {
Input[tid * Inputs] = Selectors[tid % Selectors.size()];
Input[tid * Inputs + 1u] = 0x9e3779b9u * (tid + 1u);
}
Output.fill(0xdeadbeefu);
const auto result = Compile(code, waveSize, target);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
}

void Check(std::uint32_t lanes) {
for (std::uint32_t tid = 0; tid < lanes; ++tid) {
for (std::uint32_t index = 0; index < Results; ++index) {
const auto actual = Output[tid * Results + index];
const auto expected = Expected(tid, index);
Require(actual == expected, "swappc wave" + std::to_string(lanes) + ": lane " + std::to_string(tid) + " " + Names[index] + " is " + Hex(actual) + ", expected " + Hex(expected));
}
}
}

}

int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
Run(*device, Wave32Code, 32, device->ComputeTarget(32));
Check(32);
Run(*device, Wave64Code, 64, device->Target());
Check(64);
Run(*device, Wave64Code, 64, device->ComputeTarget(32));
Check(64);
std::puts("swappc tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
Loading
Loading