From 52f8e08a70cbe1d30ba82d614b8101fc5aa3ee41 Mon Sep 17 00:00:00 2001 From: Divya Date: Sat, 7 Nov 2026 20:29:57 +0530 Subject: [PATCH] Fix JUMP_BACKWARD JIT warmup counter reset --- Lib/test/test_capi/test_opt.py | 14 ++++++++++++++ Python/bytecodes.c | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 88077aacecbf5b1..3d2da479d256c45 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 42ad83daa1ea461..58bd72427d8a486 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(); }