Skip to content

Commit

Permalink
Try to jit all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed May 16, 2020
1 parent a8f8d8b commit 6a5885c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
21 changes: 11 additions & 10 deletions src/common/scripting/jit/jit.cpp
Expand Up @@ -7,23 +7,24 @@ extern PString *TypeString;
extern PStruct *TypeVector2;
extern PStruct *TypeVector3;

IRContext* JitGetIRContext();
JITRuntime* GetJITRuntime();

JitFuncPtr JitCompile(VMScriptFunction* sfunc)
{
#if 1
#if 0
if (strcmp(sfunc->PrintableName.GetChars(), "StatusScreen.drawNum") != 0)
return nullptr;
#endif

try
{
IRContext* context = JitGetIRContext();
JitCompiler compiler(context, sfunc);
JITRuntime* jit = GetJITRuntime();
IRContext context;
JitCompiler compiler(&context, sfunc);
IRFunction* func = compiler.Codegen();
context->codegen();
std::string text = context->getFunctionAssembly(func);
return reinterpret_cast<JitFuncPtr>(context->getPointerToFunction(func));
jit->add(&context);
//std::string text = context->getFunctionAssembly(func);
return reinterpret_cast<JitFuncPtr>(jit->getPointerToFunction(func->name));
}
catch (...)
{
Expand All @@ -36,10 +37,10 @@ void JitDumpLog(FILE* file, VMScriptFunction* sfunc)
{
try
{
IRContext* context = JitGetIRContext();
JitCompiler compiler(context, sfunc);
IRContext context;
JitCompiler compiler(&context, sfunc);
IRFunction* func = compiler.Codegen();
std::string text = context->getFunctionAssembly(func);
std::string text = context.getFunctionAssembly(func);
fwrite(text.data(), text.size(), 1, file);
}
catch (const std::exception& e)
Expand Down
14 changes: 7 additions & 7 deletions src/common/scripting/jit/jit_runtime.cpp
Expand Up @@ -5,15 +5,15 @@

#if 1

static IRContext* JitIRContext = nullptr;
static JITRuntime* JITRuntimeVar = nullptr;

IRContext* JitGetIRContext()
JITRuntime* GetJITRuntime()
{
if (!JitIRContext)
if (!JITRuntimeVar)
{
JitIRContext = new IRContext();
JITRuntimeVar = new JITRuntime();
}
return JitIRContext;
return JITRuntimeVar;
}

FString JitCaptureStackTrace(int framesToSkip, bool includeNativeFrames)
Expand All @@ -23,8 +23,8 @@ FString JitCaptureStackTrace(int framesToSkip, bool includeNativeFrames)

void JitRelease()
{
delete JitIRContext;
JitIRContext = nullptr;
delete JITRuntimeVar;
JITRuntimeVar = nullptr;
}

#else
Expand Down
16 changes: 1 addition & 15 deletions src/common/scripting/vm/vmframe.cpp
Expand Up @@ -264,24 +264,10 @@ int VMScriptFunction::PCToLine(const VMOP *pc)
return -1;
}

static bool CanJit(VMScriptFunction *func)
{
// Asmjit has a 256 register limit. Stay safely away from it as the jit compiler uses a few for temporaries as well.
// Any function exceeding the limit will use the VM - a fair punishment to someone for writing a function so bloated ;)

int maxregs = 200;
if (func->NumRegA + func->NumRegD + func->NumRegF + func->NumRegS < maxregs)
return true;

Printf(TEXTCOLOR_ORANGE "%s is using too many registers (%d of max %d)! Function will not use native code.\n", func->PrintableName.GetChars(), func->NumRegA + func->NumRegD + func->NumRegF + func->NumRegS, maxregs);

return false;
}

int VMScriptFunction::FirstScriptCall(VMFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret)
{
#ifdef HAVE_VM_JIT
if (vm_jit && CanJit(static_cast<VMScriptFunction*>(func)))
if (vm_jit)
{
func->ScriptCall = JitCompile(static_cast<VMScriptFunction*>(func));
if (!func->ScriptCall)
Expand Down

0 comments on commit 6a5885c

Please sign in to comment.