Skip to content
Draft
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
18 changes: 10 additions & 8 deletions core/libs/prx/libSceAgcDriver/tests/BdaContracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,15 @@ void RunBdaContractTests() {
AgcDriver::Graphics::Require(ordered({}) && ordered({{0x1000, 0x2000, 1, 1, 0}, {0x2000, 0x2000, 1, 1, 0}, {0x2000, 0x3000, 1, 1, 0}, {0x4000, 0x5000, 1, 1, 0}}), "ascending BDA ranges were refused");
AgcDriver::Graphics::Require(ordered({{0x1000, 0x3000, 1, 1, 0}, {0x2f00, 0x3100, 1, 1, 0}}), "BDA ranges ascending by begin and overlapping were refused");
AgcDriver::Graphics::Require(!ordered({{0x4000, 0x5000, 1, 1, 0}, {0x1000, 0x2000, 1, 1, 0}}), "unsorted BDA ranges were accepted");
for (const std::uint32_t bits : {8u, 16u, 32u}) {
const auto shader = MakeBdaWriteTestShader(0x7fff1235fffdULL, 0xa1b2c3d4u, bits, false);
const auto lookups = CallSites(shader, "get_bda_write_pointer");
const auto loops = CallSites(shader, "write_bda_bytes");
AgcDriver::Graphics::Require(loops == 1u && lookups == (bits == 32u ? 2u : 1u), "a runtime V# write of " + std::to_string(bits) + " bits has " + std::to_string(lookups) + " write lookups and " + std::to_string(loops) + " byte-loop calls, not its byte path through write_bda_bytes");
const auto [byteLoops, rolled] = LoopsKeptRolled(shader, "write_bda_bytes");
AgcDriver::Graphics::Require(byteLoops == 1u && rolled == 1u, "the byte loop of write_bda_bytes may be unrolled, which copies its write lookup once per byte");
for (const bool runtime : {false, true}) {
for (const std::uint32_t bits : {8u, 16u, 32u}) {
const auto shader = runtime ? MakeBdaWriteTestShader(0x7fff1235fffdULL, 0xa1b2c3d4u, bits, false) : MakeBdaStoreTestShader(0x7fff1235fffdULL, 0xa1b2c3d4u, bits, false);
const auto lookups = CallSites(shader, "get_bda_write_pointer");
const auto loops = CallSites(shader, "write_bda_bytes");
AgcDriver::Graphics::Require(loops == 1u && lookups == 1u, std::string(runtime ? "a runtime" : "a bound") + " V# write of " + std::to_string(bits) + " bits has " + std::to_string(lookups) + " write lookups and " + std::to_string(loops) + " byte-loop calls, not one write lookup in write_bda_bytes");
const auto [byteLoops, rolled] = LoopsKeptRolled(shader, "write_bda_bytes");
AgcDriver::Graphics::Require(byteLoops == 1u && rolled == 1u, "the byte loop of write_bda_bytes may be unrolled, which copies its write lookup once per byte");
}
}
for (const bool coherent : {false, true}) {
const auto shader = MakeBdaDwordReadTestShader(0x1000, 4, coherent, false);
Expand All @@ -363,6 +365,6 @@ void RunBdaContractTests() {
const auto shader = MakeBdaDwordWriteTestShader(0x7fff12340f82ULL, dwords, dwordValues.data());
const auto lookups = CallSites(shader, "get_bda_write_pointer");
const auto loops = CallSites(shader, "write_bda_bytes");
AgcDriver::Graphics::Require(lookups == 2u && loops == 1u, "the dword-by-dword fallback of a " + std::to_string(dwords) + "-dword write has " + std::to_string(lookups) + " write lookups and " + std::to_string(loops) + " byte-loop calls, not one dword store looped over its dwords");
AgcDriver::Graphics::Require(lookups == 1u && loops == 1u, "the dword-by-dword fallback of a " + std::to_string(dwords) + "-dword write has " + std::to_string(lookups) + " write lookups and " + std::to_string(loops) + " byte-loop calls, not one dword store looped over its dwords");
}
}
83 changes: 37 additions & 46 deletions core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ namespace ShaderRecompiler {

namespace {

void EmitBdaOverflowCheck(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction) {
const auto overflow = Binary(state, spv::OpUGreaterThan, TypeBool(state), address, BdaConstant(state, std::numeric_limits<std::uint64_t>::max() - bytes));
void EmitBdaOverflowCheck(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, std::uint32_t guard = 0u) {
auto overflow = Binary(state, spv::OpUGreaterThan, TypeBool(state), address, BdaConstant(state, std::numeric_limits<std::uint64_t>::max() - bytes));
if (guard != 0u) overflow = Binary(state, spv::OpLogicalAnd, TypeBool(state), guard, overflow);
EmitIfCondition(state, overflow, [&] { RecordBdaFault(state, address, ConstantU32(state, bytes), instruction, BdaAbi::FaultReason::Overflow); });
StopBdaInvocationIf(state, overflow);
}
Expand Down Expand Up @@ -105,17 +106,6 @@ std::uint32_t EmitBdaBytes(SpirvEmitterState& state, std::uint32_t address, std:
return EmitBdaByteLoop(state, address, ConstantU32(state, bytes), instruction, accessMask);
}

void EmitBdaStoreAt(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t pointerType, std::uint32_t value, std::uint32_t instruction, std::uint32_t accessMask) {
const auto physical = state.module.AllocateId();
state.module.AddFunction(spv::OpFunctionCall, TypeScalarU64(state), physical, state.bdaWritePointerFunction, address, ConstantU32(state, bytes), instruction);
EmitIfCondition(state, Binary(state, spv::OpINotEqual, TypeBool(state), physical, BdaConstant(state, 0u)), [&] {
const auto pointer = state.module.AllocateId();
state.module.AddFunction(spv::OpConvertUToPtr, pointerType, pointer, physical);
state.module.AddFunction(spv::OpStore, pointer, value, accessMask, bytes);
state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), state.bdaNoteWriteFunction, address);
});
}

std::uint32_t BdaAddressUnaligned(SpirvEmitterState& state, std::uint32_t address) {
return Binary(state, spv::OpINotEqual, TypeBool(state), Binary(state, spv::OpBitwiseAnd, TypeScalarU64(state), address, BdaConstant(state, 3u)), BdaConstant(state, 0u));
}
Expand Down Expand Up @@ -658,26 +648,16 @@ void EmitBdaWrite(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t
if (state.bdaWritePointerFunction == 0 || state.bdaNoteWriteFunction == 0) ctx.Fail(inst, "BDA write functions are missing");
const auto instruction = BdaInstructionPc(state, inst);
if (bits != 8u && bits != 16u && bits != 32u) ctx.Fail(inst, "unsupported BDA write width");
const auto storeBytes = [&] {
EmitBdaOverflowCheck(state, address, bits / 8u, instruction);
const auto function = state.bdaByteWriteFunctions[BdaCoherent(state, inst)];
if (function == 0) ctx.Fail(inst, "BDA byte write function is missing");
state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), function, address, value, ConstantU32(state, bits / 8u), instruction);
};
if (bits != 32u) {
storeBytes();
return;
}
const auto unaligned = BdaAddressUnaligned(state, address);
EmitIfCondition(state, unaligned, storeBytes);
EmitIfCondition(state, Unary(state, spv::OpLogicalNot, TypeBool(state), unaligned), [&] {
EmitBdaStoreAt(state, address, 4u, TypePhysicalU32Pointer(state), value, instruction, BdaAccessMask(state, inst));
});
const auto function = state.bdaByteWriteFunctions[BdaCoherent(state, inst)];
if (function == 0) ctx.Fail(inst, "BDA byte write function is missing");
EmitBdaOverflowCheck(state, address, bits / 8u, instruction, bits == 32u ? BdaAddressUnaligned(state, address) : 0u);
state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), function, address, value, ConstantU32(state, bits / 8u), instruction);
}

