Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Check state before running and potentially recompile #384

Open
wants to merge 3 commits into
base: develop/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/pyjion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class JitInfo:
run_count: int
tracing: bool
profiling: bool
level: int


def info(f) -> JitInfo:
Expand All @@ -148,4 +149,5 @@ def info(f) -> JitInfo:
PgcStatus(d['pgc']),
d['run_count'],
d['tracing'],
d['profiling'])
d['profiling'],
d['level'])
44 changes: 31 additions & 13 deletions src/pyjion/pyjit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ PgcStatus nextPgcStatus(PgcStatus status){
}

PyjionJittedCode::~PyjionJittedCode() {
delete j_profile;
this->reset();
Py_XDECREF(this->j_code);
}

PyjionCodeProfile::~PyjionCodeProfile() {
// Don't decref types so that comparisons can be made to jumps
// for (auto &pos: this->stackTypes) {
// for(auto &observed: pos.second){
// Py_XDECREF(observed.second);
// }
// }
void PyjionJittedCode::reset() {
free(this->j_il);
this->j_il = nullptr;
this->j_ilLen = 0;
Py_XDECREF(this->j_graph);
}

void PyjionCodeProfile::record(size_t opcodePosition, size_t stackPosition, PyObject* value){
Expand Down Expand Up @@ -116,7 +115,7 @@ void capturePgcStackValue(PyjionCodeProfile* profile, PyObject* value, size_t op
int
Pyjit_CheckRecursiveCall(PyThreadState *tstate, const char *where)
{
int recursion_limit = g_pyjionSettings.recursionLimit;
uint32_t recursion_limit = g_pyjionSettings.recursionLimit;

if (tstate->recursion_headroom) {
if (tstate->recursion_depth > recursion_limit + 50) {
Expand Down Expand Up @@ -288,6 +287,7 @@ PyObject* PyJit_ExecuteAndCompileFrame(PyjionJittedCode* state, PyFrameObject *f
state->j_sequencePointsLen = res.compiledCode->get_sequence_points_length();
state->j_callPoints = res.compiledCode->get_call_points();
state->j_callPointsLen = res.compiledCode->get_call_points_length();
state->j_optLevel = g_pyjionSettings.optimizationLevel;

#ifdef DUMP_SEQUENCE_POINTS
printf("Method disassembly for %s\n", PyUnicode_AsUTF8(frame->f_code->co_name));
Expand Down Expand Up @@ -341,18 +341,35 @@ PyjionJittedCode* PyJit_EnsureExtra(PyObject* codeObject) {
return jitted;
}

inline bool needsRecompile(PyjionJittedCode* jitted, PyThreadState* tstate){
if (jitted->j_optLevel != g_pyjionSettings.optimizationLevel){
return true;
}
if (tstate->cframe->use_tracing && tstate->c_profilefunc && !jitted->j_profilingHooks){
return true;
}
if (tstate->cframe->use_tracing && tstate->c_tracefunc && !jitted->j_profilingHooks){
return true;
}
return false;
}

// This is our replacement evaluation function. We lookup our corresponding jitted code
// and dispatch to it if it's already compiled. If it hasn't yet been compiled we'll
// eventually compile it and invoke it. If it's not time to compile it yet then we'll
// invoke the default evaluation function.
PyObject* PyJit_EvalFrame(PyThreadState *ts, PyFrameObject *f, int throwflag) {
auto jitted = PyJit_EnsureExtra((PyObject*)f->f_code);
auto jitted = PyJit_EnsureExtra(reinterpret_cast<PyObject *>(f->f_code));
if (jitted != nullptr && !throwflag) {
if (jitted->j_addr != nullptr && (!g_pyjionSettings.pgc || jitted->j_pgc_status == Optimized)) {
if (jitted->j_addr != nullptr && needsRecompile(jitted, ts)){
jitted->reset();
jitted = new PyjionJittedCode(reinterpret_cast<PyObject *>(f->f_code));
return PyJit_ExecuteAndCompileFrame(jitted, f, ts, jitted->j_profile);
} else if (jitted->j_addr != nullptr && (!g_pyjionSettings.pgc || jitted->j_pgc_status == Optimized)) {
jitted->j_run_count++;
return PyJit_ExecuteJittedFrame((void*)jitted->j_addr, f, ts, jitted->j_profile);
}
else if (!jitted->j_failed && jitted->j_run_count++ >= jitted->j_specialization_threshold) {
} else if (!jitted->j_failed && jitted->j_run_count++ >= jitted->j_specialization_threshold) {
jitted->reset();
auto result = PyJit_ExecuteAndCompileFrame(jitted, f, ts, jitted->j_profile);
jitted->j_pgc_status = nextPgcStatus(jitted->j_pgc_status);
return result;
Expand Down Expand Up @@ -421,6 +438,7 @@ static PyObject *pyjion_info(PyObject *self, PyObject* func) {
PyDict_SetItemString(res, "compiled", jitted->j_addr != nullptr ? Py_True : Py_False);
PyDict_SetItemString(res, "optimizations", PyLong_FromLong(jitted->j_optimizations));
PyDict_SetItemString(res, "pgc", PyLong_FromLong(jitted->j_pgc_status));
PyDict_SetItemString(res, "level", PyLong_FromLong(jitted->j_optLevel));

auto runCount = PyLong_FromUnsignedLongLong(jitted->j_run_count);
PyDict_SetItemString(res, "run_count", runCount);
Expand Down
5 changes: 4 additions & 1 deletion src/pyjion/pyjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class PyjionCodeProfile{
void record(size_t opcodePosition, size_t stackPosition, PyObject* obj);
PyTypeObject* getType(size_t opcodePosition, size_t stackPosition);
AbstractValueKind getKind(size_t opcodePosition, size_t stackPosition);
~PyjionCodeProfile();
};

void capturePgcStackValue(PyjionCodeProfile* profile, PyObject* value, size_t opcodePosition, size_t stackPosition);
Expand Down Expand Up @@ -168,6 +167,7 @@ class PyjionJittedCode {
SymbolTable j_symbols;
bool j_tracingHooks;
bool j_profilingHooks;
uint8_t j_optLevel;

explicit PyjionJittedCode(PyObject* code) {
j_compile_result = 0;
Expand All @@ -189,10 +189,13 @@ class PyjionJittedCode {
j_callPointsLen = 0;
j_tracingHooks = false;
j_profilingHooks = false;
j_optLevel = g_pyjionSettings.optimizationLevel;
Py_INCREF(code);
}

~PyjionJittedCode();

void reset();
};

void setOptimizationLevel(unsigned short level);
Expand Down