diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 88077aacecbf5b..3d2da479d256c4 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -5966,6 +5966,20 @@ def testfunc(*args): # _POP_TOP_NOP is a sign the optimizer ran and didn't hit contradiction. self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 1) + def test_trace_does_not_reset_jump_backward_counter(self): + def inner(n): + for _ in range(n): + pass + + def outer(n): + for i in range(n): + if i == n - 1: + inner(4002) + + outer(4002) + self.assertIsNotNone(get_first_executor(outer)) + self.assertIsNotNone(get_first_executor(inner)) + def test_load_attr_property_frame(self): class B: @property diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 42ad83daa1ea46..58bd72427d8a48 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -6611,7 +6611,9 @@ dummy_func( if (_PyOpcode_Caches[_PyOpcode_Deopt[opcode]] // Branch opcodes use the cache for branch history, not // specialization counters. Don't reset it. - && !IS_CONDITIONAL_JUMP_OPCODE(opcode)) { + // JUMP_BACKWARD uses the cache for its JIT warmup counter. + && !IS_CONDITIONAL_JUMP_OPCODE(opcode) + && _PyOpcode_Deopt[opcode] != JUMP_BACKWARD) { (&next_instr[1])->counter = trigger_backoff_counter(); }