void DefineBdaByteWriteFunctions(SpirvEmitterState& state) {
const auto u32 = TypeU32(state);
const auto u64 = TypeScalarU64(state);
const auto boolean = TypeBool(state);
const auto voidType = state.module.Type(spv::OpTypeVoid);
const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u);
const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType);
Expand All @@ -701,6 +681,9 @@ void DefineBdaByteWriteFunctions(SpirvEmitterState& state) {
const auto counter = state.module.AllocateId();
state.module.AddFunction(spv::OpVariable, TypePointer(state, spv::StorageClassFunction, u32), counter, spv::StorageClassFunction);
state.module.AddFunction(spv::OpStore, counter, ConstantU32(state, 0u));
const auto wide = Binary(state, spv::OpLogicalAnd, boolean, Binary(state, spv::OpIEqual, boolean, bytes, ConstantU32(state, 4u)), Unary(state, spv::OpLogicalNot, boolean, BdaAddressUnaligned(state, address)));
const auto count = Select(state, u32, wide, ConstantU32(state, 1u), bytes);
const auto width = Select(state, u32, wide, ConstantU32(state, 4u), ConstantU32(state, 1u));
const auto header = state.module.AllocateId();
const auto body = state.module.AllocateId();
const auto continuation = state.module.AllocateId();
Expand All @@ -709,13 +692,33 @@ void DefineBdaByteWriteFunctions(SpirvEmitterState& state) {
EmitLabel(state, header);
const auto byte = state.module.AllocateId();
state.module.AddFunction(spv::OpLoad, u32, byte, counter);
const auto pending = Binary(state, spv::OpULessThan, TypeBool(state), byte, bytes);
const auto pending = Binary(state, spv::OpULessThan, boolean, byte, count);
state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlDontUnrollMask);
state.module.AddFunction(spv::OpBranchConditional, pending, body, merge);
EmitLabel(state, body);
const auto guest = Binary(state, spv::OpIAdd, u64, address, Unary(state, spv::OpUConvert, u64, byte));
const auto shifted = Binary(state, spv::OpShiftRightLogical, u32, value, Binary(state, spv::OpShiftLeftLogical, u32, byte, ConstantU32(state, 3u)));
EmitBdaStoreAt(state, guest, 1u, bytePointer, Unary(state, spv::OpUConvert, byteType, shifted), instruction, BdaAccessMask(coherent));
const auto physical = state.module.AllocateId();
state.module.AddFunction(spv::OpFunctionCall, u64, physical, state.bdaWritePointerFunction, guest, width, instruction);
EmitIfCondition(state, Binary(state, spv::OpINotEqual, boolean, physical, BdaConstant(state, 0u)), [&] {
const auto dwordLabel = state.module.AllocateId();
const auto byteLabel = state.module.AllocateId();
const auto stored = state.module.AllocateId();
state.module.AddFunction(spv::OpSelectionMerge, stored, spv::SelectionControlMaskNone);
state.module.AddFunction(spv::OpBranchConditional, wide, dwordLabel, byteLabel);
EmitLabel(state, dwordLabel);
const auto dwordPointer = state.module.AllocateId();
state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalU32Pointer(state), dwordPointer, physical);
state.module.AddFunction(spv::OpStore, dwordPointer, value, BdaAccessMask(coherent), 4u);
state.module.AddFunction(spv::OpBranch, stored);
EmitLabel(state, byteLabel);
const auto shifted = Binary(state, spv::OpShiftRightLogical, u32, value, Binary(state, spv::OpShiftLeftLogical, u32, byte, ConstantU32(state, 3u)));
const auto pointer = state.module.AllocateId();
state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, physical);
state.module.AddFunction(spv::OpStore, pointer, Unary(state, spv::OpUConvert, byteType, shifted), BdaAccessMask(coherent), 1u);
state.module.AddFunction(spv::OpBranch, stored);
EmitLabel(state, stored);
state.module.AddFunction(spv::OpFunctionCall, voidType, state.module.AllocateId(), state.bdaNoteWriteFunction, guest);
});
state.module.AddFunction(spv::OpBranch, continuation);
EmitLabel(state, continuation);
state.module.AddFunction(spv::OpStore, counter, Binary(state, spv::OpIAdd, u32, byte, ConstantU32(state, 1u)));
Expand All @@ -731,21 +734,9 @@ void EmitBdaStore(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t
auto& state = ctx.state;
if (bits != 8u && bits != 16u && bits != 32u) ctx.Fail(inst, "unsupported BDA store width");
if (state.bdaWritePointerFunction == 0 || state.bdaNoteWriteFunction == 0) ctx.Fail(inst, "BDA write functions are missing");
const auto instruction = BdaInstructionPc(state, inst);
const auto storeBytes = [&] {
const auto function = state.bdaByteWriteFunctions[BdaCoherent(state, inst)];
if (function == 0) ctx.Fail(inst, "BDA byte write function is missing");
state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), function, address, value, ConstantU32(state, bits / 8u), instruction);
};
if (bits != 32u) {
storeBytes();
return;
}
const auto unaligned = BdaAddressUnaligned(state, address);
EmitIfCondition(state, unaligned, storeBytes);
EmitIfCondition(state, Unary(state, spv::OpLogicalNot, TypeBool(state), unaligned), [&] {
EmitBdaStoreAt(state, address, 4u, TypePhysicalU32Pointer(state), value, instruction, BdaAccessMask(state, inst));
});
const auto function = state.bdaByteWriteFunctions[BdaCoherent(state, inst)];
if (function == 0) ctx.Fail(inst, "BDA byte write function is missing");
state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), function, address, value, ConstantU32(state, bits / 8u), BdaInstructionPc(state, inst));
}

void EmitBdaDwordWrites(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t offset, std::uint32_t dwords, std::uint32_t value) {
Expand Down