Skip to content
Merged
6 changes: 5 additions & 1 deletion core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2608,12 +2608,16 @@ if(BUILD_TESTING)
set_tests_properties(agc_structurizer_iteration_selection_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PERF_ITERATION_SELECTION=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_shader_control_flow_iteration_selection_off COMMAND agc_shader_control_flow_tests)
set_tests_properties(agc_shader_control_flow_iteration_selection_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PERF_ITERATION_SELECTION=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_structurizer_dominator_tree_off COMMAND agc_structurizer_tests)
set_tests_properties(agc_structurizer_dominator_tree_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PERF_DOMINATOR_TREE=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_shader_control_flow_dominator_tree_off COMMAND agc_shader_control_flow_tests)
set_tests_properties(agc_shader_control_flow_dominator_tree_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PERF_DOMINATOR_TREE=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_structurizer_forced_fallback COMMAND agc_structurizer_tests)
set_tests_properties(agc_structurizer_forced_fallback PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_FORCE_STRUCTURIZER_FALLBACK=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_shader_control_flow_forced_fallback COMMAND agc_shader_control_flow_tests)
set_tests_properties(agc_shader_control_flow_forced_fallback PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_FORCE_STRUCTURIZER_FALLBACK=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
if(WIN32)
set_tests_properties(agc_structurizer_intersection_off agc_structurizer_iteration_selection_off agc_shader_control_flow_iteration_selection_off agc_structurizer_forced_fallback agc_shader_control_flow_forced_fallback PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
set_tests_properties(agc_structurizer_intersection_off agc_structurizer_iteration_selection_off agc_shader_control_flow_iteration_selection_off agc_structurizer_dominator_tree_off agc_shader_control_flow_dominator_tree_off agc_structurizer_forced_fallback agc_shader_control_flow_forced_fallback PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test(NAME agc_driver_gated_dispatch_capture_short_timeout COMMAND agc_driver_gated_dispatch_capture_tests)
set_tests_properties(agc_driver_gated_dispatch_capture_short_timeout PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct BasicBlock {
std::uint32_t instructionBegin = 0;
std::uint32_t instructionEnd = 0;
std::uint32_t estimatedSpirvWords = 0;
std::uint32_t immediatePostDominator = InvalidControlFlowId;
std::vector<std::uint32_t> predecessors;
std::vector<std::uint32_t> successors;
std::vector<std::uint32_t> dominators;
Expand Down
37 changes: 30 additions & 7 deletions core/shader/recompiler/ControlFlow/src/ControlFlowGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ControlFlow/ControlFlowHelpers.hpp"
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <string>
Expand All @@ -20,6 +21,7 @@ bool contains(const std::vector<std::uint32_t>& values, std::uint32_t value) {

std::vector<std::uint32_t> intersectSorted(const std::vector<std::uint32_t>& first, const std::vector<std::uint32_t>& second) {
std::vector<std::uint32_t> result;
result.reserve(std::min(first.size(), second.size()));
std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
return result;
}
Expand All @@ -43,7 +45,9 @@ void remapIds(std::vector<std::uint32_t>& values, const std::vector<std::uint32_
for (auto& value : values) {
value = remapId(value, idMap);
}
sortUnique(values);
if (std::adjacent_find(values.begin(), values.end(), std::greater_equal<>()) != values.end()) {
sortUnique(values);
}
}

void rebuildPredecessors(ControlFlowGraph& graph) {
Expand Down Expand Up @@ -118,18 +122,37 @@ bool ControlFlowGraph::PostDominates(std::uint32_t postDominator, std::uint32_t
std::uint32_t ControlFlowGraph::FindNearestCommonPostDominator(std::uint32_t firstBlock, std::uint32_t secondBlock) const {
const auto& first = FindBlock(firstBlock);
const auto& second = FindBlock(secondBlock);
if (first.immediatePostDominator != InvalidControlFlowId && second.immediatePostDominator != InvalidControlFlowId) {
const BasicBlock* left = &first;
const BasicBlock* right = &second;
while (left->postDominators.size() > right->postDominators.size()) left = &FindBlock(left->immediatePostDominator);
while (right->postDominators.size() > left->postDominators.size()) right = &FindBlock(right->immediatePostDominator);
while (left != right) {
if (left->immediatePostDominator == left->id) {
return InvalidControlFlowId;
}
left = &FindBlock(left->immediatePostDominator);
right = &FindBlock(right->immediatePostDominator);
}
return left->id;
}
const auto common = intersectSorted(first.postDominators, second.postDominators);
if (common.empty()) {
return InvalidControlFlowId;
}
if (common.size() == 1u) {
return common.front();
}
for (const auto candidate : common) {
bool nearest = true;
for (const auto other : common) {
if (other != candidate && !PostDominates(other, candidate)) {
nearest = false;
break;
}
const auto& postDominators = FindBlock(candidate).postDominators;
if (postDominators.size() + 1u < common.size()) {
continue;
}
auto member = postDominators.begin();
const bool nearest = std::all_of(common.begin(), common.end(), [&](std::uint32_t other) {
member = std::lower_bound(member, postDominators.end(), other);
return other == candidate || (member != postDominators.end() && *member == other);
});
if (nearest) {
return candidate;
}
Expand Down
Loading
Loading