]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Clean up macros
authorKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Tue, 21 Oct 2025 21:11:44 +0000 (22:11 +0100)
committerKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Tue, 21 Oct 2025 21:11:44 +0000 (22:11 +0100)
Python/bytecodes.c
Python/ceval.c
Python/ceval_macros.h
Python/optimizer.c

index f8742e6212d795c14e74e06ac16b8fa178e2d67f..388da83dbc24bf8188a4150fe1581255ca045c12 100644 (file)
@@ -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;
index e8f1ed5f88b28dda1610f7f2aec91a6e9c63d47b..3ca8378294998cc020dd26eb8e80c1297f037ec0 100644 (file)
@@ -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.
index 1d359461ffa0678ac84137405c28a07a4af22bea..e19a614f28dec61da044e50b39663d5f118b148a 100644 (file)
     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
 
index 8482162d4a325c6266534c0e83dfa56c7877d526..f77ff500e2f9d4603d5a7645c3d4b881f587ea61 100644 (file)
@@ -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;