feat(shader): update control flow signatures

This commit is contained in:
boykopovar
2026-09-14 16:33:21 +03:00
parent f4a591a74c
commit e9be69c247
6 changed files with 184 additions and 21 deletions
@@ -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 <cstdint>
#include <limits>
#include <string>
#include <vector>
namespace ShaderRecompiler {
inline constexpr std::uint32_t InvalidControlFlowId = std::numeric_limits<std::uint32_t>::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<std::uint32_t> indirectTargetProgramCounters;
std::vector<std::uint32_t> indirectTargets;
std::vector<std::uint32_t> indirectSelectorValues;
std::vector<std::uint32_t> 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<std::uint32_t> predecessors;
std::vector<std::uint32_t> successors;
std::vector<std::uint32_t> dominators;
std::vector<std::uint32_t> 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<std::uint32_t> blocks;
std::vector<std::uint32_t> 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<std::uint32_t> blocks;
std::vector<std::uint32_t> entryBlocks;
bool irreducible = false;
};
struct ControlFlowGraph {
std::vector<BasicBlock> blocks;
std::vector<NaturalLoop> naturalLoops;
std::vector<BackEdge> backEdges;
std::uint32_t entryBlock;
std::vector<StronglyConnectedComponent> components;
std::vector<std::uint32_t> 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
@@ -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 <ControlFlow/ControlFlowGraph.hpp>
#include <RdnaDecoder/RdnaProgram.hpp>
#include "ControlFlow/ControlFlowGraph.hpp"
#include "RdnaDecoder/RdnaProgram.hpp"
#include <vector>
namespace ShaderRecompiler {
@@ -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 <ControlFlow/ControlFlowGraph.hpp>
#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;
};
@@ -1,4 +1,4 @@
#include <ControlFlow/ControlFlowGraph.hpp>
#include "ControlFlow/ControlFlowGraph.hpp"
#include <stdexcept>
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");
}
}
@@ -1,4 +1,4 @@
#include <ControlFlow/GraphBuilder.hpp>
#include "ControlFlow/GraphBuilder.hpp"
#include <stdexcept>
namespace ShaderRecompiler {
@@ -1,4 +1,4 @@
#include <ControlFlow/Structurizer.hpp>
#include "ControlFlow/Structurizer.hpp"
#include <stdexcept>
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");
}
}