Skip to content

Commit ab7527c

Browse files
Fix the counters
1 parent 4aab2df commit ab7527c

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

Include/internal/pycore_backoff.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ initial_unreachable_backoff_counter(void)
129129
return make_backoff_counter(0, UNREACHABLE_BACKOFF);
130130
}
131131

132+
// Required to not get stuck in infinite specialization loops due to specialization failure.
133+
// We use 2 here as tnere are a few scenarios:
134+
// 1. Freshly specialized from unspecialized, in which case the counter will be 1.
135+
// 2. Re-specialized from deopt, in which case the counter will be 1.
136+
// 3. Deopt -> Specialize -> Deopt -> Specialize, in which case the counter will be 2.
137+
// We do not want the 3rd case.
138+
#define MAX_SPECIALIZATION_TRIES 2
139+
132140
#ifdef __cplusplus
133141
}
134142
#endif

Python/ceval_macros.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@
135135

136136
#if (_Py_TAIL_CALL_INTERP || USE_COMPUTED_GOTOS) && _Py_TIER2
137137
# define IS_JIT_TRACING() (DISPATCH_TABLE_VAR == TRACING_DISPATCH_TABLE)
138-
// Required to not get stuck in infinite specialization loops due to specialization failure.
139-
# define IS_JIT_TRACING_MAKING_PROGRESS() (IS_JIT_TRACING() && tstate->interp->jit_state.specialize_counter < 1)
138+
# define IS_JIT_TRACING_MAKING_PROGRESS() (IS_JIT_TRACING() && tstate->interp->jit_state.specialize_counter < MAX_SPECIALIZATION_TRIES)
140139
# define ENTER_TRACING() \
141140
DISPATCH_TABLE_VAR = TRACING_DISPATCH_TABLE;
142141
# define LEAVE_TRACING() \

Python/optimizer.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,14 @@ _PyJit_translate_single_bytecode_to_trace(
557557
_Py_CODEUNIT *next_instr)
558558
{
559559

560+
#ifdef Py_DEBUG
561+
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
562+
int lltrace = 0;
563+
if (python_lltrace != NULL && *python_lltrace >= '0') {
564+
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
565+
}
566+
#endif
567+
560568
PyCodeObject *old_code = tstate->interp->jit_state.prev_instr_code;
561569
// Something else finalized the trace. This can happen in multi-threaded scenarios as our trace
562570
// addition from bytecode execution to here is not atomic.
@@ -576,13 +584,6 @@ _PyJit_translate_single_bytecode_to_trace(
576584
goto full;
577585
}
578586

579-
#ifdef Py_DEBUG
580-
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
581-
int lltrace = 0;
582-
if (python_lltrace != NULL && *python_lltrace >= '0') {
583-
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
584-
}
585-
#endif
586587
_Py_CODEUNIT *this_instr = tstate->interp->jit_state.prev_instr;
587588
_Py_CODEUNIT *target_instr = this_instr;
588589
uint32_t target = 0;
@@ -593,8 +594,8 @@ _PyJit_translate_single_bytecode_to_trace(
593594
// We must point to the first EXTENDED_ARG when deopting.
594595
int oparg = tstate->interp->jit_state.prev_instr_oparg;
595596
int opcode = this_instr->op.code;
596-
// Failed specialization twice in a row. Deopt!
597-
if (tstate->interp->jit_state.specialize_counter >= 1) {
597+
// Failed specialization many times. Deopt!
598+
if (tstate->interp->jit_state.specialize_counter >= MAX_SPECIALIZATION_TRIES) {
598599
opcode = _PyOpcode_Deopt[opcode];
599600
}
600601
int rewind_oparg = oparg;

0 commit comments

Comments
 (0)