From d11494423e7a96c941f291270b24d48a0c7fb352 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 18 Oct 2025 21:56:18 +0100 Subject: [PATCH] Cleanup, bugfixes to sys trace --- .gitattributes | 1 + Objects/frameobject.c | 5 +++++ Python/bytecodes.c | 7 +++++-- Python/ceval_macros.h | 7 +++++-- Python/generated_cases.c.h | 7 +++++-- Python/generated_tracer_cases.c.h | 7 +++++-- Python/instrumentation.c | 2 ++ Python/optimizer.c | 7 +++---- Tools/c-analyzer/cpython/_parser.py | 1 + 9 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.gitattributes b/.gitattributes index 823e3e975a23..58cd96c0d329 100644 --- a/.gitattributes +++ b/.gitattributes @@ -97,6 +97,7 @@ Programs/test_frozenmain.h generated Python/Python-ast.c generated Python/executor_cases.c.h generated Python/generated_cases.c.h generated +Python/generated_tracer_cases.c.h generated Python/optimizer_cases.c.h generated Python/opcode_targets.h generated Python/stdlib_module_names.h generated diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 0cae3703d1d0..33e57fbfc245 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -17,6 +17,8 @@ #include "frameobject.h" // PyFrameLocalsProxyObject #include "opcode.h" // EXTENDED_ARG +#include "../Include/pytypedefs.h" +#include "../Include/internal/pycore_optimizer.h" #include "clinic/frameobject.c.h" @@ -260,7 +262,10 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) return -1; } +#if _Py_TIER2 _Py_Executors_InvalidateDependency(PyInterpreterState_Get(), co, 1); + _Py_JITTracer_InvalidateDependency(PyThreadState_GET(), co); +#endif _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); _PyStackRef oldvalue = fast[i]; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 5b47782ea600..08fc82a7dd34 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2971,8 +2971,11 @@ dummy_func( DISPATCH(); } } - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); - ENTER_TRACING(); + 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); + ENTER_TRACING(); + } // Don't add the JUMP_BACKWARD_JIT instruction to the trace. DISPATCH(); } diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index f68e84e97389..39a57a6f830c 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -161,8 +161,11 @@ } \ } while (0); # define RECORD_TRACE_NO_DISPATCH() do { \ - int _is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc == NULL); \ - if (_is_sys_tracing || IS_JIT_TRACING() && add_to_code_trace(tstate, frame, old_code, old_func, this_instr, next_instr, opcode, oparg, _jump_taken)) { \ + 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, this_instr, next_instr, opcode, oparg, _jump_taken))) { \ BAIL_TRACING_NO_DISPATCH(); \ } \ } while (0); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index c47b6bec7b27..3595fb6d9376 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -7668,8 +7668,11 @@ DISPATCH(); } } - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); - ENTER_TRACING(); + 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); + ENTER_TRACING(); + } DISPATCH(); } else { diff --git a/Python/generated_tracer_cases.c.h b/Python/generated_tracer_cases.c.h index 1871af274084..1f0812677418 100644 --- a/Python/generated_tracer_cases.c.h +++ b/Python/generated_tracer_cases.c.h @@ -8711,8 +8711,11 @@ TRACING_DISPATCH(); } } - _PyJIT_InitializeTracing(tstate, frame, this_instr, STACK_LEVEL(), 0, NULL); - ENTER_TRACING(); + 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); + ENTER_TRACING(); + } TRACING_DISPATCH(); } else { diff --git a/Python/instrumentation.c b/Python/instrumentation.c index b4b2bc5dc69f..325acc34a1b5 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -18,6 +18,7 @@ #include "pycore_tuple.h" // _PyTuple_FromArraySteal() #include "opcode_ids.h" +#include "../Include/internal/pycore_optimizer.h" /* Uncomment this to dump debugging output when assertions fail */ @@ -1785,6 +1786,7 @@ force_instrument_lock_held(PyCodeObject *code, PyInterpreterState *interp) _PyCode_Clear_Executors(code); } _Py_Executors_InvalidateDependency(interp, code, 1); + _Py_JITTracer_InvalidateDependency(PyThreadState_GET(), code); #endif int code_len = (int)Py_SIZE(code); /* Exit early to avoid creating instrumentation diff --git a/Python/optimizer.c b/Python/optimizer.c index f3166afd7d59..8b16edab7cd2 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -649,8 +649,6 @@ _PyJIT_translate_single_bytecode_to_trace( const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode]; - RESERVE_RAW(expansion->nuops + needs_guard_ip + 3, "uop and various checks"); - ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0, target); assert(opcode != ENTER_EXECUTOR && opcode != EXTENDED_ARG); @@ -686,6 +684,9 @@ _PyJIT_translate_single_bytecode_to_trace( max_length--; } + RESERVE_RAW(expansion->nuops + needs_guard_ip + 3, "uop and various checks"); + + switch (opcode) { case POP_JUMP_IF_NONE: case POP_JUMP_IF_NOT_NONE: @@ -1543,7 +1544,6 @@ _Py_JITTracer_InvalidateDependency(PyThreadState *old_tstate, void *obj) _PyBloomFilter obj_filter; _Py_BloomFilter_Init(&obj_filter); _Py_BloomFilter_Add(&obj_filter, obj); - HEAD_LOCK(&_PyRuntime); PyInterpreterState *interp = old_tstate->interp; @@ -1554,7 +1554,6 @@ _Py_JITTracer_InvalidateDependency(PyThreadState *old_tstate, void *obj) } } - HEAD_UNLOCK(&_PyRuntime); } /* Invalidate all executors */ void diff --git a/Tools/c-analyzer/cpython/_parser.py b/Tools/c-analyzer/cpython/_parser.py index 17c56c9e8762..bee987cb53ad 100644 --- a/Tools/c-analyzer/cpython/_parser.py +++ b/Tools/c-analyzer/cpython/_parser.py @@ -78,6 +78,7 @@ EXCLUDED = format_conf_lines([ 'Python/deepfreeze/*.c', 'Python/frozen_modules/*.h', 'Python/generated_cases.c.h', + 'Python/generated_tracer_cases.c.h', 'Python/executor_cases.c.h', 'Python/optimizer_cases.c.h', 'Python/opcode_targets.h', -- 2.47.3