From 660a37c7a0595f34abc1ae4789ece546628bf88e Mon Sep 17 00:00:00 2001 From: Elad Ashkenazi <18193363+elad335@users.noreply.github.com> Date: Sat, 27 Apr 2024 13:46:46 +0300 Subject: [PATCH] g --- rpcs3/Emu/Cell/PPUAnalyser.cpp | 132 +++++++++++++++++++++++++++++++++ rpcs3/Emu/Cell/PPUAnalyser.h | 2 + rpcs3/Emu/Cell/PPUThread.cpp | 36 +++++++-- 3 files changed, 164 insertions(+), 6 deletions(-) diff --git a/rpcs3/Emu/Cell/PPUAnalyser.cpp b/rpcs3/Emu/Cell/PPUAnalyser.cpp index c8390b60756c..86fd75dd56a6 100644 --- a/rpcs3/Emu/Cell/PPUAnalyser.cpp +++ b/rpcs3/Emu/Cell/PPUAnalyser.cpp @@ -2078,6 +2078,138 @@ bool ppu_module::analyse(u32 lib_toc, u32 entry, const u32 sec_end, const std::b } ppu_log.notice("Block analysis: %zu blocks (%zu enqueued)", funcs.size(), block_queue.size()); + + std::unordered_map> duplicate_data_map; + duplicate_map.clear(); + + for (auto& func : funcs) + { + if (func.size == 0 || func.size > 10000u) + { + continue; + } + + auto& data = duplicate_data_map[std::string_view{get_ptr(func.addr), func.size}]; + + const usz count = data.first; + + if (!count) + { + data.first++; + data.second = func.addr; + continue; + } + + if (!data.second) + { + continue; + } + + if (count == 1) + { + const u32 faddr = func.addr; + const u32 fend = func.addr + func.size; + + bool fail = false; + + //for (const auto [addr, size] : func.blocks) + const u32 addr = func.addr; + const u32 size = func.size; + { + if (size == 0) + { + continue; + } + + auto i_ptr = ensure(get_ptr(addr)); + + for (u32 i = addr; i < fend; i += 4, i_ptr++) + { +/ const ppu_opcode_t op{*i_ptr}; + const auto itype = s_ppu_itype.decode(op.opcode); + + if (itype != ppu_itype::BC && itype != ppu_itype::B) + { + if (i == fend - 4) + { + if (!(itype & ppu_itype::branch)) + { + // Inserts a branch to following code + fail = true; + break; + } + } + + continue; + } + + if (!op.aa) + { + fail = true; + break; + } + + if (itype == ppu_itype::BC && (op.bo & 0x14) != 0x14) + { + if (i == fend - 4) + { + // Can branch to next + fail = true; + break; + } + } + } + } + + if (fail) + { + data.first = 1; + data.second = 0; + continue; + } + } + + data.first++; + + // Choose the lowest function as the source + data.second = std::min(data.second, func.addr); + } + + usz dups_count = 0; + + for (auto& func : funcs) + { + if (func.size == 0 || func.size > 10000u) + { + continue; + } + + const auto data = ::at32(duplicate_data_map, std::string_view{get_ptr(func.addr), func.size}); + + if (data.first > 1) + { + duplicate_map[func.addr] = data.second; + + for (const auto [addr, size] : func.blocks) + { + if (size == 0 || addr >= func.addr + func.size) + { + continue; + } + + duplicate_map[addr] = data.second + (addr - func.addr); + } + + if (func.addr != data.second) + { + dups_count++; + } + + ppu_log.trace("Found PPU function duplicate: func 0x%x vs 0x%x (%d times) (size=%d)", func.addr, data.second, data.first, func.size); + } + } + + ppu_log.success("Function duplication count: %d/%d (%g%)", dups_count, duplicate_data_map.size(), dups_count * 100.0 / duplicate_data_map.size()); return true; } diff --git a/rpcs3/Emu/Cell/PPUAnalyser.h b/rpcs3/Emu/Cell/PPUAnalyser.h index c1515c65c1ff..114a7b00c23e 100644 --- a/rpcs3/Emu/Cell/PPUAnalyser.h +++ b/rpcs3/Emu/Cell/PPUAnalyser.h @@ -95,6 +95,7 @@ struct ppu_module std::vector funcs{}; std::deque> allocations; std::map addr_to_seg_index; + std::unordered_map duplicate_map; // Copy info without functions void copy_part(const ppu_module& info) @@ -107,6 +108,7 @@ struct ppu_module secs = info.secs; allocations = info.allocations; addr_to_seg_index = info.addr_to_seg_index; + duplicate_map = info.duplicate_map; } bool analyse(u32 lib_toc, u32 entry, u32 end, const std::basic_string& applied, std::function check_aborted = {}); diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index 27204653559a..7210398182c2 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -3918,13 +3918,13 @@ extern void ppu_precompile(std::vector& dir_queue, std::vector offs; + for (u32 j = 0; j < hdr.count; j++) { mself_record rec{}; - std::set offs; - if (mself.read(rec) && rec.get_pos(mself.size())) { if (rec.size <= 0x20) @@ -4165,7 +4165,7 @@ extern void ppu_precompile(std::vector& dir_queue, std::vectorsecond; + } + // Fixup some information - entry.name = fmt::format("__0x%x", entry.addr - reloc); + entry.name = fmt::format("__0x%x", og_func - reloc); if (has_mfvscr && g_cfg.core.ppu_set_sat_bit) { @@ -4848,7 +4855,7 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size) settings += ppu_settings::contains_symbol_resolver; // Avoid invalidating all modules for this purpose // Write version, hash, CPU, settings - fmt::append(obj_name, "v6-kusa-%s-%s-%s.obj", fmt::base57(output, 16), fmt::base57(settings), jit_compiler::cpu(g_cfg.core.llvm_cpu)); + fmt::append(obj_name, "v7-kusa-%s-%s-%s.obj", fmt::base57(output, 16), fmt::base57(settings), jit_compiler::cpu(g_cfg.core.llvm_cpu)); } if (cpu ? cpu->state.all_of(cpu_flag::exit) : Emu.IsStopped()) @@ -5086,6 +5093,9 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size) jit_mod.symbol_resolver(vm::g_exec_addr, info.segs[0].addr); // Find a BLR-only function in order to copy it to all BLRs (some games need it) + bool early_exit = false; + + // Get and install function addresses for (const auto& func : info.funcs) { if (func.size == 4 && *info.get_ptr(func.addr) == ppu_instructions::BLR()) @@ -5156,6 +5166,11 @@ static void ppu_initialize2(jit_compiler& jit, const ppu_module& module_part, co { if (func.size) { + if (auto it = module_part.duplicate_map.find(func.addr); it != module_part.duplicate_map.end() && it->second != it->first) + { + continue; + } + const auto f = cast(_module->getOrInsertFunction(func.name, _func).getCallee()); f->setCallingConv(CallingConv::GHC); f->addParamAttr(1, llvm::Attribute::NoAlias); @@ -5229,6 +5244,15 @@ static void ppu_initialize2(jit_compiler& jit, const ppu_module& module_part, co if (module_part.funcs[fi].size) { + const u32 faddr = module_part.funcs[fi].addr; + auto it = module_part.duplicate_map.find(faddr); + + if (it != module_part.duplicate_map.end() && it->second != faddr) + { + ppu_log.trace("LLVM: Function 0x%x was skipped (duplicate)", faddr); + continue; + } + // Translate if (const auto func = translator.Translate(module_part.funcs[fi])) {