From 81d0dd686bd624d26bcb8f9bcadf8c648420a6d1 Mon Sep 17 00:00:00 2001 From: Elad <18193363+elad335@users.noreply.github.com> Date: Tue, 21 Jan 2025 13:27:10 +0200 Subject: [PATCH] LLVM: Add explicit resource-freeing at emulation stop --- Utilities/JIT.h | 3 +++ Utilities/JITLLVM.cpp | 13 +++++++++++++ rpcs3/Emu/Cell/PPUThread.cpp | 19 +++++++++++++++++++ rpcs3/Emu/System.cpp | 9 +++++++++ 4 files changed, 44 insertions(+) diff --git a/Utilities/JIT.h b/Utilities/JIT.h index 5069b63950..e17e1ccba3 100644 --- a/Utilities/JIT.h +++ b/Utilities/JIT.h @@ -498,6 +498,8 @@ namespace llvm class StringRef; } +enum class thread_state : u32; + // Temporary compiler interface class jit_compiler final { @@ -515,6 +517,7 @@ class jit_compiler final public: jit_compiler(const std::unordered_map& _link, const std::string& _cpu, u32 flags = 0, std::function symbols_cement = {}) noexcept; + jit_compiler& operator=(thread_state) noexcept; ~jit_compiler() noexcept; // Get LLVM context diff --git a/Utilities/JITLLVM.cpp b/Utilities/JITLLVM.cpp index 8487786544..741480d948 100644 --- a/Utilities/JITLLVM.cpp +++ b/Utilities/JITLLVM.cpp @@ -1,5 +1,6 @@ #include "util/types.hpp" #include "util/sysinfo.hpp" +#include "Utilities/Thread.h" #include "JIT.h" #include "StrFmt.h" #include "File.h" @@ -718,6 +719,18 @@ jit_compiler::jit_compiler(const std::unordered_map& _link, co } } +jit_compiler& jit_compiler::operator=(thread_state s) noexcept +{ + if (s == thread_state::destroying_context) + { + // Release resources explicitly + m_engine.reset(); + m_context.reset(); + } + + return *this; +} + jit_compiler::~jit_compiler() noexcept { } diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index 784c5a7966..144f55e106 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -3736,6 +3736,25 @@ namespace bucket.map.erase(found); } + + jit_module_manager& operator=(thread_state s) noexcept + { + if (s == thread_state::destroying_context) + { + for (auto& buck : buckets) + { + for (auto& mod : buck.map) + { + for (auto& jit : mod.second.pjit) + { + *jit = s; + } + } + } + } + + return *this; + } }; } #endif diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index 1423798366..424b540491 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -3337,6 +3337,15 @@ void Emulator::Kill(bool allow_autoexit, bool savestate, savestate_stage* save_s static_cast(init_mtx->init()); + // Call explcit semi-destructors (free memory before savestate) + for (const auto& [type, data] : *g_fxo) + { + if (type.thread_op) + { + type.thread_op(data, thread_state::destroying_context); + } + } + auto set_progress_message = [&](std::string_view text) { *verbose_message = stx::make_single(text);