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.
This commit is contained in:
popcoder565
2026-10-07 16:49:06 -04:00
parent a4643ede97
commit 2e60f5585e
3 changed files with 54 additions and 54 deletions
@@ -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);
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<std::uint32_t(std::uint32_t)>& 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
@@ -106,9 +106,9 @@ struct SpirvEmitterState {
std::uint32_t bdaProbeFunction = 0;
std::uint32_t bdaWritePointerFunction = 0;
std::uint32_t bdaNoteWriteFunction = 0;
std::array<std::uint32_t, 2> bdaByteWriteFunctions {};
std::uint32_t bdaFaultFunction = 0;
std::array<std::array<std::uint32_t, 2>, 2> bdaDwordReadFunctions {};
std::array<std::uint32_t, 2> bdaByteWriteFunctions {};
std::uint32_t bdaStopValue = 0;
// False for programs with workgroup barriers: faulting BDA accesses then continue (see BdaInvocationsMayStop).
bool bdaStopsInvocations = true;
@@ -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;
@@ -260,6 +208,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");