Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
#define JIT_CLEANUP_THRESHOLD 1000

int _Py_uop_analyze_and_optimize(
PyFunctionObject *func,
struct _PyThreadStateImpl *tstate,
Comment thread
cocolato marked this conversation as resolved.
Outdated
_PyUOpInstruction *trace, int trace_len, int curr_stackentries,
_PyBloomFilter *dependencies);

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_tstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct _PyJitTracerTranslatorState {

typedef struct _PyJitTracerState {
_PyUOpInstruction *code_buffer;
struct _JitOptContext *opt_context;
Comment thread
cocolato marked this conversation as resolved.
Outdated
_PyJitTracerInitialState initial_state;
_PyJitTracerPreviousState prev_state;
_PyJitTracerTranslatorState translator_state;
Expand Down
4 changes: 2 additions & 2 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1484,8 +1484,8 @@ uop_optimize(
OPT_STAT_INC(traces_created);
if (!is_noopt) {
length = _Py_uop_analyze_and_optimize(
_tstate->jit_tracer_state.initial_state.func,
buffer,length,
_tstate,
buffer, length,
curr_stackentries, dependencies);
if (length <= 0) {
return length;
Expand Down
21 changes: 15 additions & 6 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "pycore_opcode_metadata.h"
#include "pycore_opcode_utils.h"
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_tstate.h" // _PyThreadStateImpl
#include "pycore_uop_metadata.h"
#include "pycore_long.h"
#include "pycore_interpframe.h" // _PyFrame_GetCode
Expand Down Expand Up @@ -334,17 +335,25 @@ _Py_opt_assert_within_stack_bounds(
/* >0 (length) for success, 0 for not ready, clears all possible errors. */
static int
optimize_uops(
PyFunctionObject *func,
_PyThreadStateImpl *tstate,
_PyUOpInstruction *trace,
int trace_len,
int curr_stacklen,
_PyBloomFilter *dependencies
)
{
assert(!PyErr_Occurred());

JitOptContext context;
JitOptContext *ctx = &context;
PyFunctionObject *func = tstate->jit_tracer_state.initial_state.func;

// Use thread-local JitOptContext to avoid stack overflow
Comment thread
cocolato marked this conversation as resolved.
Outdated
JitOptContext *ctx = tstate->jit_tracer_state.opt_context;
if (ctx == NULL) {
ctx = (JitOptContext *)PyMem_RawMalloc(sizeof(JitOptContext));
if (ctx == NULL) {
return 0;
}
tstate->jit_tracer_state.opt_context = ctx;
}
uint32_t opcode = UINT16_MAX;

// Make sure that watchers are set up
Expand Down Expand Up @@ -574,7 +583,7 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
// > 0 - length of optimized trace
int
_Py_uop_analyze_and_optimize(
PyFunctionObject *func,
_PyThreadStateImpl *tstate,
_PyUOpInstruction *buffer,
int length,
int curr_stacklen,
Expand All @@ -584,7 +593,7 @@ _Py_uop_analyze_and_optimize(
OPT_STAT_INC(optimizer_attempts);

length = optimize_uops(
func, buffer,
tstate, buffer,
length, curr_stacklen, dependencies);

if (length == 0) {
Expand Down
7 changes: 7 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
"PYTHON_JIT_SIDE_EXIT_INITIAL_BACKOFF",
SIDE_EXIT_INITIAL_BACKOFF, 0, MAX_BACKOFF);
_tstate->jit_tracer_state.code_buffer = NULL;
_tstate->jit_tracer_state.opt_context = NULL;
#endif
tstate->delete_later = NULL;

Expand Down Expand Up @@ -1874,6 +1875,12 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
_PyObject_VirtualFree(_tstate->jit_tracer_state.code_buffer, UOP_BUFFER_SIZE);
_tstate->jit_tracer_state.code_buffer = NULL;
}
if (_tstate->jit_tracer_state.opt_context != NULL) {
// Ensure any resources in opt_context are cleaned up
_Py_uop_abstractcontext_fini(_tstate->jit_tracer_state.opt_context);
Comment thread
cocolato marked this conversation as resolved.
Outdated
PyMem_RawFree(_tstate->jit_tracer_state.opt_context);
_tstate->jit_tracer_state.opt_context = NULL;
}
#endif

HEAD_UNLOCK(runtime);
Expand Down
Loading