From 4da30e9eca65e6057b9bbc9c8b28a74cb3cf0142 Mon Sep 17 00:00:00 2001 From: kd-11 Date: Sat, 24 Aug 2024 02:08:48 +0300 Subject: [PATCH] Add proper transform pass management --- rpcs3/Emu/CPU/CPUTranslator.cpp | 18 ++++++++++++++++++ rpcs3/Emu/CPU/CPUTranslator.h | 11 +++++++---- rpcs3/Emu/Cell/PPUTranslator.cpp | 4 ++++ rpcs3/Emu/Cell/SPULLVMRecompiler.cpp | 3 +++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/rpcs3/Emu/CPU/CPUTranslator.cpp b/rpcs3/Emu/CPU/CPUTranslator.cpp index 8d101fca11..8466443a4c 100644 --- a/rpcs3/Emu/CPU/CPUTranslator.cpp +++ b/rpcs3/Emu/CPU/CPUTranslator.cpp @@ -403,6 +403,24 @@ void cpu_translator::run_transforms(llvm::Function& f) } } +void cpu_translator::register_transform_pass(std::unique_ptr& pass) +{ + m_transform_passes.emplace_back(std::move(pass)); +} + +void cpu_translator::clear_transforms() +{ + m_transform_passes.clear(); +} + +void cpu_translator::reset_transforms() +{ + for (auto& pass : m_transform_passes) + { + pass->reset(); + } +} + void cpu_translator::erase_stores(llvm::ArrayRef args) { for (auto v : args) diff --git a/rpcs3/Emu/CPU/CPUTranslator.h b/rpcs3/Emu/CPU/CPUTranslator.h index bfaa14ecaa..db2cfad032 100644 --- a/rpcs3/Emu/CPU/CPUTranslator.h +++ b/rpcs3/Emu/CPU/CPUTranslator.h @@ -3094,10 +3094,13 @@ protected: public: // Register a transformation pass to be run before final compilation by llvm - void register_transform_pass(std::unique_ptr& pass) - { - m_transform_passes.emplace_back(std::move(pass)); - } + void register_transform_pass(std::unique_ptr& pass); + + // Delete all transform passes + void clear_transforms(); + + // Reset internal state of all passes to evict caches and such. Use when resetting a JIT. + void reset_transforms(); // Convert a C++ type to an LLVM type (TODO: remove) template diff --git a/rpcs3/Emu/Cell/PPUTranslator.cpp b/rpcs3/Emu/Cell/PPUTranslator.cpp index 03c6f41127..1af6ed02c6 100644 --- a/rpcs3/Emu/Cell/PPUTranslator.cpp +++ b/rpcs3/Emu/Cell/PPUTranslator.cpp @@ -35,6 +35,8 @@ PPUTranslator::PPUTranslator(LLVMContext& context, Module* _module, const ppu_mo cpu_translator::initialize(context, engine); // Initialize transform passes + clear_transforms(); + #ifdef ARCH_ARM64 { // Base reg table definition @@ -63,6 +65,8 @@ PPUTranslator::PPUTranslator(LLVMContext& context, Module* _module, const ppu_mo } #endif + reset_transforms(); + // Thread context struct (TODO: safer member access) const u32 off0 = offset32(&ppu_thread::state); const u32 off1 = offset32(&ppu_thread::gpr); diff --git a/rpcs3/Emu/Cell/SPULLVMRecompiler.cpp b/rpcs3/Emu/Cell/SPULLVMRecompiler.cpp index 84d63faeac..28a4acea59 100644 --- a/rpcs3/Emu/Cell/SPULLVMRecompiler.cpp +++ b/rpcs3/Emu/Cell/SPULLVMRecompiler.cpp @@ -1474,6 +1474,7 @@ public: m_md_unlikely = llvm::MDTuple::get(m_context, {md_name, md_low, md_high}); // Initialize transform passes + clear_transforms(); #ifdef ARCH_ARM64 { auto should_exclude_function = [](const std::string& fn_name) @@ -1498,6 +1499,8 @@ public: } #endif } + + reset_transforms(); } void init_luts()