Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/anykyty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ jobs:
sanitizer: address
env:
GPU_TESTS: >-
^(kyty_shim_(packed_unorm_blend|compute_execution|wave64_cross_lane|empty_buffer_replay|empty_buffer_replay_off|depth_target_alias|frame_capture_round_trip|buffer_null_range|descriptor_tables_draw|merged_hull_draw|image_write_back_alias|buffer_cache_pages))$
^(kyty_shim_(packed_unorm_blend|compute_execution|compute_sampler_spill|wave64_cross_lane|empty_buffer_replay|empty_buffer_replay_off|depth_target_alias|frame_capture_round_trip|buffer_null_range|descriptor_tables_draw|merged_hull_draw|image_write_back_alias|buffer_cache_pages))$
CPU_TESTS: ^(kyty_shim_.*|guest_gpu_fault|guest_host_write_filter|agc_kyty_shaders)$
steps:
- uses: actions/checkout@v7
Expand Down
120 changes: 118 additions & 2 deletions core/kyty/graphics/shader/recompiler/ir/passes/ConstantPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class FoldWorklist {
};

void Register(Block::iterator instruction) {
Queue(locations.emplace(&*instruction, Location {instruction}).first->second);
const auto [entry, inserted] = locations.emplace(&*instruction, Location {instruction});
if (inserted && instruction->GetOpcode() == ValueOpcode::Phi) phis.push_back(&*instruction);
Queue(entry->second);
}

