Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ struct OwnedRecompileRequest {
std::vector<std::byte> header;
std::vector<std::uint32_t> userData;
std::vector<std::uint32_t> fallbacks;
std::vector<std::uint32_t> tableLiterals;
std::vector<std::uint32_t> splitState;
std::vector<std::vector<std::byte>> regionBytes;
std::vector<ShaderRecompiler::MemoryRegion> regions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_SHADERS_TABLECALLEETARGETS_HPP
#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_SHADERS_TABLECALLEETARGETS_HPP

#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include <utility>
#include <vector>

namespace AgcDriver::DriverDetail {

class TableCalleeTargets {
public:
using Target = std::pair<std::uint64_t, std::uint32_t>;

explicit TableCalleeTargets(std::size_t maxTargets) : maxTargets(maxTargets) {
targetOffsets.reserve(maxTargets + 1u);
}

void Add(std::uint64_t target, std::uint32_t offset) {
if (target == 0u || tooManyTargets) return;
const auto [known, inserted] = targetOffsets.try_emplace(target, offset);
if (!inserted) return;
targets.emplace_back(target, known->second);
tooManyTargets = targets.size() > maxTargets;
}

[[nodiscard]] bool TooManyTargets() const { return tooManyTargets; }
[[nodiscard]] const std::vector<Target>& Targets() const { return targets; }

private:
std::size_t maxTargets;
std::unordered_map<std::uint64_t, std::uint32_t> targetOffsets;
std::vector<Target> targets;
bool tooManyTargets = false;
};

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "prx/libSceAgcDriver/Execution/include/Driver/DriverMemory.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/TableCalleeTargets.hpp"
#include "prx/libSceAgcDriver/Graphics/include/PipelineLibrary.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
Expand Down Expand Up @@ -55,7 +56,6 @@
#include <stdexcept>
#include <thread>
#include <tuple>
#include <unordered_map>
#include "Optimization/ShaderStageInputInfo.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/RegistrationPreparationBudget.hpp"
#include "prx/libSceAgcDriver/Execution/include/PerformanceOverlay.hpp"
Expand Down Expand Up @@ -1095,7 +1095,8 @@ std::vector<ShaderRecompiler::TableCallee> ReadTableCallees(const ShaderRecompil
if (bytes > limitBytes) throw std::runtime_error("its call table of " + std::to_string(bytes) + " bytes is larger than the " + std::to_string(limitBytes) + " bytes that are searched for callees");
const auto table = bytes == 0u ? std::vector<std::uint8_t>{} : ReadGuestBytes(descriptor.Base48(), bytes, "call table", dependencies, false);
auto dependency = std::make_shared<ShaderRecompiler::SrtGuestWords>();
std::set<std::uint64_t> targets;
const auto maxCallees = split ? splitCallees : limitCallees;
TableCalleeTargets targets(maxCallees);
for (std::uint64_t offset = call.entryOffset; offset + 8u <= bytes; offset += call.entryStride) {
std::uint64_t target = 0;
std::memcpy(&target, table.data() + offset, sizeof(target));
Expand All @@ -1106,7 +1107,7 @@ std::vector<ShaderRecompiler::TableCallee> ReadTableCallees(const ShaderRecompil
} else {
dependency->runs.emplace_back(address, 2u);
}
if (target != 0u) targets.insert(target);
targets.Add(target, static_cast<std::uint32_t>(offset));
if (call.entryStride == 0u) break;
}
for (const auto& [address, count] : dependency->runs) {
Expand All @@ -1115,10 +1116,13 @@ std::vector<ShaderRecompiler::TableCallee> ReadTableCallees(const ShaderRecompil
std::memcpy(dependency->words.data() + offset, table.data() + address - descriptor.Base48(), count * sizeof(std::uint32_t));
}
RecordLinkDependency(std::move(dependency), dependencies);
if (split && targets.size() > splitCallees) throw std::runtime_error("its call table holds " + std::to_string(targets.size()) + " distinct targets; at most " + std::to_string(splitCallees) + " are split");
if (!split && targets.size() > limitCallees) throw std::runtime_error("its call table holds " + std::to_string(targets.size()) + " distinct targets; at most " + std::to_string(limitCallees) + " are linked");
if (targets.TooManyTargets()) {
const auto mode = split ? "split" : "linked";
throw std::runtime_error("its call table has more than " + std::to_string(maxCallees)
+ " distinct targets; at most " + std::to_string(maxCallees) + " are " + mode);
}
std::vector<ShaderRecompiler::TableCallee> callees;
for (const auto target : targets) callees.push_back({target, ReadUserDataCallee(target, call.linkRegister, dependencies, true)});
for (const auto& [target, offset] : targets.Targets()) callees.push_back({target, ReadUserDataCallee(target, call.linkRegister, dependencies, true), offset});
return callees;
}

Expand Down Expand Up @@ -1340,6 +1344,8 @@ std::shared_ptr<OwnedRecompileRequest> OwnRecompileRequest(const ShaderRecompile
owned->request.context.userData = owned->userData;
owned->fallbacks.assign(source.context.linkedCallFallbacks.begin(), source.context.linkedCallFallbacks.end());
owned->request.context.linkedCallFallbacks = owned->fallbacks;
owned->tableLiterals.assign(source.context.linkedTableLiterals.begin(), source.context.linkedTableLiterals.end());
owned->request.context.linkedTableLiterals = owned->tableLiterals;
owned->regionBytes.reserve(memory.size());
owned->regions.reserve(memory.size());
for (const auto& region : memory) {
Expand Down Expand Up @@ -1435,6 +1441,7 @@ std::shared_ptr<SplitTableCallParts> SplitTableCallPartsFor(const ShaderRecompil
for (const auto& callee : callees) {
key.push_back(callee.target);
key.push_back(HashWords(0xcbf29ce484222325ull, callee.code));
key.push_back(callee.entryOffset ? (static_cast<std::uint64_t>(*callee.entryOffset) << 1u) | 1u : 0u);
}
static std::mutex mutex;
static std::map<std::vector<std::uint64_t>, std::shared_ptr<SplitTableCallParts>> known;
Expand All @@ -1443,10 +1450,10 @@ std::shared_ptr<SplitTableCallParts> SplitTableCallPartsFor(const ShaderRecompil
if (const auto found = known.find(key); found != known.end()) return found->second;
for (const auto& [built, builtParts] : known) {
if (built.size() < key.size() || !std::equal(key.begin(), key.begin() + 4, built.begin())) continue;
std::set<std::pair<std::uint64_t, std::uint64_t>> held;
for (std::size_t index = 4; index + 1 < built.size(); index += 2) held.emplace(built[index], built[index + 1]);
std::set<std::tuple<std::uint64_t, std::uint64_t, std::uint64_t>> held;
for (std::size_t index = 4; index + 2 < built.size(); index += 3) held.emplace(built[index], built[index + 1], built[index + 2]);
bool covered = true;
for (std::size_t index = 4; covered && index + 1 < key.size(); index += 2) covered = held.contains({key[index], key[index + 1]});
for (std::size_t index = 4; covered && index + 2 < key.size(); index += 3) covered = held.contains({key[index], key[index + 1], key[index + 2]});
if (covered) return builtParts;
}
}
Expand Down Expand Up @@ -1517,6 +1524,7 @@ ShaderRecompiler::PreparedShaderInvocation SplitTableCallPartInvocation(SplitTab
source.shader.code = parts.programs[part].code;
source.context.splitState = parts.programs[part].plan;
source.context.linkedCallFallbacks = {};
source.context.linkedTableLiterals = parts.programs[part].tableLiterals;
std::shared_ptr<const ShaderRecompiler::SourceHandle> handle;
{
std::lock_guard lock(parts.mutex);
Expand Down Expand Up @@ -1733,6 +1741,7 @@ ShaderRecompiler::PreparedShaderInvocation InvocationFor(const ShaderSnapshot& s
if (!linked.code.empty()) {
source.shader.code = linked.code;
source.context.linkedCallFallbacks = linked.fallbacks;
source.context.linkedTableLiterals = linked.tableLiterals;
}
std::unique_lock lock(snapshot.prepared->mutex);
AwaitRegisteredPreparation(*snapshot.prepared, lock);
Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9295,7 +9295,7 @@ void verifyPixelRequestSerialization() {
for (std::size_t bytes = 0; bytes < 45u; ++bytes) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(requestPrefix(encoded, mappingOffset + bytes))); }, "truncated data", "a truncated pixel mapping, packing, dual-source, alpha-to-coverage, stencil reference export flag, primitive class, layer export, dither or front-face flag, quad pixel mask or quad sample mask was accepted");
}
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQTIAAAA=", "NVNQQToAAAA=", "NVNQQT0AAAA=", "NVNQQUEAAAA="}) {
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQTIAAAA=", "NVNQQToAAAA=", "NVNQQT0AAAA=", "NVNQQUIAAAA="}) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(unsupported)); }, "serialization version", "an unsupported request version was accepted");
}
static constexpr std::string_view version18Request{
Expand Down
42 changes: 38 additions & 4 deletions core/libs/prx/libSceAgcDriver/tests/SplitTableCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ RecompileResult RecompileSplit(const SplitTableCallProgram& part, std::span<cons
request.context.userData = userData;
request.context.compute = ShaderComputeStageInfo{{64u, 1u, 1u}, 0u, {true, false, false}, false, 1u};
request.context.splitState = part.plan;
request.context.linkedTableLiterals = part.tableLiterals;
request.useCache = false;
return Recompile(request);
}
Expand All @@ -98,15 +99,48 @@ void CheckSplitPrograms() {
const SwappcInfo swappc{false, 0u, 16u};
const auto calls = FindTableCalls(program, swappc);
Check(calls.size() == 1u, "the waterfall caller holds no table call");
const std::vector<TableCallee> callees{{0x1000u, {0xbf068000u, 0x4a020285u, 0xbe80200eu}}, {0x2000u, {0x4a0202c0u, 0xbf068001u, 0xbe80200eu}}};
auto table = calls[0];
table.userDataRegister = 0u;
table.descriptorOffset = 0u;
table.constantPointer.reset();
table.descriptorInUserData = false;
table.nestedDescriptorOffset.reset();
const std::vector<TableCallee> callees{
{0x1000u, {0xbf068000u, 0x4a020285u, 0xbe80200eu}, 0x20u},
{0x2000u, {0x4a0202c0u, 0xbf068001u, 0xbe80200eu}, 0x24u},
};
std::string refusal;
const auto split = SplitTableCall(program, calls[0], callees, 32u, refusal);
const auto split = SplitTableCall(program, table, callees, 32u, refusal);
Check(split.has_value(), "the waterfall caller cannot split: " + refusal);
const auto programs = BuildSplitTableCallPrograms(program, calls[0], callees, *split, 4u);
const auto programs = BuildSplitTableCallPrograms(program, table, callees, *split, 4u);
const std::array<std::uint32_t, 16> userData{};
CheckSplitModule(RecompileSplit(programs.entry, userData, 0x710000u), "entry");
for (std::size_t callee = 0; callee < programs.callees.size(); ++callee) CheckSplitModule(RecompileSplit(programs.callees[callee], userData, 0x720000u + callee * 0x1000u), "callee " + std::to_string(callee));
for (std::size_t callee = 0; callee < programs.callees.size(); ++callee) {
const auto& literals = programs.callees[callee].tableLiterals;
Check(literals.size() == TableLiteralWords * 4u,
"callee " + std::to_string(callee) + ": runtime table literal metadata was dropped");
Check(literals[7u] == *callees[callee].entryOffset && literals[15u] == *callees[callee].entryOffset + 4u
&& literals[23u] == *callees[callee].entryOffset && literals[31u] == *callees[callee].entryOffset + 4u,
"callee " + std::to_string(callee) + ": runtime table entry offsets were not preserved");
CheckSplitModule(RecompileSplit(programs.callees[callee], userData, 0x720000u + callee * 0x1000u), "callee " + std::to_string(callee));
}
CheckSplitModule(RecompileSplit(programs.resume, userData, 0x730000u), "resume");

TableCallContinuation continuation{};
continuation.headerProgramCounter = program.instructions[2].programCounter;
continuation.replayProgramCounter = program.instructions[2].programCounter;
continuation.callProgramCounter = table.programCounter;
const auto continued = BuildTableCallContinuation(program, table, callees, continuation);
for (std::size_t callee = 0; callee < continued.callees.size(); ++callee) {
const auto& literals = continued.callees[callee].tableLiterals;
Check(literals.size() == TableLiteralWords * 4u,
"continued callee " + std::to_string(callee) + ": runtime table literal metadata was dropped");
Check(literals[7u] == *callees[callee].entryOffset && literals[15u] == *callees[callee].entryOffset + 4u
&& literals[23u] == *callees[callee].entryOffset && literals[31u] == *callees[callee].entryOffset + 4u,
"continued callee " + std::to_string(callee) + ": runtime table entry offsets were not preserved");
CheckSplitModule(RecompileSplit(continued.callees[callee], userData, 0x740000u + callee * 0x1000u),
"continued callee " + std::to_string(callee));
}
}

struct Interpolation {
Expand Down
69 changes: 69 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/TableCalleeTargets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "../Execution/include/Driver/Shaders/TableCalleeTargets.hpp"
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>

namespace {

void Check(bool condition, const std::string& message) {
if (!condition) throw std::runtime_error(message);
}

void CheckLargeTable() {
constexpr std::size_t entryCount = 65536u;
constexpr std::size_t entryStride = 16u;
std::vector<std::uint8_t> table(entryCount * entryStride);
for (std::size_t index = 0; index < entryCount; ++index) {
const auto target = 0x100000000ull + index * 0x1000ull;
std::memcpy(table.data() + index * entryStride, &target, sizeof(target));
}

AgcDriver::DriverDetail::TableCalleeTargets targets(64u);
for (std::size_t offset = 0; offset + sizeof(std::uint64_t) <= table.size(); offset += entryStride) {
std::uint64_t target = 0;
std::memcpy(&target, table.data() + offset, sizeof(target));
targets.Add(target, static_cast<std::uint32_t>(offset));
}
Check(targets.TooManyTargets(), "a 1 MiB table with 65,536 distinct targets was not rejected");
Check(targets.Targets().size() == 65u, "large-table target collection retained more than the limit plus one");
for (std::size_t index = 0; index < targets.Targets().size(); ++index) {
Check(targets.Targets()[index].first == 0x100000000ull + index * 0x1000ull,
"large-table target collection changed first-seen target order");
Check(targets.Targets()[index].second == index * entryStride,
"large-table target collection changed the first entry offset");
}
}

void CheckDuplicates() {
AgcDriver::DriverDetail::TableCalleeTargets targets(3u);
targets.Add(0x2000u, 16u);
targets.Add(0x1000u, 32u);
targets.Add(0x2000u, 48u);
targets.Add(0u, 64u);
targets.Add(0x3000u, 80u);
Check(!targets.TooManyTargets() && targets.Targets().size() == 3u,
"zero or duplicate table entries consumed the distinct-target limit");
Check(targets.Targets()[0] == AgcDriver::DriverDetail::TableCalleeTargets::Target{0x2000u, 16u},
"duplicate table entries did not preserve the first offset and order");
targets.Add(0x4000u, 96u);
targets.Add(0x5000u, 112u);
Check(targets.TooManyTargets() && targets.Targets().size() == 4u,
"target collection did not stop at the limit plus one distinct target");
}

}

int main() {
try {
CheckLargeTable();
CheckDuplicates();
} catch (const std::exception& error) {
std::fprintf(stderr, "table callee targets: %s\n", error.what());
return 1;
}
std::puts("table callee target tests passed");
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_test_executable(agc_table_callee_targets_tests EXCLUDE_FROM_ALL tests/TableCalleeTargets.cpp)
target_link_libraries(agc_table_callee_targets_tests PRIVATE libc)
set_target_properties(agc_table_callee_targets_tests PROPERTIES CXX_EXTENSIONS OFF)
configure_windows_unwind(agc_table_callee_targets_tests)

if(BUILD_TESTING)
agc_add_default_test(agc_table_callee_targets_tests)
set_tests_properties(agc_table_callee_targets PROPERTIES TIMEOUT 10)
endif()
Loading
Loading