mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
perf(shader): intern SPIR-V type and constant ids without a per-call allocation
Every SpirvModule::Type and Constant call built a heap std::vector key and looked it up in a std::map keyed on std::vector, so the comparison walked the key bytes. At the one to three such calls per emitted instruction a shader issues thousands of them per compile. Add a fixed-size, linearly probed declaration cache that holds the key inline and is consulted before the key vector is built, making a repeated declaration a hash and a compare. The cacheable shapes are those whose operands are a word: OpTypeVoid, OpTypeBool, OpTypeInt, OpTypeFloat, OpTypeVector, OpTypePointer, OpTypeStruct, OpTypeArray, OpTypeFunction and the scalar OpConstant family. The overloads are constrained on word-sized operands so that enum operands such as spv::StorageClassFunction reach the cache instead of binding the variadic template by reference. Measured with a harness driving the module through the emitters' call shapes: 80000 hot type and constant calls drop from 80074 heap allocations to 140. The assigned ids and the emitted SPIR-V word stream are byte-identical. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#ifndef CORE_SHADER_RECOMPILIER_SPIRVBACKEND_INCLUDE_SPIRVBACKEND_SPIRVMODULE_HPP
|
||||
#define CORE_SHADER_RECOMPILIER_SPIRVBACKEND_INCLUDE_SPIRVBACKEND_SPIRVMODULE_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
@@ -51,6 +53,28 @@ public:
|
||||
return declareType(opcode, makeTypeKey(opcode, operands...));
|
||||
}
|
||||
|
||||
std::uint32_t Type(std::uint32_t opcode) {
|
||||
const std::uint32_t key[2] = {opcode, 0u};
|
||||
return interned(key, 2u, [this, opcode]() { return declareType(opcode, makeTypeKey(opcode)); });
|
||||
}
|
||||
|
||||
template<typename TWord>
|
||||
requires wordSized<TWord>
|
||||
std::uint32_t Type(std::uint32_t opcode, TWord width) {
|
||||
const auto narrowed = narrowWord(width);
|
||||
const std::uint32_t key[3] = {opcode, 1u, narrowed};
|
||||
return interned(key, 3u, [this, opcode, width]() { return declareType(opcode, makeTypeKey(opcode, width)); });
|
||||
}
|
||||
|
||||
template<typename TWord, typename TSign>
|
||||
requires (wordSized<TWord> && wordSized<TSign>)
|
||||
std::uint32_t Type(std::uint32_t opcode, TWord width, TSign signness) {
|
||||
const auto narrowedWidth = narrowWord(width);
|
||||
const auto narrowedSignness = narrowWord(signness);
|
||||
const std::uint32_t key[4] = {opcode, 2u, narrowedWidth, narrowedSignness};
|
||||
return interned(key, 4u, [this, opcode, width, signness]() { return declareType(opcode, makeTypeKey(opcode, width, signness)); });
|
||||
}
|
||||
|
||||
template<typename... TOperands>
|
||||
std::uint32_t DecoratedType(std::uint32_t opcode, std::initializer_list<SpirvTypeAnnotation> annotations, const TOperands&... operands) {
|
||||
return declareDecoratedType(opcode, makeTypeKey(opcode, operands...), annotations);
|
||||
@@ -64,6 +88,19 @@ public:
|
||||
return declareConstant(opcode, std::move(key));
|
||||
}
|
||||
|
||||
template<typename TValue>
|
||||
requires wordSized<TValue>
|
||||
std::uint32_t Constant(std::uint32_t opcode, std::uint32_t type, TValue value) {
|
||||
const auto narrowed = narrowWord(value);
|
||||
const std::uint32_t key[3] = {opcode, type, narrowed};
|
||||
return interned(key, 3u, [this, opcode, type, value]() {
|
||||
std::vector<std::uint32_t> keyWords;
|
||||
keyWords.reserve(3u);
|
||||
appendOperands(keyWords, opcode, type, value);
|
||||
return declareConstant(opcode, std::move(keyWords));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename... TOperands>
|
||||
void AddExecutionMode(std::uint32_t entryPoint, std::uint32_t mode, const TOperands&... operands) {
|
||||
appendInstruction(executionModes, spv::OpExecutionMode, entryPoint, mode, operands...);
|
||||
@@ -80,6 +117,54 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::size_t DeclarationCacheSize = 512;
|
||||
static constexpr std::size_t MaxCachedDeclarationWords = 4;
|
||||
|
||||
template<typename TOperand>
|
||||
static constexpr bool wordSized = (std::is_integral_v<TOperand> || std::is_enum_v<TOperand>) && sizeof(TOperand) <= sizeof(std::uint32_t);
|
||||
|
||||
template<typename TOperand>
|
||||
static std::uint32_t narrowWord(TOperand operand) {
|
||||
return static_cast<std::uint32_t>(operand);
|
||||
}
|
||||
|
||||
struct DeclarationSlot {
|
||||
std::array<std::uint32_t, MaxCachedDeclarationWords> words = {};
|
||||
std::uint32_t length = 0;
|
||||
std::uint32_t id = 0;
|
||||
};
|
||||
|
||||
static std::size_t declarationCacheSlot(const std::uint32_t* words, std::uint32_t length) {
|
||||
std::uint64_t hash = 14695981039346656037ull;
|
||||
for (std::uint32_t index = 0; index < length; index++) {
|
||||
hash ^= words[index];
|
||||
hash *= 1099511628211ull;
|
||||
}
|
||||
return static_cast<std::size_t>(hash) & (DeclarationCacheSize - 1u);
|
||||
}
|
||||
|
||||
template<typename TDeclare>
|
||||
std::uint32_t interned(const std::uint32_t* words, std::uint32_t length, TDeclare&& declare) {
|
||||
const auto start = declarationCacheSlot(words, length);
|
||||
std::size_t target = start;
|
||||
for (std::size_t probe = 0; probe < DeclarationCacheSize; ++probe) {
|
||||
target = (start + probe) & (DeclarationCacheSize - 1u);
|
||||
auto& slot = declarationCache[target];
|
||||
if (slot.id == 0) {
|
||||
break;
|
||||
}
|
||||
if (slot.length == length && std::equal(words, words + length, slot.words.begin())) {
|
||||
return slot.id;
|
||||
}
|
||||
}
|
||||
const auto id = declare();
|
||||
auto& slot = declarationCache[target];
|
||||
std::copy(words, words + length, slot.words.begin());
|
||||
slot.length = length;
|
||||
slot.id = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
static void appendOperand(std::vector<std::uint32_t>& words, std::uint32_t value) {
|
||||
words.push_back(value);
|
||||
}
|
||||
@@ -147,6 +232,7 @@ private:
|
||||
std::set<std::string> requiredExtensions;
|
||||
std::map<std::string, std::uint32_t> importIds;
|
||||
std::map<std::vector<std::uint32_t>, std::uint32_t> declarationIds;
|
||||
std::array<DeclarationSlot, DeclarationCacheSize> declarationCache = {};
|
||||
std::size_t unpatchedPhiIncomings = 0;
|
||||
std::vector<std::uint32_t> capabilities;
|
||||
std::vector<std::uint32_t> extensions;
|
||||
|
||||
Reference in New Issue
Block a user