void Queue(Location& location) {
Expand Down Expand Up @@ -72,6 +74,7 @@ class FoldWorklist {
std::unordered_map<Inst*, Location> locations;
std::vector<Location*> pending;
std::unordered_set<Inst*> lowered_ancillary;
std::vector<Inst*> phis;

private:
std::vector<Inst*> affected;
Expand Down Expand Up @@ -169,6 +172,119 @@ bool FoldPhi(Inst& inst) {
return true;
}

struct PhiConstant {
Value value;
bool varying = false;
std::vector<PhiConstant*> users;

bool Merge(Value incoming, bool incoming_varies) {
if (varying) return false;
if (incoming_varies || (!value.IsEmpty() && !incoming.IsEmpty() && value != incoming)) {
varying = true;
return true;
}
if (!value.IsEmpty() || incoming.IsEmpty()) return false;
value = incoming;
return true;
}
};

bool FoldReachablePhis(const BlockList& blocks, FoldWorklist& worklist) {
if (worklist.phis.empty()) return false;
std::unordered_map<const Block*, const Block*> reachable;
std::vector<const Block*> pending_blocks {blocks.front()};
while (!pending_blocks.empty()) {
const auto* block = pending_blocks.back();
pending_blocks.pop_back();
const auto [entry, inserted] = reachable.try_emplace(block, nullptr);
if (!inserted) continue;
const auto& term = block->terminator;
const auto condition = block->condition.Resolve();
if (term.kind == CFG::TerminatorKind::ConditionalBranch && IsImmediate(condition, Type::U1)) {
entry->second = condition.U1() ? term.true_block : term.false_block;
}
if (entry->second != nullptr) {
pending_blocks.push_back(entry->second);
} else {
const auto successors = block->ImmSuccessors();
pending_blocks.insert(pending_blocks.end(), successors.begin(), successors.end());
}
}

std::unordered_map<Inst*, PhiConstant> constants;
std::vector<Inst*> phis;
const auto enabled = [&](const Inst& phi, size_t arg) {
const auto edge = reachable.find(phi.PhiBlock(arg));
return edge != reachable.end() && (edge->second == nullptr || edge->second == phi.Parent());
};
bool changed = false;
const auto fold = [&](Inst& phi, Value value) {
worklist.QueueUsers(phi.Uses());
Replace(phi, value);
changed = true;
};
for (auto* phi: worklist.phis) {
if (phi->GetOpcode() != ValueOpcode::Phi || !reachable.contains(phi->Parent())) continue;
PhiConstant state;
bool dependent = false;
for (size_t arg = 0; arg < phi->NumArgs() && !state.varying; ++arg) {
if (!enabled(*phi, arg)) continue;
const auto source = Arg(*phi, arg);
const auto* inst = source.TryInstruction();
if (inst != nullptr && inst->GetOpcode() == ValueOpcode::Phi) {
dependent = true;
} else {
state.Merge(source, !source.IsImmediate() || source.IsEmpty());
}
}
if (state.varying) continue;
if (dependent) {
constants.try_emplace(phi);
phis.push_back(phi);
} else if (!state.value.IsEmpty()) {
fold(*phi, state.value);
}
}
std::vector<PhiConstant*> pending;
for (auto* phi: phis) {
auto& state = constants.at(phi);
for (size_t arg = 0; arg < phi->NumArgs(); ++arg) {
if (!enabled(*phi, arg)) continue;
const auto source = Arg(*phi, arg);
if (const auto from = constants.find(source.TryInstruction()); from != constants.end()) {
from->second.users.push_back(&state);
} else if (source.IsImmediate() && !source.IsEmpty()) {
state.Merge(source, false);
} else {
state.Merge({}, true);
}
}
if (state.varying || !state.value.IsEmpty()) pending.push_back(&state);
}
const auto propagate = [&] {
for (size_t i = 0; i < pending.size(); ++i) {
const auto* source = pending[i];
for (auto* target: source->users) {
if (target->Merge(source->value, source->varying)) pending.push_back(target);
}
}
pending.clear();
};
propagate();
for (auto* phi: phis) {
auto& state = constants.at(phi);
if (state.value.IsEmpty() && state.Merge({}, true)) pending.push_back(&state);
}
propagate();

for (auto* phi: phis) {
const auto& state = constants.at(phi);
if (state.varying) continue;
fold(*phi, state.value);
}
return changed;
}

bool FoldBitCast(Inst& inst, ValueOpcode reverse) {
const auto value = Arg(inst, 0);
if (IsImmediate(value, Type::F32) && inst.GetOpcode() == ValueOpcode::BitCastU32F32) {
Expand Down Expand Up @@ -992,7 +1108,7 @@ void ConstantPropagationPass(const BlockList& blocks, uint32_t wave_size) {
drain();
bool normalized;
do {
normalized = false;
normalized = FoldReachablePhis(blocks, worklist);
for (auto* source: worklist.lowered_ancillary) {
if (source->GetOpcode() == ValueOpcode::Identity) continue;
const bool retained_only = std::ranges::all_of(source->Uses(), [](const Use& use) {
Expand Down
9 changes: 9 additions & 0 deletions core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ if(NOT ANYPS5_KYTY_DLL)
add_test(NAME kyty_shim_recompiler_stale_lane_spill COMMAND kyty_shim_recompiler_robustness_tests stale-lane-spill-after-read-lane-elimination)
set_tests_properties(kyty_shim_recompiler_stale_lane_spill PROPERTIES TIMEOUT 60 PASS_REGULAR_EXPRESSION "kyty_shim_recompiler_robustness: ok"
FAIL_REGULAR_EXPRESSION "terminate called|FAILED")
add_test(NAME kyty_shim_recompiler_sampler_spill_phi COMMAND kyty_shim_recompiler_robustness_tests sampler-spill-phi)
set_tests_properties(kyty_shim_recompiler_sampler_spill_phi PROPERTIES TIMEOUT 20
PASS_REGULAR_EXPRESSION "sampler-spill-phi: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")
add_test(NAME kyty_shim_recompiler_phi_reachability COMMAND kyty_shim_recompiler_robustness_tests phi-reachability)
set_tests_properties(kyty_shim_recompiler_phi_reachability PROPERTIES TIMEOUT 20
PASS_REGULAR_EXPRESSION "phi-reachability: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")
endif()

add_test_executable(kyty_shim_compute_execution_tests ${CMAKE_CURRENT_SOURCE_DIR}/ComputeExecution.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/Log.cpp)
Expand All @@ -483,6 +489,9 @@ target_link_libraries(kyty_shim_compute_execution_tests PRIVATE Kyty::gpu libker
add_test(NAME kyty_shim_compute_execution COMMAND kyty_shim_compute_execution_tests)
set_tests_properties(kyty_shim_compute_execution PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77
PASS_REGULAR_EXPRESSION "kyty_shim_compute_execution: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")
add_test(NAME kyty_shim_compute_sampler_spill COMMAND kyty_shim_compute_execution_tests sampler-spill)
set_tests_properties(kyty_shim_compute_sampler_spill PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77
PASS_REGULAR_EXPRESSION "sampler spill: 32/32 lanes sampled 1.0" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")

add_test_executable(kyty_shim_pair_codegen_tests ${CMAKE_CURRENT_SOURCE_DIR}/PairCodegen.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/Log.cpp)
target_include_directories(kyty_shim_pair_codegen_tests PRIVATE ${LIBS_INCLUDE_DIR})
Expand Down
Loading
Loading