From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 20 Sep 2025 22:55:09 +0000 (+0100) Subject: Fix handling of ENTER_EXECUTOR X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3066963b60873f08356c5917d2548182a651cb04;p=thirdparty%2FPython%2Fcpython.git Fix handling of ENTER_EXECUTOR --- diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 166383e4af6b..a377bb5a385f 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -3044,7 +3044,11 @@ dummy_func( } assert(executor != tstate->interp->cold_executor); tstate->jit_exit = NULL; - TIER1_TO_TIER2(executor); + if (IS_JIT_TRACING()) { + RECORD_TRACE(); + BAIL_TRACING_NO_DISPATCH(); + } + TIER1_TO_TIER2(executor, 1); #else Py_FatalError("ENTER_EXECUTOR is not supported in this build"); #endif /* _Py_TIER2 */ diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 32815ad8c1a5..47da16592453 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -134,13 +134,15 @@ # define IS_JIT_TRACING() (DISPATCH_TABLE_VAR == TRACING_DISPATCH_TABLE) # define ENTER_TRACING() DISPATCH_TABLE_VAR = TRACING_DISPATCH_TABLE; # define LEAVE_TRACING() DISPATCH_TABLE_VAR = DISPATCH_TABLE; -# define BAIL_TRACING() \ +# define BAIL_TRACING_NO_DISPATCH() \ LEAVE_TRACING(); \ int err = _PyOptimizer_Optimize(frame, tstate); \ tstate->interp->jit_tracer_code_curr_size = 0; \ if (err < 0) { \ JUMP_TO_LABEL(error); \ - } \ + } +# define BAIL_TRACING() \ + BAIL_TRACING_NO_DISPATCH() \ DISPATCH(); # define RECORD_TRACE() do { \ frame->instr_ptr = next_instr; \ @@ -395,7 +397,7 @@ _PyFrame_SetStackPointer(frame, stack_pointer) /* Tier-switching macros. */ -#define TIER1_TO_TIER2(EXECUTOR) \ +#define TIER1_TO_TIER2(EXECUTOR, IN_ENTER_EXECUTOR) \ do { \ OPT_STAT_INC(traces_executed); \ next_instr = _Py_jit_entry((EXECUTOR), frame, stack_pointer, tstate); \ @@ -407,7 +409,7 @@ do { \ next_instr = frame->instr_ptr; \ JUMP_TO_LABEL(error); \ } \ - if (keep_tracing_bit) { \ + if (!IN_ENTER_EXECUTOR && keep_tracing_bit) { \ ENTER_TRACING(); \ _PyJIT_InitializeTracing(tstate, frame, next_instr, STACK_LEVEL(), 0); \ } \ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b612a51bc056..4183e23cdf18 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -5809,7 +5809,11 @@ } assert(executor != tstate->interp->cold_executor); tstate->jit_exit = NULL; - TIER1_TO_TIER2(executor); + if (IS_JIT_TRACING()) { + RECORD_TRACE(); + BAIL_TRACING_NO_DISPATCH(); + } + TIER1_TO_TIER2(executor, 1); #else Py_FatalError("ENTER_EXECUTOR is not supported in this build"); #endif /* _Py_TIER2 */ @@ -18942,7 +18946,11 @@ } assert(executor != tstate->interp->cold_executor); tstate->jit_exit = NULL; - TIER1_TO_TIER2(executor); + if (IS_JIT_TRACING()) { + RECORD_TRACE(); + BAIL_TRACING_NO_DISPATCH(); + } + TIER1_TO_TIER2(executor, 1); #else Py_FatalError("ENTER_EXECUTOR is not supported in this build"); #endif /* _Py_TIER2 */ diff --git a/Python/optimizer.c b/Python/optimizer.c index f0b207f721cd..b973fdd7ac41 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -585,7 +585,6 @@ _PyJIT_translate_single_bytecode_to_trace( if (opcode == EXTENDED_ARG) { return 1; } - assert(opcode != ENTER_EXECUTOR && opcode != EXTENDED_ARG); if (opcode == NOP) { return 1; } @@ -594,6 +593,8 @@ _PyJIT_translate_single_bytecode_to_trace( goto full; } + assert(opcode != ENTER_EXECUTOR && opcode != EXTENDED_ARG); + bool needs_guard_ip = _PyOpcode_NeedsGuardIp[opcode] && !(opcode == FOR_ITER_RANGE || opcode == FOR_ITER_LIST || opcode == FOR_ITER_TUPLE) && !(opcode == JUMP_BACKWARD_NO_INTERRUPT || opcode == JUMP_BACKWARD || opcode == JUMP_BACKWARD_JIT) &&