]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close loops
authorKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Mon, 20 Oct 2025 02:51:41 +0000 (03:51 +0100)
committerKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Mon, 20 Oct 2025 02:51:41 +0000 (03:51 +0100)
Include/internal/pycore_interp_structs.h
Include/internal/pycore_optimizer.h
Python/bytecodes.c
Python/executor_cases.c.h
Python/generated_cases.c.h
Python/generated_tracer_cases.c.h
Python/optimizer.c
Python/pystate.c

index 44f8a23fd4727818e5670d41496b9f42ba46ddda..fdbba611c2dec2ac3e1e1867b28574d83b2127c2 100644 (file)
@@ -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
index d9a47cf056f96be248ebc2feff4e581d51a3d5f7..b06593377324e34509802150e55cb28e4bf9e206 100644 (file)
@@ -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);
 
index 9ef58978d88f17af694b81dfbbb671a391917b89..0ba8286fc467d745e7c4133a83943afc4ed0c95e 100644 (file)
@@ -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);
             }
index e32c144b9e9132e7ed453edfb720f7f671c80d07..03846f48d538dace41ecb56f5dfbf000095ea83e 100644 (file)
                 _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);
             }
index b9ba4ca57cbef5f96db715d7b97e378a63ef6d70..28691b2c5c836d6dc6805b703ceb75dde02b4c79 100644 (file)
                     }
                     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;
index ff7b91cc239e7eea9298993c902a1ea2fe9015c9..cdd7246fbb4a0628070a5b948075db4da460e07e 100644 (file)
                     }
                     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;
index f20b669cfb859d08b3364013df98e108ec340800..2fcf6b763691acb14618a014221e3dec90629dd7 100644 (file)
@@ -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;
index 01eab6d3f5ea1b33a3834b60f0e40340e804f61a..f0790322abd1ffc34abaeb06d34dd2d75fbc1a09 100644 (file)
@@ -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;