Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,59 @@

#include <cstdint>
#include <span>
#include <unordered_map>
#include <vector>

namespace ShaderRecompiler::Detail {

class ValueCache {
public:
const std::uint64_t* Find(const IrValue* key) const {
if (_slots.empty()) return nullptr;
for (auto index = slotOf(key);; index = (index + 1) & (_slots.size() - 1)) {
const auto& slot = _slots[index];
if (slot.key == key) return &slot.value;
if (slot.key == nullptr) return nullptr;
}
}
void Insert(const IrValue* key, std::uint64_t value) {
if ((_count + 1) * 4 > _slots.size() * 3) grow();
for (auto index = slotOf(key);; index = (index + 1) & (_slots.size() - 1)) {
auto& slot = _slots[index];
if (slot.key == key) {
slot.value = value;
return;
}
if (slot.key == nullptr) {
slot = {key, value};
++_count;
return;
}
}
}

private:
struct Slot {
const IrValue* key = nullptr;
std::uint64_t value = 0;
};
std::size_t slotOf(const IrValue* key) const {
auto bits = reinterpret_cast<std::uintptr_t>(key);
bits ^= bits >> 17u;
bits *= 0x9e3779b97f4a7c15ull;
return static_cast<std::size_t>(bits >> 32u) & (_slots.size() - 1);
}
void grow() {
auto old = std::move(_slots);
_slots.assign(old.empty() ? 64 : old.size() * 2, Slot{});
_count = 0;
for (const auto& slot : old) {
if (slot.key != nullptr) Insert(slot.key, slot.value);
}
}
std::vector<Slot> _slots;
std::size_t _count = 0;
};

class Evaluator {
public:
Evaluator(const IrResourcePlan& program, const SrtRuntime& runtime, std::span<const std::uint8_t> cleanFlatSlots = {}, Evaluator* cleanEvaluator = nullptr, IrValue* activeMask = nullptr) : _program(program), _runtime(runtime), _cleanFlatSlots(cleanFlatSlots), _cleanEvaluator(cleanEvaluator), _activeMask(activeMask != nullptr ? activeMask->Resolve() : nullptr) {}
Expand All @@ -33,7 +81,7 @@ class Evaluator {
std::span<const std::uint8_t> _cleanFlatSlots;
Evaluator* _cleanEvaluator = nullptr;
IrValue* _activeMask = nullptr;
std::unordered_map<IrValue*, std::uint64_t> _cache;
ValueCache _cache;
std::vector<IrValue*> _visiting;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ bool Evaluator::EvaluateWide(IrValue* raw, std::uint64_t& result) {
if (_activeMask != nullptr && IsRuntimeSelect(inst->Opcode()) && inst->ArgumentCount() == 3 && inst->Argument(0)->Resolve() == _activeMask) {
return EvaluateWide(inst->Argument(1), result);
}
if (const auto found = _cache.find(inst); found != _cache.end()) {
result = found->second;
if (const auto* found = _cache.Find(inst)) {
result = *found;
return true;
}
if (std::find(_visiting.begin(), _visiting.end(), inst) != _visiting.end()) {
Expand All @@ -58,7 +58,7 @@ bool Evaluator::EvaluateWide(IrValue* raw, std::uint64_t& result) {
if (debug) std::fprintf(stderr, "[srt] cannot evaluate %s (%zu arguments)\n", std::string(IrOpcodeName(inst->Opcode())).c_str(), inst->ArgumentCount());
return false;
}
_cache.emplace(inst, out);
_cache.Insert(inst, out);
result = out;
return true;
}
Expand Down
Loading