Skip to content

Commit

Permalink
jit: Add interface to precompile functions.
Browse files Browse the repository at this point in the history
This doesn't actually do any preloading yet, it just adds an API.
  • Loading branch information
unknownbrackets committed Jan 7, 2018
1 parent ccd562d commit 6149ac5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ struct Config {
bool bForceLagSync;
bool bFuncReplacements;
bool bHideSlowWarnings;
bool bPreloadFunctions;

bool bSeparateSASThread;
bool bSeparateIOThread;
Expand Down
6 changes: 6 additions & 0 deletions Core/HLE/sceKernelModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion Core/HLE/sceKernelThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions Core/MIPS/JitCommon/JitCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 25 additions & 0 deletions Core/MIPS/MIPSAnalyst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unordered_set>
#include <mutex>

#include "base/timeutil.h"
#include "ext/cityhash/city.h"
#include "Common/FileUtil.h"
#include "Core/Config.h"
Expand Down Expand Up @@ -904,6 +905,30 @@ namespace MIPSAnalyst {
}
}

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<std::recursive_mutex> 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;
Expand Down
3 changes: 2 additions & 1 deletion Core/MIPS/MIPSAnalyst.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6149ac5

Please sign in to comment.