From 79633a7740baac5c3f0378b18e57227fca3aa68f Mon Sep 17 00:00:00 2001 From: DH Date: Tue, 8 Apr 2025 20:31:15 +0300 Subject: [PATCH] llvm: Fix memory leak on jit destroy Fix msvc build --- ps3fw/include/rpcsx/fw/ps3/cellGame.h | 1 + rpcs3/util/JIT.h | 4 ++-- rpcs3/util/JITLLVM.cpp | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ps3fw/include/rpcsx/fw/ps3/cellGame.h b/ps3fw/include/rpcsx/fw/ps3/cellGame.h index e0f625b08..928c279b3 100644 --- a/ps3fw/include/rpcsx/fw/ps3/cellGame.h +++ b/ps3fw/include/rpcsx/fw/ps3/cellGame.h @@ -2,6 +2,7 @@ #include "Emu/Cell/ErrorCodes.h" #include "Emu/Memory/vm_ptr.h" +#include // Return Codes enum diff --git a/rpcs3/util/JIT.h b/rpcs3/util/JIT.h index df0eec7db..a43a74488 100644 --- a/rpcs3/util/JIT.h +++ b/rpcs3/util/JIT.h @@ -509,10 +509,10 @@ enum class thread_state : u32; class jit_compiler final { // Local LLVM context - std::unique_ptr m_context{}; + std::unique_ptr m_context{nullptr, [](llvm::LLVMContext*) {}}; // Execution instance - std::unique_ptr m_engine{}; + std::unique_ptr m_engine{nullptr, [](llvm::ExecutionEngine*) {}}; // Arch std::string m_cpu{}; diff --git a/rpcs3/util/JITLLVM.cpp b/rpcs3/util/JITLLVM.cpp index 898726c26..812b4c11c 100644 --- a/rpcs3/util/JITLLVM.cpp +++ b/rpcs3/util/JITLLVM.cpp @@ -640,7 +640,11 @@ bool jit_compiler::add_sub_disk_space(ssz space) } jit_compiler::jit_compiler(const std::unordered_map& _link, const std::string& _cpu, u32 flags, std::function symbols_cement) noexcept - : m_context(new llvm::LLVMContext), m_cpu(cpu(_cpu)) + : m_context(new llvm::LLVMContext, [](llvm::LLVMContext* context) + { + delete context; + }), + m_cpu(cpu(_cpu)) { [[maybe_unused]] static const bool s_install_llvm_error_handler = []() { @@ -681,7 +685,9 @@ jit_compiler::jit_compiler(const std::unordered_map& _link, co } { - m_engine.reset(llvm::EngineBuilder(std::move(null_mod)) + + m_engine = std::unique_ptr{ + llvm::EngineBuilder(std::move(null_mod)) .setErrorStr(&result) .setEngineKind(llvm::EngineKind::JIT) .setMCJITMemoryManager(std::move(mem)) @@ -692,7 +698,12 @@ jit_compiler::jit_compiler(const std::unordered_map& _link, co #endif .setRelocationModel(llvm::Reloc::Model::PIC_) .setMCPU(m_cpu) - .create()); + .create(), + [](llvm::ExecutionEngine* engine) + { + delete engine; + }, + }; } if (!_link.empty())