From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 20 Oct 2025 02:51:41 +0000 (+0100) Subject: Close loops X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=754b3b78fe9275229f4ee653eb261af3cf69ca7f;p=thirdparty%2FPython%2Fcpython.git Close loops --- diff --git a/Include/internal/pycore_interp_structs.h b/Include/internal/pycore_interp_structs.h index 44f8a23fd472..fdbba611c2de 100644 --- a/Include/internal/pycore_interp_structs.h +++ b/Include/internal/pycore_interp_structs.h @@ -763,7 +763,8 @@ typedef struct _PyJitState { _PyBloomFilter jit_tracer_dependencies; bool jit_tracer_dependencies_still_valid; _PyUOpInstruction *jit_tracer_code_buffer; - _Py_CODEUNIT *jit_tracer_initial_instr; + _Py_CODEUNIT *jit_tracer_insert_exec_instr; + _Py_CODEUNIT *jit_tracer_close_loop_instr; int jit_tracer_initial_stack_depth; int jit_tracer_initial_chain_depth; PyCodeObject *jit_tracer_initial_code; // Strong diff --git a/Include/internal/pycore_optimizer.h b/Include/internal/pycore_optimizer.h index d9a47cf056f9..b06593377324 100644 --- a/Include/internal/pycore_optimizer.h +++ b/Include/internal/pycore_optimizer.h @@ -373,7 +373,7 @@ _PyJIT_translate_single_bytecode_to_trace( int jump_taken); void -_PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *next_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit); +_PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *insert_exec_instr, _Py_CODEUNIT *close_loop_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit); void _PyJIT_FinalizeTracing(PyThreadState *tstate); diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 9ef58978d88f..0ba8286fc467 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) { - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); + _PyJIT_InitializeTracing(tstate, frame, this_instr, next_instr, STACK_LEVEL(), 0, NULL); ENTER_TRACING(); } int _jump_taken = false; @@ -5451,7 +5451,7 @@ dummy_func( _PyExecutorObject *previous_executor = _PyExecutor_FromExit(exit); assert(tstate->current_executor == (PyObject *)previous_executor); int chain_depth = previous_executor->vm_data.chain_depth + 1; - _PyJIT_InitializeTracing(tstate, frame, target, STACK_LEVEL(), chain_depth, exit); + _PyJIT_InitializeTracing(tstate, frame, target, target, STACK_LEVEL(), chain_depth, exit); exit->temperature = initial_temperature_backoff_counter(); GOTO_TIER_ONE(target, 1); } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index e32c144b9e91..03846f48d538 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -7502,7 +7502,7 @@ _PyExecutorObject *previous_executor = _PyExecutor_FromExit(exit); assert(tstate->current_executor == (PyObject *)previous_executor); int chain_depth = previous_executor->vm_data.chain_depth + 1; - _PyJIT_InitializeTracing(tstate, frame, target, STACK_LEVEL(), chain_depth, exit); + _PyJIT_InitializeTracing(tstate, frame, target, target, STACK_LEVEL(), chain_depth, exit); exit->temperature = initial_temperature_backoff_counter(); GOTO_TIER_ONE(target, 1); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b9ba4ca57cbe..28691b2c5c83 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -7671,7 +7671,7 @@ } int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); if (!_is_sys_tracing) { - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); + _PyJIT_InitializeTracing(tstate, frame, this_instr, next_instr, STACK_LEVEL(), 0, NULL); ENTER_TRACING(); } int _jump_taken = false; diff --git a/Python/generated_tracer_cases.c.h b/Python/generated_tracer_cases.c.h index ff7b91cc239e..cdd7246fbb4a 100644 --- a/Python/generated_tracer_cases.c.h +++ b/Python/generated_tracer_cases.c.h @@ -8956,7 +8956,7 @@ } int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL); if (!_is_sys_tracing) { - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); + _PyJIT_InitializeTracing(tstate, frame, this_instr, next_instr, STACK_LEVEL(), 0, NULL); ENTER_TRACING(); } int _jump_taken = false; diff --git a/Python/optimizer.c b/Python/optimizer.c index f20b669cfb85..2fcf6b763691 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -131,7 +131,7 @@ _PyOptimizer_Optimize( chain_depth %= MAX_CHAIN_DEPTH; bool progress_needed = chain_depth == 0; PyCodeObject *code = (PyCodeObject *)tstate->interp->jit_state.jit_tracer_initial_code; - _Py_CODEUNIT *start = tstate->interp->jit_state.jit_tracer_initial_instr; + _Py_CODEUNIT *start = tstate->interp->jit_state.jit_tracer_insert_exec_instr; // A recursive trace might've cleared the values. In that case, bail. if (code == NULL) { interp->compiling = false; @@ -570,7 +570,7 @@ _PyJIT_translate_single_bytecode_to_trace( int jump_taken) { - int is_first_instr = tstate->interp->jit_state.jit_tracer_initial_instr == this_instr; + int is_first_instr = tstate->interp->jit_state.jit_tracer_close_loop_instr == this_instr; bool progress_needed = (tstate->interp->jit_state.jit_tracer_initial_chain_depth % MAX_CHAIN_DEPTH) == 0;; _PyBloomFilter *dependencies = &tstate->interp->jit_state.jit_tracer_dependencies; _Py_BloomFilter_Add(dependencies, old_code); @@ -686,7 +686,7 @@ _PyJIT_translate_single_bytecode_to_trace( } // Loop back to the start - if (is_first_instr && tstate->interp->jit_state.jit_tracer_code_curr_size > 2) { + if (is_first_instr && tstate->interp->jit_state.jit_tracer_code_curr_size > 5) { ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0, 0); goto done; } @@ -876,7 +876,7 @@ full: } void -_PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *next_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit) +_PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *insert_exec_instr, _Py_CODEUNIT *close_loop_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit) { PyCodeObject *code = _PyFrame_GetCode(frame); #ifdef Py_DEBUG @@ -890,14 +890,15 @@ _PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_ PyUnicode_AsUTF8(code->co_qualname), PyUnicode_AsUTF8(code->co_filename), code->co_firstlineno, - 2 * INSTR_IP(next_instr, code), + 2 * INSTR_IP(close_loop_instr, code), chain_depth); #endif - add_to_trace(tstate->interp->jit_state.jit_tracer_code_buffer, 0, _START_EXECUTOR, 0, (uintptr_t)next_instr, INSTR_IP(next_instr, code)); + add_to_trace(tstate->interp->jit_state.jit_tracer_code_buffer, 0, _START_EXECUTOR, 0, (uintptr_t)insert_exec_instr, INSTR_IP(insert_exec_instr, code)); add_to_trace(tstate->interp->jit_state.jit_tracer_code_buffer, 1, _MAKE_WARM, 0, 0, 0); tstate->interp->jit_state.jit_tracer_code_curr_size = 2; tstate->interp->jit_state.jit_tracer_code_max_size = UOP_MAX_TRACE_LENGTH; - tstate->interp->jit_state.jit_tracer_initial_instr = next_instr; + tstate->interp->jit_state.jit_tracer_insert_exec_instr = insert_exec_instr; + tstate->interp->jit_state.jit_tracer_close_loop_instr = close_loop_instr; tstate->interp->jit_state.jit_tracer_initial_code = (PyCodeObject *)Py_NewRef(code); tstate->interp->jit_state.jit_tracer_initial_func = (PyFunctionObject *)Py_NewRef(_PyFrame_GetFunction(frame)); tstate->interp->jit_state.jit_tracer_previous_exit = exit; diff --git a/Python/pystate.c b/Python/pystate.c index 01eab6d3f5ea..f0790322abd1 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -557,7 +557,6 @@ init_interpreter(PyInterpreterState *interp, #ifdef _Py_TIER2 interp->jit_state.jit_tracer_code_buffer = NULL; - interp->jit_state.jit_tracer_initial_instr = NULL; interp->jit_state.jit_tracer_initial_stack_depth = -1; interp->jit_state.jit_tracer_initial_chain_depth = -1; interp->jit_state.jit_tracer_initial_code = NULL;