Skip to content

Commit 92a09a6

Browse files
Address review
1 parent 03e5dab commit 92a09a6

File tree

6 files changed

+43
-49
lines changed

6 files changed

+43
-49
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ _PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame,
233233
_Py_CODEUNIT *close_loop_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit,
234234
int oparg, _PyExecutorObject *current_executor);
235235

236-
void _PyJit_FinalizeTracing(PyThreadState *tstate);
236+
void _PyJit_FinalizeTracing(PyThreadState *tstate, int err);
237237
void _PyJit_TracerFree(_PyThreadStateImpl *_tstate);
238238

239239
void _PyJit_Tracer_InvalidateDependency(PyThreadState *old_tstate, void *obj);

Python/bytecodes.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5607,16 +5607,7 @@ dummy_func(
56075607
#else
56085608
assert(_PyErr_Occurred(tstate));
56095609
#endif
5610-
#if _Py_TIER2
5611-
if (IS_JIT_TRACING()) {
5612-
LEAVE_TRACING();
5613-
int res = stop_tracing_and_jit(tstate, frame);
5614-
(void)res;
5615-
// We shouldn't ven have compiled in the first place.
5616-
assert(res == 0);
5617-
}
5618-
#endif
5619-
5610+
STOP_TRACING();
56205611

56215612
/* Log traceback info. */
56225613
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
@@ -5631,6 +5622,7 @@ dummy_func(
56315622
}
56325623

56335624
spilled label(exception_unwind) {
5625+
STOP_TRACING();
56345626
/* We can't use frame->instr_ptr here, as RERAISE may have set it */
56355627
int offset = INSTR_OFFSET()-1;
56365628
int level, handler, lasti;

Python/ceval.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,32 +1460,7 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
14601460
if (!_PyErr_Occurred(tstate) && !_is_sys_tracing) {
14611461
err = _PyOptimizer_Optimize(frame, tstate);
14621462
}
1463-
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1464-
// Deal with backoffs
1465-
_PyJitTracerState *tracer = _tstate->jit_tracer_state;
1466-
assert(tracer != NULL);
1467-
_PyExitData *exit = tracer->initial_state.exit;
1468-
if (exit == NULL) {
1469-
// We hold a strong reference to the code object, so the instruction won't be freed.
1470-
if (err <= 0) {
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);
1473-
}
1474-
else {
1475-
tracer->initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(&_tstate->policy);
1476-
}
1477-
}
1478-
else if (tracer->initial_state.executor->vm_data.valid) {
1479-
// Likewise, we hold a strong reference to the executor containing this exit, so the exit is guaranteed
1480-
// to be valid to access.
1481-
if (err <= 0) {
1482-
exit->temperature = restart_backoff_counter(exit->temperature);
1483-
}
1484-
else {
1485-
exit->temperature = initial_temperature_backoff_counter(&_tstate->policy);
1486-
}
1487-
}
1488-
_PyJit_FinalizeTracing(tstate);
1463+
_PyJit_FinalizeTracing(tstate, err);
14891464
return err;
14901465
}
14911466
#endif

Python/ceval_macros.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@
156156
# define LEAVE_TRACING() tracing_mode = 0
157157
#endif
158158

159+
#if _Py_TIER2
160+
#define STOP_TRACING() \
161+
do { \
162+
if (IS_JIT_TRACING()) { \
163+
LEAVE_TRACING(); \
164+
_PyJit_FinalizeTracing(tstate, 0); \
165+
} \
166+
} while (0);
167+
#else
168+
#define STOP_TRACING() (void(0));
169+
#endif
170+
171+
159172
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
160173
#ifdef Py_DEBUG
161174
#define PRE_DISPATCH_GOTO() if (frame->lltrace >= 5) { \

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,33 @@ _PyJit_TryInitializeTracing(
10911091
}
10921092

10931093
Py_NO_INLINE void
1094-
_PyJit_FinalizeTracing(PyThreadState *tstate)
1094+
_PyJit_FinalizeTracing(PyThreadState *tstate, int err)
10951095
{
10961096
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
10971097
_PyJitTracerState *tracer = _tstate->jit_tracer_state;
1098+
// Deal with backoffs
1099+
assert(tracer != NULL);
1100+
_PyExitData *exit = tracer->initial_state.exit;
1101+
if (exit == NULL) {
1102+
// We hold a strong reference to the code object, so the instruction won't be freed.
1103+
if (err <= 0) {
1104+
_Py_BackoffCounter counter = tracer->initial_state.jump_backward_instr[1].counter;
1105+
tracer->initial_state.jump_backward_instr[1].counter = restart_backoff_counter(counter);
1106+
}
1107+
else {
1108+
tracer->initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(&_tstate->policy);
1109+
}
1110+
}
1111+
else if (tracer->initial_state.executor->vm_data.valid) {
1112+
// Likewise, we hold a strong reference to the executor containing this exit, so the exit is guaranteed
1113+
// to be valid to access.
1114+
if (err <= 0) {
1115+
exit->temperature = restart_backoff_counter(exit->temperature);
1116+
}
1117+
else {
1118+
exit->temperature = initial_temperature_backoff_counter(&_tstate->policy);
1119+
}
1120+
}
10981121
Py_CLEAR(tracer->initial_state.code);
10991122
Py_CLEAR(tracer->initial_state.func);
11001123
Py_CLEAR(tracer->initial_state.executor);

0 commit comments

Comments
 (0)