#include "Optimization/ResourceTracker.hpp" #include "Optimization/DeadCodeEliminator.hpp" #include "Optimization/SrtWalker.hpp" #include "Optimization/ResourceMaterializer.hpp" #include "SpirvBackend/SpirvBufferFormat.hpp" #include "IntermediateRepresentation/IrBuilder.hpp" #include "Optimization/SrtWalker/SrtInstructionPredicates.hpp" #include "RdnaDecoder/RdnaDescriptorFormat.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ShaderRecompiler { namespace { constexpr std::uint32_t samplerBorderClampMask = (1u << 2u) | (1u << 5u) | (1u << 8u); constexpr std::uint32_t samplerDword3ReservedMask = 0x3ffff000u; std::uint32_t knownSetBits(const IrValue* value, unsigned depth = 0u) { value = value->Resolve(); if (value->HasImmediate()) return value->ImmediateU32(); if (depth >= 8u || value->ArgumentCount() != 2u) return 0u; if (value->Opcode() != IrOpcode::BitwiseOr32 && value->Opcode() != IrOpcode::BitwiseAnd32 && value->Opcode() != IrOpcode::ShiftLeftLogical32 && value->Opcode() != IrOpcode::ShiftRightLogical32) return 0u; const auto left = knownSetBits(value->Argument(0), depth + 1u); const auto right = knownSetBits(value->Argument(1), depth + 1u); if (value->Opcode() == IrOpcode::BitwiseOr32) return left | right; if (value->Opcode() == IrOpcode::BitwiseAnd32) return left & right; if (value->Argument(1)->Resolve()->HasImmediate()) { const auto shift = right & 31u; if (value->Opcode() == IrOpcode::ShiftLeftLogical32) return left << shift; if (value->Opcode() == IrOpcode::ShiftRightLogical32) return left >> shift; } return 0u; } [[noreturn]] void fail(const std::string& message) { throw std::runtime_error(message); } // Debug aid: APS5_TRACE_BDA=1 names the accesses that make a program address-based (see Collect). bool bdaTraceEnabled() { static const bool enabled = std::getenv("APS5_TRACE_BDA") != nullptr; return enabled; } constexpr unsigned bdaTraceLimit = 8; std::string formatHex32(std::uint32_t value) { static constexpr char digits[] = "0123456789abcdef"; std::string hex(8u, '0'); for (std::uint32_t index = 0; index < 8u; index++) { hex[7u - index] = digits[(value >> (index * 4u)) & 0xfu]; } return hex; } std::string describeValueChain(const IrValue* value, std::uint32_t depth) { value = value->Resolve(); if (value->HasImmediate()) { return "Immediate"; } std::string text = std::string(IrOpcodeName(value->Opcode())); if (value->Opcode() == IrOpcode::LoadAddressU32 || value->Opcode() == IrOpcode::ReadConstBuffer) { text += "@pc=0x" + formatHex32(value->Flags().pc); } if (depth == 0u || value->ArgumentCount() == 0u) { return text; } text += "("; for (std::uint32_t index = 0; index < value->ArgumentCount(); index++) { if (index != 0u) { text += ", "; } text += describeValueChain(value->Argument(index), depth - 1u); } text += ")"; return text; } std::uint32_t possibleU32BitsDepth(const IrValue* value, std::uint32_t depth) { value = value->Resolve(); if (value->HasImmediate()) { return value->Type() == IrType::U32 ? value->ImmediateU32() : std::numeric_limits::max(); } if (depth == 0u) return std::numeric_limits::max(); switch (value->Opcode()) { case IrOpcode::BitwiseAnd32: return possibleU32BitsDepth(value->Argument(0), depth - 1u) & possibleU32BitsDepth(value->Argument(1), depth - 1u); case IrOpcode::BitwiseOr32: return possibleU32BitsDepth(value->Argument(0), depth - 1u) | possibleU32BitsDepth(value->Argument(1), depth - 1u); case IrOpcode::ShiftLeftLogical32: { const IrValue* shift = value->Argument(1)->Resolve(); return shift->HasImmediate() && shift->Type() == IrType::U32 ? possibleU32BitsDepth(value->Argument(0), depth - 1u) << (shift->ImmediateU32() & 31u) : std::numeric_limits::max(); } case IrOpcode::ReadFirstLane: return value->ArgumentCount() >= 1u ? possibleU32BitsDepth(value->Argument(0), depth - 1u) : std::numeric_limits::max(); case IrOpcode::SelectU32: return value->ArgumentCount() == 3u ? (possibleU32BitsDepth(value->Argument(1), depth - 1u) | possibleU32BitsDepth(value->Argument(2), depth - 1u)) : std::numeric_limits::max(); default: return std::numeric_limits::max(); } } std::uint32_t possibleU32Bits(const IrValue* value) { return possibleU32BitsDepth(value, 8u); } std::uint32_t possibleU32UpperBoundDepth(const IrValue* value, std::uint32_t depth) { value = value->Resolve(); if (value->HasImmediate()) { return value->Type() == IrType::U32 ? value->ImmediateU32() : std::numeric_limits::max(); } if (depth == 0u) return std::numeric_limits::max(); const auto add = [](std::uint32_t left, std::uint32_t right) { return left > std::numeric_limits::max() - right ? std::numeric_limits::max() : left + right; }; switch (value->Opcode()) { case IrOpcode::BitwiseAnd32: return std::min(possibleU32UpperBoundDepth(value->Argument(0), depth - 1u), possibleU32UpperBoundDepth(value->Argument(1), depth - 1u)); case IrOpcode::BitwiseOr32: return add(possibleU32UpperBoundDepth(value->Argument(0), depth - 1u), possibleU32UpperBoundDepth(value->Argument(1), depth - 1u)); case IrOpcode::ShiftLeftLogical32: { const IrValue* shift = value->Argument(1)->Resolve(); if (!shift->HasImmediate() || shift->Type() != IrType::U32) return std::numeric_limits::max(); const auto bits = shift->ImmediateU32() & 31u; const auto upper = possibleU32UpperBoundDepth(value->Argument(0), depth - 1u); return bits != 0u && upper > (std::numeric_limits::max() >> bits) ? std::numeric_limits::max() : upper << bits; } case IrOpcode::ReadFirstLane: return value->ArgumentCount() >= 1u ? possibleU32UpperBoundDepth(value->Argument(0), depth - 1u) : std::numeric_limits::max(); case IrOpcode::SelectU32: return value->ArgumentCount() == 3u ? (possibleU32UpperBoundDepth(value->Argument(1), depth - 1u) | possibleU32UpperBoundDepth(value->Argument(2), depth - 1u)) : std::numeric_limits::max(); case IrOpcode::UMin32: return value->ArgumentCount() == 2u ? std::min(possibleU32UpperBoundDepth(value->Argument(0), depth - 1u), possibleU32UpperBoundDepth(value->Argument(1), depth - 1u)) : std::numeric_limits::max(); case IrOpcode::UMax32: return value->ArgumentCount() == 2u ? std::max(possibleU32UpperBoundDepth(value->Argument(0), depth - 1u), possibleU32UpperBoundDepth(value->Argument(1), depth - 1u)) : std::numeric_limits::max(); case IrOpcode::IAdd32: return value->ArgumentCount() == 2u ? add(possibleU32UpperBoundDepth(value->Argument(0), depth - 1u), possibleU32UpperBoundDepth(value->Argument(1), depth - 1u)) : std::numeric_limits::max(); case IrOpcode::Phi: { std::uint32_t upper = 0; for (std::uint32_t index = 0; index < value->ArgumentCount(); ++index) upper |= possibleU32UpperBoundDepth(value->Argument(index), depth - 1u); return upper; } case IrOpcode::FindILsb32: case IrOpcode::FindUMsb32: return 31u; default: return std::numeric_limits::max(); } } std::uint32_t possibleU32UpperBound(const IrValue* value) { return possibleU32UpperBoundDepth(value, 8u); } std::uint32_t byteExtent(const MemoryInfo& memory) { const auto bytes = std::max((memory.dataBits + 7u) / 8u, 1u); const auto count = std::max(memory.dataDwords, 1u); const auto end = static_cast(memory.offset) + static_cast(bytes) * count; return end > std::numeric_limits::max() ? std::numeric_limits::max() : static_cast(end); } class Tracker { public: explicit Tracker(IrProgram& program) : m_program(program), m_info(program.Resources().info), m_builder(program) { m_info.buffers.clear(); m_info.images.clear(); m_info.samplers.clear(); m_info.sampledPairs.clear(); m_info.usesDma = false; m_info.bdaWrites = false; } void Run() { if (m_program.Resources().resourceTrackingComplete) { fail("resources already tracked"); } if (!m_program.Resources().srtPlanComplete) { fail("SRT plan is not ready"); } SplitDescriptorPhis(); if (m_foldedWeb) { DeadCodeEliminator{}.Eliminate(m_program); } PlanTableColumns(); std::unordered_map blockIndices; for (const IrBlock* block : m_program.BlockOrder()) { blockIndices.emplace(block, static_cast(blockIndices.size())); } m_blockSources.assign(blockIndices.size(), {}); m_blockImages.assign(blockIndices.size(), {}); for (auto& block : m_program.Blocks()) { const auto found = blockIndices.find(block.get()); m_currentBlock = found == blockIndices.end() ? std::numeric_limits::max() : found->second; for (IrValue* inst : block->Instructions()) { Collect(*inst); } } m_currentBlock = std::numeric_limits::max(); EmitDescriptorGuards(); PlanResourceControlFlow(); if (m_bdaTraces > bdaTraceLimit) std::fprintf(stderr, "[bda] %u more address accesses in this program not shown\n", m_bdaTraces - bdaTraceLimit); CheckTableStores(); AssignTables(); LinkImageAliases(); for (const auto& patch : m_handlePatches) { patch.handle->SetFlags(patch.resource); } for (const auto& patch : m_memoryPatches) { auto& memory = m_program.Resources().memoryInfo[patch.index]; memory.resource = patch.resource; if (patch.hasSampler) { memory.sampler = patch.sampler; } } std::vector referenced; for (const auto& plan : m_tableColumns) { const auto& column = *m_sources[plan.source].tableColumn; IrValue* offset = column.selected ? &m_builder.Constant(column.addend) : plan.offset; for (std::uint32_t argument = 0; argument < plan.handle->ArgumentCount(); argument++) { const auto value = plan.parentPointer != nullptr && (argument == 1u || argument == 2u) ? plan.parentPointer->Argument(argument - 1u) : offset; plan.handle->ReplaceArgument(argument, value); } KeepSourceAlive(*plan.handle, m_sources[column.heapSource], referenced); if (column.selected) { KeepSourceAlive(*plan.handle, m_sources[column.selection], referenced); } if (plan.keyDomain.has_value()) { KeepSourceAlive(*plan.handle, m_sources[plan.keyDomain->source], referenced); } } for (const auto index : m_tablePlanningMemory) { m_program.Resources().memoryInfo[index].planningOnly = true; } std::erase_if(m_program.Metadata().dynamicReads, [&](IrValue* value) { const IrValue* inst = value->Resolve(); return std::ranges::find(m_tablePlanningReads, inst) != m_tablePlanningReads.end(); }); m_program.Resources().descriptorSources = std::move(m_sources); m_program.Resources().info = std::move(m_info); m_program.Resources().resourceTrackingComplete = true; } private: struct HandlePatch { IrValue* handle = nullptr; std::uint32_t resource = 0; }; struct MemoryPatch { std::uint32_t index = 0; std::uint32_t resource = 0; std::uint32_t sampler = 0; bool hasSampler = false; }; struct TableColumnPlan { IrValue* handle = nullptr; std::uint32_t source = 0; IrValue* offset = nullptr; IrValue* parentPointer = nullptr; std::vector memory; std::vector reads; std::vector hopMemory; std::vector hopReads; std::optional keyDomain; std::vector heapLive; std::vector heapPlanned; }; struct DescriptorGuard { IrValue* inst = nullptr; std::vector live; std::vector planned; }; void MakeSource(const IrValue& handle, std::uint32_t width, bool sampler, bool sampleAdjust, DescriptorSource& descriptor) { if (handle.ArgumentCount() != width) { fail(std::string(IrOpcodeName(handle.Opcode())) + " has " + std::to_string(handle.ArgumentCount()) + " descriptor dwords, expected " + std::to_string(width)); } std::array words {}; for (std::uint32_t i = 0; i < width; i++) { words[i] = handle.Argument(i); } MakeSource(words, width, sampler, sampleAdjust, descriptor); } void MakeSource(const std::array& words, std::uint32_t width, bool sampler, bool sampleAdjust, DescriptorSource& descriptor) { descriptor.dwordCount = width; for (std::uint32_t i = 0; i < width; i++) { descriptor.dwords[i] = words[i]->Resolve(); } if (sampleAdjust) { descriptor.dwords[3] = canonicalizeSampleAdjustDword3(descriptor.dwords[3]); } const IrValue* dword0 = descriptor.dwords[0]->Resolve(); if (sampler && dword0->HasImmediate() && dword0->Type() == IrType::U32 && (dword0->ImmediateU32() & samplerBorderClampMask) == 0u) { descriptor.dwords[3] = &m_builder.Constant(0u); } } IrValue* canonicalizeSampleAdjustDword3(IrValue* value) { for (;;) { value = value->Resolve(); if ((possibleU32Bits(value) & ~samplerDword3ReservedMask) == 0u) { return &m_builder.Constant(0u); } if (value->Opcode() != IrOpcode::BitwiseOr32) { return value; } IrValue* left = value->Argument(0)->Resolve(); IrValue* right = value->Argument(1)->Resolve(); const bool leftReserved = (possibleU32Bits(left) & ~samplerDword3ReservedMask) == 0u; const bool rightReserved = (possibleU32Bits(right) & ~samplerDword3ReservedMask) == 0u; if (leftReserved) { value = right; } else if (rightReserved) { value = left; } else { return value; } } } bool ValidateSource(const DescriptorSource& descriptor, std::uint32_t& badDword) const { constexpr SrtWalker walker; for (std::uint32_t i = 0; i < descriptor.dwordCount; i++) { badDword = i; if (descriptor.dwords[i]->Resolve()->Type() != IrType::U32) { return false; } if (!walker.ValidateRuntimeValue(m_program.Resources(), descriptor.dwords[i])) { return false; } } return true; } std::uint32_t InternSource(DescriptorSource descriptor) { if (!m_readOrigins.empty()) { std::vector pending(descriptor.dwords.begin(), descriptor.dwords.begin() + descriptor.dwordCount); std::unordered_set visited; const auto& order = m_program.BlockOrder(); while (!pending.empty()) { const IrValue* value = pending.back()->Resolve(); pending.pop_back(); if (!visited.insert(value).second) continue; if (const auto origin = m_readOrigins.find(value); origin != m_readOrigins.end()) { const auto block = std::ranges::find(order, origin->second); if (block == order.end()) fail("copied descriptor read has no resource control-flow block"); descriptor.requiredBlocks.push_back(static_cast(block - order.begin())); } for (const IrValue* argument : value->Arguments()) pending.push_back(argument); } std::ranges::sort(descriptor.requiredBlocks); descriptor.requiredBlocks.erase(std::unique(descriptor.requiredBlocks.begin(), descriptor.requiredBlocks.end()), descriptor.requiredBlocks.end()); } for (std::uint32_t candidate = 0; candidate < m_sources.size(); candidate++) { const auto& current = m_sources[candidate]; if (current.dwordCount != descriptor.dwordCount || current.tableColumn != descriptor.tableColumn || current.requiredBlocks != descriptor.requiredBlocks) { continue; } bool same = true; for (std::uint32_t i = 0; i < descriptor.dwordCount; i++) { same = same && EquivalentValue(m_program.Resources(), current.dwords[i], descriptor.dwords[i]); } if (same) { NoteBlockSource(candidate); return candidate; } } m_sources.push_back(descriptor); NoteBlockSource(static_cast(m_sources.size() - 1)); return static_cast(m_sources.size() - 1); } void NoteBlockImage(std::uint32_t image) { if (m_currentBlock >= m_blockImages.size()) { return; } auto& images = m_blockImages[m_currentBlock]; if (std::ranges::find(images, image) == images.end()) { images.push_back(image); } } void NoteBlockSource(std::uint32_t source) { if (m_currentBlock >= m_blockSources.size()) { return; } auto& sources = m_blockSources[m_currentBlock]; if (std::ranges::find(sources, source) == sources.end()) { sources.push_back(source); } } void PlanResourceControlFlow() { const auto& order = m_program.BlockOrder(); const auto& info = m_program.Metadata().blockInfo; if (order.empty() || order.size() != info.size()) { if (std::ranges::any_of(m_sources, [](const DescriptorSource& source) { return !source.requiredBlocks.empty(); })) fail("copied descriptor reads require resource control flow"); return; } std::unordered_map indexOf; std::unordered_map idToIndex; for (std::uint32_t index = 0; index < order.size(); index++) { indexOf.emplace(order[index], index); idToIndex.emplace(info[index].id, index); } std::vector controlFlow(order.size()); for (std::uint32_t index = 0; index < order.size(); index++) { auto& block = controlFlow[index]; const auto& terminator = info[index].terminator; const auto target = [&](std::uint32_t id) { return idToIndex.find(id); }; const auto trueEdge = target(terminator.trueBlock); const auto falseEdge = target(terminator.falseBlock); if (terminator.kind == TerminatorKind::ConditionalBranch && !terminator.loopHeader && info[index].condition != nullptr && trueEdge != idToIndex.end() && falseEdge != idToIndex.end()) { block.condition = info[index].condition; block.successors = {trueEdge->second, falseEdge->second}; } else if (terminator.kind != TerminatorKind::Return) { for (const IrBlock* successor : order[index]->Successors()) { const auto found = indexOf.find(successor); if (found == indexOf.end()) { return; } block.successors.push_back(found->second); } } block.sources = m_blockSources[index]; block.images = m_blockImages[index]; for (const IrValue* inst : order[index]->Instructions()) { if (inst->Opcode() != IrOpcode::ReadConst || inst->ArgumentCount() != 2u || inst->Argument(0)->Resolve()->Opcode() != IrOpcode::GetSrtResource) continue; const IrValue* slot = inst->Argument(1)->Resolve(); if (!slot->HasImmediate() || slot->Type() != IrType::U32 || slot->ImmediateU32() >= m_program.Resources().srtReads.size()) fail("resource control flow has an invalid flat SRT slot"); if (std::ranges::find(block.flatSlots, slot->ImmediateU32()) == block.flatSlots.end()) block.flatSlots.push_back(slot->ImmediateU32()); } } std::vector unconditional(order.size()); for (std::uint32_t index = 0; index < order.size() && unconditional[index] == 0u;) { unconditional[index] = 1u; if (controlFlow[index].successors.size() != 1u) { break; } index = controlFlow[index].successors.front(); } std::vector reached(m_sources.size()); std::vector reachedImages(m_info.images.size()); std::vector reachedFlat(m_program.Resources().srtReads.size()); for (std::uint32_t index = 0; index < order.size(); index++) { if (unconditional[index] == 0u) { continue; } for (const auto source : controlFlow[index].sources) { reached[source] = 1u; } for (const auto image : controlFlow[index].images) { reachedImages[image] = 1u; } for (const auto slot : controlFlow[index].flatSlots) { reachedFlat[slot] = 1u; } } bool guarded = std::ranges::any_of(m_sources, [](const DescriptorSource& source) { return !source.requiredBlocks.empty(); }); for (const auto& block : controlFlow) { for (const auto source : block.sources) { guarded = guarded || reached[source] == 0u; } for (const auto image : block.images) { guarded = guarded || reachedImages[image] == 0u; } for (const auto slot : block.flatSlots) { guarded = guarded || reachedFlat[slot] == 0u; } } if (guarded) { m_program.Resources().controlFlow = std::move(controlFlow); } } static bool immediateU32(IrValue* value, std::uint32_t& result) { value = value->Resolve(); if (!value->HasImmediate() || value->Type() != IrType::U32) { return false; } result = value->ImmediateU32(); return true; } const MemoryInfo* ScalarReadMemory(const IrValue& read, std::uint32_t& index) const { if (read.Opcode() != IrOpcode::ReadConstBuffer || read.ArgumentCount() != 2u) { return nullptr; } index = read.Flags().index; if (index >= m_program.Resources().memoryInfo.size()) { return nullptr; } const auto& memory = m_program.Resources().memoryInfo[index]; return memory.kind == ResourceKind::ScalarBuffer && memory.dataBits == 32u && memory.dataDwords == 1u ? &memory : nullptr; } const MemoryInfo* ScalarAddressMemory(const IrValue& read, std::uint32_t& index) const { if (read.Opcode() != IrOpcode::LoadAddressU32 || read.ArgumentCount() != 4u) { return nullptr; } index = read.Flags().index; if (index >= m_program.Resources().memoryInfo.size()) { return nullptr; } const auto& memory = m_program.Resources().memoryInfo[index]; std::uint32_t high = 0; const IrValue* active = read.Argument(3)->Resolve(); const bool always = active->HasImmediate() && active->Type() == IrType::U1 && active->ImmediateBool(); return memory.kind == ResourceKind::ScalarAddress && !memory.addressIsFull && memory.dataBits == 32u && memory.dataDwords == 1u && immediateU32(read.Argument(2), high) && high == 0u && always ? &memory : nullptr; } static bool AccessesMemory(IrOpcode op) { return BufferAccessOf(op) != BufferAccess::None || AddressOpcodeInfoOf(op).access != AddressAccess::None || ImageOpcodeInfoOf(op).access != ImageAccess::None; } void IndexMemoryUsers() { for (const auto& block : m_program.Blocks()) { for (const IrValue* inst : block->Instructions()) { if (AccessesMemory(inst->Opcode())) { m_memoryUsers[inst->Flags().index].push_back(inst); } } } m_memoryUsersIndexed = true; } bool MemoryIndexBelongsTo(std::uint32_t index, const IrValue& owner) const { if (m_memoryUsersIndexed) { const auto users = m_memoryUsers.find(index); return users == m_memoryUsers.end() || std::ranges::all_of(users->second, [&](const IrValue* user) { return user == &owner; }); } for (const auto& block : m_program.Blocks()) { for (const IrValue* inst : block->Instructions()) { if (!AccessesMemory(inst->Opcode()) || inst == &owner) { continue; } if (inst->Flags().index == index) { return false; } } } return true; } bool MakeRuntimeSource(const IrValue& handle, IrOpcode expected, std::uint32_t width, std::uint32_t& source, DescriptorSource& descriptor) { if (handle.Opcode() != expected) { return false; } MakeSource(handle, width, false, false, descriptor); std::uint32_t badDword = 0; if (!ValidateSource(descriptor, badDword)) { return false; } source = InternSource(descriptor); return true; } static bool MatchScale(IrValue* value, IrValue*& key, std::uint32_t& stride) { value = value->Resolve(); if (value->ArgumentCount() != 2u) { return false; } std::uint32_t immediate = 0; if (value->Opcode() == IrOpcode::ShiftLeftLogical32) { if (!immediateU32(value->Argument(1), immediate) || immediate >= 32u) { return false; } key = value->Argument(0)->Resolve(); stride = 1u << immediate; return true; } if (value->Opcode() != IrOpcode::IMul32) { return false; } if (immediateU32(value->Argument(1), immediate)) { key = value->Argument(0)->Resolve(); } else if (immediateU32(value->Argument(0), immediate)) { key = value->Argument(1)->Resolve(); } else { return false; } stride = immediate; return true; } bool MatchAffineOffset(IrValue* value, IrValue*& key, std::uint32_t& stride, std::uint32_t& addend) const { value = value->Resolve(); if (value->Opcode() == IrOpcode::ReadFirstLane && value->ArgumentCount() == 2u) { const auto* activeMask = value->Argument(1)->Resolve(); value = value->Argument(0)->Resolve(); while (value->Opcode() == IrOpcode::SelectU32 && value->ArgumentCount() == 3u && Detail::ActiveMaskImplies(m_program.Resources(), activeMask, value->Argument(0))) { value = value->Argument(1)->Resolve(); } } addend = 0; bool matched = MatchScale(value, key, stride); if (!matched && value->Opcode() == IrOpcode::IAdd32 && value->ArgumentCount() == 2u) { if (immediateU32(value->Argument(0), addend)) { matched = MatchScale(value->Argument(1), key, stride); } else if (immediateU32(value->Argument(1), addend)) { matched = MatchScale(value->Argument(0), key, stride); } } return matched && stride >= 4u && stride % 4u == 0u && key->Type() == IrType::U32; } static bool MatchMaskedOffset(IrValue* value, IrValue*& key, std::uint32_t& stride) { value = value->Resolve(); if (value->Opcode() != IrOpcode::BitwiseAnd32 || value->ArgumentCount() != 2u) { return false; } std::uint32_t mask = 0; IrValue* scaled = nullptr; if (immediateU32(value->Argument(1), mask)) { scaled = value->Argument(0); } else if (immediateU32(value->Argument(0), mask)) { scaled = value->Argument(1); } else { return false; } return MatchScale(scaled, key, stride) && std::has_single_bit(stride) && stride >= 4u && (mask & ~(stride - 1u)) != 0u && key->Type() == IrType::U32; } IrValue* ActiveLaneValue(IrValue* value, const IrValue* mask) const { value = value->Resolve(); while (mask != nullptr && Detail::IsRuntimeSelect(value->Opcode()) && value->ArgumentCount() == 3u && Detail::ActiveMaskImplies(m_program.Resources(), mask, value->Argument(0))) { value = value->Argument(1)->Resolve(); } return value; } std::uint32_t ScaledBase(IrValue* value, const IrValue* mask, IrValue*& base) const { value = ActiveLaneValue(value, mask); std::uint32_t immediate = 0; std::uint32_t scale = 1u; while (value->ArgumentCount() == 2u && immediateU32(value->Argument(1), immediate) && ((value->Opcode() == IrOpcode::ShiftLeftLogical32 && immediate < 32u) || value->Opcode() == IrOpcode::IMul32)) { const std::uint64_t next = static_cast(scale) * (value->Opcode() == IrOpcode::IMul32 ? immediate : 1u << immediate); if (next > std::numeric_limits::max()) { break; } scale = static_cast(next); value = ActiveLaneValue(value->Argument(0), mask); } base = value; return scale; } struct MultipleSearch { std::vector visiting; std::unordered_map known; const IrValue* mask = nullptr; bool cycle = false; }; std::uint32_t KnownMultiple(IrValue* value, MultipleSearch& search, std::uint32_t depth) const { value = ActiveLaneValue(value, search.mask); std::uint32_t immediate = 0; if (immediateU32(value, immediate)) { return immediate; } if (std::ranges::find(search.visiting, value) != search.visiting.end()) { search.cycle = true; return 0u; } if (const auto found = search.known.find(value); found != search.known.end()) { return found->second; } if (depth == 0u || value->Type() != IrType::U32) { return 1u; } const bool outerCycle = search.cycle; search.cycle = false; auto& visiting = search.visiting; visiting.push_back(value); const auto product = [](std::uint32_t multiple, std::uint32_t scale) { const std::uint64_t result = static_cast(multiple) * scale; return result > std::numeric_limits::max() ? std::uint32_t{1u} << std::countr_zero(scale) : static_cast(result); }; std::uint32_t multiple = 1u; const auto op = value->Opcode(); if (value->IsPhi()) { multiple = 0u; for (std::size_t index = 0; index < value->ArgumentCount(); index++) { multiple = std::gcd(multiple, KnownMultiple(value->Argument(index), search, depth - 1u)); } } else if ((op == IrOpcode::ShiftLeftLogical32 || op == IrOpcode::IMul32) && value->ArgumentCount() == 2u && immediateU32(value->Argument(1), immediate)) { IrValue* base = nullptr; const auto scale = ScaledBase(value, search.mask, base); multiple = product(KnownMultiple(base, search, depth - 1u), scale); } else if (op == IrOpcode::IAdd32 && value->ArgumentCount() == 2u) { IrValue* left = nullptr; IrValue* right = nullptr; const auto leftScale = ScaledBase(value->Argument(0), search.mask, left); const auto rightScale = ScaledBase(value->Argument(1), search.mask, right); const std::uint64_t sum = static_cast(leftScale) + rightScale; if (EquivalentValue(m_program.Resources(), left, right) && sum <= std::numeric_limits::max()) { multiple = product(KnownMultiple(left, search, depth - 1u), static_cast(sum)); } else { multiple = std::gcd(KnownMultiple(value->Argument(0), search, depth - 1u), KnownMultiple(value->Argument(1), search, depth - 1u)); } } else if (Detail::IsRuntimeSelect(op) && value->ArgumentCount() == 3u) { multiple = std::gcd(KnownMultiple(value->Argument(1), search, depth - 1u), KnownMultiple(value->Argument(2), search, depth - 1u)); } else if (op == IrOpcode::ReadFirstLane && value->ArgumentCount() == 2u && search.mask == nullptr) { search.mask = value->Argument(1)->Resolve(); multiple = KnownMultiple(value->Argument(0), search, depth - 1u); search.mask = nullptr; } visiting.pop_back(); if (!search.cycle) { search.known.emplace(value, multiple); } search.cycle = search.cycle || outerCycle; return multiple; } bool MatchFirstLaneOffset(IrValue* value, IrValue*& key, std::uint32_t& stride) const { value = value->Resolve(); if (value->Opcode() != IrOpcode::ReadFirstLane || value->ArgumentCount() != 2u || value->Type() != IrType::U32) { return false; } MultipleSearch search; const auto multiple = KnownMultiple(value, search, firstLaneMultipleDepth); if (multiple < 4u || multiple % 4u != 0u) { return false; } key = value; stride = multiple; return true; } static bool ReadsThroughDescriptor(const IrValue* value) { std::vector pending{value}; std::vector seen; while (!pending.empty()) { const IrValue* current = pending.back()->Resolve(); pending.pop_back(); if (std::find(seen.begin(), seen.end(), current) != seen.end()) continue; seen.push_back(current); if (current->Opcode() == IrOpcode::ReadConstBuffer || current->Opcode() == IrOpcode::LoadAddressU32) return true; for (std::size_t index = 0; index < current->ArgumentCount(); index++) pending.push_back(current->Argument(index)); } return false; } std::optional MatchKeyDomain(IrValue* key) { key = key->Resolve(); while (key->Opcode() == IrOpcode::ReadFirstLane && key->ArgumentCount() >= 1u) key = key->Argument(0)->Resolve(); DescriptorSource direct; direct.dwordCount = 1u; direct.dwords[0] = key; std::uint32_t badDword = 0; if (ValidateSource(direct, badDword) && !ReadsThroughDescriptor(key)) { KeyDomain domain{InternSource(direct), 0u, 4u}; domain.value = true; return domain; } std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarReadMemory(*key, memoryIndex); if (memory == nullptr) { return std::nullopt; } DescriptorSource source; std::uint32_t sourceIndex = 0; if (!MakeRuntimeSource(*key->Argument(0)->Resolve(), IrOpcode::GetBufferResource, 4u, sourceIndex, source)) { return std::nullopt; } IrValue* selector = nullptr; std::uint32_t stride = 0; std::uint32_t addend = 0; if (MatchAffineOffset(key->Argument(1), selector, stride, addend)) { return KeyDomain{sourceIndex, memory->offset + addend, stride}; } const auto bits = possibleU32Bits(key->Argument(1)); const auto zeros = bits == 0u ? 31u : std::min(static_cast(std::countr_zero(bits)), 31u); return KeyDomain{sourceIndex, memory->offset, std::max(4u, 1u << zeros)}; } static IrValue* SplitImmediateAddend(IrValue* value, std::uint32_t& addend) { value = value->Resolve(); addend = 0; while (value->Opcode() == IrOpcode::IAdd32 && value->ArgumentCount() == 2u) { std::uint32_t immediate = 0; IrValue* rest = nullptr; if (immediateU32(value->Argument(0), immediate)) rest = value->Argument(1)->Resolve(); else if (immediateU32(value->Argument(1), immediate)) rest = value->Argument(0)->Resolve(); else break; addend += immediate; value = rest; } return value; } struct FirstLaneOffset { const IrValue* lane = nullptr; const IrValue* mask = nullptr; std::uint32_t shift = 0; std::uint32_t addend = 0; }; bool ParseFirstLaneOffset(IrValue* value, const IrValue* mask, FirstLaneOffset& form, std::uint32_t depth) const { std::uint32_t addend = 0; value = SplitImmediateAddend(value, addend); while (mask != nullptr && Detail::IsRuntimeSelect(value->Opcode()) && value->ArgumentCount() == 3u && Detail::ActiveMaskImplies(m_program.Resources(), mask, value->Argument(0))) { std::uint32_t inner = 0; value = SplitImmediateAddend(value->Argument(1), inner); addend += inner; } std::uint32_t shift = 0; if (depth != 0u && value->Opcode() == IrOpcode::ShiftLeftLogical32 && value->ArgumentCount() == 2u && immediateU32(value->Argument(1), shift) && shift < 32u) { if (!ParseFirstLaneOffset(value->Argument(0)->Resolve(), mask, form, depth - 1u) || form.shift + shift >= 32u) return false; form.shift += shift; form.addend <<= shift; } else if (depth != 0u && mask == nullptr && value->Opcode() == IrOpcode::ReadFirstLane && value->ArgumentCount() == 2u) { if (!ParseFirstLaneOffset(value->Argument(0)->Resolve(), value->Argument(1)->Resolve(), form, depth - 1u)) return false; } else { if (mask == nullptr) return false; form = FirstLaneOffset{value, mask, 0u, 0u}; } form.addend += addend; return true; } bool SameFirstLaneOffset(const FirstLaneOffset& left, const FirstLaneOffset& right) const { return left.shift == right.shift && EquivalentValue(m_program.Resources(), left.mask, right.mask) && EquivalentValue(m_program.Resources(), left.lane, right.lane); } bool TryMakeTableColumn(IrValue& handle, bool sampler, bool r128, TableColumnPlan& plan, bool buffer = false) { const auto expected = buffer ? IrOpcode::GetBufferResource : sampler ? IrOpcode::GetSamplerResource : IrOpcode::GetImageResource; const std::uint32_t width = sampler || buffer ? 4u : 8u; if (handle.Opcode() != expected || handle.ArgumentCount() != width) { return false; } std::uint32_t words = width; if (!sampler && r128) { bool zeroTail = true; for (std::uint32_t dword = 4u; dword < 8u; dword++) { std::uint32_t immediate = 0; zeroTail = zeroTail && immediateU32(handle.Argument(dword), immediate) && immediate == 0u; } if (zeroTail) { words = 4u; } } IrValue* heapHandle = nullptr; IrValue* offset = nullptr; IrValue* base = nullptr; std::uint32_t baseAddend = 0; std::uint32_t carry = 0; bool folded = false; std::uint32_t first = 0; bool address = false; FirstLaneOffset baseLane; bool laneBase = false; for (std::uint32_t dword = 0; dword < words; dword++) { IrValue* read = ResolveInvariantPhi(m_program.Resources(), handle.Argument(dword)); if (read == nullptr) return false; std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarReadMemory(*read, memoryIndex); const bool addressRead = memory == nullptr && (memory = ScalarAddressMemory(*read, memoryIndex)) != nullptr; if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *read)) { return false; } std::uint32_t readAddend = 0; IrValue* readBase = SplitImmediateAddend(read->Argument(1), readAddend); bool sameBase = dword == 0u || EquivalentValue(m_program.Resources(), base, readBase); if (!sameBase && laneBase) { FirstLaneOffset lane; if (ParseFirstLaneOffset(read->Argument(1)->Resolve(), nullptr, lane, phiSearchDepth) && SameFirstLaneOffset(baseLane, lane)) { readAddend = lane.addend; sameBase = true; } } if (dword == 0u) { first = memory->offset; address = addressRead; base = readBase; baseAddend = readAddend; laneBase = ParseFirstLaneOffset(read->Argument(1)->Resolve(), nullptr, baseLane, phiSearchDepth) && baseLane.addend == baseAddend; } else if (addressRead != address || memory->offset + readAddend != first + baseAddend + dword * static_cast(sizeof(std::uint32_t))) { return false; } else if (readAddend != baseAddend) { if (readAddend < baseAddend || static_cast(memory->offset) + (readAddend - baseAddend) > 0x7fffffffu) { return false; } folded = true; carry = std::max(carry, readAddend - baseAddend); } IrValue* currentHandle = read->Argument(0)->Resolve(); if (heapHandle != nullptr && currentHandle != heapHandle && !EquivalentValue(m_program.Resources(), heapHandle, currentHandle)) { return false; } if (heapHandle == nullptr) heapHandle = currentHandle; if (dword == 0u) { offset = read->Argument(1)->Resolve(); } else if (!sameBase) { return false; } plan.memory.push_back(memoryIndex); plan.reads.push_back(read); } if (address) { DescriptorSource direct; MakeSource(handle, width, false, false, direct); std::uint32_t badDword = 0; if (ValidateSource(direct, badDword)) { return false; } } IrValue* key = nullptr; std::uint32_t stride = 0; std::uint32_t addend = 0; bool masked = false; std::uint32_t descriptorOffset = 0; if (address && immediateU32(offset, descriptorOffset)) { return TryMakeIndirectColumn(handle, *heapHandle, sampler, words, descriptorOffset, first, plan); } std::uint32_t fixedOffset = 0; const bool fixed = !address && !buffer && immediateU32(offset, fixedOffset); if (fixed) { key = &m_builder.Constant(0u); stride = width * static_cast(sizeof(std::uint32_t)); addend = fixedOffset; } else if (!MatchAffineOffset(offset, key, stride, addend)) { if (!MatchMaskedOffset(SplitImmediateAddend(offset, addend), key, stride) && (address || !MatchFirstLaneOffset(offset, key, stride))) { return false; } masked = true; } DescriptorSource heapSource; std::uint32_t heapIndex = 0; if (MakeRuntimeSource(*heapHandle, address ? IrOpcode::GetAddressResource : IrOpcode::GetBufferResource, address ? 2u : 4u, heapIndex, heapSource)) { if (fixed) return false; } else { if (address || buffer || heapHandle->Opcode() != IrOpcode::GetBufferResource || !std::ranges::all_of(heapHandle->Uses(), [&](const IrValue* use) { return std::ranges::find(plan.reads, use) != plan.reads.end(); })) return false; const auto sameHandle = [&](const IrValue* user) { if (user->Opcode() != handle.Opcode() || user->ArgumentCount() != handle.ArgumentCount()) return false; for (std::uint32_t argument = 0; argument < handle.ArgumentCount(); argument++) { if (user->Argument(argument)->Resolve() != handle.Argument(argument)->Resolve()) return false; } return true; }; if (!std::ranges::all_of(plan.reads, [&](const IrValue* read) { return std::ranges::all_of(read->Uses(), sameHandle); })) return false; TableColumnPlan parent; if (!TryMakeTableColumn(*heapHandle, false, false, parent, true)) return false; if (parent.parentPointer == nullptr) return false; if (!std::ranges::all_of(parent.reads, [&](const IrValue* read) { return std::ranges::all_of(read->Uses(), [&](const IrValue* user) { return user->Opcode() == IrOpcode::GetBufferResource && std::ranges::all_of(user->Uses(), [&](const IrValue* use) { return std::ranges::find(plan.reads, use) != plan.reads.end(); }); }); })) return false; heapIndex = parent.source; heapSource = m_sources[heapIndex]; plan.parentPointer = parent.parentPointer; m_tableColumns.push_back(std::move(parent)); } TableColumn column; column.heapSource = heapIndex; column.stride = stride; column.addend = addend; column.offset = first; column.dwordCount = words; column.maxKey = possibleU32UpperBound(key); column.sampler = sampler; column.address = address; column.buffer = buffer; column.foldedAddend = folded ? baseAddend : 0u; column.foldedCarry = folded ? carry : 0u; if (!address && !sampler && std::ranges::find(m_unselectedHandles, &handle) == m_unselectedHandles.end()) { DescriptorSource direct; MakeSource(handle, width, false, false, direct); DescriptorSource selection; selection.dwordCount = 1u; selection.dwords[0] = offset; std::uint32_t badDword = 0; if (ValidateSource(direct, badDword) && ValidateSource(selection, badDword)) { column.selected = true; column.selection = InternSource(selection); } } const auto domain = masked ? std::optional{} : MatchKeyDomain(key); DescriptorSource columnSource = heapSource; columnSource.tableColumn = column; plan.handle = &handle; plan.source = InternSource(columnSource); plan.keyDomain = domain; auto& shared = *m_sources[plan.source].tableColumn; shared.maxKey = std::max(shared.maxKey, column.maxKey); if (!domain.has_value()) { shared.unkeyed = true; shared.keyDomains.clear(); } else if (!shared.unkeyed && std::find(shared.keyDomains.begin(), shared.keyDomains.end(), *domain) == shared.keyDomains.end()) { shared.keyDomains.push_back(*domain); } plan.offset = offset; return true; } static bool LaneMaskWord(const IrValue& value) { return value.Opcode() == IrOpcode::CompositeExtractU32x4 && value.ArgumentCount() == 2u && value.Argument(0)->Resolve()->Opcode() == IrOpcode::Ballot && value.Argument(1)->Resolve()->HasImmediate(); } bool SameRecordColumn(IrValue& first, IrValue& second) const { IrValue* firstKey = nullptr; IrValue* secondKey = nullptr; std::uint32_t firstStride = 0; std::uint32_t firstAddend = 0; std::uint32_t secondStride = 0; std::uint32_t secondAddend = 0; return EquivalentValue(m_program.Resources(), first.Argument(0), second.Argument(0)) && MatchAffineOffset(first.Argument(1), firstKey, firstStride, firstAddend) && MatchAffineOffset(second.Argument(1), secondKey, secondStride, secondAddend) && firstStride == secondStride && firstAddend == secondAddend; } static bool LaneIndex(const IrValue* value) { std::vector pending{value}; std::unordered_set seen; while (!pending.empty()) { const IrValue* current = pending.back()->Resolve(); pending.pop_back(); if (!seen.insert(current).second || (current->HasImmediate() && current->Type() == IrType::U32)) continue; switch (current->Opcode()) { case IrOpcode::FindILsb32: case IrOpcode::FindUMsb32: case IrOpcode::ReadFirstLane: continue; case IrOpcode::BitwiseAnd32: case IrOpcode::IAdd32: case IrOpcode::ISub32: if (current->ArgumentCount() != 2u) return false; pending.push_back(current->Argument(0)); pending.push_back(current->Argument(1)); continue; case IrOpcode::SelectU32: if (current->ArgumentCount() != 3u) return false; pending.push_back(current->Argument(1)); pending.push_back(current->Argument(2)); continue; default: return false; } } return true; } static bool LaneBit(const IrValue& value) { if (value.Opcode() != IrOpcode::ShiftLeftLogical32 || value.ArgumentCount() != 2u) return false; const IrValue* one = value.Argument(0)->Resolve(); return one->HasImmediate() && one->Type() == IrType::U32 && one->ImmediateU32() == 1u && LaneIndex(value.Argument(1)); } static bool LaneMaskOperand(const IrValue* value) { const IrValue* root = value->Resolve(); std::vector pending{root}; std::unordered_set seen; bool lane = false; while (!pending.empty()) { const IrValue* current = pending.back()->Resolve(); pending.pop_back(); if (!seen.insert(current).second) continue; if (LaneMaskWord(*current) || LaneBit(*current)) { lane = true; continue; } if (current != root && current->HasImmediate() && current->Type() == IrType::U32) continue; const auto opcode = current->Opcode(); const bool combines = (opcode == IrOpcode::BitwiseAnd32 || opcode == IrOpcode::BitwiseOr32) && current->ArgumentCount() == 2u; const bool inverts = opcode == IrOpcode::BitwiseNot32 && current->ArgumentCount() == 1u; if (!current->IsPhi() && !combines && !inverts) return false; for (std::size_t index = 0; index < current->ArgumentCount(); ++index) pending.push_back(current->Argument(index)); } return lane; } bool PointerRecordReads(IrValue* value, std::vector& reads) const { std::vector pending{value}; std::unordered_set seen; while (!pending.empty()) { auto* current = pending.back()->Resolve(); pending.pop_back(); if (!seen.insert(current).second) continue; if (current->IsPhi()) { for (std::size_t index = 0; index < current->ArgumentCount(); ++index) pending.push_back(current->Argument(index)); continue; } if (current->HasImmediate() && current->Type() == IrType::U32) continue; std::uint32_t memory = 0; const MemoryInfo* info = ScalarReadMemory(*current, memory); if (info == nullptr || !MemoryIndexBelongsTo(memory, *current)) return false; if (!reads.empty() && !EquivalentValue(m_program.Resources(), reads.back(), current)) return false; reads.push_back(current); } return !reads.empty(); } static bool PointerFreeWord(const IrValue& word) { return (word.HasImmediate() && word.Type() == IrType::U32) || LaneMaskOperand(&word); } static IrValue* MaskedRecordWord(const IrValue& word) { if (word.Opcode() != IrOpcode::BitwiseAnd32 || word.ArgumentCount() != 2u) return nullptr; const bool left = LaneMaskOperand(word.Argument(0)); if (left == LaneMaskOperand(word.Argument(1))) return nullptr; return word.Argument(left ? 1u : 0u); } bool PointerRecordRead(IrValue& read, std::vector& reads) const { std::uint32_t memory = 0; const MemoryInfo* info = ScalarReadMemory(read, memory); if (info == nullptr || !MemoryIndexBelongsTo(memory, read)) return false; if (std::ranges::find(reads, &read) != reads.end()) return true; if (!reads.empty() && (ScalarReadMemory(*reads.back(), memory)->offset != info->offset || !SameRecordColumn(*reads.back(), read))) return false; reads.push_back(&read); return true; } struct ChaseSite { IrValue* handle = nullptr; IrValue* pointer = nullptr; PointerChase chase; std::vector inner; }; bool ChaseRead(IrValue& read, IrValue*& handle, std::uint32_t& readOffset, std::uint32_t& readImmediate) const { std::uint32_t memory = 0; const MemoryInfo* info = ScalarReadMemory(read, memory); if (info == nullptr || !MemoryIndexBelongsTo(memory, read) || !immediateU32(read.Argument(1), readOffset)) return false; handle = read.Argument(0)->Resolve(); readImmediate = info->offset; return handle->Opcode() == IrOpcode::GetBufferResource && handle->ArgumentCount() == 4u; } static IrValue* EditedWord(IrValue* word, DescriptorWordEdit& edit) { word = word->Resolve(); std::uint32_t operand = 0; if (word->ArgumentCount() != 2u) return word; if (word->Opcode() == IrOpcode::ShiftLeftLogical32) { if (!immediateU32(word->Argument(1), operand) || operand > 31u) return word; edit.shift = operand; return word->Argument(0)->Resolve(); } if (word->Opcode() != IrOpcode::BitwiseAnd32 && word->Opcode() != IrOpcode::BitwiseOr32) return word; const bool left = immediateU32(word->Argument(0), operand); if (!left && !immediateU32(word->Argument(1), operand)) return word; (word->Opcode() == IrOpcode::BitwiseAnd32 ? edit.mask : edit.bits) = operand; return word->Argument(left ? 1u : 0u)->Resolve(); } bool ChaseVsharp(IrValue& handle, IrValue*& pointer, PointerChase& chase) const { for (std::uint32_t dword = 0; dword < 4u; dword++) { IrValue* word = EditedWord(handle.Argument(dword), chase.edits[dword]); std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarAddressMemory(*word, memoryIndex); std::uint32_t offset = 0; if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *word) || !immediateU32(word->Argument(1), offset)) return false; IrValue* address = word->Argument(0)->Resolve(); if (address->Opcode() != IrOpcode::GetAddressResource || address->ArgumentCount() != 2u) return false; if (dword == 0u) { pointer = address; chase.offset = offset; chase.immediate = memory->offset; } else if (offset != chase.offset || memory->offset != chase.immediate + dword * static_cast(sizeof(std::uint32_t)) || (address != pointer && !EquivalentValue(m_program.Resources(), pointer, address))) { return false; } } return true; } bool DirectChaseRead(IrValue& read, IrValue*& pointer, std::uint32_t& offset, std::uint32_t& immediate) const { std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarAddressMemory(read, memoryIndex); if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, read) || !immediateU32(read.Argument(1), offset)) return false; pointer = read.Argument(0)->Resolve(); immediate = memory->offset; return pointer->Opcode() == IrOpcode::GetAddressResource && pointer->ArgumentCount() == 2u && immediate % sizeof(std::uint32_t) == 0u; } std::optional DirectChaseHighRead(IrValue& read, const IrValue& pointer, std::uint32_t offset, std::uint32_t immediate) const { IrValue* highPointer = nullptr; std::uint32_t highOffset = 0; std::uint32_t highImmediate = 0; if (!DirectChaseRead(read, highPointer, highOffset, highImmediate) || (highPointer != &pointer && !EquivalentValue(m_program.Resources(), &pointer, highPointer))) return std::nullopt; if (highOffset != offset || highImmediate == immediate) return std::nullopt; return highImmediate; } std::optional ChaseHighRead(IrValue& read, const IrValue& handle, std::uint32_t readOffset, std::uint32_t readImmediate) const { IrValue* highHandle = nullptr; std::uint32_t highOffset = 0; std::uint32_t highImmediate = 0; if (!ChaseRead(read, highHandle, highOffset, highImmediate) || (highHandle != &handle && !EquivalentValue(m_program.Resources(), &handle, highHandle))) return std::nullopt; if (highOffset != readOffset || highImmediate % sizeof(std::uint32_t) != 0u || highImmediate == readImmediate) return std::nullopt; return highImmediate; } bool PointerRecordPairs(IrValue* low, IrValue* high, std::vector& lowReads, std::vector& highReads, std::vector& chases, std::uint32_t& chaseDepth) const { std::vector sites; std::vector looseHandles; const auto site = [&](IrValue& handle, IrValue& pointer, const PointerChase& chase) { const auto found = std::ranges::find_if(sites, [&](const ChaseSite& known) { return known.chase == chase && (known.handle == &handle || EquivalentValue(m_program.Resources(), known.handle, &handle)); }); if (found != sites.end()) return static_cast(found - sites.begin()); sites.push_back(ChaseSite{&handle, &pointer, chase, {}}); return sites.size() - 1u; }; const auto walk = [&](IrValue* rootLow, IrValue* rootHigh, std::vector& reached) { std::vector> pending{{rootLow, rootHigh}}; std::set> seen; while (!pending.empty()) { auto* lowWord = pending.back().first->Resolve(); auto* highWord = pending.back().second->Resolve(); pending.pop_back(); if (!seen.emplace(lowWord, highWord).second || PointerFreeWord(*lowWord)) continue; if (lowWord->IsPhi()) { const bool paired = highWord->IsPhi() && highWord->Parent() == lowWord->Parent(); for (std::size_t index = 0; index < lowWord->ArgumentCount(); ++index) { IrValue* edge = paired ? PhiIncoming(*highWord, lowWord->PhiBlock(index)) : highWord; if (edge == nullptr) return false; if (paired && lowWord->Argument(index)->Resolve()->HasImmediate() && edge->Resolve()->HasImmediate()) return false; pending.emplace_back(lowWord->Argument(index), edge); } continue; } if (IrValue* word = MaskedRecordWord(*lowWord)) { pending.emplace_back(word, highWord); continue; } IrValue* handle = nullptr; IrValue* pointer = nullptr; PointerChase chase; std::uint32_t looseOffset = 0; std::uint32_t looseImmediate = 0; if (DirectChaseRead(*lowWord, pointer, chase.offset, chase.immediate)) { if (highWord->IsPhi()) { for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); continue; } const auto highImmediate = DirectChaseHighRead(*highWord, *pointer, chase.offset, chase.immediate); if (!highImmediate.has_value()) return false; chase.readHighImmediate = *highImmediate; chase.direct = true; const auto index = site(*pointer, *pointer, chase); if (std::ranges::find(reached, index) == reached.end()) reached.push_back(index); continue; } if (ChaseRead(*lowWord, handle, chase.readOffset, chase.readImmediate) && ChaseVsharp(*handle, pointer, chase)) { if (highWord->IsPhi()) { for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); continue; } const auto highImmediate = ChaseHighRead(*highWord, *handle, chase.readOffset, chase.readImmediate); if (!highImmediate.has_value()) return false; chase.readHighImmediate = *highImmediate; const auto index = site(*handle, *pointer, chase); if (std::ranges::find(reached, index) == reached.end()) reached.push_back(index); continue; } if (!PointerRecordRead(*lowWord, lowReads)) return false; if (highWord->IsPhi()) { for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); } else if (IrValue* word = MaskedRecordWord(*highWord)) { pending.emplace_back(lowWord, word); } else if (PointerFreeWord(*highWord) || PointerRecordRead(*highWord, highReads)) { continue; } else if (IrValue* highHandle = nullptr; ChaseRead(*highWord, highHandle, looseOffset, looseImmediate)) { looseHandles.push_back(highHandle); } else { return false; } } return true; }; std::vector roots; if (!walk(low, high, roots)) return false; for (std::size_t index = 0; index < sites.size(); ++index) { std::vector inner; if (!walk(sites[index].pointer->Argument(0), sites[index].pointer->Argument(1), inner)) return false; sites[index].inner = std::move(inner); } if (!std::ranges::all_of(looseHandles, [&](const IrValue* handle) { return std::ranges::any_of(sites, [&](const ChaseSite& known) { return known.handle == handle || EquivalentValue(m_program.Resources(), known.handle, handle); }); })) return false; std::vector depths(sites.size(), 0u); std::vector states(sites.size(), 0u); const auto depthOf = [&](const auto& self, std::size_t index) -> std::optional { if (states[index] == 2u) return depths[index]; if (states[index] == 1u) return std::nullopt; states[index] = 1u; std::uint32_t deepest = 0; for (const auto inner : sites[index].inner) { const auto depth = self(self, inner); if (!depth.has_value()) return std::nullopt; deepest = std::max(deepest, *depth); } states[index] = 2u; depths[index] = deepest + 1u; return depths[index]; }; chaseDepth = 0; for (const auto root : roots) { const auto depth = depthOf(depthOf, root); if (!depth.has_value()) return false; chaseDepth = std::max(chaseDepth, *depth); } if (chaseDepth > MaxPointerChaseDepth) return false; chases.clear(); for (const auto& known : sites) { if (std::ranges::find(chases, known.chase) == chases.end()) chases.push_back(known.chase); } if (chaseDepth == 0u) chases.clear(); return !lowReads.empty() && !highReads.empty(); } bool Precedes(const IrValue* value, const IrValue& use) const { value = value->Resolve(); const IrBlock* definition = value->Parent(); const IrBlock* target = use.Parent(); if (definition == nullptr) return true; if (target == nullptr) return false; if (definition == target) { for (const IrValue* inst : target->Instructions()) { if (inst == value) return true; if (inst == &use) return false; } return false; } std::vector pending{m_program.BlockOrder().front()}; std::unordered_set seen{pending.front()}; while (!pending.empty()) { const IrBlock* block = pending.back(); pending.pop_back(); if (block == definition) continue; if (block == target) return false; for (const IrBlock* successor : block->Successors()) { if (seen.insert(successor).second) pending.push_back(successor); } } return true; } IrValue* UnwrapTableRead(IrValue* value) const { if (value == nullptr) return nullptr; value = value->Resolve(); if (!value->IsPhi()) return value; IrValue* candidate = nullptr; std::vector pending{value}; std::unordered_set visited; while (!pending.empty()) { auto* current = pending.back()->Resolve(); pending.pop_back(); if (!visited.insert(current).second) continue; if (current->IsPhi()) { for (std::size_t i = 0; i < current->ArgumentCount(); i++) { pending.push_back(current->Argument(i)); } continue; } std::uint32_t memoryIndex = 0; IrValue* key = nullptr; std::uint32_t stride = 0; std::uint32_t addend = 0; if (ScalarReadMemory(*current, memoryIndex) == nullptr || !MemoryIndexBelongsTo(memoryIndex, *current) || !MatchAffineOffset(current->Argument(1), key, stride, addend)) { return nullptr; } if (candidate == nullptr) { candidate = current; } else if (!EquivalentValue(m_program.Resources(), candidate, current)) { std::uint32_t candidateIndex = 0; const auto* candMem = ScalarReadMemory(*candidate, candidateIndex); const auto* curMem = ScalarReadMemory(*current, memoryIndex); if (candMem == nullptr || curMem == nullptr || candMem->offset != curMem->offset || !EquivalentValue(m_program.Resources(), candidate->Argument(0), current->Argument(0)) || !EquivalentValue(m_program.Resources(), candidate->Argument(1), current->Argument(1))) { return nullptr; } } } return candidate; } bool BlockDominates(const IrBlock* dominator, const IrBlock* block) const { if (dominator == block) return true; std::vector pending{block}; std::unordered_set visited{block}; while (!pending.empty()) { const IrBlock* current = pending.back(); pending.pop_back(); if (current->Predecessors().empty()) return false; for (const IrBlock* predecessor : current->Predecessors()) { if (predecessor != dominator && visited.insert(predecessor).second) pending.push_back(predecessor); } } return true; } bool AvailableAt(IrValue* value, const IrBlock* block) const { value = value->Resolve(); if (value->HasImmediate() || value->Parent() == nullptr) return true; return BlockDominates(value->Parent(), block); } bool SrtWord(const IrValue* value, const IrValue*& pointer, std::uint64_t& byte) const { value = value->Resolve(); std::uint32_t slot = 0; if (value->Opcode() != IrOpcode::ReadConst || value->ArgumentCount() != 2u || value->Argument(0)->Resolve()->Opcode() != IrOpcode::GetSrtResource || !immediateU32(value->Argument(1), slot)) { return false; } const auto& reads = m_program.Resources().srtReads; if (slot >= reads.size() || reads[slot].value == nullptr) { return false; } const IrValue& read = *reads[slot].value; const auto index = read.Flags().index; std::uint32_t addend = 0; if (!Detail::IsRawRead(m_program.Resources(), read) || read.ArgumentCount() < 2u || !immediateU32(read.Argument(1), addend)) { return false; } pointer = read.Argument(0)->Resolve(); byte = static_cast(m_program.Resources().memoryInfo[index].offset) + addend; return true; } bool GuardedSingleSource(const IrValue& handle, std::uint32_t width, std::array& planned) const { if (StrictDescriptorWebs() || handle.ArgumentCount() != width) { return false; } bool differs = false; for (std::uint32_t dword = 0; dword < width; dword++) { IrValue* live = handle.Argument(dword)->Resolve(); if (live->HasImmediate()) { planned[dword] = live; continue; } IrValue* candidate = nullptr; std::vector pending {live}; std::unordered_set visited; std::size_t leaves = 0; while (!pending.empty()) { IrValue* current = pending.back()->Resolve(); pending.pop_back(); if (!visited.insert(current).second) { continue; } if (current->IsPhi()) { for (std::size_t index = 0; index < current->ArgumentCount(); index++) { pending.push_back(current->Argument(index)); } continue; } if (++leaves > DescriptorWeb::maxStates) { return false; } if (current->HasImmediate() || Detail::HoldsFoldableValue(current, phiSearchDepth) || !RebuildBlocker(current, phiSearchDepth).empty()) { continue; } if (candidate != nullptr && candidate != current && !EquivalentValue(m_program.Resources(), candidate, current)) { return false; } if (candidate == nullptr) { candidate = current; } } if (candidate == nullptr) { return false; } planned[dword] = candidate; differs = differs || candidate != live; } const IrValue* pointer = nullptr; std::uint64_t first = 0; bool anchored = false; for (std::uint32_t dword = 0; dword < width; dword++) { if (planned[dword]->HasImmediate()) { continue; } const IrValue* wordPointer = nullptr; std::uint64_t byte = 0; if (!SrtWord(planned[dword], wordPointer, byte)) { return false; } if (byte < static_cast(dword) * sizeof(std::uint32_t)) { return false; } const std::uint64_t base = byte - static_cast(dword) * sizeof(std::uint32_t); if (!anchored) { pointer = wordPointer; first = base; anchored = true; } else if (base != first || (wordPointer != pointer && !EquivalentValue(m_program.Resources(), pointer, wordPointer))) { return false; } } return anchored && differs; } bool LiveBefore(IrValue* value, IrValue& position) const { value = value->Resolve(); if (value->HasImmediate() || value->Parent() == nullptr) { return true; } if (value->IsPhi()) { return value->Parent() == position.Parent() || Dominates(value->Parent(), position.Parent()); } return AvailableBefore(*value, position); } void EmitDescriptorGuards() { for (const auto& guard : m_descriptorGuards) { IrValue& inst = *guard.inst; IrValue* condition = nullptr; for (std::size_t dword = 0; dword < guard.live.size(); dword++) { IrValue* live = guard.live[dword]->Resolve(); if (!LiveBefore(live, inst)) { fail("a guarded descriptor's live dword " + std::to_string(dword) + " is not available at the instruction at pc 0x" + formatHex32(inst.Flags().pc)); } if (live == guard.planned[dword]->Resolve()) { continue; } IrValue* planned = Rematerialize(guard.planned[dword], inst, true, phiSearchDepth); if (planned == nullptr) { fail("a guarded descriptor's planned dword " + std::to_string(dword) + " is not rebuilt before the instruction at pc 0x" + formatHex32(inst.Flags().pc)); } const std::array compare {live, planned}; IrValue* differs = EmitBefore(inst, IrOpcode::INotEqual32, IrType::Bool, compare); if (condition != nullptr) { const std::array either {condition, differs}; differs = EmitBefore(inst, IrOpcode::LogicalOr, IrType::Bool, either); } condition = differs; } if (condition != nullptr) { EmitDescriptorFault(inst, *condition, *guard.live[0]->Resolve(), *guard.live[1]->Resolve()); } } } bool TryMakeIndirectColumn(IrValue& handle, IrValue& descriptorPointer, bool sampler, std::uint32_t words, std::uint32_t descriptorOffset, std::uint32_t descriptorImmediate, TableColumnPlan& plan) { std::vector hops; IrValue* pointer = &descriptorPointer; while (pointer->Opcode() == IrOpcode::GetAddressResource && pointer->ArgumentCount() == 2u) { std::uint32_t memoryIndex = 0; if (ScalarAddressMemory(*pointer->Argument(0)->Resolve(), memoryIndex) == nullptr) { break; } PointerHop hop; IrValue* next = nullptr; for (std::uint32_t dword = 0; dword < 2u; dword++) { IrValue* read = pointer->Argument(dword)->Resolve(); const MemoryInfo* memory = ScalarAddressMemory(*read, memoryIndex); std::uint32_t readOffset = 0; if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *read) || !immediateU32(read->Argument(1), readOffset)) { return false; } IrValue* readPointer = read->Argument(0)->Resolve(); if (dword == 0u) { hop = PointerHop{readOffset, memory->offset}; next = readPointer; } else if (readOffset != hop.offset || memory->offset != hop.immediate + static_cast(sizeof(std::uint32_t)) || (readPointer != next && !EquivalentValue(m_program.Resources(), next, readPointer))) { return false; } plan.hopMemory.push_back(memoryIndex); plan.hopReads.push_back(read); } hops.insert(hops.begin(), hop); pointer = next; } if (pointer->Opcode() != IrOpcode::GetAddressResource || pointer->ArgumentCount() != 2u) { return false; } IrValue* bufferHandle = nullptr; IrValue* offset = nullptr; const bool buffer = handle.Opcode() == IrOpcode::GetBufferResource; const bool merged = pointer->Argument(0)->Resolve()->IsPhi() && UnwrapTableRead(pointer->Argument(0)) == nullptr; const bool pointerKeyed = merged && !buffer; if (!hops.empty() && buffer) return false; std::vector lowReads; std::vector highReads; std::vector chases; std::uint32_t chaseDepth = 0; if (pointerKeyed ? !PointerRecordPairs(pointer->Argument(0), pointer->Argument(1), lowReads, highReads, chases, chaseDepth) : merged && !PointerRecordReads(pointer->Argument(0), lowReads)) return false; auto* low = merged ? lowReads.back() : UnwrapTableRead(pointer->Argument(0)); if (low == nullptr) return false; auto* high = pointerKeyed ? pointer->Argument(1)->Resolve() : UnwrapTableRead(pointer->Argument(1)); if (high == nullptr) return false; std::uint32_t first = 0; for (std::uint32_t dword = 0; dword < 2u; dword++) { IrValue* read = dword == 0u ? low : pointerKeyed ? highReads.back() : high; std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarReadMemory(*read, memoryIndex); if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *read)) { return false; } if (dword == 0u) { first = memory->offset; bufferHandle = read->Argument(0)->Resolve(); offset = read->Argument(1)->Resolve(); } else if (memory->offset != first + static_cast(sizeof(std::uint32_t)) || (pointerKeyed ? !SameRecordColumn(*low, *read) : read->Argument(0)->Resolve() != bufferHandle || !EquivalentValue(m_program.Resources(), offset, read->Argument(1)))) { return false; } } IrValue* recordOffset = offset; if (pointerKeyed) { IrValue* source = high; if (highReads.size() != 1u || highReads.front() != high) { lowReads.insert(lowReads.begin(), highReads.begin(), highReads.end()); const auto found = std::ranges::find_if(lowReads, [&](const IrValue* read) { const IrValue* table = read->Argument(0)->Resolve(); for (std::size_t index = 0; index < table->ArgumentCount(); ++index) { if (!Precedes(table->Argument(index), handle)) return false; } return true; }); if (found == lowReads.end()) return false; source = *found; } bufferHandle = source->Argument(0)->Resolve(); recordOffset = source->Argument(1)->Resolve(); offset = source == high ? recordOffset : high; } if (!AvailableAt(offset, handle.Parent())) { return false; } IrValue* key = nullptr; std::uint32_t stride = 0; std::uint32_t addend = 0; if (!MatchAffineOffset(recordOffset, key, stride, addend)) { return false; } DescriptorSource heapSource; std::uint32_t heapIndex = 0; if (!MakeRuntimeSource(*bufferHandle, IrOpcode::GetBufferResource, 4u, heapIndex, heapSource)) { std::array planned {}; if (buffer || bufferHandle->Opcode() != IrOpcode::GetBufferResource || !GuardedSingleSource(*bufferHandle, 4u, planned)) { return false; } for (std::uint32_t dword = 0; dword < 4u; dword++) { if (!AvailableAt(bufferHandle->Argument(dword), handle.Parent())) { return false; } } MakeSource(planned, 4u, false, false, heapSource); std::uint32_t badDword = 0; if (!ValidateSource(heapSource, badDword)) { return false; } heapIndex = InternSource(heapSource); plan.heapLive.assign(bufferHandle->Arguments().begin(), bufferHandle->Arguments().end()); plan.heapPlanned.assign(planned.begin(), planned.begin() + 4); } TableColumn column; column.heapSource = heapIndex; column.stride = stride; column.addend = addend; column.offset = first; column.dwordCount = words; column.maxKey = merged ? 0xffffffffu : possibleU32UpperBound(key); column.sampler = sampler; column.indirect = true; column.buffer = buffer; column.indirectOffset = descriptorOffset; column.indirectImmediate = descriptorImmediate; column.pointerKeyed = pointerKeyed; column.pointerHops = std::move(hops); column.pointerChases = std::move(chases); column.pointerChaseDepth = chaseDepth; const auto domain = merged ? std::optional{} : MatchKeyDomain(key); DescriptorSource columnSource = heapSource; columnSource.tableColumn = column; plan.handle = &handle; plan.source = InternSource(columnSource); plan.keyDomain = domain; auto& shared = *m_sources[plan.source].tableColumn; shared.maxKey = std::max(shared.maxKey, column.maxKey); if (!domain.has_value()) { shared.unkeyed = true; shared.keyDomains.clear(); } else if (!shared.unkeyed && std::find(shared.keyDomains.begin(), shared.keyDomains.end(), *domain) == shared.keyDomains.end()) { shared.keyDomains.push_back(*domain); } plan.offset = offset; if (buffer || merged) plan.parentPointer = pointer; return true; } const TableColumnPlan* FindTableColumn(const IrValue& handle) const { const auto found = std::ranges::find_if(m_tableColumns, [&](const TableColumnPlan& plan) { return plan.handle == &handle; }); return found == m_tableColumns.end() ? nullptr : &*found; } bool PlanTableColumn(IrValue& handle, bool sampler, bool r128) { if (FindTableColumn(handle) != nullptr) { return true; } TableColumnPlan plan; if (!TryMakeTableColumn(handle, sampler, r128, plan)) { return false; } m_tableColumns.push_back(std::move(plan)); return true; } static constexpr std::uint32_t phiSearchDepth = 8u; static constexpr std::uint32_t firstLaneMultipleDepth = 32u; static bool SplittableImageRead(const IrValue& inst) { switch (inst.Opcode()) { case IrOpcode::ImageSampleRaw: case IrOpcode::ImageGatherRaw: case IrOpcode::ImageQueryLod: case IrOpcode::ImageQueryDimensions: case IrOpcode::ImageRead: return inst.Type() == IrType::U32x4; default: return false; } } void CollectDescriptorPhis(IrValue* value, std::vector& phis, std::uint32_t depth) const { value = value->Resolve(); if (value->HasImmediate()) { return; } if (value->IsPhi()) { if (ResolveInvariantPhi(m_program.Resources(), value) == nullptr && std::ranges::find(phis, value) == phis.end()) { phis.push_back(value); } return; } if (depth != 0u && value->Opcode() == IrOpcode::ReadFirstLane && value->ArgumentCount() == 2u) { CollectDescriptorPhis(value->Argument(0), phis, depth - 1u); return; } if (depth == 0u || !Detail::IsRuntimeUniformOp(value->Opcode())) { return; } for (std::size_t index = 0; index < value->ArgumentCount(); index++) { CollectDescriptorPhis(value->Argument(index), phis, depth - 1u); } } static IrValue* PhiIncoming(const IrValue& phi, const IrBlock* predecessor) { for (std::size_t index = 0; index < phi.ArgumentCount(); index++) { if (phi.PhiBlock(index) == predecessor) { return phi.Argument(index); } } return nullptr; } IrValue* EmitBefore(IrValue& position, IrOpcode op, IrType type, std::span arguments, std::uint64_t flags = 0) { IrValue& value = m_program.CreateValue(op, type, flags); for (IrValue* argument : arguments) { value.AddArgument(argument); } position.Parent()->InsertInstructionBefore(&position, &value); return &value; } void EmitDescriptorFault(IrValue& inst, IrValue& condition, IrValue& low, IrValue& high) { IrValue* exec = inst.Argument(inst.ArgumentCount() - 1u); if (exec->Type() != IrType::Bool) { fail("a split image instruction has no EXEC operand"); } const std::array active {&condition, exec}; IrValue* fault = EmitBefore(inst, IrOpcode::LogicalAnd, IrType::Bool, active); const std::array arguments {fault, &low, &high, &m_builder.Constant(inst.Flags().pc)}; static_cast(EmitBefore(inst, IrOpcode::DescriptorFault, IrType::Void, arguments)); m_info.usesFaultBuffer = true; } IrValue& UndefinedImageWords(IrValue& inst, const std::array& words) { const auto emit = [&](IrOpcode op, IrType type, std::initializer_list arguments) { return EmitBefore(inst, op, type, std::vector(arguments)); }; const auto constant = [&](std::uint32_t value) { return &m_builder.Constant(value); }; IrValue* address = emit(IrOpcode::LogicalOr, IrType::Bool, {emit(IrOpcode::INotEqual32, IrType::Bool, {words[0], constant(0u)}), emit(IrOpcode::INotEqual32, IrType::Bool, {emit(IrOpcode::BitwiseAnd32, IrType::U32, {words[1], constant(0xffu)}), constant(0u)})}); IrValue* type = emit(IrOpcode::ShiftRightLogical32, IrType::U32, {words[2], constant(28u)}); IrValue* format = emit(IrOpcode::BitwiseAnd32, IrType::U32, {emit(IrOpcode::ShiftRightLogical32, IrType::U32, {words[1], constant(20u)}), constant(0x1ffu)}); IrValue* defined = nullptr; for (const auto& range : DefinedImageFormats) { IrValue* inside = emit(IrOpcode::LogicalAnd, IrType::Bool, {emit(IrOpcode::UGreaterThanEqual32, IrType::Bool, {format, constant(range.first)}), emit(IrOpcode::ULessThan32, IrType::Bool, {format, constant(range.last + 1u)})}); defined = defined == nullptr ? inside : emit(IrOpcode::LogicalOr, IrType::Bool, {defined, inside}); } IrValue* notImage = emit(IrOpcode::ULessThan32, IrType::Bool, {type, constant(static_cast(ImageType::Color1D))}); IrValue* undefined = emit(IrOpcode::LogicalOr, IrType::Bool, {notImage, emit(IrOpcode::LogicalNot, IrType::Bool, {defined})}); IrValue* typed = emit(IrOpcode::UGreaterThanEqual32, IrType::Bool, {type, constant(2u)}); return *emit(IrOpcode::LogicalAnd, IrType::Bool, {emit(IrOpcode::LogicalAnd, IrType::Bool, {address, typed}), undefined}); } IrValue* EmitCopy(const IrValue& original, std::span arguments, IrValue& position) { if (Detail::IsRawRead(m_program.Resources(), original)) { auto flags = original.Flags(); auto memory = m_program.Resources().memoryInfo[flags.index]; memory.planningOnly = true; flags.index = static_cast(m_program.Resources().memoryInfo.size()); m_program.Resources().memoryInfo.push_back(memory); std::uint64_t rawFlags = 0; std::memcpy(&rawFlags, &flags, sizeof(flags)); IrValue* copy = EmitBefore(position, original.Opcode(), original.Type(), arguments, rawFlags); const auto origin = m_readOrigins.find(&original); m_readOrigins.emplace(copy, origin != m_readOrigins.end() ? origin->second : original.Parent()); return copy; } return EmitBefore(position, original.Opcode(), original.Type(), arguments, original.Flags()); } bool Dominates(const IrBlock* definition, const IrBlock* use) const { const auto& order = m_program.BlockOrder(); if (definition == nullptr || use == nullptr || order.empty() || std::ranges::find(order, definition) == order.end() || std::ranges::find(order, use) == order.end()) return false; if (definition == use) return true; std::vector pending{order.front()}; std::unordered_set visited; while (!pending.empty()) { const IrBlock* block = pending.back(); pending.pop_back(); if (!visited.insert(block).second || block == definition) continue; if (block == use) return false; for (const IrBlock* successor : block->Successors()) pending.push_back(successor); } return true; } bool AvailableBefore(const IrValue& definition, const IrValue& position) const { if (definition.Parent() == nullptr || position.Parent() == nullptr || &definition == &position) return false; if (definition.Parent() != position.Parent()) return Dominates(definition.Parent(), position.Parent()); for (const IrValue* instruction : definition.Parent()->Instructions()) { if (instruction == &definition) return true; if (instruction == &position) return false; } return false; } bool RegisterOnlyOffset(IrValue* value, std::uint32_t depth) const { value = value->Resolve(); if (value->HasImmediate()) return true; if (value->IsPhi()) { IrValue* invariant = ResolveInvariantPhi(m_program.Resources(), value); return invariant != nullptr && !invariant->IsPhi() && RegisterOnlyOffset(invariant, depth); } if (value->Opcode() == IrOpcode::GetUserData) return value->ArgumentCount() == 1u; if (depth == 0u || !Detail::IsRuntimeUniformOp(value->Opcode())) return false; for (std::size_t index = 0; index < value->ArgumentCount(); index++) { if (!RegisterOnlyOffset(value->Argument(index), depth - 1u)) return false; } return true; } IrValue* Rematerialize(IrValue* value, IrValue& position, bool emit, std::uint32_t depth, bool pointerOperand = false) { value = value->Resolve(); if (value->HasImmediate()) { return value; } if (value->IsPhi()) { IrValue* invariant = ResolveInvariantPhi(m_program.Resources(), value); if (invariant == nullptr) return pointerOperand && AvailableBefore(*value, position) ? value : nullptr; return invariant->IsPhi() ? nullptr : Rematerialize(invariant, position, emit, depth, pointerOperand); } if (pointerOperand && Detail::IsRawRead(m_program.Resources(), *value) && AvailableBefore(*value, position)) return value; if (depth == 0u) { return nullptr; } const auto op = value->Opcode(); const bool register_ = op == IrOpcode::GetUserData; const bool argumentless = (op == IrOpcode::GetShaderBase || op == IrOpcode::GetSrtResource) && value->ArgumentCount() == 0u; const bool srtRead = op == IrOpcode::ReadConst && value->ArgumentCount() == 2u && value->Argument(0)->Resolve()->Opcode() == IrOpcode::GetSrtResource && value->Argument(1)->Resolve()->HasImmediate(); std::uint32_t memoryIndex = 0u; const bool rawRead = op == IrOpcode::LoadAddressU32 && ScalarAddressMemory(*value, memoryIndex) != nullptr && RegisterOnlyOffset(value->Argument(1), phiSearchDepth); const bool sourceHandle = op == IrOpcode::GetAddressResource && value->ArgumentCount() == 2u; if (!register_ && !argumentless && !srtRead && !rawRead && !sourceHandle && !Detail::IsRuntimeUniformOp(op)) { return nullptr; } std::vector arguments; for (std::size_t index = 0; index < value->ArgumentCount(); index++) { IrValue* argument = register_ ? value->Argument(index) : Rematerialize(value->Argument(index), position, emit, depth - 1u, pointerOperand || (rawRead && index == 0u)); if (argument == nullptr) { return nullptr; } arguments.push_back(argument); } return emit ? EmitCopy(*value, arguments, position) : value; } IrValue* SubstituteEdge(IrValue* value, const IrBlock* predecessor, IrValue& position, bool emit, std::uint32_t depth) { value = value->Resolve(); std::vector phis; CollectDescriptorPhis(value, phis, depth); if (phis.empty()) { return value; } if (value->IsPhi()) { IrValue* incoming = PhiIncoming(*value, predecessor); return incoming == nullptr ? nullptr : Rematerialize(incoming, position, emit, phiSearchDepth); } std::vector arguments; for (std::size_t index = 0; index < value->ArgumentCount(); index++) { IrValue* argument = SubstituteEdge(value->Argument(index), predecessor, position, emit, depth - 1u); if (argument == nullptr) { return nullptr; } arguments.push_back(argument); } return emit ? EmitCopy(*value, arguments, position) : value; } IrValue& EdgeSelector(IrBlock& block, const std::vector& edges) { for (const auto& selector : m_edgeSelectors) { if (selector.block == &block && selector.edges == edges) { return *selector.value; } } IrValue& phi = m_program.CreateValue(IrOpcode::Phi, IrType::Bool); const auto& predecessors = block.Predecessors(); for (std::size_t index = 0; index < predecessors.size(); index++) { phi.AddPhiOperand(predecessors[index], &m_builder.ConstantBool(edges[index])); } block.InsertInstructionBefore(nullptr, &phi); m_edgeSelectors.push_back({&block, edges, &phi}); return phi; } void SplitDescriptorPhi(IrValue& inst) { const auto info = ImageOpcodeInfoOf(inst.Opcode()); const std::uint32_t handleCount = info.needsSampler ? 2u : 1u; if (inst.ArgumentCount() < handleCount || inst.Parent() == nullptr) { return; } std::array handles {}; std::array split {}; std::vector phis; for (std::uint32_t slot = 0; slot < handleCount; slot++) { IrValue* handle = inst.Argument(slot)->Resolve(); const auto expected = slot == 0u ? IrOpcode::GetImageResource : IrOpcode::GetSamplerResource; if (handle->Opcode() != expected || handle->ArgumentCount() != (slot == 0u ? 8u : 4u)) { return; } handles[slot] = handle; std::vector handlePhis; for (std::size_t dword = 0; dword < handle->ArgumentCount(); dword++) { CollectDescriptorPhis(handle->Argument(dword), handlePhis, phiSearchDepth); } split[slot] = !handlePhis.empty(); for (IrValue* phi : handlePhis) { if (std::ranges::find(phis, phi) == phis.end()) { phis.push_back(phi); } } } if (phis.empty()) { return; } if (!SplitDescriptorEdges(inst, handles, split, handleCount, phis)) { SplitDescriptorWeb(inst, handles, split, handleCount); } } bool SplitDescriptorEdges(IrValue& inst, const std::array& handles, const std::array& split, std::uint32_t handleCount, const std::vector& phis) { IrBlock* block = phis.front()->Parent(); if (block == nullptr) { return false; } const auto& predecessors = block->Predecessors(); if (predecessors.size() < 2u) { return false; } for (const IrValue* phi : phis) { if (phi->Parent() != block || phi->PhiBlockCount() != predecessors.size()) { return false; } for (const IrBlock* predecessor : predecessors) { if (PhiIncoming(*phi, predecessor) == nullptr) { return false; } } } std::vector edgeArm(predecessors.size()); std::vector arms; for (std::size_t edge = 0; edge < predecessors.size(); edge++) { std::uint32_t arm = 0; for (; arm < arms.size(); arm++) { const bool same = std::ranges::all_of(phis, [&](const IrValue* phi) { return EquivalentValue(m_program.Resources(), PhiIncoming(*phi, predecessors[edge]), PhiIncoming(*phi, arms[arm])); }); if (same) { break; } } if (arm == arms.size()) { arms.push_back(predecessors[edge]); } edgeArm[edge] = arm; } for (const IrBlock* arm : arms) { for (std::uint32_t slot = 0; slot < handleCount; slot++) { for (std::size_t dword = 0; split[slot] && dword < handles[slot]->ArgumentCount(); dword++) { if (SubstituteEdge(handles[slot]->Argument(dword), arm, inst, false, phiSearchDepth) == nullptr) { return false; } } } } std::vector conditions; for (std::uint32_t arm = 0; arm + 1u < arms.size(); arm++) { std::vector edges(edgeArm.size()); for (std::size_t edge = 0; edge < edgeArm.size(); edge++) { edges[edge] = edgeArm[edge] == arm; } conditions.push_back(&EdgeSelector(*block, edges)); } ReplaceWithArmCopies(inst, handles, split, handleCount, conditions, false, [&](std::uint32_t arm, std::uint32_t slot, std::size_t dword) { return SubstituteEdge(handles[slot]->Argument(dword), arms[arm], inst, true, phiSearchDepth); }); return true; } template void ReplaceWithArmCopies(IrValue& inst, const std::array& handles, const std::array& split, std::uint32_t handleCount, std::span conditions, bool optionalArms, Dword&& dword) { std::vector copies; const auto flags = inst.Flags(); const bool optional = optionalArms && !conditions.empty() && split[0]; if (optional) { m_program.Resources().memoryInfo.at(flags.index).descriptorWebArm = true; } std::vector> armWords; const auto memory = m_program.Resources().memoryInfo.at(flags.index); for (std::uint32_t arm = 0; arm <= conditions.size(); arm++) { std::vector arguments(inst.Arguments().begin(), inst.Arguments().end()); for (std::uint32_t slot = 0; slot < handleCount; slot++) { if (!split[slot]) { continue; } std::vector dwords; for (std::size_t index = 0; index < handles[slot]->ArgumentCount(); index++) { dwords.push_back(dword(arm, slot, index)); } if (slot == 0u && optional) { armWords.push_back({dwords[0], dwords[1], dwords[3]}); } arguments[slot] = EmitCopy(*handles[slot], dwords, inst); } auto copyFlags = flags; if (arm != 0u) { copyFlags.index = static_cast(m_program.Resources().memoryInfo.size()); m_program.Resources().memoryInfo.push_back(memory); } std::uint64_t rawFlags = 0; std::memcpy(&rawFlags, ©Flags, sizeof(copyFlags)); copies.push_back(EmitBefore(inst, inst.Opcode(), inst.Type(), arguments, rawFlags)); } if (optional) { std::array selected = armWords.back(); for (std::uint32_t arm = static_cast(armWords.size()) - 1u; arm-- > 0u;) { for (std::size_t word = 0; word < selected.size(); word++) { const std::array arguments {conditions[arm], armWords[arm][word], selected[word]}; selected[word] = EmitBefore(inst, IrOpcode::SelectU32, IrType::U32, arguments); } } EmitDescriptorFault(inst, UndefinedImageWords(inst, selected), *selected[0], *selected[1]); } IrValue* result = copies.back(); if (copies.size() > 1u) { std::array components {}; for (std::uint32_t component = 0; component < components.size(); component++) { const auto extract = [&](IrValue* vector) { const std::array arguments {vector, &m_builder.Constant(component)}; return EmitBefore(inst, IrOpcode::CompositeExtractU32x4, IrType::U32, arguments); }; IrValue* selected = extract(copies.back()); for (std::uint32_t arm = static_cast(copies.size()) - 1u; arm-- > 0u;) { const std::array arguments {conditions[arm], extract(copies[arm]), selected}; selected = EmitBefore(inst, IrOpcode::SelectU32, IrType::U32, arguments); } components[component] = selected; } result = EmitBefore(inst, IrOpcode::CompositeConstructU32x4, IrType::U32x4, components); } inst.ReplaceAllUsesWith(result); inst.Invalidate(); inst.Parent()->RemoveInstruction(&inst); } struct DescriptorWeb { static constexpr std::size_t maxStates = 512u; static constexpr std::size_t maxArms = 64u; static constexpr std::uint32_t foldedArm = 0xffffffffu; std::string failure; bool foldedValue = false; std::vector> arms; std::map, IrValue*> selectors; std::vector> phis; std::vector> lanes; std::unordered_map resolved; std::unordered_map order; }; static bool StrictDescriptorWebs() { static const bool strict = [] { const char* value = std::getenv("APS5_STRICT_DESCRIPTOR_WEBS"); return value != nullptr && std::strcmp(value, "0") != 0; }(); return strict; } static IrValue* GiveUp(DescriptorWeb& web, std::string reason) { if (web.failure.empty()) { web.failure = std::move(reason); } return nullptr; } std::string RebuildBlocker(IrValue* value, std::uint32_t depth) const { value = value->Resolve(); if (value->HasImmediate()) { return {}; } if (value->IsPhi()) { IrValue* invariant = ResolveInvariantPhi(m_program.Resources(), value); if (invariant == nullptr || invariant->IsPhi()) { return "a Phi that does not resolve to one value: " + describeValueChain(value, 3u); } return RebuildBlocker(invariant, depth); } if (depth == 0u) { return "a chain deeper than " + std::to_string(phiSearchDepth) + " operations"; } const auto op = value->Opcode(); const bool register_ = op == IrOpcode::GetUserData; const bool argumentless = (op == IrOpcode::GetShaderBase || op == IrOpcode::GetSrtResource) && value->ArgumentCount() == 0u; const bool srtRead = op == IrOpcode::ReadConst && value->ArgumentCount() == 2u && value->Argument(0)->Resolve()->Opcode() == IrOpcode::GetSrtResource && value->Argument(1)->Resolve()->HasImmediate(); if (!register_ && !argumentless && !srtRead && !Detail::IsRuntimeUniformOp(op)) { return describeValueChain(value, 3u); } for (std::size_t index = 0; !register_ && index < value->ArgumentCount(); index++) { auto blocker = RebuildBlocker(value->Argument(index), depth - 1u); if (!blocker.empty()) { return blocker; } } return {}; } IrValue* WebSelector(std::vector tuple, DescriptorWeb& web, IrValue& position) { for (IrValue*& value : tuple) { value = value->Resolve(); if (!value->IsPhi()) { continue; } const auto [entry, inserted] = web.resolved.try_emplace(value, value); if (inserted) { IrValue* invariant = ResolveInvariantPhi(m_program.Resources(), value); entry->second = invariant == nullptr || !AvailableAt(invariant, value->Parent()) ? value : invariant->Resolve(); } value = entry->second; } if (const auto known = web.selectors.find(tuple); known != web.selectors.end()) { return known->second; } if (web.order.empty()) { const auto& order = m_program.BlockOrder(); for (std::size_t index = 0; index < order.size(); index++) { web.order.emplace(order[index], index); } } const auto phi = std::ranges::find_if(tuple, [](const IrValue* value) { return value->IsPhi(); }); IrValue* lane = nullptr; std::size_t laneOrder = 0; for (IrValue* value : tuple) { if (value->Opcode() != IrOpcode::SelectU32 && value->Opcode() != IrOpcode::ReadFirstLane) { continue; } const auto found = web.order.find(value->Parent()); const std::size_t index = found == web.order.end() ? 0u : found->second; if (lane == nullptr || index > laneOrder) { lane = value; laneOrder = index; } } std::size_t phiOrder = 0; for (const IrValue* value : tuple) { if (!value->IsPhi()) { continue; } const auto found = web.order.find(value->Parent()); phiOrder = std::max(phiOrder, found == web.order.end() ? 0u : found->second); } if (lane != nullptr && (phi == tuple.end() || laneOrder > phiOrder)) { return LaneSelector(tuple, *lane, web, position); } if (phi == tuple.end()) { if (std::ranges::any_of(tuple, [](IrValue* value) { return Detail::HoldsFoldableValue(value, phiSearchDepth); })) { if (StrictDescriptorWebs()) { return GiveUp(web, "a descriptor dword depends on the program counter, EXEC, a lane mask, the lane index or texels"); } web.foldedValue = true; return &m_builder.Constant(DescriptorWeb::foldedArm); } for (IrValue* value : tuple) { if (Rematerialize(value, position, false, phiSearchDepth) == nullptr) { if (StrictDescriptorWebs()) { return GiveUp(web, "a descriptor dword is not rebuilt from user data, the SRT and uniform operations before the read (" + RebuildBlocker(value, phiSearchDepth) + ")"); } web.foldedValue = true; return &m_builder.Constant(DescriptorWeb::foldedArm); } } const auto same = std::ranges::find_if(web.arms, [&](const std::vector& arm) { return std::ranges::equal(tuple, arm, [&](IrValue* left, IrValue* right) { return EquivalentValue(m_program.Resources(), left, right); }); }); if (same != web.arms.end()) { return &m_builder.Constant(static_cast(same - web.arms.begin())); } if (web.arms.size() == DescriptorWeb::maxArms) { return GiveUp(web, "more than 64 distinct descriptors"); } web.arms.push_back(std::move(tuple)); return &m_builder.Constant(static_cast(web.arms.size() - 1u)); } IrBlock* block = nullptr; std::size_t latest = 0; for (const IrValue* value : tuple) { if (!value->IsPhi()) { continue; } const auto found = web.order.find(value->Parent()); if (found == web.order.end()) { return GiveUp(web, "a descriptor Phi is in a block outside the program order"); } if (block == nullptr || found->second > latest) { block = value->Parent(); latest = found->second; } } if (block == nullptr) { return GiveUp(web, "a descriptor Phi has no block"); } if (web.selectors.size() == DescriptorWeb::maxStates) { return GiveUp(web, "more than 512 descriptor states"); } const auto& predecessors = block->Predecessors(); if (predecessors.size() < 2u) { return GiveUp(web, "a descriptor Phi's block has fewer than two predecessors"); } for (const IrValue* value : tuple) { if (value->IsPhi() && value->Parent() == block && value->PhiBlockCount() != predecessors.size()) { return GiveUp(web, "a descriptor Phi's operands do not match its block's predecessors"); } } IrValue& selector = m_program.CreateValue(IrOpcode::Phi, IrType::U32); web.selectors.emplace(tuple, &selector); web.phis.emplace_back(block, &selector); for (IrBlock* predecessor : predecessors) { std::vector incoming; for (IrValue* value : tuple) { IrValue* edge = value->IsPhi() && value->Parent() == block ? PhiIncoming(*value, predecessor) : value; if (edge == nullptr) { return GiveUp(web, "a descriptor Phi has no value for one of its predecessors"); } incoming.push_back(edge); } IrValue* edgeSelector = WebSelector(std::move(incoming), web, position); if (edgeSelector == nullptr) { return nullptr; } selector.AddPhiOperand(predecessor, edgeSelector); } return &selector; } static bool EarlierPhi(const IrValue& value, const IrBlock* block, const DescriptorWeb& web) { if (!value.IsPhi()) { return false; } const auto phiBlock = web.order.find(value.Parent()); const auto laneBlock = web.order.find(block); return phiBlock != web.order.end() && laneBlock != web.order.end() && phiBlock->second < laneBlock->second; } IrValue* LaneSelector(const std::vector& tuple, IrValue& first, DescriptorWeb& web, IrValue& position) { IrBlock* block = first.Parent(); const auto op = first.Opcode(); const bool select = op == IrOpcode::SelectU32; if (block == nullptr || first.ArgumentCount() != (select ? 3u : 2u)) { return GiveUp(web, "a per-lane descriptor selection has an unexpected form"); } if (web.selectors.size() == DescriptorWeb::maxStates) { return GiveUp(web, "more than 512 descriptor states"); } IrValue* shared = first.Argument(select ? 0u : 1u); std::vector left; std::vector right; for (IrValue* value : tuple) { if (value->Opcode() == op && value->Parent() == block && value->ArgumentCount() == first.ArgumentCount() && EquivalentValue(m_program.Resources(), value->Argument(select ? 0u : 1u), shared)) { left.push_back(value->Argument(select ? 1u : 0u)); if (select) { right.push_back(value->Argument(2u)); } } else if (value->HasImmediate() || (select && !value->IsPhi() && value->Opcode() != IrOpcode::ReadFirstLane) || EarlierPhi(*value, block, web)) { left.push_back(value); if (select) { right.push_back(value); } } else { return GiveUp(web, "a per-lane descriptor selection mixes dwords selected differently"); } } auto& instructions = block->Instructions(); auto latest = instructions.end(); for (auto it = instructions.begin(); it != instructions.end(); ++it) { if (std::ranges::find(tuple, *it) != tuple.end()) { latest = it; } } if (latest == instructions.end()) { return GiveUp(web, "a per-lane descriptor selection is not in its block"); } IrValue* before = std::next(latest) == instructions.end() ? nullptr : *std::next(latest); IrValue* leftSelector = WebSelector(std::move(left), web, position); if (leftSelector == nullptr) { return nullptr; } IrValue& node = m_program.CreateValue(op, IrType::U32, first.Flags()); if (select) { IrValue* rightSelector = WebSelector(std::move(right), web, position); if (rightSelector == nullptr) { return nullptr; } node.AddArgument(shared); node.AddArgument(leftSelector); node.AddArgument(rightSelector); } else { node.AddArgument(leftSelector); node.AddArgument(shared); } web.selectors.emplace(tuple, &node); web.lanes.emplace_back(block, before, &node); return &node; } void SplitDescriptorWeb(IrValue& inst, const std::array& handles, const std::array& split, std::uint32_t handleCount) { std::vector tuple; std::array offsets {}; for (std::uint32_t slot = 0; slot < handleCount; slot++) { offsets[slot] = tuple.size(); for (std::size_t dword = 0; split[slot] && dword < handles[slot]->ArgumentCount(); dword++) { tuple.push_back(handles[slot]->Argument(dword)); } } DescriptorWeb web; IrValue* selector = WebSelector(tuple, web, inst); const bool select = selector != nullptr && !web.arms.empty() && (web.arms.size() > 1u || web.foldedValue); if (select) { for (const auto& [block, phi] : web.phis) { block->InsertInstructionBefore(nullptr, phi); } for (const auto& [block, before, node] : web.lanes) { if (before == nullptr) { block->AppendInstruction(node); } else { block->InsertInstructionBefore(before, node); } } } else { for (const auto& [block, phi] : web.phis) { for (std::size_t index = 0; index < phi->ArgumentCount(); index++) { phi->ReplaceArgument(index, nullptr); } } for (const auto& [block, before, node] : web.lanes) { for (std::size_t index = 0; index < node->ArgumentCount(); index++) { node->ReplaceArgument(index, nullptr); } } for (const auto& [block, phi] : web.phis) { phi->Invalidate(); } for (const auto& [block, before, node] : web.lanes) { node->Invalidate(); } } if (selector == nullptr || web.arms.empty()) { const std::string reason = !web.failure.empty() ? web.failure : web.foldedValue ? std::string("no path's descriptor can be rebuilt from supported runtime values") : std::string("no descriptor was found on any path"); for (std::uint32_t slot = 0; slot < handleCount; slot++) { if (split[slot]) { m_splitFailures.try_emplace(handles[slot], reason); } } return; } if (web.foldedValue) { m_foldedWeb = true; const std::array compare {selector, &m_builder.Constant(DescriptorWeb::foldedArm)}; IrValue* folded = EmitBefore(inst, IrOpcode::IEqual32, IrType::Bool, compare); EmitDescriptorFault(inst, *folded, m_builder.Constant(0u), m_builder.Constant(0u)); const std::array first {folded, &m_builder.Constant(0u), selector}; selector = EmitBefore(inst, IrOpcode::SelectU32, IrType::U32, first); } std::vector conditions; for (std::uint32_t arm = 0; arm + 1u < web.arms.size(); arm++) { const std::array compare {selector, &m_builder.Constant(arm)}; conditions.push_back(EmitBefore(inst, IrOpcode::IEqual32, IrType::Bool, compare)); } ReplaceWithArmCopies(inst, handles, split, handleCount, conditions, true, [&](std::uint32_t arm, std::uint32_t slot, std::size_t dword) { return Rematerialize(web.arms[arm][offsets[slot] + dword], inst, true, phiSearchDepth); }); } std::string SplitFailure(const IrValue& handle) const { const auto found = m_splitFailures.find(&handle); return found == m_splitFailures.end() ? std::string() : "; descriptor web: " + found->second; } void SplitDescriptorPhis() { std::vector candidates; for (auto& block : m_program.Blocks()) { for (IrValue* inst : block->Instructions()) { if (SplittableImageRead(*inst)) { candidates.push_back(inst); } } } for (IrValue* inst : candidates) { SplitDescriptorPhi(*inst); } } void PlanTableColumns() { static const bool enabled = std::getenv("APS5_NO_IMAGE_TABLES") == nullptr; if (!enabled) { return; } const auto sources = m_sources; const auto columns = m_tableColumns; const auto reads = m_tablePlanningReads; const auto memory = m_tablePlanningMemory; PlanTableColumnPass(); while (!PlannedWithinLimits()) { const auto group = LargestSelectedGroup(); if (group.empty()) { return; } m_unselectedHandles.insert(m_unselectedHandles.end(), group.begin(), group.end()); m_sources = sources; m_tableColumns = columns; m_tablePlanningReads = reads; m_tablePlanningMemory = memory; PlanTableColumnPass(); } } bool PlannedWithinLimits() { std::vector images; std::vector> pairs; std::vector directImages; std::vector> directSamplers; const auto direct = std::numeric_limits::max() / 2u; for (auto& block : m_program.Blocks()) { for (IrValue* inst : block->Instructions()) { const auto imageInfo = ImageOpcodeInfoOf(inst->Opcode()); if (inst->Opcode() == IrOpcode::ImageBvhIntersectRay || imageInfo.access == ImageAccess::None || imageInfo.resourceClass == ImageResourceClass::None || inst->ArgumentCount() == 0u) { continue; } const auto flags = inst->Flags(); if (flags.index >= m_program.Resources().memoryInfo.size()) { continue; } const auto& memory = m_program.Resources().memoryInfo[flags.index]; if (memory.kind != ResourceKind::Image || memory.planningOnly || IsTablePlanningMemory(flags.index)) { continue; } const IrValue* handle = inst->Argument(0)->Resolve(); const TableColumnPlan* column = FindTableColumn(*handle); std::uint32_t source = 0; if (column != nullptr) { source = column->source; } else { const auto found = std::ranges::find_if(directImages, [&](const IrValue* other) { return SameHandle(*other, *handle); }); source = direct + static_cast(found - directImages.begin()); if (found == directImages.end()) directImages.push_back(handle); } const auto key = ImageKey(source, memory, inst->Opcode()); const auto image = std::ranges::find_if(images, [&](const ImageResource& other) { return SameImageKey(other, key); }); const auto imageIndex = static_cast(image - images.begin()); if (image == images.end()) images.push_back(key); if (!imageInfo.needsSampler || inst->ArgumentCount() < 2u) { continue; } const IrValue* samplerHandle = inst->Argument(1)->Resolve(); const TableColumnPlan* samplerColumn = FindTableColumn(*samplerHandle); std::uint32_t sampler = 0; if (samplerColumn != nullptr) { sampler = samplerColumn->source; } else { const bool sampleAdjust = (memory.imageSampleFlags & RdnaImageSampleFlagAdjust) != 0; const auto found = std::ranges::find_if(directSamplers, [&](const auto& other) { return other.second == sampleAdjust && SameHandle(*other.first, *samplerHandle); }); sampler = direct + static_cast(found - directSamplers.begin()); if (found == directSamplers.end()) directSamplers.emplace_back(samplerHandle, sampleAdjust); } if (std::ranges::find(pairs, std::pair{imageIndex, sampler}) == pairs.end()) pairs.emplace_back(imageIndex, sampler); } } return images.size() <= ShaderInfo::MaxImages && pairs.size() <= ShaderInfo::MaxSampledPairs; } std::vector LargestSelectedGroup() const { const auto unselected = [](TableColumn column) { column.selected = false; column.selection = 0u; return column; }; std::vector> groups; for (const auto& plan : m_tableColumns) { const auto& column = m_sources[plan.source].tableColumn; if (!column.has_value()) { continue; } const auto group = std::ranges::find_if(groups, [&](const std::vector& members) { return unselected(*m_sources[members.front()].tableColumn) == unselected(*column); }); if (group == groups.end()) { groups.push_back({plan.source}); } else if (std::ranges::find(*group, plan.source) == group->end()) { group->push_back(plan.source); } } const std::vector* largest = nullptr; for (const auto& group : groups) { const bool selected = std::ranges::any_of(group, [&](std::uint32_t source) { return m_sources[source].tableColumn->selected; }); if (selected && group.size() > 1u && (largest == nullptr || group.size() > largest->size())) { largest = &group; } } std::vector handles; if (largest == nullptr) { return handles; } for (const auto& plan : m_tableColumns) { if (std::ranges::find(*largest, plan.source) != largest->end()) { handles.push_back(plan.handle); } } return handles; } void PlanTableColumnPass() { IndexMemoryUsers(); const auto directImages = DirectImageHandles(); for (auto& block : m_program.Blocks()) { for (IrValue* inst : block->Instructions()) { const auto imageInfo = ImageOpcodeInfoOf(inst->Opcode()); if (imageInfo.access == ImageAccess::None || inst->ArgumentCount() == 0u) { continue; } const auto flags = inst->Flags(); if (flags.index >= m_program.Resources().memoryInfo.size()) { continue; } const auto memory = m_program.Resources().memoryInfo[flags.index]; IrValue& image = *inst->Argument(0)->Resolve(); if (std::ranges::none_of(directImages, [&](const IrValue* handle) { return SameHandle(*handle, image); })) { static_cast(PlanTableColumn(image, false, memory.imageR128)); } if (imageInfo.needsSampler && inst->ArgumentCount() > 1u && !DirectSampler(*inst->Argument(1)->Resolve())) { static_cast(PlanTableColumn(*inst->Argument(1)->Resolve(), true, false)); } } } for (const auto& plan : m_tableColumns) { for (std::size_t index = 0; index < plan.reads.size(); index++) { const IrValue* read = plan.reads[index]; const bool planned = std::ranges::all_of(read->Uses(), [&](const IrValue* user) { return FindTableColumn(*user) != nullptr; }); if (planned && std::ranges::find(m_tablePlanningReads, read) == m_tablePlanningReads.end()) { m_tablePlanningReads.push_back(read); m_tablePlanningMemory.push_back(plan.memory[index]); } } } for (bool marked = true; marked;) { marked = false; for (const auto& plan : m_tableColumns) { for (std::size_t index = 0; index < plan.hopReads.size(); index++) { const IrValue* read = plan.hopReads[index]; if (std::ranges::find(m_tablePlanningReads, read) != m_tablePlanningReads.end()) { continue; } const bool planned = std::ranges::all_of(read->Uses(), [&](const IrValue* pointer) { return pointer->Opcode() == IrOpcode::GetAddressResource && std::ranges::all_of(pointer->Uses(), [&](const IrValue* user) { return std::ranges::find(m_tablePlanningReads, user) != m_tablePlanningReads.end(); }); }); if (planned) { m_tablePlanningReads.push_back(read); m_tablePlanningMemory.push_back(plan.hopMemory[index]); marked = true; } } } } m_memoryUsersIndexed = false; m_memoryUsers.clear(); } void KeepSourceAlive(IrValue& position, const DescriptorSource& source, std::vector& referenced) { for (std::uint32_t dword = 0; dword < source.dwordCount; dword++) { IrValue* value = source.dwords[dword]->Resolve(); if (value->HasImmediate() || std::ranges::find(referenced, value) != referenced.end()) { continue; } referenced.push_back(value); const std::array arguments{value}; EmitBefore(position, IrOpcode::ReferenceU32, IrType::Void, arguments); } } bool DirectSampler(const IrValue& handle) { if (handle.Opcode() != IrOpcode::GetSamplerResource || handle.ArgumentCount() != 4u || FindTableColumn(handle) != nullptr) { return false; } DescriptorSource descriptor; MakeSource(handle, 4u, true, false, descriptor); std::uint32_t badDword = 0; return ValidateSource(descriptor, badDword); } bool DirectImage(const IrValue& handle) { if (handle.Opcode() != IrOpcode::GetImageResource || handle.ArgumentCount() != 8u) { return false; } DescriptorSource descriptor; MakeSource(handle, 8u, false, false, descriptor); std::uint32_t badDword = 0; return ValidateSource(descriptor, badDword); } std::vector DirectImageHandles() { std::vector handles; for (auto& block : m_program.Blocks()) { for (IrValue* inst : block->Instructions()) { const auto imageInfo = ImageOpcodeInfoOf(inst->Opcode()); if (imageInfo.access == ImageAccess::None || inst->ArgumentCount() == 0u) { continue; } const auto flags = inst->Flags(); if (flags.index >= m_program.Resources().memoryInfo.size()) { continue; } const auto& memory = m_program.Resources().memoryInfo[flags.index]; const bool tableUnsupported = imageInfo.resourceClass == ImageResourceClass::Storage || memory.imagePacked || memory.imageByElements != 0u; const IrValue* handle = inst->Argument(0)->Resolve(); if (tableUnsupported && DirectImage(*handle)) { handles.push_back(handle); } } } return handles; } bool SameHandle(const IrValue& left, const IrValue& right) const { if (&left == &right) { return true; } if (left.Opcode() != right.Opcode() || left.ArgumentCount() != right.ArgumentCount()) { return false; } for (std::size_t index = 0; index < left.ArgumentCount(); index++) { if (!EquivalentValue(m_program.Resources(), left.Argument(index), right.Argument(index))) { return false; } } return true; } bool IsTablePlanningMemory(std::uint32_t index) const { return std::ranges::find(m_tablePlanningMemory, index) != m_tablePlanningMemory.end(); } bool TableSource(std::uint32_t source) const { return source < m_sources.size() && m_sources[source].tableColumn.has_value(); } bool SameBufferSource(const IrValue& handle, std::uint32_t source) const { const auto& current = m_sources[source]; if (current.tableColumn.has_value() || current.dwordCount != 4u || handle.ArgumentCount() != 4u) { return false; } for (std::uint32_t dword = 0; dword < 4u; dword++) { if (!EquivalentValue(m_program.Resources(), current.dwords[dword], handle.Argument(dword))) { return false; } } return true; } bool SelectionWritten(const TableColumn& column) const { const auto written = [&](const auto& matches) { return std::any_of(m_info.buffers.begin(), m_info.buffers.end(), [&](const auto& buffer) { return (buffer.written || buffer.atomic) && matches(buffer.source); }); }; std::vector pending{m_sources[column.selection].dwords[0]}; std::vector seen; while (!pending.empty()) { const IrValue* current = pending.back()->Resolve(); pending.pop_back(); if (std::ranges::find(seen, current) != seen.end()) { continue; } seen.push_back(current); const bool pointer = current->Opcode() == IrOpcode::LoadAddressU32; const bool buffer = current->Opcode() == IrOpcode::ReadConstBuffer && current->ArgumentCount() != 0u; if ((pointer || buffer) && m_info.bdaWrites) { return true; } if (pointer && written([](std::uint32_t) { return true; })) { return true; } if (buffer) { const IrValue* handle = current->Argument(0)->Resolve(); if (written([&](std::uint32_t source) { return SameBufferSource(*handle, source); })) { return true; } } for (std::size_t index = 0; index < current->ArgumentCount(); index++) { pending.push_back(current->Argument(index)); } } return false; } void CheckTableStores() { for (auto& source : m_sources) { if (source.tableColumn.has_value() && source.tableColumn->selected && SelectionWritten(*source.tableColumn)) { source.tableColumn->selected = false; source.tableColumn->keyDomains.clear(); } if (!source.tableColumn.has_value() || source.tableColumn->keyDomains.empty()) { continue; } auto& column = *source.tableColumn; const bool written = std::any_of(column.keyDomains.begin(), column.keyDomains.end(), [&](const KeyDomain& domain) { return std::any_of(m_info.buffers.begin(), m_info.buffers.end(), [&](const auto& buffer) { return (buffer.written || buffer.atomic) && buffer.source == domain.source; }); }); if (written) { column.keyDomains.clear(); } } } void AssignTables() { std::uint32_t next = 0; for (auto& image : m_info.images) { if (!TableSource(image.source)) { continue; } image.table = next++; } for (auto& sampler : m_info.samplers) { if (TableSource(sampler.source)) { sampler.table = next++; } } } void GetHandle(IrValue& inst, IrValue* value, IrOpcode expected, std::uint32_t width, IrValue*& handle, std::uint32_t& source, bool sampler = false, bool sampleAdjust = false) { handle = value->Resolve(); if (handle->Opcode() != expected) { fail("memory operation requires " + std::string(IrOpcodeName(expected))); } DescriptorSource descriptor; MakeSource(*handle, width, sampler, sampleAdjust, descriptor); std::uint32_t badDword = 0; if (!ValidateSource(descriptor, badDword)) { std::array planned {}; std::uint32_t plannedBad = 0; DescriptorSource guarded; if (SplitFailure(*handle).empty() && GuardedSingleSource(*handle, width, planned)) { MakeSource(planned, width, sampler, sampleAdjust, guarded); if (ValidateSource(guarded, plannedBad)) { source = InternSource(guarded); m_descriptorGuards.push_back({&inst, std::vector(handle->Arguments().begin(), handle->Arguments().end()), std::vector(planned.begin(), planned.begin() + width)}); return; } } fail(std::string(IrOpcodeName(expected)) + " dword " + std::to_string(badDword) + " is not a valid runtime value; chain: " + describeValueChain(descriptor.dwords[badDword], 8u) + SplitFailure(*handle)); } source = InternSource(descriptor); } bool TakeGpuDescriptor(IrValue& inst, std::uint32_t memoryIndex) { const IrValue* handle = inst.Argument(0)->Resolve(); if (handle->Opcode() != IrOpcode::GetBufferResource || handle->ArgumentCount() != 4u) { return false; } auto& scalarMemory = m_program.Resources().memoryInfo[memoryIndex]; if (scalarMemory.kind == ResourceKind::ScalarBuffer && static_cast((knownSetBits(handle->Argument(1)) >> 16u) & 0x3fffu) * knownSetBits(handle->Argument(2)) > std::numeric_limits::max()) { scalarMemory.gpuDescriptor = true; m_info.usesDma = true; return true; } DescriptorSource descriptor; MakeSource(*handle, 4u, false, false, descriptor); std::uint32_t badDword = 0; auto& memory = m_program.Resources().memoryInfo[memoryIndex]; const auto access = BufferAccessOf(inst.Opcode()); if (ValidateSource(descriptor, badDword)) { const auto source = InternSource(descriptor); const auto resource = AddBuffer(source, memory, inst.Opcode(), inst.Flags().pc); if (resource == std::numeric_limits::max()) fail("buffer resource limit exceeded"); AddMemoryPatch(memoryIndex, resource, 0u, false); memory.gpuDescriptor = false; m_info.usesFaultBuffer = m_info.usesFaultBuffer || access == BufferAccess::Atomic; return true; } memory.gpuDescriptor = true; m_info.usesDma = true; m_info.bdaWrites = m_info.bdaWrites || access == BufferAccess::Write || access == BufferAccess::Atomic; return true; } void ValidateAddressHandle(IrValue* value) const { const IrValue* handle = value->Resolve(); if (handle->Opcode() != IrOpcode::GetAddressResource) { fail("address operation requires GetAddressResource"); } if (handle->ArgumentCount() != 2) { fail("GetAddressResource must have two address dwords"); } } std::uint32_t AddBuffer(std::uint32_t source, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) { for (std::uint32_t i = 0; i < m_info.buffers.size(); i++) { if (m_info.buffers[i].source == source) { Merge(m_info.buffers[i], memory, op, pc); return i; } } BufferResource resource; resource.source = source; resource.firstUsePc = pc; Merge(resource, memory, op, pc); m_info.buffers.push_back(resource); return static_cast(m_info.buffers.size() - 1); } static void Merge(BufferResource& resource, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) { const auto access = BufferAccessOf(op); const bool atomic = access == BufferAccess::Atomic; const bool write = access == BufferAccess::Write || atomic; resource.firstUsePc = std::min(resource.firstUsePc, pc); resource.maxByteExtent = std::max(resource.maxByteExtent, byteExtent(memory)); resource.read = resource.read || !write || atomic; resource.written = resource.written || write; resource.atomic = resource.atomic || atomic; resource.formatted = resource.formatted || memory.formatted; resource.descriptorFormatted = resource.descriptorFormatted || (memory.formatted && !memory.typed); if (memory.formatted && memory.typed) { const auto format = GetFormatInfo(DecodeTBufferFormat(memory.dataFormat, memory.numberFormat)); if (format.byteSize == 0u) fail("typed buffer instruction has an invalid format"); resource.typedAlignment = std::max(resource.typedAlignment, static_cast(std::min(format.byteSize, 4u))); } if (memory.formatted && !memory.typed && !write) resource.formattedReadMask |= (1u << std::min(memory.dataDwords, 4u)) - 1u; resource.scalar = resource.scalar || op == IrOpcode::ReadConstBuffer || memory.kind == ResourceKind::ScalarBuffer; } static ImageResource ImageKey(std::uint32_t source, const MemoryInfo& memory, IrOpcode op) { ImageResource image; image.source = source; image.resourceClass = ImageOpcodeInfoOf(op).resourceClass; image.depthCompare = (memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0; image.remappedDepth1D = image.depthCompare && (memory.imageDimension == RdnaImageDimension::Dim1D || memory.imageDimension == RdnaImageDimension::Dim1DArray); image.dimension = image.remappedDepth1D ? (memory.imageDimension == RdnaImageDimension::Dim1D ? RdnaImageDimension::Dim2D : RdnaImageDimension::Dim2DArray) : memory.imageDimension; image.mipMode = image.resourceClass == ImageResourceClass::Storage && memory.imageHasMip ? ImageMipMode::DynamicStorage : ImageMipMode::None; image.r128 = memory.imageR128; image.packed = memory.imagePacked; image.byElements = memory.imageByElements; image.byComponents = memory.imageByElements != 0u ? memory.dataDwords / memory.imageByElements : 0u; return image; } static bool SameImageKey(const ImageResource& image, const ImageResource& key) { return image.source == key.source && image.resourceClass == key.resourceClass && image.dimension == key.dimension && image.mipMode == key.mipMode && image.depthCompare == key.depthCompare && image.remappedDepth1D == key.remappedDepth1D && image.r128 == key.r128 && image.packed == key.packed && image.byElements == key.byElements && image.byComponents == key.byComponents; } std::uint32_t AddImage(std::uint32_t source, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) { const auto key = ImageKey(source, memory, op); for (std::uint32_t i = 0; i < m_info.images.size(); i++) { auto& image = m_info.images[i]; if (SameImageKey(image, key)) { Merge(image, memory, op, pc); return i; } } if (m_info.images.size() >= ShaderInfo::MaxImages) { return std::numeric_limits::max(); } ImageResource image = key; image.singleMipPlaneBiasCompatible = m_program.Resources().stage == IrShaderStage::Pixel; image.volumeArraySampleCompatible = true; image.firstUsePc = pc; image.optionalDescriptor = memory.descriptorWebArm; Merge(image, memory, op, pc); m_info.images.push_back(image); return static_cast(m_info.images.size() - 1); } static void Merge(ImageResource& image, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) { image.optionalDescriptor = image.optionalDescriptor && memory.descriptorWebArm; const auto access = ImageOpcodeInfoOf(op).access; if (memory.imagePacked && op != IrOpcode::ImageRead && op != IrOpcode::ImageWrite) image.tableOperation = TableOperation::Unsupported; const bool atomic = access == ImageAccess::Atomic; const bool write = access == ImageAccess::Write || atomic; const bool atomic64 = IsImageAtomic64Opcode(op); if ((image.read || image.written) && image.atomic64 != atomic64) { throw std::runtime_error("an image accessed by 64-bit atomics is also accessed in another way"); } image.atomic64 = atomic64; image.firstUsePc = std::min(image.firstUsePc, pc); image.read = image.read || !write || atomic; image.written = image.written || write; image.atomic = image.atomic || atomic; image.gathered = image.gathered || op == IrOpcode::ImageGatherRaw; image.srgbDecodeCompatible = image.srgbDecodeCompatible && !ImageOpcodeInfoOf(op).needsSampler; image.fmaskCompatible = image.fmaskCompatible && op == IrOpcode::ImageRead && memory.dataBits == 32u; image.singleMipPlaneBiasCompatible = image.singleMipPlaneBiasCompatible && op == IrOpcode::ImageSampleRaw && memory.imageSampleFlags == RdnaImageSampleFlagBias && memory.dataBits == 32u && !memory.imageR128 && !memory.imagePacked && memory.imageByElements == 0u; image.depthBitsCompatible = image.depthBitsCompatible && memory.dataBits == 32u; image.constantSwizzleCompatible = image.constantSwizzleCompatible && (op == IrOpcode::ImageSampleRaw || op == IrOpcode::ImageGatherRaw); const bool cubeFaceLoad = op == IrOpcode::ImageRead && !memory.imageHasMip && memory.imageSampleFlags == 0u; const auto addressedFlags = memory.imageSampleFlags & ~RdnaImageSampleFlagAdjust; image.adjustedSample = image.adjustedSample || (op == IrOpcode::ImageSampleRaw && (memory.imageSampleFlags & RdnaImageSampleFlagAdjust) != 0u); const bool cubeFaceSample = op == IrOpcode::ImageSampleRaw && (addressedFlags == 0u || addressedFlags == RdnaImageSampleFlagLevelZero || addressedFlags == RdnaImageSampleFlagLod); const bool cubeFaceStore = op == IrOpcode::ImageWrite && !memory.imageHasMip; const bool cubeFaceGather = op == IrOpcode::ImageGatherRaw && (memory.imageSampleFlags == 0u || memory.imageSampleFlags == RdnaImageSampleFlagLevelZero); image.cubeFaceCompatible = image.cubeFaceCompatible && (cubeFaceLoad || cubeFaceSample || cubeFaceStore || cubeFaceGather) && !memory.imagePacked && memory.imageByElements == 0u; image.volumeArraySampleCompatible = image.volumeArraySampleCompatible && cubeFaceSample && !memory.imageR128 && !memory.imagePacked && memory.imageByElements == 0u; const bool flatVolumeLoad = op == IrOpcode::ImageRead && memory.imageSampleFlags == 0u; const bool flatVolumeSample = op == IrOpcode::ImageSampleRaw && (memory.imageSampleFlags & ~(RdnaImageSampleFlagLevelZero | RdnaImageSampleFlagLod | RdnaImageSampleFlagBias | RdnaImageSampleFlagOffset)) == 0u; const bool flatVolumeQuery = op == IrOpcode::ImageQueryDimensions; image.volumeGather = image.volumeGather || (op == IrOpcode::ImageGatherRaw && image.dimension == RdnaImageDimension::Dim3D); image.flatVolumeCompatible = image.flatVolumeCompatible && (flatVolumeLoad || flatVolumeSample || flatVolumeQuery) && !memory.imagePacked && memory.imageByElements == 0u; const bool flatLineLoad = op == IrOpcode::ImageRead && !memory.imageHasMip && memory.imageSampleFlags == 0u; const auto lineSampleFlags = memory.imageSampleFlags & ~RdnaImageSampleFlagCompare; const bool flatLineSample = op == IrOpcode::ImageSampleRaw && (lineSampleFlags == RdnaImageSampleFlagLevelZero || lineSampleFlags == 0u); const bool flatLineExplicitLod = op == IrOpcode::ImageSampleRaw && memory.imageSampleFlags == RdnaImageSampleFlagLod; const bool flatLineLevelZero = op == IrOpcode::ImageSampleRaw && memory.imageSampleFlags == RdnaImageSampleFlagLevelZero; const bool flatLineGather = op == IrOpcode::ImageGatherRaw && memory.imageSampleFlags == RdnaImageSampleFlagLevelZero; const bool flatLineBiasOrOffset = op == IrOpcode::ImageSampleRaw && (memory.imageSampleFlags & (RdnaImageSampleFlagBias | RdnaImageSampleFlagOffset)) != 0u && (memory.imageSampleFlags & ~(RdnaImageSampleFlagLevelZero | RdnaImageSampleFlagLod | RdnaImageSampleFlagBias | RdnaImageSampleFlagOffset)) == 0u; const bool flatLineMipLoad = op == IrOpcode::ImageRead && memory.imageHasMip && memory.imageSampleFlags == 0u; const bool flatLineAccess = image.dimension == RdnaImageDimension::Dim2DArray ? flatLineLevelZero || flatLineExplicitLod : flatLineLoad || flatLineMipLoad || flatLineSample || flatLineExplicitLod || flatLineGather || flatLineBiasOrOffset; image.flatLineCompatible = image.flatLineCompatible && flatLineAccess && !memory.imagePacked && memory.imageByElements == 0u; image.flatVolumeLineCompatible = image.flatVolumeLineCompatible && (flatLineLoad || flatLineSample) && !memory.imagePacked && memory.imageByElements == 0u; const bool layerRowsSample = op == IrOpcode::ImageSampleRaw && (memory.imageSampleFlags == RdnaImageSampleFlagLevelZero || memory.imageSampleFlags == RdnaImageSampleFlagLod); image.layerRowsCompatible = image.layerRowsCompatible && layerRowsSample && !memory.imageR128 && !memory.imagePacked && memory.imageByElements == 0u; image.dimensionQueryOnly = image.dimensionQueryOnly && op == IrOpcode::ImageQueryDimensions; image.sampledOnly = image.sampledOnly && ImageOpcodeInfoOf(op).needsSampler; if ((memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0u) { constexpr auto unsupported = RdnaImageSampleFlagLod | RdnaImageSampleFlagDerivative; if ((memory.imageSampleFlags & unsupported) != 0u || (op == IrOpcode::ImageGatherRaw && (memory.imageSampleFlags & RdnaImageSampleFlagGatherHorizontal) != 0u)) image.emulatedCompare |= EmulatedCompare::Unsupported; if ((memory.imageSampleFlags & RdnaImageSampleFlagLevelZero) == 0u) image.emulatedCompare |= EmulatedCompare::RequiresSingleLevel; } else if (ImageOpcodeInfoOf(op).needsSampler) { if (op != IrOpcode::ImageSampleRaw) image.emulatedCompare |= EmulatedCompare::ConvertedSampleUnsupported; if ((memory.imageSampleFlags & RdnaImageSampleFlagLevelZero) == 0u) image.emulatedCompare |= EmulatedCompare::ConvertedSampleRequiresSingleLevel; } } std::uint32_t AddSampler(std::uint32_t source, std::uint32_t pc) { for (std::uint32_t i = 0; i < m_info.samplers.size(); i++) { if (m_info.samplers[i].source == source) { m_info.samplers[i].firstUsePc = std::min(m_info.samplers[i].firstUsePc, pc); return i; } } if (m_info.samplers.size() >= ShaderInfo::MaxSamplers) { return std::numeric_limits::max(); } m_info.samplers.push_back({source, pc}); return static_cast(m_info.samplers.size() - 1); } void AddSampledPair(std::uint32_t image, std::uint32_t sampler, std::uint32_t pc, bool volumeGather) { for (auto& pair : m_info.sampledPairs) { if (pair.image == image && pair.sampler == sampler) { pair.firstUsePc = std::min(pair.firstUsePc, pc); pair.volumeGather = pair.volumeGather || volumeGather; return; } } if (m_info.sampledPairs.size() >= ShaderInfo::MaxSampledPairs) { fail("sampled image/sampler pair limit exceeded"); } m_info.sampledPairs.push_back({image, sampler, pc, volumeGather}); } void AddHandlePatch(IrValue* handle, std::uint32_t resource) { for (const auto& patch : m_handlePatches) { if (patch.handle == handle) { if (patch.resource != resource) { fail(std::string(IrOpcodeName(handle->Opcode())) + " is reused with incompatible resource classes"); } return; } } m_handlePatches.push_back({handle, resource}); } void AddMemoryPatch(std::uint32_t index, std::uint32_t resource, std::uint32_t sampler, bool hasSampler) { for (auto& patch : m_memoryPatches) { if (patch.index != index) { continue; } if (patch.resource != resource || (hasSampler && patch.hasSampler && patch.sampler != sampler)) { fail("memory metadata is reused with incompatible resources"); } if (hasSampler) { patch.sampler = sampler; patch.hasSampler = true; } return; } m_memoryPatches.push_back({index, resource, sampler, hasSampler}); } void NoteHeapGuard(IrValue& inst, const TableColumnPlan& column) { if (!column.heapLive.empty()) { m_descriptorGuards.push_back({&inst, column.heapLive, column.heapPlanned}); } } void Collect(IrValue& inst) { const auto op = inst.Opcode(); if (op == IrOpcode::ImageBvhIntersectRay) { m_info.usesDma = true; return; } const auto buffer = BufferAccessOf(op); const auto addressInfo = AddressOpcodeInfoOf(op); const auto imageInfo = ImageOpcodeInfoOf(op); if (buffer == BufferAccess::None && addressInfo.access == AddressAccess::None && imageInfo.access == ImageAccess::None) { return; } const auto flags = inst.Flags(); if (flags.index >= m_program.Resources().memoryInfo.size()) { fail("memory metadata index " + std::to_string(flags.index) + " is out of range"); } if (inst.ArgumentCount() == 0) { fail("memory operation has no resource handle"); } const auto& memory = m_program.Resources().memoryInfo[flags.index]; if (memory.planningOnly || IsTablePlanningMemory(flags.index)) { return; } IrValue* handle = nullptr; std::uint32_t source = 0; std::uint32_t resource = 0; if (buffer != BufferAccess::None) { if (!TakeGpuDescriptor(inst, flags.index)) fail("buffer operation requires a four-dword runtime V#"); return; } if (addressInfo.access != AddressAccess::None) { if (!IsAddressResourceKind(memory.kind)) { fail("address operation has invalid resource kind"); } if (memory.kind == ResourceKind::Scratch) { handle = inst.Argument(0)->Resolve(); if (handle->Opcode() != IrOpcode::GetScratchResource || handle->ArgumentCount() != 0) { fail("scratch operation requires GetScratchResource"); } if (m_info.scratchDwords == 0) { fail("scratch operation requires a nonzero AGC per-thread size"); } return; } ValidateAddressHandle(inst.Argument(0)); // Debug aid: APS5_TRACE_BDA=1 names every access that makes the program address-based // (a raw scalar load the SRT walker left in place, or a flat/global access), so the // reason a stage takes the BDA path can be read off without a shader dump. // The first few accesses of a program are printed (a Bink kernel has hundreds); Run // reports how many more there were. if (bdaTraceEnabled() && ++m_bdaTraces <= bdaTraceLimit) { const auto* kind = memory.kind == ResourceKind::ScalarAddress ? "scalar address" : memory.kind == ResourceKind::Global ? "global" : "flat"; const IrValue* offset = inst.ArgumentCount() > 1 ? inst.Argument(1)->Resolve() : nullptr; const bool immediateOffset = offset != nullptr && offset->HasImmediate(); std::fprintf(stderr, "[bda] %s at pc 0x%08x: %s access, offset %s%s\n", std::string(IrOpcodeName(op)).c_str(), flags.pc, kind, immediateOffset ? "immediate" : "dynamic", memory.kind == ResourceKind::ScalarAddress && !immediateOffset ? " (a register offset is not planned by the SRT walker)" : ""); } m_info.usesDma = true; m_info.bdaWrites = m_info.bdaWrites || addressInfo.access == AddressAccess::Write || addressInfo.access == AddressAccess::Atomic; return; } if (memory.kind != ResourceKind::Image || imageInfo.resourceClass == ImageResourceClass::None) { fail("image operation has invalid resource kind"); } handle = inst.Argument(0)->Resolve(); const TableColumnPlan* column = FindTableColumn(*handle); if (column != nullptr) { source = column->source; NoteHeapGuard(inst, *column); } else { GetHandle(inst, inst.Argument(0), IrOpcode::GetImageResource, 8, handle, source); } resource = AddImage(source, memory, op, flags.pc); NoteBlockImage(resource); if (resource == std::numeric_limits::max()) { fail("image resource limit exceeded (" + std::to_string(m_info.images.size() + 1u) + " of " + std::to_string(ShaderInfo::MaxImages) + ")"); } AddHandlePatch(handle, resource); std::uint32_t sampler = 0; if (imageInfo.needsSampler) { if (inst.ArgumentCount() < 2) { fail("sampled image operation has no sampler handle"); } IrValue* samplerHandle = inst.Argument(1)->Resolve(); std::uint32_t samplerSource = 0; const TableColumnPlan* samplerColumn = FindTableColumn(*samplerHandle); if (samplerColumn != nullptr) { samplerSource = samplerColumn->source; NoteHeapGuard(inst, *samplerColumn); } else { const bool sampleAdjust = (memory.imageSampleFlags & RdnaImageSampleFlagAdjust) != 0; GetHandle(inst, inst.Argument(1), IrOpcode::GetSamplerResource, 4, samplerHandle, samplerSource, true, sampleAdjust); } sampler = AddSampler(samplerSource, flags.pc); if (sampler == std::numeric_limits::max()) { fail("sampler resource limit exceeded"); } AddHandlePatch(samplerHandle, sampler); AddSampledPair(resource, sampler, flags.pc, op == IrOpcode::ImageGatherRaw && m_info.images[resource].dimension == RdnaImageDimension::Dim3D); } AddMemoryPatch(flags.index, resource, sampler, imageInfo.needsSampler); } const DescriptorSource* Source(std::uint32_t source) const { return source < m_sources.size() ? &m_sources[source] : nullptr; } void LinkImageAliases() { for (auto& buffer : m_info.buffers) { const DescriptorSource* bufferSource = Source(buffer.source); if (bufferSource == nullptr || bufferSource->dwordCount != 4) { continue; } for (std::uint32_t image = 0; image < m_info.images.size(); image++) { const DescriptorSource* imageSource = Source(m_info.images[image].source); if (imageSource == nullptr || imageSource->dwordCount != 8 || imageSource->tableColumn.has_value()) { continue; } bool alias = true; for (std::uint32_t dword = 0; dword < 4; dword++) { alias = alias && EquivalentValue(m_program.Resources(), bufferSource->dwords[dword], imageSource->dwords[dword]); } if (alias) { buffer.imageAlias = image; break; } } } } IrProgram& m_program; ShaderInfo m_info; IrBuilder m_builder; std::vector m_sources; std::unordered_map m_readOrigins; std::vector> m_blockSources; std::vector> m_blockImages; std::uint32_t m_currentBlock = std::numeric_limits::max(); std::vector m_handlePatches; std::vector m_memoryPatches; // APS5_TRACE_BDA: address accesses seen by Collect (the first bdaTraceLimit are printed). unsigned m_bdaTraces = 0; std::unordered_map> m_memoryUsers; bool m_memoryUsersIndexed = false; std::vector m_tableColumns; std::vector m_tablePlanningReads; std::vector m_tablePlanningMemory; std::vector m_unselectedHandles; struct EdgeSelectorEntry { IrBlock* block = nullptr; std::vector edges; IrValue* value = nullptr; }; std::vector m_edgeSelectors; std::unordered_map m_splitFailures; bool m_foldedWeb = false; std::vector m_descriptorGuards; }; } void ResourceTracker::Track(IrProgram& program) const { Tracker(program).Run(); } }