From a62fe40ccb6101f3795dbd3bdf5c5d9fdb692edb Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Tue, 21 Oct 2025 22:11:44 +0100 Subject: [PATCH] Clean up macros --- Python/bytecodes.c | 2 +- Python/ceval.c | 14 ++++++++++++++ Python/ceval_macros.h | 35 +++++++++++------------------------ Python/optimizer.c | 5 +++++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index f8742e6212d7..388da83dbc24 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2979,7 +2979,7 @@ dummy_func( } int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); if (!_is_sys_tracing) { - /* Back up over EXTENDED_ARGs so executor is inserted at the corret place */ + /* Back up over EXTENDED_ARGs so executor is inserted at the correct place */ _Py_CODEUNIT *insert_exec_at = this_instr; while (oparg > 255) { oparg >>= 8; diff --git a/Python/ceval.c b/Python/ceval.c index e8f1ed5f88b2..3ca837829499 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -993,6 +993,20 @@ add_to_code_trace(PyThreadState *tstate, _PyInterpreterFrame *frame, PyCodeObjec assert(tstate->interp->jit_state.code_curr_size < UOP_MAX_TRACE_LENGTH); return !_PyJit_translate_single_bytecode_to_trace(tstate, frame, this_instr, next_instr, old_code, old_func, old_stack_level, opcode, oparg, jump_taken); } + + +// 0 for success, -1 for error. +static int +bail_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame) +{ + int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); + int err = 0; + if (!_PyErr_Occurred(tstate) && !_is_sys_tracing) { + err = _PyOptimizer_Optimize(frame, tstate); + } + _PyJit_FinalizeTracing(tstate); + return err; +} #endif /* _PyEval_EvalFrameDefault is too large to optimize for speed with PGO on MSVC. diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 1d359461ffa0..e19a614f28de 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -144,32 +144,19 @@ DISPATCH_TABLE_VAR = TRACING_DISPATCH_TABLE; # define LEAVE_TRACING() \ DISPATCH_TABLE_VAR = DISPATCH_TABLE; -# define BAIL_TRACING_NO_DISPATCH() \ - do { \ - LEAVE_TRACING(); \ - if (!_PyErr_Occurred(tstate) && !_is_sys_tracing) { \ - _PyFrame_SetStackPointer(frame, stack_pointer); \ - int _err = _PyOptimizer_Optimize(frame, tstate); \ - _PyJit_FinalizeTracing(tstate); \ - stack_pointer = _PyFrame_GetStackPointer(frame); \ - if (_err < 0) { \ - JUMP_TO_LABEL(error); \ - } \ - } \ - else { \ - _PyFrame_SetStackPointer(frame, stack_pointer); \ - _PyJit_FinalizeTracing(tstate); \ - stack_pointer = _PyFrame_GetStackPointer(frame); \ - } \ - } while (0); # define RECORD_TRACE_NO_DISPATCH() do { \ - int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); \ - if (_is_sys_tracing) { \ - LEAVE_TRACING(); \ - } \ - else if ((IS_JIT_TRACING() && add_to_code_trace(tstate, frame, old_code, old_func, _old_stack_level, this_instr, next_instr, opcode, oparg, _jump_taken))) { \ - BAIL_TRACING_NO_DISPATCH(); \ + int err = 0; \ + _PyFrame_SetStackPointer(frame, stack_pointer); \ + /* We need to check once more here in case it swapped out halfway. */ \ + if (IS_JIT_TRACING()) { \ + int full = add_to_code_trace(tstate, frame, old_code, old_func, _old_stack_level, this_instr, next_instr, opcode, oparg, _jump_taken); \ + if (full) { \ + LEAVE_TRACING(); \ + err = bail_tracing_and_jit(tstate, frame); \ + } \ } \ + stack_pointer = _PyFrame_GetStackPointer(frame); \ + if (err < 0) { JUMP_TO_LABEL(error); } \ } while (0); #endif diff --git a/Python/optimizer.c b/Python/optimizer.c index 8482162d4a32..f77ff500e2f9 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -572,6 +572,11 @@ _PyJit_translate_single_bytecode_to_trace( _PyUOpInstruction *trace = tstate->interp->jit_state.code_buffer; int max_length = tstate->interp->jit_state.code_max_size; + int is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); + if (is_sys_tracing) { + goto full; + } + #ifdef Py_DEBUG char *python_lltrace = Py_GETENV("PYTHON_LLTRACE"); int lltrace = 0; -- 2.47.3