From 6e84cd7f381bc5d83b51f9a847add9cf10a8077b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 11 Jun 2025 16:07:26 +0200 Subject: [PATCH] Correct the clearing/destruction order when switching JITs. Fixes #20502 --- Common/Log.h | 5 +++-- Core/MIPS/IR/IRJit.h | 3 +++ Core/MIPS/MIPS.cpp | 36 ++++++++++++++++++------------------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Common/Log.h b/Common/Log.h index 809ac060ad..fdbc245e1d 100644 --- a/Common/Log.h +++ b/Common/Log.h @@ -140,8 +140,9 @@ __attribute__((format(printf, 5, 6))) // They can have a value between 0 and 15. enum class DebugCounter { APP_BOOT = 0, - GAME_BOOT = 0, - GAME_SHUTDOWN = 0, + GAME_BOOT = 1, + GAME_SHUTDOWN = 2, + CPUCORE_SWITCHES = 3, }; bool HitAnyAsserts(); diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h index 4b3e0efac5..cf87552717 100644 --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -119,6 +119,9 @@ private: class IRBlockCache : public JitBlockCacheDebugInterface { public: IRBlockCache(bool compileToNative); + ~IRBlockCache() { + Clear(); + } void Clear(); std::vector FindInvalidatedBlockNumbers(u32 address, u32 length); diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index a4a201532f..4335c45e59 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -228,39 +228,39 @@ void MIPSState::UpdateCore(CPUCore desired) { return; } - PSP_CoreParameter().cpuCore = desired; - MIPSComp::JitInterface *oldjit = MIPSComp::jit; - MIPSComp::JitInterface *newjit = nullptr; + IncrementDebugCounter(DebugCounter::CPUCORE_SWITCHES); + // Get rid of the old JIT first, before switching. + { + std::lock_guard guard(MIPSComp::jitLock); + if (MIPSComp::jit) { + delete MIPSComp::jit; + MIPSComp::jit = nullptr; + } + } + + PSP_CoreParameter().cpuCore = desired; + + MIPSComp::JitInterface *newjit = nullptr; switch (PSP_CoreParameter().cpuCore) { case CPUCore::JIT: case CPUCore::JIT_IR: INFO_LOG(Log::CPU, "Switching to JIT%s", PSP_CoreParameter().cpuCore == CPUCore::JIT_IR ? " IR" : ""); - if (oldjit) { - std::lock_guard guard(MIPSComp::jitLock); - MIPSComp::jit = nullptr; - delete oldjit; - } newjit = MIPSComp::CreateNativeJit(this, PSP_CoreParameter().cpuCore == CPUCore::JIT_IR); break; case CPUCore::IR_INTERPRETER: INFO_LOG(Log::CPU, "Switching to IR interpreter"); - if (oldjit) { - std::lock_guard guard(MIPSComp::jitLock); - MIPSComp::jit = nullptr; - delete oldjit; - } newjit = new MIPSComp::IRJit(this, false); break; case CPUCore::INTERPRETER: INFO_LOG(Log::CPU, "Switching to interpreter"); - if (oldjit) { - std::lock_guard guard(MIPSComp::jitLock); - MIPSComp::jit = nullptr; - delete oldjit; - } + // Leaving newjit as null. + break; + + default: + WARN_LOG(Log::CPU, "Invalid value for cpuCore, falling back to interpreter"); break; }