diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvModule.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvModule.hpp index ce1d7b620..a8e1f5048 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvModule.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvModule.hpp @@ -1,6 +1,8 @@ #ifndef CORE_SHADER_RECOMPILIER_SPIRVBACKEND_INCLUDE_SPIRVBACKEND_SPIRVMODULE_HPP #define CORE_SHADER_RECOMPILIER_SPIRVBACKEND_INCLUDE_SPIRVBACKEND_SPIRVMODULE_HPP +#include +#include #include #include #include @@ -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 + requires wordSized + 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 + requires (wordSized && wordSized) + 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 std::uint32_t DecoratedType(std::uint32_t opcode, std::initializer_list annotations, const TOperands&... operands) { return declareDecoratedType(opcode, makeTypeKey(opcode, operands...), annotations); @@ -64,6 +88,19 @@ public: return declareConstant(opcode, std::move(key)); } + template + requires wordSized + 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 keyWords; + keyWords.reserve(3u); + appendOperands(keyWords, opcode, type, value); + return declareConstant(opcode, std::move(keyWords)); + }); + } + template 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 + static constexpr bool wordSized = (std::is_integral_v || std::is_enum_v) && sizeof(TOperand) <= sizeof(std::uint32_t); + + template + static std::uint32_t narrowWord(TOperand operand) { + return static_cast(operand); + } + + struct DeclarationSlot { + std::array 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(hash) & (DeclarationCacheSize - 1u); + } + + template + 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& words, std::uint32_t value) { words.push_back(value); } @@ -147,6 +232,7 @@ private: std::set requiredExtensions; std::map importIds; std::map, std::uint32_t> declarationIds; + std::array declarationCache = {}; std::size_t unpatchedPhiIncomings = 0; std::vector capabilities; std::vector extensions;