From 6149ac584f73484bc583795a2ceda393f33b2779 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 6 Jan 2018 16:43:38 -0800 Subject: [PATCH] jit: Add interface to precompile functions. This doesn't actually do any preloading yet, it just adds an API. --- Core/Config.cpp | 1 + Core/Config.h | 1 + Core/HLE/sceKernelModule.cpp | 6 ++++++ Core/HLE/sceKernelThread.cpp | 3 ++- Core/MIPS/JitCommon/JitCommon.h | 1 + Core/MIPS/MIPSAnalyst.cpp | 25 +++++++++++++++++++++++++ Core/MIPS/MIPSAnalyst.h | 3 ++- 7 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 744695e058..d35c3bc3e9 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -406,6 +406,7 @@ static ConfigSetting cpuSettings[] = { ConfigSetting("FastMemoryAccess", &g_Config.bFastMemory, true, true, true), ReportedConfigSetting("FuncReplacements", &g_Config.bFuncReplacements, true, true, true), ConfigSetting("HideSlowWarnings", &g_Config.bHideSlowWarnings, false, true, false), + ConfigSetting("PreloadFunctions", &g_Config.bPreloadFunctions, false, true, true), ReportedConfigSetting("CPUSpeed", &g_Config.iLockedCPUSpeed, 0, true, true), ConfigSetting(false), diff --git a/Core/Config.h b/Core/Config.h index de342753a8..12534e3e6d 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -126,6 +126,7 @@ public: bool bForceLagSync; bool bFuncReplacements; bool bHideSlowWarnings; + bool bPreloadFunctions; bool bSeparateSASThread; bool bSeparateIOThread; diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 26d54d8794..47a9ae7d84 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -712,6 +712,7 @@ void ImportFuncSymbol(const FuncSymbolImport &func, bool reimporting) { } WriteSyscall(func.moduleName, func.nid, func.stubAddr); currentMIPS->InvalidateICache(func.stubAddr, 8); + MIPSAnalyst::PrecompileFunction(func.stubAddr, 8); return; } @@ -730,6 +731,7 @@ void ImportFuncSymbol(const FuncSymbolImport &func, bool reimporting) { } WriteFuncStub(func.stubAddr, it->symAddr); currentMIPS->InvalidateICache(func.stubAddr, 8); + MIPSAnalyst::PrecompileFunction(func.stubAddr, 8); return; } } @@ -768,6 +770,7 @@ void ExportFuncSymbol(const FuncSymbolExport &func) { INFO_LOG(LOADER, "Resolving function %s/%08x", func.moduleName, func.nid); WriteFuncStub(it->stubAddr, func.symAddr); currentMIPS->InvalidateICache(it->stubAddr, 8); + MIPSAnalyst::PrecompileFunction(it->stubAddr, 8); } } } @@ -1450,6 +1453,9 @@ static Module *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 loadAdd // use module_start_func instead of entry_addr if entry_addr is 0 if (module->nm.entry_addr == 0) module->nm.entry_addr = module->nm.module_start_func; + + MIPSAnalyst::PrecompileFunctions(); + } else { module->nm.entry_addr = -1; } diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 0e15bfe1e6..16f71881d0 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -26,7 +26,7 @@ #include "Common/CommonTypes.h" #include "Core/HLE/HLE.h" #include "Core/HLE/HLETables.h" -#include "Core/MIPS/MIPSInt.h" +#include "Core/MIPS/MIPSAnalyst.h" #include "Core/MIPS/MIPSCodeUtils.h" #include "Core/MIPS/MIPS.h" #include "Core/CoreTiming.h" @@ -887,6 +887,7 @@ static void __KernelWriteFakeSysCall(u32 nid, u32 *ptr, u32 &pos) *ptr = pos; pos += 8; WriteSyscall("FakeSysCalls", nid, *ptr); + MIPSAnalyst::PrecompileFunction(*ptr, 8); } void __KernelThreadingInit() diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index 74341169b5..33e07b1955 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -129,6 +129,7 @@ namespace MIPSComp { virtual void DoState(PointerWrap &p) = 0; virtual void RunLoopUntil(u64 globalticks) = 0; virtual void Compile(u32 em_address) = 0; + virtual void CompileFunction(u32 start_address, u32 length) { } virtual void ClearCache() = 0; virtual MIPSOpcode GetOriginalOp(MIPSOpcode op) = 0; diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 99afd042d3..036af24154 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -21,6 +21,7 @@ #include #include +#include "base/timeutil.h" #include "ext/cityhash/city.h" #include "Common/FileUtil.h" #include "Core/Config.h" @@ -904,6 +905,30 @@ skip: } } + void PrecompileFunction(u32 startAddr, u32 length) { + // Direct calls to this ignore the bPreloadFunctions flag, since it's just for stubs. + if (MIPSComp::jit) { + MIPSComp::jit->CompileFunction(startAddr, length); + } + } + + void PrecompileFunctions() { + if (!g_Config.bPreloadFunctions) { + return; + } + std::lock_guard guard(functions_lock); + + double st = real_time_now(); + for (auto iter = functions.begin(), end = functions.end(); iter != end; iter++) { + const AnalyzedFunction &f = *iter; + + PrecompileFunction(f.start, f.end - f.start + 4); + } + double et = real_time_now(); + + NOTICE_LOG(JIT, "Precompiled %d MIPS functions in %0.2f milliseconds", (int)functions.size(), (et - st) * 1000.0); + } + static const char *DefaultFunctionName(char buffer[256], u32 startAddr) { sprintf(buffer, "z_un_%08x", startAddr); return buffer; diff --git a/Core/MIPS/MIPSAnalyst.h b/Core/MIPS/MIPSAnalyst.h index 867d96895c..9d6683c48a 100644 --- a/Core/MIPS/MIPSAnalyst.h +++ b/Core/MIPS/MIPSAnalyst.h @@ -107,7 +107,8 @@ namespace MIPSAnalyst void RegisterFunction(u32 startAddr, u32 size, const char *name); void ScanForFunctions(u32 startAddr, u32 endAddr, bool insertSymbols); void ForgetFunctions(u32 startAddr, u32 endAddr); - void CompileLeafs(); + void PrecompileFunctions(); + void PrecompileFunction(u32 startAddr, u32 length); void SetHashMapFilename(const std::string& filename = ""); void LoadBuiltinHashMap();