Skip to content
Closed
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
2 changes: 2 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/BdaExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ void RunBdaExecutionTests(const Context& context) {
run(guest, 8, 0x11, static_cast<Abi::FaultReason>(0));
run(guest + 1, 16, 0x3322, static_cast<Abi::FaultReason>(0));
run(guest + 1, 32, 0x55443322, static_cast<Abi::FaultReason>(0));
run(guest + 2, 16, 0x4433, static_cast<Abi::FaultReason>(0));
run(guest + 4, 16, 0, Abi::FaultReason::Unmapped);
run(guest + 5, 8, 0, Abi::FaultReason::Unmapped, 2, 64);
run(guest - 1, 8, 0, Abi::FaultReason::Unmapped);
run(std::numeric_limits<std::uint64_t>::max() - 1, 32, 0, Abi::FaultReason::Overflow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ struct SpirvEmitterState {
std::uint32_t bdaNoteWriteFunction = 0;
std::array<std::uint32_t, 2> bdaByteWriteFunctions {};
std::uint32_t bdaFaultFunction = 0;
std::uint32_t bdaCacheBegin = 0;
std::uint32_t bdaCacheEnd = 0;
std::uint32_t bdaCacheBase = 0;
std::uint32_t bdaCachePermissions = 0;
bool nativeF16ModesEmitted = false;
std::array<std::array<std::uint32_t, 2>, 2> bdaDwordReadFunctions {};
std::array<std::array<std::uint32_t, 2>, 2> bdaSpanReadFunctions {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SpirvModule {
[[nodiscard]] std::uint32_t Import(const std::string& name);
[[nodiscard]] std::uint32_t DefineGlobalVariable(std::uint32_t pointerType, std::uint32_t storageClass);
void DefineGlobalVariable(std::uint32_t id, std::uint32_t pointerType, std::uint32_t storageClass);
[[nodiscard]] std::uint32_t DefineInitializedGlobalVariable(std::uint32_t pointerType, std::uint32_t storageClass, std::uint32_t initializer);
void AddMemoryModel(std::uint32_t addressingModel, std::uint32_t memoryModel);
void AddName(std::uint32_t target, const std::string& name);
void AddFunction(std::span<const std::uint32_t> words);
Expand Down
52 changes: 47 additions & 5 deletions core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "SpirvBackend/SpirvMemory/SpirvTypes.hpp"
#include "SpirvBackend/SpirvMemory/SpirvConstants.hpp"
#include <bit>
#include <cstdlib>

namespace ShaderRecompiler {

Expand All @@ -19,14 +20,15 @@ void ReturnBdaZeroIf(SpirvEmitterState& state, std::uint32_t condition) {

// (u64 address, u32 bytes, u32 instruction) -> u64 device address of the range holding all the bytes,
std::uint32_t DefineBdaLookup(SpirvEmitterState& state, const char* name, bool recordFaults, std::uint32_t permission = BdaAbi::Read) {
const bool cached = state.bdaCacheBegin != 0;
const auto u32 = TypeU32(state);
const auto u64 = TypeScalarU64(state);
const auto boolean = TypeBool(state);
const auto constant = [&](std::uint32_t value) { return ConstantU32(state, value); };
const auto binary = [&](std::uint32_t op, std::uint32_t type, std::uint32_t left, std::uint32_t right) { return Binary(state, op, type, left, right); };
const auto load = [&](std::uint32_t pointer) {
const auto load = [&](std::uint32_t type, std::uint32_t pointer) {
const auto value = state.module.AllocateId();
state.module.AddFunction(spv::OpLoad, u32, value, pointer);
state.module.AddFunction(spv::OpLoad, type, value, pointer);
return value;
};
const auto function = state.module.AllocateId();
Expand All @@ -51,6 +53,21 @@ std::uint32_t DefineBdaLookup(SpirvEmitterState& state, const char* name, bool r
ReturnBdaZeroIf(state, condition);
}
};
if (cached) {
const auto cachedBegin = load(u64, state.bdaCacheBegin);
const auto accessEnd = binary(spv::OpIAdd, u64, address, Unary(state, spv::OpUConvert, u64, bytes));
auto hit = binary(spv::OpUGreaterThanEqual, boolean, address, cachedBegin);
hit = binary(spv::OpLogicalAnd, boolean, hit, binary(spv::OpULessThanEqual, boolean, accessEnd, load(u64, state.bdaCacheEnd)));
hit = binary(spv::OpLogicalAnd, boolean, hit, binary(spv::OpUGreaterThan, boolean, accessEnd, address));
hit = binary(spv::OpLogicalAnd, boolean, hit, binary(spv::OpIEqual, boolean, binary(spv::OpBitwiseAnd, u32, load(u32, state.bdaCachePermissions), constant(permission)), constant(permission)));
const auto found = state.module.AllocateId();
const auto search = state.module.AllocateId();
state.module.AddFunction(spv::OpSelectionMerge, search, spv::SelectionControlMaskNone);
state.module.AddFunction(spv::OpBranchConditional, hit, found, search);
EmitLabel(state, found);
state.module.AddFunction(spv::OpReturnValue, binary(spv::OpIAdd, u64, load(u64, state.bdaCacheBase), binary(spv::OpISub, u64, address, cachedBegin)));
EmitLabel(state, search);
}
const auto length = state.module.AllocateId();
state.module.AddFunction(spv::OpArrayLength, u32, length, state.bdaPagetableVariable, 0u);
fail(binary(spv::OpULessThan, boolean, length, constant(4)), BdaAbi::FaultReason::InvalidTable);
Expand All @@ -72,8 +89,8 @@ std::uint32_t DefineBdaLookup(SpirvEmitterState& state, const char* name, bool r
const auto merge = state.module.AllocateId();
state.module.AddFunction(spv::OpBranch, header);
EmitLabel(state, header);
const auto lower = load(low);
const auto upper = load(high);
const auto lower = load(u32, low);
const auto upper = load(u32, high);
const auto search = binary(spv::OpULessThan, boolean, lower, upper);
state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlMaskNone);
state.module.AddFunction(spv::OpBranchConditional, search, body, merge);
Expand All @@ -91,7 +108,7 @@ std::uint32_t DefineBdaLookup(SpirvEmitterState& state, const char* name, bool r
EmitLabel(state, continuation);
state.module.AddFunction(spv::OpBranch, header);
EmitLabel(state, merge);
const auto index = load(low);
const auto index = load(u32, low);
fail(binary(spv::OpIEqual, boolean, index, constant(0)), BdaAbi::FaultReason::Unmapped);
const auto selected = binary(spv::OpIAdd, u32, constant(4), binary(spv::OpIMul, u32, binary(spv::OpISub, u32, index, constant(1)), constant(8)));
const auto at = [&](std::uint32_t offset) { return binary(spv::OpIAdd, u32, selected, constant(offset)); };
Expand All @@ -109,6 +126,12 @@ std::uint32_t DefineBdaLookup(SpirvEmitterState& state, const char* name, bool r
fail(binary(spv::OpULessThan, boolean, result, base), BdaAbi::FaultReason::Overflow);
const auto deviceEnd = binary(spv::OpIAdd, u64, result, Unary(state, spv::OpUConvert, u64, bytes));
fail(binary(spv::OpULessThanEqual, boolean, deviceEnd, result), BdaAbi::FaultReason::Overflow);
if (cached) {
state.module.AddFunction(spv::OpStore, state.bdaCacheBegin, begin);
state.module.AddFunction(spv::OpStore, state.bdaCacheEnd, finish);
state.module.AddFunction(spv::OpStore, state.bdaCacheBase, base);
state.module.AddFunction(spv::OpStore, state.bdaCachePermissions, permissions);
}
state.module.AddFunction(spv::OpReturnValue, result);
state.module.AddFunction(spv::OpFunctionEnd);
return function;
Expand Down Expand Up @@ -178,9 +201,28 @@ static std::uint32_t DefineBdaNoteWrite(SpirvEmitterState& state) {
return function;
}

static bool BdaLookupCached() {
static const bool disabled = std::getenv("APS5_NO_BDA_LOOKUP_CACHE") != nullptr;
return !disabled;
}

static void DefineBdaLookupCache(SpirvEmitterState& state) {
const auto address = TypePointer(state, spv::StorageClassPrivate, TypeScalarU64(state));
const auto zero = BdaConstant(state, 0u);
state.bdaCacheBegin = state.module.DefineInitializedGlobalVariable(address, spv::StorageClassPrivate, zero);
state.bdaCacheEnd = state.module.DefineInitializedGlobalVariable(address, spv::StorageClassPrivate, zero);
state.bdaCacheBase = state.module.DefineInitializedGlobalVariable(address, spv::StorageClassPrivate, zero);
state.bdaCachePermissions = state.module.DefineInitializedGlobalVariable(TypePointer(state, spv::StorageClassPrivate, TypeU32(state)), spv::StorageClassPrivate, ConstantU32(state, 0u));
state.module.AddName(state.bdaCacheBegin, "bda_cache_begin");
state.module.AddName(state.bdaCacheEnd, "bda_cache_end");
state.module.AddName(state.bdaCacheBase, "bda_cache_base");
state.module.AddName(state.bdaCachePermissions, "bda_cache_permissions");
}

void DefineGetBdaPointer(SpirvEmitterState& state) {
if (state.faultBufferVariable != 0) DefineBdaFaultFunction(state);
if (!state.program.Info().usesDma) return;
if (BdaLookupCached()) DefineBdaLookupCache(state);
state.bdaPointerFunction = DefineBdaLookup(state, "get_bda_pointer", true);
if (!BdaByteReadsForced()) state.bdaProbeFunction = DefineBdaLookup(state, "probe_bda_pointer", false);
DefineBdaDwordReadFunctions(state);
Expand Down
6 changes: 6 additions & 0 deletions core/shader/recompiler/SpirvBackend/src/SpirvModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ void SpirvModule::DefineGlobalVariable(std::uint32_t id, std::uint32_t pointerTy
appendInstruction(declarations, spv::OpVariable, pointerType, id, storageClass);
}

std::uint32_t SpirvModule::DefineInitializedGlobalVariable(std::uint32_t pointerType, std::uint32_t storageClass, std::uint32_t initializer) {
const auto id = AllocateId();
appendInstruction(declarations, spv::OpVariable, pointerType, id, storageClass, initializer);
return id;
}

void SpirvModule::AddMemoryModel(std::uint32_t addressingModel, std::uint32_t memoryModel) {
appendInstruction(this->memoryModel, spv::OpMemoryModel, addressingModel, memoryModel);
}
Expand Down
Loading