mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #586 from oneandonlydean/fix/recompiler-lds-array-bound
fix(recompiler): size per-invocation LDS arrays to the addresses they reach
This commit is contained in:
@@ -893,6 +893,97 @@ void verifyTwoLaneUniformValues() {
|
||||
require(multiplies(32u) == oneLane, "two-lane uniform values: a two-lane invocation computes a scalar value once per lane");
|
||||
}
|
||||
|
||||
void verifyFunctionLdsBound() {
|
||||
using namespace ShaderRecompiler;
|
||||
const auto build = [](const auto& body) {
|
||||
IrProgram program;
|
||||
program.Resources().stage = IrShaderStage::Pixel;
|
||||
for (const std::uint32_t offset : {0u, 256u, 512u, 0xfffffff0u}) {
|
||||
MemoryInfo lds;
|
||||
lds.kind = ResourceKind::Lds;
|
||||
lds.offset = offset;
|
||||
program.Resources().memoryInfo.push_back(lds);
|
||||
}
|
||||
auto& block = program.CreateBlock();
|
||||
program.SetEntryBlock(block);
|
||||
program.BlockOrder().push_back(&block);
|
||||
const auto emit = [&](IrOpcode opcode, IrType type, std::initializer_list<IrValue*> arguments, std::uint32_t memory = ~0u) -> IrValue& {
|
||||
std::uint64_t bits = 0;
|
||||
if (memory != ~0u) {
|
||||
MemoryFlags flags{memory, 0u};
|
||||
std::memcpy(&bits, &flags, sizeof(flags));
|
||||
}
|
||||
auto& value = program.CreateValue(opcode, type, bits);
|
||||
for (auto* argument : arguments) value.AddArgument(argument);
|
||||
block.AppendInstruction(&value);
|
||||
return value;
|
||||
};
|
||||
const auto constant = [&](std::uint32_t immediate) -> IrValue& {
|
||||
auto& value = program.CreateValue(IrOpcode::Void, IrType::U32);
|
||||
value.SetImmediateU32(immediate);
|
||||
return value;
|
||||
};
|
||||
auto& active = program.CreateValue(IrOpcode::Void, IrType::U1);
|
||||
active.SetImmediateBool(true);
|
||||
body(program, block, emit, constant, active);
|
||||
return std::pair{FunctionLdsDwords(program), AnalyzeProgramRequirements(program)};
|
||||
};
|
||||
|
||||
const auto [laneSlots, laneRequirements] = build([](IrProgram&, IrBlock&, auto& emit, auto& constant, IrValue& active) {
|
||||
auto& lane = emit(IrOpcode::LaneId, IrType::U32, {});
|
||||
auto& address = emit(IrOpcode::ShiftLeftLogical32, IrType::U32, {&lane, &constant(2u)});
|
||||
for (const std::uint32_t memory : {0u, 1u, 2u}) emit(IrOpcode::WriteSharedU32, IrType::Void, {&address, &lane, &active}, memory);
|
||||
emit(IrOpcode::LoadSharedU32, IrType::U32, {&address, &active}, 2u);
|
||||
});
|
||||
require(laneSlots == 192u, "function LDS: lane-strided slots up to byte 767 must take 192 dwords");
|
||||
require(laneRequirements.functionLds && laneRequirements.functionLdsDwords == 192u, "function LDS: the requirements do not carry the bounded size");
|
||||
|
||||
const auto [wide, wideRequirements] = build([](IrProgram&, IrBlock&, auto& emit, auto& constant, IrValue& active) {
|
||||
auto& lane = emit(IrOpcode::LaneId, IrType::U32, {});
|
||||
auto& masked = emit(IrOpcode::BitwiseAnd32, IrType::U32, {&lane, &constant(7u)});
|
||||
auto& scaled = emit(IrOpcode::IMul32, IrType::U32, {&masked, &constant(16u)});
|
||||
auto& chosen = emit(IrOpcode::SelectU32, IrType::U32, {&active, &scaled, &constant(0x100u)});
|
||||
emit(IrOpcode::LoadSharedU32x4, IrType::U32x4, {&chosen, &active}, 0u);
|
||||
});
|
||||
require(wide == 128u, "function LDS: a 4-dword access at byte 0x100 must take 68 dwords, rounded to 128");
|
||||
require(wideRequirements.functionLdsDwords == 128u, "function LDS: the requirements do not carry the wide access's size");
|
||||
|
||||
const auto [unbounded, unboundedRequirements] = build([](IrProgram&, IrBlock&, auto& emit, auto& constant, IrValue& active) {
|
||||
auto& user = emit(IrOpcode::GetUserData, IrType::U32, {&constant(0u)});
|
||||
emit(IrOpcode::WriteSharedU32, IrType::Void, {&user, &user, &active}, 0u);
|
||||
});
|
||||
require(unbounded == FunctionLdsDwordLimit && unboundedRequirements.functionLdsDwords == FunctionLdsDwordLimit, "function LDS: an address without a bound must keep the full array");
|
||||
|
||||
const auto wrapping = build([](IrProgram&, IrBlock&, auto& emit, auto& constant, IrValue& active) {
|
||||
emit(IrOpcode::LoadSharedU32, IrType::U32, {&constant(0x20u), &active}, 3u);
|
||||
}).first;
|
||||
require(wrapping == FunctionLdsDwordLimit, "function LDS: an offset that can wrap the address must keep the full array");
|
||||
|
||||
const auto loop = build([](IrProgram& program, IrBlock& block, auto& emit, auto& constant, IrValue& active) {
|
||||
auto& phi = program.CreateValue(IrOpcode::Phi, IrType::U32);
|
||||
block.AppendInstruction(&phi);
|
||||
auto& next = emit(IrOpcode::IAdd32, IrType::U32, {&phi, &constant(4u)});
|
||||
phi.AddPhiOperand(&block, &constant(0u));
|
||||
phi.AddPhiOperand(&block, &next);
|
||||
emit(IrOpcode::WriteSharedU32, IrType::Void, {&phi, &next, &active}, 0u);
|
||||
}).first;
|
||||
require(loop == FunctionLdsDwordLimit, "function LDS: a loop-carried address must keep the full array");
|
||||
|
||||
const auto joined = build([](IrProgram& program, IrBlock& block, auto& emit, auto& constant, IrValue& active) {
|
||||
auto& phi = program.CreateValue(IrOpcode::Phi, IrType::U32);
|
||||
block.AppendInstruction(&phi);
|
||||
phi.AddPhiOperand(&block, &constant(0x40u));
|
||||
phi.AddPhiOperand(&block, &constant(0x3fcu));
|
||||
emit(IrOpcode::WriteSharedU32, IrType::Void, {&phi, &constant(1u), &active}, 0u);
|
||||
}).first;
|
||||
require(joined == 256u, "function LDS: a phi of bounded addresses must take its largest");
|
||||
|
||||
const auto unsized = build([](IrProgram&, IrBlock&, auto& emit, auto& constant, IrValue& active) {
|
||||
emit(IrOpcode::LoadShared, IrType::U32, {&constant(0u), &active}, 0u);
|
||||
}).first;
|
||||
require(unsized == FunctionLdsDwordLimit, "function LDS: an access without a known width must keep the full array");
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
using namespace ShaderRecompiler;
|
||||
@@ -908,6 +999,7 @@ int main() {
|
||||
verifyComputedTexelOffsets();
|
||||
verifyWaveUniformValues();
|
||||
verifyTwoLaneUniformValues();
|
||||
verifyFunctionLdsBound();
|
||||
#if ANYPS5_ENABLE_SPIRV_TOOLS
|
||||
const std::vector<std::uint32_t> minimalSpirv{
|
||||
0x07230203u, 0x00010000u, 0u, 5u, 0u,
|
||||
|
||||
@@ -17,6 +17,7 @@ struct SpirvRequirements {
|
||||
bool imageGatherExtended = false;
|
||||
bool functionLds = false;
|
||||
bool ldsLock = false;
|
||||
std::uint32_t functionLdsDwords = 0;
|
||||
bool functionScratch = false;
|
||||
bool pixelValidMask = false;
|
||||
bool bufferInt64Atomics = false;
|
||||
@@ -28,6 +29,8 @@ struct SpirvRequirements {
|
||||
};
|
||||
|
||||
[[nodiscard]] SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program);
|
||||
inline constexpr std::uint32_t FunctionLdsDwordLimit = 8192u;
|
||||
[[nodiscard]] std::uint32_t FunctionLdsDwords(const IrProgram& program);
|
||||
[[nodiscard]] std::unordered_set<const IrValue*> WaveUniformValues(const IrProgram& program);
|
||||
[[nodiscard]] bool IsWaveMaskBranch(BranchCondition condition);
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "SpirvBackend/SpirvAnalysis.hpp"
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace ShaderRecompiler {
|
||||
|
||||
@@ -63,6 +66,178 @@ bool LaneSource(const IrProgram& program, const IrValue& value) {
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class AddressBound {
|
||||
public:
|
||||
std::optional<std::uint64_t> Of(const IrValue* value) {
|
||||
if (value == nullptr) return std::nullopt;
|
||||
value = value->Resolve();
|
||||
if (value == nullptr) return std::nullopt;
|
||||
if (value->HasImmediate()) return static_cast<std::uint64_t>(value->ImmediateU32());
|
||||
if (const auto known = bounds.find(value); known != bounds.end()) return known->second;
|
||||
if (!visiting.insert(value).second || visiting.size() > MaxDepth) return std::nullopt;
|
||||
const auto result = compute(*value);
|
||||
visiting.erase(value);
|
||||
bounds.emplace(value, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::size_t MaxDepth = 256;
|
||||
static constexpr std::uint64_t Max32 = 0xffffffffull;
|
||||
|
||||
static std::optional<std::uint64_t> fits(std::uint64_t value) {
|
||||
return value <= Max32 ? std::optional<std::uint64_t>(value) : std::nullopt;
|
||||
}
|
||||
static std::uint64_t ones(std::uint64_t value) {
|
||||
std::uint64_t mask = 0;
|
||||
while (mask < value) mask = (mask << 1u) | 1u;
|
||||
return mask;
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> compute(const IrValue& value) {
|
||||
const auto argument = [&](std::size_t index) { return index < value.ArgumentCount() ? Of(value.Argument(index)) : std::nullopt; };
|
||||
switch (value.Opcode()) {
|
||||
case IrOpcode::LaneId:
|
||||
return 63u;
|
||||
case IrOpcode::IAdd32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (!a || !b) return std::nullopt;
|
||||
return fits(*a + *b);
|
||||
}
|
||||
case IrOpcode::IMul32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (!a || !b || (*b != 0 && *a > Max32 / *b)) return std::nullopt;
|
||||
return *a * *b;
|
||||
}
|
||||
case IrOpcode::ShiftLeftLogical32:
|
||||
case IrOpcode::IShiftLeft32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (!a || !b || *b >= 32u) return std::nullopt;
|
||||
return fits(*a << *b);
|
||||
}
|
||||
case IrOpcode::ShiftRightLogical32:
|
||||
case IrOpcode::IShiftRightLogical32:
|
||||
return argument(0);
|
||||
case IrOpcode::BitwiseAnd32:
|
||||
case IrOpcode::IAnd32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (a && b) return std::min(*a, *b);
|
||||
return a ? a : b;
|
||||
}
|
||||
case IrOpcode::BitwiseOr32:
|
||||
case IrOpcode::IOr32:
|
||||
case IrOpcode::BitwiseXor32:
|
||||
case IrOpcode::IXor32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (!a || !b) return std::nullopt;
|
||||
return ones(std::max(*a, *b));
|
||||
}
|
||||
case IrOpcode::UMin32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (a && b) return std::min(*a, *b);
|
||||
return a ? a : b;
|
||||
}
|
||||
case IrOpcode::UMax32: {
|
||||
const auto a = argument(0), b = argument(1);
|
||||
if (!a || !b) return std::nullopt;
|
||||
return std::max(*a, *b);
|
||||
}
|
||||
case IrOpcode::SelectU32: {
|
||||
const auto a = argument(1), b = argument(2);
|
||||
if (!a || !b) return std::nullopt;
|
||||
return std::max(*a, *b);
|
||||
}
|
||||
case IrOpcode::BitFieldUExtract: {
|
||||
const auto width = argument(2);
|
||||
if (width && *width < 32u) return (1ull << *width) - 1u;
|
||||
return argument(0);
|
||||
}
|
||||
case IrOpcode::Phi: {
|
||||
std::uint64_t result = 0;
|
||||
for (std::size_t index = 0; index < value.ArgumentCount(); ++index) {
|
||||
const auto incoming = Of(value.Argument(index));
|
||||
if (!incoming) return std::nullopt;
|
||||
result = std::max(result, *incoming);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<const IrValue*, std::optional<std::uint64_t>> bounds;
|
||||
std::unordered_set<const IrValue*> visiting;
|
||||
};
|
||||
|
||||
std::uint32_t SharedAccessDwords(IrOpcode opcode) {
|
||||
switch (opcode) {
|
||||
case IrOpcode::LoadSharedU8:
|
||||
case IrOpcode::LoadSharedU16:
|
||||
case IrOpcode::LoadSharedU32:
|
||||
case IrOpcode::WriteSharedU8:
|
||||
case IrOpcode::WriteSharedU16:
|
||||
case IrOpcode::WriteSharedU32:
|
||||
case IrOpcode::SharedAtomicFMin32:
|
||||
case IrOpcode::SharedAtomicFMax32:
|
||||
case IrOpcode::SharedAtomicSwap32:
|
||||
case IrOpcode::SharedAtomicIAdd32:
|
||||
case IrOpcode::SharedAtomicISub32:
|
||||
case IrOpcode::SharedAtomicInc32:
|
||||
case IrOpcode::SharedAtomicDec32:
|
||||
case IrOpcode::SharedAtomicSMin32:
|
||||
case IrOpcode::SharedAtomicUMin32:
|
||||
case IrOpcode::SharedAtomicSMax32:
|
||||
case IrOpcode::SharedAtomicUMax32:
|
||||
case IrOpcode::SharedAtomicAnd32:
|
||||
case IrOpcode::SharedAtomicOr32:
|
||||
case IrOpcode::SharedAtomicXor32:
|
||||
case IrOpcode::SharedAtomicRsub32:
|
||||
case IrOpcode::SharedAtomicFAdd32:
|
||||
case IrOpcode::SharedAtomicCmpst32:
|
||||
case IrOpcode::SharedAtomicCmpstF32:
|
||||
case IrOpcode::SharedAtomicMskor32:
|
||||
case IrOpcode::SharedAtomicWrap32:
|
||||
return 1u;
|
||||
case IrOpcode::LoadSharedU32x2:
|
||||
case IrOpcode::WriteSharedU32x2:
|
||||
return 2u;
|
||||
case IrOpcode::LoadSharedU32x3:
|
||||
case IrOpcode::WriteSharedU32x3:
|
||||
return 3u;
|
||||
case IrOpcode::LoadSharedU32x4:
|
||||
case IrOpcode::WriteSharedU32x4:
|
||||
return 4u;
|
||||
default:
|
||||
return 0u;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::uint32_t FunctionLdsDwords(const IrProgram& program) {
|
||||
AddressBound bound;
|
||||
std::uint64_t needed = 0;
|
||||
for (const IrBlock* block : program.BlockOrder()) {
|
||||
for (const IrValue* inst : block->Instructions()) {
|
||||
if (SharedAccessOf(inst->Opcode()) == SharedAccess::None) continue;
|
||||
const auto index = inst->Flags<MemoryFlags>().index;
|
||||
if (index >= program.Resources().memoryInfo.size()) return FunctionLdsDwordLimit;
|
||||
const auto& memory = program.Resources().memoryInfo[index];
|
||||
if (memory.kind != ResourceKind::Lds) continue;
|
||||
const auto dwords = SharedAccessDwords(inst->Opcode());
|
||||
if (dwords == 0u || inst->ArgumentCount() == 0) return FunctionLdsDwordLimit;
|
||||
const auto address = bound.Of(inst->Argument(0));
|
||||
if (!address || *address + memory.offset > 0xffffffffull) return FunctionLdsDwordLimit;
|
||||
needed = std::max(needed, ((*address + memory.offset) >> 2u) + dwords);
|
||||
if (needed >= FunctionLdsDwordLimit) return FunctionLdsDwordLimit;
|
||||
}
|
||||
}
|
||||
return static_cast<std::uint32_t>(std::min<std::uint64_t>(std::max<std::uint64_t>((needed + 63u) & ~63ull, 64u), FunctionLdsDwordLimit));
|
||||
}
|
||||
|
||||
SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program) {
|
||||
SpirvRequirements requirements {};
|
||||
for (const IrBlock* block : program.BlockOrder()) {
|
||||
@@ -197,6 +372,7 @@ SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program) {
|
||||
requirements.subgroupBallot = true;
|
||||
}
|
||||
}
|
||||
if (requirements.functionLds) requirements.functionLdsDwords = FunctionLdsDwords(program);
|
||||
return requirements;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@ namespace ShaderRecompiler
|
||||
{
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t FunctionLdsDwords = 8192u;
|
||||
|
||||
[[noreturn]] void FailEmit(const std::string& reason) {
|
||||
throw std::runtime_error("SPIR-V module emission failed: " + reason);
|
||||
}
|
||||
@@ -96,7 +94,8 @@ void EmitMemoryOffsets(SpirvEmitterState& state) {
|
||||
|
||||
std::uint32_t LdsDwordCount(const SpirvEmitterState& state) {
|
||||
const auto* workgroup = ShaderWorkgroupInput(state);
|
||||
return workgroup != nullptr ? workgroup->ldsSizeDwords : FunctionLdsDwords;
|
||||
if (workgroup != nullptr) return workgroup->ldsSizeDwords;
|
||||
return state.requirements.functionLdsDwords != 0u ? state.requirements.functionLdsDwords : FunctionLdsDwordLimit;
|
||||
}
|
||||
|
||||
std::uint32_t EmitLdsLockPointer(SpirvEmitterState& state) {
|
||||
|
||||
Reference in New Issue
Block a user