Skip to content

Commit e0fb278

Browse files
gh-143421: Allocate all JIT state in one go (GH-143626)
1 parent 1932127 commit e0fb278

File tree

9 files changed

+126
-121
lines changed

9 files changed

+126
-121
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ _PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame,
234234
int oparg);
235235

236236
void _PyJit_FinalizeTracing(PyThreadState *tstate);
237+
void _PyJit_TracerFree(_PyThreadStateImpl *_tstate);
237238

238239
void _PyJit_Tracer_InvalidateDependency(PyThreadState *old_tstate, void *obj);
239240

Include/internal/pycore_tstate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ typedef struct _PyJitTracerState {
5656
_PyJitTracerInitialState initial_state;
5757
_PyJitTracerPreviousState prev_state;
5858
_PyJitTracerTranslatorState translator_state;
59-
JitOptContext *opt_context;
60-
_PyUOpInstruction *code_buffer;
59+
JitOptContext opt_context;
60+
_PyUOpInstruction code_buffer[UOP_MAX_TRACE_LENGTH];
6161
} _PyJitTracerState;
6262

6363
#endif
@@ -153,7 +153,7 @@ typedef struct _PyThreadStateImpl {
153153
Py_ssize_t reftotal; // this thread's total refcount operations
154154
#endif
155155
#if _Py_TIER2
156-
_PyJitTracerState jit_tracer_state;
156+
_PyJitTracerState *jit_tracer_state;
157157
#endif
158158
_PyPolicy policy;
159159
} _PyThreadStateImpl;

Python/bytecodes.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,15 +5738,17 @@ dummy_func(
57385738
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
57395739
// JIT should have disabled super instructions, as we can
57405740
// do these optimizations ourselves in the JIT.
5741-
_tstate->jit_tracer_state.prev_state.instr = next_instr;
5741+
_PyJitTracerState *tracer = _tstate->jit_tracer_state;
5742+
assert(tracer != NULL);
5743+
tracer->prev_state.instr = next_instr;
57425744
PyObject *prev_code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
5743-
if (_tstate->jit_tracer_state.prev_state.instr_code != (PyCodeObject *)prev_code) {
5744-
Py_SETREF(_tstate->jit_tracer_state.prev_state.instr_code, (PyCodeObject*)Py_NewRef((prev_code)));
5745+
if (tracer->prev_state.instr_code != (PyCodeObject *)prev_code) {
5746+
Py_SETREF(tracer->prev_state.instr_code, (PyCodeObject*)Py_NewRef((prev_code)));
57455747
}
57465748

5747-
_tstate->jit_tracer_state.prev_state.instr_frame = frame;
5748-
_tstate->jit_tracer_state.prev_state.instr_oparg = oparg;
5749-
_tstate->jit_tracer_state.prev_state.instr_stacklevel = PyStackRef_IsNone(frame->f_executable) ? 2 : STACK_LEVEL();
5749+
tracer->prev_state.instr_frame = frame;
5750+
tracer->prev_state.instr_oparg = oparg;
5751+
tracer->prev_state.instr_stacklevel = PyStackRef_IsNone(frame->f_executable) ? 2 : STACK_LEVEL();
57505752
if (_PyOpcode_Caches[_PyOpcode_Deopt[opcode]]) {
57515753
(&next_instr[1])->counter = trigger_backoff_counter();
57525754
}

Python/ceval.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,15 +1462,17 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
14621462
}
14631463
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
14641464
// Deal with backoffs
1465-
_PyExitData *exit = _tstate->jit_tracer_state.initial_state.exit;
1465+
_PyJitTracerState *tracer = _tstate->jit_tracer_state;
1466+
assert(tracer != NULL);
1467+
_PyExitData *exit = tracer->initial_state.exit;
14661468
if (exit == NULL) {
14671469
// We hold a strong reference to the code object, so the instruction won't be freed.
14681470
if (err <= 0) {
1469-
_Py_BackoffCounter counter = _tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter;
1470-
_tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter = restart_backoff_counter(counter);
1471+
_Py_BackoffCounter counter = tracer->initial_state.jump_backward_instr[1].counter;
1472+
tracer->initial_state.jump_backward_instr[1].counter = restart_backoff_counter(counter);
14711473
}
14721474
else {
1473-
_tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(&_tstate->policy);
1475+
tracer->initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(&_tstate->policy);
14741476
}
14751477
}
14761478
else {

Python/ceval_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ do { \
420420
JUMP_TO_LABEL(error); \
421421
} \
422422
if (keep_tracing_bit) { \
423-
assert(((_PyThreadStateImpl *)tstate)->jit_tracer_state.prev_state.code_curr_size == 2); \
423+
assert(((_PyThreadStateImpl *)tstate)->jit_tracer_state->prev_state.code_curr_size == 2); \
424424
ENTER_TRACING(); \
425425
DISPATCH_NON_TRACING(); \
426426
} \

Python/generated_cases.c.h

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)