mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #894 from alfalfa-star/fix/structurizer-shared-merge-budget
fix(recompiler): merge a loop selection where its other arm joins or leaves
This commit is contained in:
@@ -53,6 +53,13 @@ std::vector<std::uint32_t> constructBlocks(const ControlFlowGraph& graph, std::u
|
||||
return blocks;
|
||||
}
|
||||
|
||||
bool inEnclosingContinueConstruct(const ControlFlowGraph& graph, std::uint32_t header, std::uint32_t member) {
|
||||
return std::any_of(graph.blocks.begin(), graph.blocks.end(), [&](const BasicBlock& loop) {
|
||||
const auto continueBlock = loop.terminator.continueBlock;
|
||||
return loop.terminator.loopHeader && graph.Dominates(loop.id, header) && !graph.Dominates(continueBlock, header) && graph.Dominates(continueBlock, member);
|
||||
});
|
||||
}
|
||||
|
||||
void verifyStructured(const std::string& prefix, const ControlFlowGraph& graph) {
|
||||
std::map<std::uint32_t, std::uint32_t> mergeOwners;
|
||||
std::vector<std::pair<std::uint32_t, std::uint32_t>> loopExits;
|
||||
@@ -66,6 +73,7 @@ void verifyStructured(const std::string& prefix, const ControlFlowGraph& graph)
|
||||
const auto& terminator = block.terminator;
|
||||
if (terminator.loopHeader || terminator.mergeBlock == InvalidControlFlowId) continue;
|
||||
for (const auto member : constructBlocks(graph, block.id, terminator.mergeBlock)) {
|
||||
if (inEnclosingContinueConstruct(graph, block.id, member)) continue;
|
||||
for (const auto successor : graph.FindBlock(member).successors) {
|
||||
if (successor == terminator.mergeBlock || (graph.Dominates(block.id, successor) && !graph.Dominates(terminator.mergeBlock, successor))) continue;
|
||||
const bool loopExit = std::any_of(loopExits.begin(), loopExits.end(), [&](const auto& exits) { return successor == exits.first || successor == exits.second; });
|
||||
@@ -363,6 +371,29 @@ latch:
|
||||
buffer_store_dword v1, off, s[0:3], 0
|
||||
s_endpgm)",
|
||||
Store({0xbe880380u, 0x7e020280u, 0x7d880008u, 0xbf860004u, 0x7d880088u, 0xbf870003u, 0x4a020281u, 0xbf820002u, 0x4a020282u, 0x4a020283u, 0x80089008u, 0xbf0ac008u, 0xbf85fff5u}), Split::Clone},
|
||||
{"continue beside the inner merge of a nested selection in a loop", R"(
|
||||
s_mov_b32 s8, 0
|
||||
v_mov_b32 v1, 0
|
||||
loop:
|
||||
v_cmp_gt_u32 vcc, s8, v0
|
||||
s_cbranch_vccz join
|
||||
v_cmp_gt_u32 vcc, 8, v0
|
||||
s_cbranch_vccnz inner
|
||||
v_cmp_gt_u32 vcc, 4, v0
|
||||
s_cbranch_vccnz latch
|
||||
inner:
|
||||
v_add_nc_u32 v1, 1, v1
|
||||
join:
|
||||
v_add_nc_u32 v1, 2, v1
|
||||
s_cmp_ge_u32 s8, 64
|
||||
s_cbranch_scc1 done
|
||||
latch:
|
||||
s_add_u32 s8, s8, 16
|
||||
s_branch loop
|
||||
done:
|
||||
buffer_store_dword v1, off, s[0:3], 0
|
||||
s_endpgm)",
|
||||
Store({0xbe880380u, 0x7e020280u, 0x7d880008u, 0xbf860005u, 0x7d880088u, 0xbf870002u, 0x7d880084u, 0xbf870004u, 0x4a020281u, 0x4a020282u, 0xbf09c008u, 0xbf850002u, 0x80089008u, 0xbf82fff4u}), Split::None},
|
||||
{"loop exit to the end of the program beside a kill exit", R"(
|
||||
v_mov_b32 v1, 0
|
||||
s_mov_b64 s[20:21], exec
|
||||
|
||||
@@ -464,6 +464,19 @@ std::uint32_t findSelectionMerge(const ControlFlowGraph& graph, const BasicBlock
|
||||
if (isLoopControlGateway(graph, *loop, falseTarget) && graph.Dominates(block.id, falseTarget) && isInsideLoopConstruct(graph, *loop, trueTarget)) {
|
||||
return falseTarget;
|
||||
}
|
||||
const auto joinsAt = [&](std::uint32_t join, std::uint32_t other) {
|
||||
const auto& arm = graph.FindBlock(other);
|
||||
const bool armLeavesOrJoins = arm.terminator.kind == TerminatorKind::ConditionalBranch && arm.predecessors.size() == 1u && std::all_of(arm.successors.begin(), arm.successors.end(), [&](std::uint32_t successor) {
|
||||
return successor == join || successor == loop->continueBlock || successor == loop->mergeBlock;
|
||||
});
|
||||
return armLeavesOrJoins && isInsideLoopConstruct(graph, *loop, other) && isInsideLoopConstruct(graph, *loop, join) && graph.Dominates(block.id, join);
|
||||
};
|
||||
if (joinsAt(trueTarget, falseTarget)) {
|
||||
return trueTarget;
|
||||
}
|
||||
if (joinsAt(falseTarget, trueTarget)) {
|
||||
return falseTarget;
|
||||
}
|
||||
if (globalMerge != InvalidControlFlowId) {
|
||||
const bool trueJoins = reachesWithinIteration(graph, *loop, trueTarget, globalMerge);
|
||||
const bool falseJoins = reachesWithinIteration(graph, *loop, falseTarget, globalMerge);
|
||||
|
||||
Reference in New Issue
Block a user