From 29d38460db0094fb233a9236219cf5cafd801067 Mon Sep 17 00:00:00 2001 From: popcoder565 <339285150+popcoder565@users.noreply.github.com> Date: Wed, 7 Oct 2026 16:13:15 -0400 Subject: [PATCH 1/5] perf(recompiler): emit BDA byte stores through one looping helper Every 8- and 16-bit BDA store and the unaligned path of every 32-bit store emitted one get_bda_write_pointer lookup and one note_bda_write per byte at the store site, five write lookups per 32-bit store. They now call write_bda_bytes, which loops over the bytes and does, per byte and in order, the same lookup, the store when it is mapped and the write note, so faults are recorded exactly as before. On an RTX 3060 the agc_driver_flat_store binaries shrink from 496,256 to 138,240 bytes (wave32) and from 928,384 to 225,152 bytes (wave64), and the cold run from 19.7 s to 2.2 s. --- .../include/SpirvBackend/SpirvBda.hpp | 1 + .../SpirvBackend/SpirvEmitterState.hpp | 1 + .../SpirvBackend/src/SpirvBdaLookup.cpp | 1 + .../SpirvBackend/src/SpirvBdaRead.cpp | 65 ++++++++++++++++--- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp index 4b68725c70da..22edd6c87e93 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp @@ -15,6 +15,7 @@ std::uint32_t BdaLoadWord(SpirvEmitterState& state, std::uint32_t index); std::uint32_t BdaLoadAddress(SpirvEmitterState& state, std::uint32_t index); void DefineBdaFaultFunction(SpirvEmitterState& state); void DefineBdaDwordReadFunctions(SpirvEmitterState& state); +void DefineBdaByteWriteFunctions(SpirvEmitterState& state); void RecordBdaFault(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, BdaAbi::FaultReason reason); void ReturnBdaFailureIf(SpirvEmitterState& state, std::uint32_t condition, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, BdaAbi::FaultReason reason); void ValidateBdaTarget(const IrProgram& program, const SpirvTargetOptions& target); diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp index a2498d029292..6803b6bf29f2 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp @@ -113,6 +113,7 @@ struct SpirvEmitterState { std::uint32_t bdaNoteWriteFunction = 0; std::uint32_t bdaFaultFunction = 0; std::array, 2> bdaDwordReadFunctions {}; + std::array bdaByteWriteFunctions {}; std::uint32_t bdaStopValue = 0; // False for programs with workgroup barriers: faulting BDA accesses then continue (see BdaInvocationsMayStop). bool bdaStopsInvocations = true; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp index e6d905db31d8..bcdd27124808 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp @@ -166,6 +166,7 @@ void DefineGetBdaPointer(SpirvEmitterState& state) { state.bdaWritePointerFunction = DefineBdaLookup(state, "get_bda_write_pointer", true, BdaAbi::Write); state.bdaAtomicPointerFunction = DefineBdaLookup(state, "get_bda_atomic_pointer", true, BdaAbi::Read | BdaAbi::Write); state.bdaNoteWriteFunction = DefineBdaNoteWrite(state); + DefineBdaByteWriteFunctions(state); } } diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp index 60df7b5428d3..92fd1a810366 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp @@ -143,6 +143,58 @@ void DefineBdaDwordReadFunctions(SpirvEmitterState& state) { state.bdaStopValue = 0; } +void DefineBdaByteWriteFunctions(SpirvEmitterState& state) { + const auto u32 = TypeU32(state); + const auto u64 = TypeScalarU64(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); + const auto type = state.module.Type(spv::OpTypeFunction, voidType, u64, u32, u32, u32); + const auto& memory = state.program.Resources().memoryInfo; + const bool coherentAccesses = std::any_of(memory.begin(), memory.end(), [](const MemoryInfo& info) { return info.coherent; }); + for (const bool coherent : {false, true}) { + if (coherent && !coherentAccesses) continue; + const auto function = state.module.AllocateId(); + state.module.AddName(function, std::string("write_bda_bytes") + (coherent ? "_coherent" : "")); + state.module.AddFunction(spv::OpFunction, voidType, function, spv::FunctionControlDontInlineMask, type); + const auto address = state.module.AllocateId(); + const auto value = state.module.AllocateId(); + const auto bytes = state.module.AllocateId(); + const auto instruction = state.module.AllocateId(); + state.module.AddFunction(spv::OpFunctionParameter, u64, address); + state.module.AddFunction(spv::OpFunctionParameter, u32, value); + state.module.AddFunction(spv::OpFunctionParameter, u32, bytes); + state.module.AddFunction(spv::OpFunctionParameter, u32, instruction); + EmitLabel(state, state.module.AllocateId()); + 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 header = state.module.AllocateId(); + const auto body = state.module.AllocateId(); + const auto continuation = state.module.AllocateId(); + const auto merge = state.module.AllocateId(); + state.module.AddFunction(spv::OpBranch, header); + 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); + state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlMaskNone); + 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)); + state.module.AddFunction(spv::OpBranch, continuation); + EmitLabel(state, continuation); + state.module.AddFunction(spv::OpStore, counter, Binary(state, spv::OpIAdd, u32, byte, ConstantU32(state, 1u))); + state.module.AddFunction(spv::OpBranch, header); + EmitLabel(state, merge); + state.module.AddFunction(spv::OpReturn); + state.module.AddFunction(spv::OpFunctionEnd); + state.bdaByteWriteFunctions[coherent] = function; + } +} + // APS5_BDA_BYTE_READS=1 restores one lookup and one byte load per byte for every read. bool BdaByteReadsForced() { static const bool forced = std::getenv("APS5_BDA_BYTE_READS") != nullptr; @@ -231,15 +283,10 @@ void EmitBdaStore(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t 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 = ConstantU32(state, inst.Flags().pc); - const auto accessMask = BdaAccessMask(state, inst); const auto storeBytes = [&] { - const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u); - const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType); - for (std::uint32_t byte = 0; byte < bits / 8u; ++byte) { - const auto guest = byte == 0u ? address : Binary(state, spv::OpIAdd, TypeScalarU64(state), address, BdaConstant(state, byte)); - const auto shifted = byte == 0u ? value : Binary(state, spv::OpShiftRightLogical, TypeU32(state), value, ConstantU32(state, byte * 8u)); - EmitBdaStoreAt(state, guest, 1u, bytePointer, Unary(state, spv::OpUConvert, byteType, shifted), instruction, accessMask); - } + 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(); @@ -248,7 +295,7 @@ void EmitBdaStore(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t 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, accessMask); + EmitBdaStoreAt(state, address, 4u, TypePhysicalU32Pointer(state), value, instruction, BdaAccessMask(state, inst)); }); } From a5a7e64574f08384b1a302ffed0e26b6dad4b6ce Mon Sep 17 00:00:00 2001 From: popcoder565 <339285150+popcoder565@users.noreply.github.com> Date: Wed, 7 Oct 2026 16:15:53 -0400 Subject: [PATCH 2/5] perf(recompiler): probe the written-page slots of a BDA write in a loop note_bda_write unrolled its 8 compare-exchange probes, and drivers inline it at every BDA store and atomic. The probes now run in a loop that tries the same slots in the same order and stops at the same slot, so the recorded pages and the overflow flag are unchanged. With the byte store helper, the agc_driver_flat_store binaries on an RTX 3060 go from 138,240 to 75,648 bytes (wave32) and from 225,152 to 130,560 bytes (wave64), and the cold run from 2.2 s to 1.2 s. --- .../SpirvBackend/src/SpirvBdaLookup.cpp | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp index bcdd27124808..651d883e3da4 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cpp @@ -130,22 +130,38 @@ static std::uint32_t DefineBdaNoteWrite(SpirvEmitterState& state) { EmitLabel(state, state.module.AllocateId()); const auto done = state.module.AllocateId(); state.module.AddFunction(spv::OpVariable, TypePointer(state, spv::StorageClassFunction, boolean), done, spv::StorageClassFunction); + const auto counter = state.module.AllocateId(); + state.module.AddFunction(spv::OpVariable, TypePointer(state, spv::StorageClassFunction, u32), counter, spv::StorageClassFunction); state.module.AddFunction(spv::OpStore, done, ConstantBool(state, false)); + state.module.AddFunction(spv::OpStore, counter, constant(0u)); const auto page = binary(spv::OpIAdd, u32, Unary(state, spv::OpUConvert, u32, binary(spv::OpShiftRightLogical, TypeScalarU64(state), address, BdaConstant(state, BdaAbi::WrittenPageShift))), constant(1)); const auto hash = binary(spv::OpShiftRightLogical, u32, binary(spv::OpIMul, u32, page, constant(0x9e3779b1u)), constant(32u - static_cast(std::countr_zero(BdaAbi::WrittenPageSlots)))); const auto scope = constant(spv::ScopeDevice); const auto relaxed = constant(spv::MemorySemanticsMaskNone); - for (std::uint32_t probe = 0; probe < BdaAbi::WrittenPageProbes; probe++) { - const auto pending = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, boolean, pending, done); - EmitIfCondition(state, Unary(state, spv::OpLogicalNot, boolean, pending), [&] { - const auto slot = binary(spv::OpIAdd, u32, constant(BdaAbi::WrittenSlotsWord), binary(spv::OpBitwiseAnd, u32, binary(spv::OpIAdd, u32, hash, constant(probe)), constant(BdaAbi::WrittenPageSlots - 1u))); - const auto previous = state.module.AllocateId(); - state.module.AddFunction(spv::OpAtomicCompareExchange, u32, previous, BdaWord(state, state.faultBufferVariable, slot), scope, relaxed, relaxed, page, constant(0u)); - const auto taken = binary(spv::OpLogicalOr, boolean, binary(spv::OpIEqual, boolean, previous, constant(0u)), binary(spv::OpIEqual, boolean, previous, page)); - state.module.AddFunction(spv::OpStore, done, taken); - }); - } + const auto header = state.module.AllocateId(); + const auto body = state.module.AllocateId(); + const auto continuation = state.module.AllocateId(); + const auto merge = state.module.AllocateId(); + state.module.AddFunction(spv::OpBranch, header); + EmitLabel(state, header); + const auto probe = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, u32, probe, counter); + const auto found = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, boolean, found, done); + const auto searching = binary(spv::OpLogicalAnd, boolean, Unary(state, spv::OpLogicalNot, boolean, found), binary(spv::OpULessThan, boolean, probe, constant(BdaAbi::WrittenPageProbes))); + state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlMaskNone); + state.module.AddFunction(spv::OpBranchConditional, searching, body, merge); + EmitLabel(state, body); + const auto slot = binary(spv::OpIAdd, u32, constant(BdaAbi::WrittenSlotsWord), binary(spv::OpBitwiseAnd, u32, binary(spv::OpIAdd, u32, hash, probe), constant(BdaAbi::WrittenPageSlots - 1u))); + const auto previous = state.module.AllocateId(); + state.module.AddFunction(spv::OpAtomicCompareExchange, u32, previous, BdaWord(state, state.faultBufferVariable, slot), scope, relaxed, relaxed, page, constant(0u)); + const auto taken = binary(spv::OpLogicalOr, boolean, binary(spv::OpIEqual, boolean, previous, constant(0u)), binary(spv::OpIEqual, boolean, previous, page)); + state.module.AddFunction(spv::OpStore, done, taken); + state.module.AddFunction(spv::OpBranch, continuation); + EmitLabel(state, continuation); + state.module.AddFunction(spv::OpStore, counter, binary(spv::OpIAdd, u32, probe, constant(1u))); + state.module.AddFunction(spv::OpBranch, header); + EmitLabel(state, merge); const auto noted = state.module.AllocateId(); state.module.AddFunction(spv::OpLoad, boolean, noted, done); EmitIfCondition(state, Unary(state, spv::OpLogicalNot, boolean, noted), [&] { From c6f295062dcaae4d6a3c3b4ca43cd0ad15a12d19 Mon Sep 17 00:00:00 2001 From: popcoder565 <339285150+popcoder565@users.noreply.github.com> Date: Wed, 7 Oct 2026 16:49:06 -0400 Subject: [PATCH 3/5] refactor(recompiler): keep the BDA byte write helper beside the store path The declaration, the state field and the definition of write_bda_bytes now sit next to EmitBdaStore and the other BDA write functions instead of next to the dword read functions, which #1267 extends at the same lines. No change to the emitted code. --- .../include/SpirvBackend/SpirvBda.hpp | 2 +- .../SpirvBackend/SpirvEmitterState.hpp | 2 +- .../SpirvBackend/src/SpirvBdaRead.cpp | 104 +++++++++--------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp index 22edd6c87e93..7e1dc86f6331 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hpp @@ -15,7 +15,6 @@ std::uint32_t BdaLoadWord(SpirvEmitterState& state, std::uint32_t index); std::uint32_t BdaLoadAddress(SpirvEmitterState& state, std::uint32_t index); void DefineBdaFaultFunction(SpirvEmitterState& state); void DefineBdaDwordReadFunctions(SpirvEmitterState& state); -void DefineBdaByteWriteFunctions(SpirvEmitterState& state); void RecordBdaFault(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, BdaAbi::FaultReason reason); void ReturnBdaFailureIf(SpirvEmitterState& state, std::uint32_t condition, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, BdaAbi::FaultReason reason); void ValidateBdaTarget(const IrProgram& program, const SpirvTargetOptions& target); @@ -26,6 +25,7 @@ void StopBdaInvocationIf(SpirvEmitterState& state, std::uint32_t condition); std::uint32_t EmitBdaRead(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t bits); void EmitBdaWrite(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t value, std::uint32_t bits = 32u); void EmitBdaStore(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t value, std::uint32_t bits); +void DefineBdaByteWriteFunctions(SpirvEmitterState& state); std::uint32_t EmitBdaAtomic(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t bytes, const std::function& operation); // Reads the dwords of a 1-4 dword load at address + offset that the program extracts, with one // table lookup for the whole span; the per-byte lookups of EmitBdaRead remain the fallback (and diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp index 6803b6bf29f2..b093309a06af 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp @@ -111,9 +111,9 @@ struct SpirvEmitterState { std::uint32_t bdaWritePointerFunction = 0; std::uint32_t bdaAtomicPointerFunction = 0; std::uint32_t bdaNoteWriteFunction = 0; + std::array bdaByteWriteFunctions {}; std::uint32_t bdaFaultFunction = 0; std::array, 2> bdaDwordReadFunctions {}; - std::array bdaByteWriteFunctions {}; std::uint32_t bdaStopValue = 0; // False for programs with workgroup barriers: faulting BDA accesses then continue (see BdaInvocationsMayStop). bool bdaStopsInvocations = true; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp index 92fd1a810366..894ada6fda56 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp @@ -143,58 +143,6 @@ void DefineBdaDwordReadFunctions(SpirvEmitterState& state) { state.bdaStopValue = 0; } -void DefineBdaByteWriteFunctions(SpirvEmitterState& state) { - const auto u32 = TypeU32(state); - const auto u64 = TypeScalarU64(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); - const auto type = state.module.Type(spv::OpTypeFunction, voidType, u64, u32, u32, u32); - const auto& memory = state.program.Resources().memoryInfo; - const bool coherentAccesses = std::any_of(memory.begin(), memory.end(), [](const MemoryInfo& info) { return info.coherent; }); - for (const bool coherent : {false, true}) { - if (coherent && !coherentAccesses) continue; - const auto function = state.module.AllocateId(); - state.module.AddName(function, std::string("write_bda_bytes") + (coherent ? "_coherent" : "")); - state.module.AddFunction(spv::OpFunction, voidType, function, spv::FunctionControlDontInlineMask, type); - const auto address = state.module.AllocateId(); - const auto value = state.module.AllocateId(); - const auto bytes = state.module.AllocateId(); - const auto instruction = state.module.AllocateId(); - state.module.AddFunction(spv::OpFunctionParameter, u64, address); - state.module.AddFunction(spv::OpFunctionParameter, u32, value); - state.module.AddFunction(spv::OpFunctionParameter, u32, bytes); - state.module.AddFunction(spv::OpFunctionParameter, u32, instruction); - EmitLabel(state, state.module.AllocateId()); - 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 header = state.module.AllocateId(); - const auto body = state.module.AllocateId(); - const auto continuation = state.module.AllocateId(); - const auto merge = state.module.AllocateId(); - state.module.AddFunction(spv::OpBranch, header); - 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); - state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlMaskNone); - 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)); - state.module.AddFunction(spv::OpBranch, continuation); - EmitLabel(state, continuation); - state.module.AddFunction(spv::OpStore, counter, Binary(state, spv::OpIAdd, u32, byte, ConstantU32(state, 1u))); - state.module.AddFunction(spv::OpBranch, header); - EmitLabel(state, merge); - state.module.AddFunction(spv::OpReturn); - state.module.AddFunction(spv::OpFunctionEnd); - state.bdaByteWriteFunctions[coherent] = function; - } -} - // APS5_BDA_BYTE_READS=1 restores one lookup and one byte load per byte for every read. bool BdaByteReadsForced() { static const bool forced = std::getenv("APS5_BDA_BYTE_READS") != nullptr; @@ -278,6 +226,58 @@ void EmitBdaWrite(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t }); } +void DefineBdaByteWriteFunctions(SpirvEmitterState& state) { + const auto u32 = TypeU32(state); + const auto u64 = TypeScalarU64(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); + const auto type = state.module.Type(spv::OpTypeFunction, voidType, u64, u32, u32, u32); + const auto& memory = state.program.Resources().memoryInfo; + const bool coherentAccesses = std::any_of(memory.begin(), memory.end(), [](const MemoryInfo& info) { return info.coherent; }); + for (const bool coherent : {false, true}) { + if (coherent && !coherentAccesses) continue; + const auto function = state.module.AllocateId(); + state.module.AddName(function, std::string("write_bda_bytes") + (coherent ? "_coherent" : "")); + state.module.AddFunction(spv::OpFunction, voidType, function, spv::FunctionControlDontInlineMask, type); + const auto address = state.module.AllocateId(); + const auto value = state.module.AllocateId(); + const auto bytes = state.module.AllocateId(); + const auto instruction = state.module.AllocateId(); + state.module.AddFunction(spv::OpFunctionParameter, u64, address); + state.module.AddFunction(spv::OpFunctionParameter, u32, value); + state.module.AddFunction(spv::OpFunctionParameter, u32, bytes); + state.module.AddFunction(spv::OpFunctionParameter, u32, instruction); + EmitLabel(state, state.module.AllocateId()); + 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 header = state.module.AllocateId(); + const auto body = state.module.AllocateId(); + const auto continuation = state.module.AllocateId(); + const auto merge = state.module.AllocateId(); + state.module.AddFunction(spv::OpBranch, header); + 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); + state.module.AddFunction(spv::OpLoopMerge, merge, continuation, spv::LoopControlMaskNone); + 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)); + state.module.AddFunction(spv::OpBranch, continuation); + EmitLabel(state, continuation); + state.module.AddFunction(spv::OpStore, counter, Binary(state, spv::OpIAdd, u32, byte, ConstantU32(state, 1u))); + state.module.AddFunction(spv::OpBranch, header); + EmitLabel(state, merge); + state.module.AddFunction(spv::OpReturn); + state.module.AddFunction(spv::OpFunctionEnd); + state.bdaByteWriteFunctions[coherent] = function; + } +} + void EmitBdaStore(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t value, std::uint32_t bits) { auto& state = ctx.state; if (bits != 8u && bits != 16u && bits != 32u) ctx.Fail(inst, "unsupported BDA store width"); From b96f053702fba5b9db51fe1bf667076ea9f0700d Mon Sep 17 00:00:00 2001 From: gunerky <85804496+gunerky@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:28:14 +0300 Subject: [PATCH 4/5] test(relinker): split TLS coverage into bounded CTest groups --- core/relinker/CMakeLists.txt | 14 +- .../tests/test_tls_function_coverage.py | 305 ++++++++++-------- 2 files changed, 175 insertions(+), 144 deletions(-) diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 711fcf509e92..c7e0493f7869 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -200,8 +200,18 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $) add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_empty_tls.py $) - add_test(NAME tls_function_coverage COMMAND ${Python3_EXECUTABLE} - ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_tls_function_coverage.py $) + set(tlsCoverageGroups metadata padding register_loads displacement_bounds alu_execution register_address_loads rejected) + foreach(register IN ITEMS 0 1 2 3 5 6 7 8 9 10 11 12 13 14 15) + list(APPEND tlsCoverageGroups displacement_r${register}) + endforeach() + foreach(opcode IN ITEMS add or adc sbb and sub xor cmp) + list(APPEND tlsCoverageGroups alu_${opcode}) + endforeach() + foreach(group IN LISTS tlsCoverageGroups) + add_test(NAME tls_function_coverage_${group} COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_tls_function_coverage.py $ ${group}) + set_tests_properties(tls_function_coverage_${group} PROPERTIES TIMEOUT 30 LABELS tls_function_coverage) + endforeach() add_test(NAME tls_diagnostics COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_tls_diagnostics.py $) add_test(NAME linux_load_alignment COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/relinker/tests/test_tls_function_coverage.py b/core/relinker/relinker/tests/test_tls_function_coverage.py index 420fcc7fbd7f..b5d4e7d00cd2 100644 --- a/core/relinker/relinker/tests/test_tls_function_coverage.py +++ b/core/relinker/relinker/tests/test_tls_function_coverage.py @@ -1,3 +1,4 @@ +import argparse import os from pathlib import Path import platform @@ -188,8 +189,8 @@ def stack_store(target, offset): return load_offset, code -def displacement_cases(): - for register in range(16): +def displacement_cases(registers=range(16)): + for register in registers: if register == 4: continue for flags in (0x202, 0xad7): @@ -389,8 +390,16 @@ def add_alias(image, size, begin=0x1200): def main(): - relinker = Path(sys.argv[1]).resolve() - with tempfile.TemporaryDirectory(prefix="anyps5-tls-coverage-") as directory: + groups = ("metadata", "padding", "register_loads", "displacement_bounds", "alu_execution", + "register_address_loads", "rejected") + groups += tuple(f"displacement_r{register}" for register in range(16) if register != 4) + groups += tuple(f"alu_{name}" for name in ALU_READ) + parser = argparse.ArgumentParser(description="Run Windows guest TLS function coverage fixtures") + parser.add_argument("relinker", type=Path) + parser.add_argument("group", nargs="?", default="all", choices=("all",) + groups) + args = parser.parse_args() + relinker = args.relinker.resolve() + with tempfile.TemporaryDirectory(prefix=f"anyps5-tls-{args.group}-") as directory: work = Path(directory) def convert(name, image, error=None, tls_address=0x1240, displacement=0, error_offset=None): @@ -422,144 +431,156 @@ def convert(name, image, error=None, tls_address=0x1240, displacement=0, error_o executed = subprocess.run([sys.executable, str(RUNNER), str(output), hex(entry)], capture_output=True, timeout=30) assert executed.returncode == 42, (name, executed.returncode, executed.stderr) - for metadata in ("unwind", "symbol"): - for transfer in ("table", "register", "memory"): - convert(metadata + "-" + transfer, make_image(transfer, metadata)) - convert(metadata + "-truncated", make_image("register", metadata, 0x46), "Code analysis:") - convert(metadata + "-outside", make_image("register", metadata, 0x1000), "Code analysis: function exceeds executable segment") - for first, second in ((0x47, 0x51), (0x51, 0x47), (0, 0x51), (0x51, 0)): - convert(f"symbol-alias-{first:x}-{second:x}", - add_alias(make_image("register", "symbol", first), second)) - for first, second in ((0x47, 0x1000), (0x1000, 0x47)): - convert(f"symbol-alias-outside-{first:x}-{second:x}", - add_alias(make_image("register", "symbol", first), second), - "Code analysis: function exceeds executable segment") - alias_tail = add_alias(make_image("register", "symbol", 0x51), 0x60) - alias_tail[0x1258:0x1260] = bytes.fromhex("64 8b 04 25 28 00 00 00") - convert("symbol-alias-unreachable-tls-tail", alias_tail, - "Unsupported Windows guest TLS instruction", error_offset=0x1258) - for metadata in ("unwind", "symbol"): - convert(f"branch-into-cut-tail-{metadata}", - make_image("register", metadata, 0x50, TLS_LOAD + bytes.fromhex("eb 00 8b 40 f0 c3"))) - convert("branch-into-undecodable-tail", - make_image("register", "unwind", 0x50, TLS_LOAD + bytes.fromhex("eb 01 66 0f 78 c0 01 02 c3")), - "Code analysis: branch into skipped range tail", error_offset=0x124f) - split_body = TLS_LOAD + bytes.fromhex("eb 00 8b 40 f0 48 b9 00 00 00 65 2e 62 69 6e") + b"\xc3" * 8 - convert("function-begins-inside-instruction", - add_alias(make_image("register", "symbol", 0x55, split_body), 0x0c, 0x1256)) - overlapping = make_image("register", "unwind") - overlapping[0x1200:0x1205] = b"\xe9" + struct.pack("> 3) << 2), opcode, 0xc0 | ((register & 7) << 3)]) - assert expected in stub, (case, expected.hex(), stub.hex()) - for name, image in alu_execution_cases(): - convert(name, image) - for name, image, address, instruction, register, wide, moved in register_load_cases(): - convert(name, image, tls_address=address) - pe = (work / (name + ".exe")).read_bytes() - patched = pe_bytes_at(pe, 0x10000 + address, 5) - stub = pe_bytes_at(pe, 0x10000 + address + 5 + struct.unpack_from("> 3) << 2), opcode, 0xc0 | ((register & 7) << 3)]) + assert expected in stub, (case, expected.hex(), stub.hex()) + if args.group in ("all", "alu_execution"): + for name, image in alu_execution_cases(): + convert(name, image) + if args.group in ("all", "register_address_loads"): + for name, image, address, instruction, register, wide, moved in register_load_cases(): + convert(name, image, tls_address=address) + pe = (work / (name + ".exe")).read_bytes() + patched = pe_bytes_at(pe, 0x10000 + address, 5) + stub = pe_bytes_at(pe, 0x10000 + address + 5 + struct.unpack_from(" Date: Fri, 9 Oct 2026 07:15:56 +0200 Subject: [PATCH 5/5] test(agc): bound store execution cases without reducing coverage --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 40 +++++++- .../tests/execution/FlatStore.cpp | 97 ++++++++++++++----- .../tests/execution/GlobalVccBase.cpp | 75 ++++++++++---- 3 files changed, 165 insertions(+), 47 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 775e5901edf6..188ff5a3f19b 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -630,12 +630,42 @@ if(BUILD_TESTING) endif() string(REGEX REPLACE "_tests$" "" agcTestName "${agcTest}") set_target_properties(${agcTest} PROPERTIES EXCLUDE_FROM_ALL OFF) - add_test(NAME ${agcTestName} COMMAND ${agcTest}) - set_tests_properties(${agcTestName} PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") - if(WIN32) - set_tests_properties(${agcTestName} PROPERTIES ENVIRONMENT_MODIFICATION - "PATH=path_list_prepend:$") + set(agcModes default) + if(agcTestName STREQUAL "agc_driver_flat_store") + set(agcModes) + foreach(agcGroup IN ITEMS scalar feedback narrow unaligned vector2 vector3 vector4) + foreach(agcWave IN ITEMS wave32 wave64 read-only) + list(APPEND agcModes "${agcGroup}-${agcWave}") + endforeach() + endforeach() + elseif(agcTestName STREQUAL "agc_driver_global_vcc_base") + set(agcModes) + foreach(agcGroup IN ITEMS dword vector atomic) + foreach(agcWave IN ITEMS wave32 wave64 split) + list(APPEND agcModes "base-${agcGroup}-${agcWave}") + endforeach() + endforeach() + list(APPEND agcModes addtid-wave32 addtid-wave64 addtid-split) endif() + foreach(agcMode IN LISTS agcModes) + set(agcRegisteredName ${agcTestName}) + set(agcArguments) + if(NOT agcMode STREQUAL "default") + set(agcArguments ${agcMode}) + if(NOT agcMode MATCHES "^(scalar|base-dword)-wave32$") + set(agcRegisteredName "${agcTestName}_${agcMode}") + endif() + endif() + add_test(NAME ${agcRegisteredName} COMMAND ${agcTest} ${agcArguments}) + set_tests_properties(${agcRegisteredName} PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(NOT agcMode STREQUAL "default") + set_tests_properties(${agcRegisteredName} PROPERTIES TIMEOUT 30 LABELS ${agcTestName}) + endif() + if(WIN32) + set_tests_properties(${agcRegisteredName} PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + endforeach() endforeach() set_tests_properties(agc_driver_flat_store PROPERTIES RUN_SERIAL TRUE) add_test(NAME agc_driver_pm4_failure COMMAND agc_driver_pm4_tests failure) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/FlatStore.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/FlatStore.cpp index 25bcf9396643..e3eb5432cd9c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/FlatStore.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/FlatStore.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace { @@ -37,6 +38,26 @@ alignas(256) constexpr std::array FlatStoreCode{ 0x00000f01, 0x7da80090, 0xdc708300, 0x00000a01, 0xbf810000, }; +std::vector Kernel(const std::string& group) { + if (group == "all") return {FlatStoreCode.begin(), FlatStoreCode.end()}; + std::vector code(FlatStoreCode.begin(), FlatStoreCode.begin() + 19); + const auto append = [&](std::size_t begin, std::size_t end) { + code.insert(code.end(), FlatStoreCode.begin() + begin, FlatStoreCode.begin() + end); + }; + if (group == "scalar") append(19, 25); + else if (group == "feedback") { + append(19, 21); + append(37, 44); + } else if (group == "narrow") append(25, 29); + else if (group == "unaligned") append(29, 31); + else if (group == "vector4") append(31, 33); + else if (group == "vector3") append(35, 37); + else if (group == "vector2") append(33, 35); + else throw std::invalid_argument("flat store: invalid instruction group"); + code.push_back(FlatStoreCode.back()); + return code; +} + class GuestBlock { public: explicit GuestBlock(bool writable) { @@ -64,34 +85,47 @@ class GuestBlock { std::uint8_t* block = nullptr; }; -std::vector Expected() { +std::vector Expected(const std::string& group) { std::vector image(CheckedBytes, Fill); const auto put = [&](std::uint32_t offset, std::uint32_t value, std::uint32_t bytes) { for (std::uint32_t byte = 0; byte < bytes; ++byte) image.at(offset + byte) = static_cast(value >> (byte * 8u)); }; for (std::uint32_t tid = 0; tid < Threads; ++tid) { const std::array data{0xa1b2c300u + tid, 0x51000000u + tid * 4u, 0x00c0ffeeu + tid * 16u, 0x7e570000u + tid * 8u}; - put(0x000u + tid * 4u, data[0], 4); - put(0x100u + tid * 4u, data[1], 4); - put(0x200u + tid * 4u, data[2], 4); - if (tid < MaskedThreads) put(0x300u + tid * 4u, data[2], 4); - put(0x400u + tid, data[0], 1); - put(0x480u + tid * 2u, data[1], 2); - put(0x500u + tid * 4u, data[0], 4); - put(0x601u + tid * 8u, data[3], 4); - for (std::uint32_t dword = 0; dword < 3; ++dword) put(0x800u + tid * 16u + dword * 4u, data[dword + 1u], 4); - for (std::uint32_t dword = 0; dword < 4; ++dword) put(0x1000u + tid * 16u + dword * 4u, data[dword], 4); - for (std::uint32_t dword = 0; dword < 2; ++dword) put(0x1400u + tid * 16u + dword * 4u, data[dword], 4); + if (group == "all" || group == "scalar" || group == "feedback") put(0x000u + tid * 4u, data[0], 4); + if (group == "all" || group == "scalar") { + put(0x100u + tid * 4u, data[1], 4); + put(0x200u + tid * 4u, data[2], 4); + } + if (group == "all" || group == "feedback") { + if (tid < MaskedThreads) put(0x300u + tid * 4u, data[2], 4); + put(0x500u + tid * 4u, data[0], 4); + } + if (group == "all" || group == "narrow") { + put(0x400u + tid, data[0], 1); + put(0x480u + tid * 2u, data[1], 2); + } + if (group == "all" || group == "unaligned") put(0x601u + tid * 8u, data[3], 4); + if (group == "all" || group == "vector3") { + for (std::uint32_t dword = 0; dword < 3; ++dword) put(0x800u + tid * 16u + dword * 4u, data[dword + 1u], 4); + } + if (group == "all" || group == "vector4") { + for (std::uint32_t dword = 0; dword < 4; ++dword) put(0x1000u + tid * 16u + dword * 4u, data[dword], 4); + } + if (group == "all" || group == "vector2") { + for (std::uint32_t dword = 0; dword < 2; ++dword) put(0x1400u + tid * 16u + dword * 4u, data[dword], 4); + } } return image; } -void Dispatch(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const std::uint8_t* base) { +void Dispatch(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const std::uint8_t* base, const std::string& group) { const auto address = static_cast(reinterpret_cast(base)); std::vector userData(8, 0u); userData[0] = static_cast(address); userData[1] = static_cast(address >> 32u); - const std::span code(FlatStoreCode); + const auto kernel = Kernel(group); + const std::span code(kernel); const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1}; ShaderRecompiler::RecompileRequest request{ @@ -106,18 +140,18 @@ void Dispatch(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const std device.WaitIdle(); } -void RunStores(AgcDriver::VulkanDevice& device, GuestBlock& guest, std::uint32_t waveSize) { +void RunStores(AgcDriver::VulkanDevice& device, GuestBlock& guest, std::uint32_t waveSize, const std::string& group) { guest.Clear(); - Dispatch(device, waveSize, guest.Data()); - const auto expected = Expected(); + Dispatch(device, waveSize, guest.Data(), group); + const auto expected = Expected(group); for (std::uint32_t offset = 0; offset < CheckedBytes; ++offset) { const auto actual = guest.Data()[offset]; Require(actual == expected[offset], "flat store: wave" + std::to_string(waveSize) + " byte " + std::to_string(offset) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected[offset])); } } -void RunReadOnly(AgcDriver::VulkanDevice& device, const GuestBlock& guest) { - Dispatch(device, 32, guest.Data()); +void RunReadOnly(AgcDriver::VulkanDevice& device, const GuestBlock& guest, const std::string& group) { + Dispatch(device, 32, guest.Data(), group); for (std::uint32_t offset = 0; offset < CheckedBytes; ++offset) { Require(guest.Data()[offset] == Fill, "flat store: a store into a read-only range changed byte " + std::to_string(offset)); } @@ -125,15 +159,28 @@ void RunReadOnly(AgcDriver::VulkanDevice& device, const GuestBlock& guest) { } -int main() { +int main(int argc, char** argv) { try { + std::string mode = argc == 1 ? "all" : argv[1]; + std::string group = "all"; + for (const std::string candidate : {"scalar", "feedback", "narrow", "unaligned", "vector2", "vector3", "vector4"}) { + if (!mode.starts_with(candidate + "-")) continue; + group = candidate; + mode.erase(0, candidate.size() + 1u); + break; + } + Require(argc <= 2 && (mode == "all" || mode == "wave32" || mode == "wave64" || mode == "read-only"), "flat store: invalid test mode"); const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; - GuestBlock writable(true); - const GuestBlock readOnly(false); - RunStores(*device, writable, 32); - RunStores(*device, writable, 64); - RunReadOnly(*device, readOnly); + if (mode != "read-only") { + GuestBlock writable(true); + if (mode == "all" || mode == "wave32") RunStores(*device, writable, 32, group); + if (mode == "all" || mode == "wave64") RunStores(*device, writable, 64, group); + } + if (mode == "all" || mode == "read-only") { + const GuestBlock readOnly(false); + RunReadOnly(*device, readOnly, group); + } std::puts("flat store tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/GlobalVccBase.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/GlobalVccBase.cpp index e0e44198972f..af5e121ea296 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/GlobalVccBase.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/GlobalVccBase.cpp @@ -9,6 +9,7 @@ #endif #include #endif +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include namespace { @@ -47,6 +49,24 @@ alignas(256) constexpr std::array VccAddtidCode{ 0x006a0506, 0xdc5c8400, 0x006a0500, 0xbf810000, }; +std::vector BaseKernel(const std::string& group) { + if (group == "all") return {VccBaseCode.begin(), VccBaseCode.end()}; + std::vector code(VccBaseCode.begin(), VccBaseCode.begin() + 8); + const auto append = [&](std::size_t begin, std::size_t end) { + code.insert(code.end(), VccBaseCode.begin() + begin, VccBaseCode.begin() + end); + }; + if (group == "dword") append(8, 10); + else if (group == "vector") append(12, 14); + else if (group == "atomic") append(10, 12); + else throw std::invalid_argument("global vcc base: invalid instruction group"); + code.push_back(VccBaseCode[14]); + if (group == "dword") append(17, 19); + else if (group == "vector") append(15, 17); + else append(19, 21); + code.push_back(VccBaseCode.back()); + return code; +} + class GuestBlock { public: GuestBlock() { @@ -89,15 +109,19 @@ std::vector Initial() { return memory; } -std::vector ExpectedBase() { +std::vector ExpectedBase(const std::string& group) { const auto initial = Initial(); auto memory = initial; for (std::uint32_t tid = 0; tid < Threads; ++tid) { auto* out = &memory[OutputBase + tid * OutputDwords]; - for (std::uint32_t component = 0; component < 4u; ++component) out[component] = initial[VectorBase + tid * 4u + component]; - out[4] = initial[DwordBase + tid]; - out[5] = initial[AtomicBase + tid]; - memory[AtomicBase + tid] = initial[AtomicBase + tid] + tid * 16u; + if (group == "all" || group == "vector") { + for (std::uint32_t component = 0; component < 4u; ++component) out[component] = initial[VectorBase + tid * 4u + component]; + } + if (group == "all" || group == "dword") out[4] = initial[DwordBase + tid]; + if (group == "all" || group == "atomic") { + out[5] = initial[AtomicBase + tid]; + memory[AtomicBase + tid] = initial[AtomicBase + tid] + tid * 16u; + } } return memory; } @@ -166,23 +190,40 @@ void CheckRejections(const AgcDriver::VulkanDevice& device) { } -int main() { +int main(int argc, char** argv) { try { + const std::string mode = argc == 1 ? "all" : argv[1]; + const std::array modes{ + "base-dword-wave32", "base-dword-wave64", "base-dword-split", + "base-vector-wave32", "base-vector-wave64", "base-vector-split", + "base-atomic-wave32", "base-atomic-wave64", "base-atomic-split", + "addtid-wave32", "addtid-wave64", "addtid-split" + }; + Require(argc <= 2 && (mode == "all" || std::find(modes.begin(), modes.end(), mode) != modes.end()), "global vcc base: invalid test mode"); const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; - CheckRejections(*device); + if (mode == "all" || mode == "base-dword-wave32") CheckRejections(*device); GuestBlock guest; - const auto base = ExpectedBase(); - Run(*device, guest, VccBaseCode, 32, device->Target(), base, "wave32"); - Run(*device, guest, VccBaseCode, 64, device->Target(), base, "wave64"); - Run(*device, guest, VccBaseCode, 64, device->ComputeTarget(32), base, "wave64 split"); - if (device->Target().subgroupSize < 32u) { - std::printf("addtid cases skipped, the device's subgroups are narrower than a wave (%u lanes)\n", device->Target().subgroupSize); - } else { - Run(*device, guest, VccAddtidCode, 32, device->Target(), ExpectedAddtid(32), "addtid wave32"); - Run(*device, guest, VccAddtidCode, 64, device->Target(), ExpectedAddtid(64), "addtid wave64"); - Run(*device, guest, VccAddtidCode, 64, device->ComputeTarget(32), ExpectedAddtid(64), "addtid wave64 split"); + std::size_t checked = 0; + for (const auto& candidate : modes) { + if (mode != "all" && mode != candidate) continue; + const bool addtid = candidate.starts_with("addtid"); + if (addtid && device->Target().subgroupSize < 32u) { + std::printf("%s skipped, the device's subgroups are narrower than a wave (%u lanes)\n", candidate.c_str(), device->Target().subgroupSize); + continue; + } + const std::uint32_t waveSize = candidate.ends_with("wave32") ? 32u : 64u; + const auto target = candidate.ends_with("split") ? device->ComputeTarget(32) : device->Target(); + if (addtid) Run(*device, guest, VccAddtidCode, waveSize, target, ExpectedAddtid(waveSize), candidate); + else { + const auto separator = candidate.find('-', 5u); + const auto group = candidate.substr(5u, separator - 5u); + const auto code = BaseKernel(group); + Run(*device, guest, code, waveSize, target, ExpectedBase(group), candidate); + } + ++checked; } + if (checked == 0u) return VulkanTestSkipped; std::puts("global vcc base tests passed"); return 0; } catch (const std::exception& error) {