diff --git a/core/shader/recompilier/ControlFlow/include/ControlFlow/ControlFlowGraph.hpp b/core/shader/recompilier/ControlFlow/include/ControlFlow/ControlFlowGraph.hpp index 07e73b704..ec77a1990 100644 --- a/core/shader/recompilier/ControlFlow/include/ControlFlow/ControlFlowGraph.hpp +++ b/core/shader/recompilier/ControlFlow/include/ControlFlow/ControlFlowGraph.hpp @@ -1,38 +1,125 @@ -#ifndef SHADER_RECOMPILIER_CONTROLFLOW_CONTROLFLOWGRAPH_HPP -#define SHADER_RECOMPILIER_CONTROLFLOW_CONTROLFLOWGRAPH_HPP +#ifndef CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_CONTROLFLOWGRAPH_HPP +#define CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_CONTROLFLOWGRAPH_HPP #include +#include +#include #include namespace ShaderRecompiler { +inline constexpr std::uint32_t InvalidControlFlowId = std::numeric_limits::max(); + +enum class BranchCondition { + Always, + SccZero, + SccNonZero, + VccZero, + VccNonZero, + ExecZero, + ExecNonZero, + ScalarInstruction, + GotoVariable, + Unknown +}; + +enum class TerminatorKind { + Branch, + ConditionalBranch, + IndirectBranch, + Return, + Unsupported +}; + +enum class FailureKind { + None, + InvalidInput, + UnsupportedInstruction, + InvalidBranchTarget, + MissingFallthrough, + InvalidLabel, + IrreducibleControlFlow, + StructuredControlFlow +}; + +struct Terminator { + TerminatorKind kind = TerminatorKind::Return; + BranchCondition condition = BranchCondition::Always; + std::uint32_t trueBlock = InvalidControlFlowId; + std::uint32_t falseBlock = InvalidControlFlowId; + std::uint32_t mergeBlock = InvalidControlFlowId; + std::uint32_t continueBlock = InvalidControlFlowId; + std::uint32_t indirectPcSgpr = InvalidControlFlowId; + std::uint32_t indirectSelectorCode = InvalidControlFlowId; + std::vector indirectTargetProgramCounters; + std::vector indirectTargets; + std::vector indirectSelectorValues; + std::vector indirectSelectorTargets; + std::uint32_t gotoVariable = InvalidControlFlowId; + std::int32_t gotoValue = -1; + bool loopHeader = false; +}; + struct BasicBlock { - std::uint32_t id; - std::uint32_t startProgramCounter; - std::uint32_t endProgramCounter; + std::uint32_t id = 0; + std::uint32_t startProgramCounter = 0; + std::uint32_t endProgramCounter = 0; + std::uint32_t instructionBegin = 0; + std::uint32_t instructionEnd = 0; std::vector predecessors; std::vector successors; + std::vector dominators; + std::vector postDominators; + Terminator terminator; }; struct NaturalLoop { - std::uint32_t headerBlock; + std::uint32_t headerBlock = InvalidControlFlowId; + std::uint32_t latchBlock = InvalidControlFlowId; + std::uint32_t mergeBlock = InvalidControlFlowId; + std::uint32_t continueBlock = InvalidControlFlowId; std::vector blocks; + std::vector exitBlocks; }; struct BackEdge { - std::uint32_t sourceBlock; - std::uint32_t targetBlock; + std::uint32_t sourceBlock = InvalidControlFlowId; + std::uint32_t targetBlock = InvalidControlFlowId; + bool natural = false; +}; + +struct StronglyConnectedComponent { + std::vector blocks; + std::vector entryBlocks; + bool irreducible = false; }; struct ControlFlowGraph { std::vector blocks; std::vector naturalLoops; std::vector backEdges; - std::uint32_t entryBlock; + std::vector components; + std::vector codeTableLoadProgramCounters; + std::uint32_t entryBlock = InvalidControlFlowId; + bool irreducible = false; + bool unsupported = false; + FailureKind failureKind = FailureKind::None; + std::uint32_t failureBlock = InvalidControlFlowId; + std::string unsupportedReason; [[nodiscard]] const BasicBlock& FindBlock(std::uint32_t blockId) const; + [[nodiscard]] BasicBlock& FindBlock(std::uint32_t blockId); + [[nodiscard]] const BasicBlock& FindBlockByProgramCounter(std::uint32_t programCounter) const; + [[nodiscard]] BasicBlock& FindBlockByProgramCounter(std::uint32_t programCounter); + [[nodiscard]] bool Dominates(std::uint32_t dominator, std::uint32_t blockId) const; + [[nodiscard]] bool PostDominates(std::uint32_t postDominator, std::uint32_t blockId) const; + [[nodiscard]] std::uint32_t FindNearestCommonPostDominator(std::uint32_t firstBlock, std::uint32_t secondBlock) const; }; +[[nodiscard]] std::string BranchConditionToString(BranchCondition condition); +[[nodiscard]] std::string FailureKindToString(FailureKind kind); +[[nodiscard]] std::string GraphToString(const ControlFlowGraph& graph); + } #endif diff --git a/core/shader/recompilier/ControlFlow/include/ControlFlow/GraphBuilder.hpp b/core/shader/recompilier/ControlFlow/include/ControlFlow/GraphBuilder.hpp index eef25d508..58934a077 100644 --- a/core/shader/recompilier/ControlFlow/include/ControlFlow/GraphBuilder.hpp +++ b/core/shader/recompilier/ControlFlow/include/ControlFlow/GraphBuilder.hpp @@ -1,8 +1,8 @@ -#ifndef SHADER_RECOMPILIER_CONTROLFLOW_GRAPHBUILDER_HPP -#define SHADER_RECOMPILIER_CONTROLFLOW_GRAPHBUILDER_HPP +#ifndef CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_GRAPHBUILDER_HPP +#define CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_GRAPHBUILDER_HPP -#include -#include +#include "ControlFlow/ControlFlowGraph.hpp" +#include "RdnaDecoder/RdnaProgram.hpp" #include namespace ShaderRecompiler { diff --git a/core/shader/recompilier/ControlFlow/include/ControlFlow/Structurizer.hpp b/core/shader/recompilier/ControlFlow/include/ControlFlow/Structurizer.hpp index 4a6aefde1..d2e209964 100644 --- a/core/shader/recompilier/ControlFlow/include/ControlFlow/Structurizer.hpp +++ b/core/shader/recompilier/ControlFlow/include/ControlFlow/Structurizer.hpp @@ -1,7 +1,7 @@ -#ifndef SHADER_RECOMPILIER_CONTROLFLOW_STRUCTURIZER_HPP -#define SHADER_RECOMPILIER_CONTROLFLOW_STRUCTURIZER_HPP +#ifndef CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_STRUCTURIZER_HPP +#define CORE_SHADER_RECOMPILIER_CONTROLFLOW_INCLUDE_CONTROLFLOW_STRUCTURIZER_HPP -#include +#include "ControlFlow/ControlFlowGraph.hpp" namespace ShaderRecompiler { @@ -10,8 +10,16 @@ public: void Structurize(ControlFlowGraph& graph) const; private: - void computeDominatorTree(const ControlFlowGraph& graph) const; + void computeDominatorTree(ControlFlowGraph& graph) const; void detectNaturalLoops(ControlFlowGraph& graph) const; + void computePostDominators(ControlFlowGraph& graph) const; + void computeBackEdges(ControlFlowGraph& graph) const; + void computeStronglyConnectedComponents(ControlFlowGraph& graph) const; + void recomputeAnalyses(ControlFlowGraph& graph) const; + void canonicalizeNaturalLoops(ControlFlowGraph& graph) const; + void splitSharedMergeBlocks(ControlFlowGraph& graph) const; + void isolateSemanticLoopHeaders(ControlFlowGraph& graph) const; + void clearStructuredTerminators(ControlFlowGraph& graph) const; void verifyReducibility(const ControlFlowGraph& graph) const; }; diff --git a/core/shader/recompilier/ControlFlow/src/ControlFlowGraph.cpp b/core/shader/recompilier/ControlFlow/src/ControlFlowGraph.cpp index 9491d4fa2..64d318115 100644 --- a/core/shader/recompilier/ControlFlow/src/ControlFlowGraph.cpp +++ b/core/shader/recompilier/ControlFlow/src/ControlFlowGraph.cpp @@ -1,4 +1,4 @@ -#include +#include "ControlFlow/ControlFlowGraph.hpp" #include namespace ShaderRecompiler { @@ -7,4 +7,40 @@ const BasicBlock& ControlFlowGraph::FindBlock(std::uint32_t blockId) const { throw std::runtime_error("ControlFlowGraph::FindBlock not implemented"); } +BasicBlock& ControlFlowGraph::FindBlock(std::uint32_t blockId) { + throw std::runtime_error("ControlFlowGraph::FindBlock not implemented"); +} + +const BasicBlock& ControlFlowGraph::FindBlockByProgramCounter(std::uint32_t programCounter) const { + throw std::runtime_error("ControlFlowGraph::FindBlockByProgramCounter not implemented"); +} + +BasicBlock& ControlFlowGraph::FindBlockByProgramCounter(std::uint32_t programCounter) { + throw std::runtime_error("ControlFlowGraph::FindBlockByProgramCounter not implemented"); +} + +bool ControlFlowGraph::Dominates(std::uint32_t dominator, std::uint32_t blockId) const { + throw std::runtime_error("ControlFlowGraph::Dominates not implemented"); +} + +bool ControlFlowGraph::PostDominates(std::uint32_t postDominator, std::uint32_t blockId) const { + throw std::runtime_error("ControlFlowGraph::PostDominates not implemented"); +} + +std::uint32_t ControlFlowGraph::FindNearestCommonPostDominator(std::uint32_t firstBlock, std::uint32_t secondBlock) const { + throw std::runtime_error("ControlFlowGraph::FindNearestCommonPostDominator not implemented"); +} + +std::string BranchConditionToString(BranchCondition condition) { + throw std::runtime_error("BranchConditionToString not implemented"); +} + +std::string FailureKindToString(FailureKind kind) { + throw std::runtime_error("FailureKindToString not implemented"); +} + +std::string GraphToString(const ControlFlowGraph& graph) { + throw std::runtime_error("GraphToString not implemented"); +} + } diff --git a/core/shader/recompilier/ControlFlow/src/GraphBuilder.cpp b/core/shader/recompilier/ControlFlow/src/GraphBuilder.cpp index a34c83935..a45485271 100644 --- a/core/shader/recompilier/ControlFlow/src/GraphBuilder.cpp +++ b/core/shader/recompilier/ControlFlow/src/GraphBuilder.cpp @@ -1,4 +1,4 @@ -#include +#include "ControlFlow/GraphBuilder.hpp" #include namespace ShaderRecompiler { diff --git a/core/shader/recompilier/ControlFlow/src/Structurizer.cpp b/core/shader/recompilier/ControlFlow/src/Structurizer.cpp index 84d82ac51..403e55ffa 100644 --- a/core/shader/recompilier/ControlFlow/src/Structurizer.cpp +++ b/core/shader/recompilier/ControlFlow/src/Structurizer.cpp @@ -1,4 +1,4 @@ -#include +#include "ControlFlow/Structurizer.hpp" #include namespace ShaderRecompiler { @@ -6,7 +6,7 @@ namespace ShaderRecompiler { void Structurizer::Structurize(ControlFlowGraph& graph) const { throw std::runtime_error("Structurizer::Structurize not implemented"); } -void Structurizer::computeDominatorTree(const ControlFlowGraph& graph) const { +void Structurizer::computeDominatorTree(ControlFlowGraph& graph) const { throw std::runtime_error("Structurizer::computeDominatorTree not implemented"); } void Structurizer::detectNaturalLoops(ControlFlowGraph& graph) const { @@ -16,4 +16,36 @@ void Structurizer::verifyReducibility(const ControlFlowGraph& graph) const { throw std::runtime_error("Structurizer::verifyReducibility not implemented"); } +void Structurizer::computePostDominators(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::computePostDominators not implemented"); +} + +void Structurizer::computeBackEdges(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::computeBackEdges not implemented"); +} + +void Structurizer::computeStronglyConnectedComponents(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::computeStronglyConnectedComponents not implemented"); +} + +void Structurizer::recomputeAnalyses(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::recomputeAnalyses not implemented"); +} + +void Structurizer::canonicalizeNaturalLoops(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::canonicalizeNaturalLoops not implemented"); +} + +void Structurizer::splitSharedMergeBlocks(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::splitSharedMergeBlocks not implemented"); +} + +void Structurizer::isolateSemanticLoopHeaders(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::isolateSemanticLoopHeaders not implemented"); +} + +void Structurizer::clearStructuredTerminators(ControlFlowGraph& graph) const { + throw std::runtime_error("Structurizer::clearStructuredTerminators not implemented"); +} + }