gh-106529: Support JUMP_BACKWARD in Tier 2 (uops) (#106543) · python/cpython@cabd6e8 · GitHub
Skip to content

Commit cabd6e8

Browse files
authored
gh-106529: Support JUMP_BACKWARD in Tier 2 (uops) (#106543)
During superblock generation, a JUMP_BACKWARD instruction is translated to either a JUMP_TO_TOP micro-op (when the target of the jump is exactly the beginning of the superblock, closing the loop), or a SAVE_IP + EXIT_TRACE pair, when the jump goes elsewhere. The new JUMP_TO_TOP instruction includes a CHECK_EVAL_BREAKER() call, so a closed loop can still be interrupted.
1 parent 292ac4b commit cabd6e8

5 files changed

Lines changed: 63 additions & 30 deletions

File tree

Lib/test/test_capi/test_misc.py

Lines changed: 14 additions & 2 deletions

Python/ceval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,13 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
27832783
break;
27842784
}
27852785

2786+
case JUMP_TO_TOP:
2787+
{
2788+
pc = 0;
2789+
CHECK_EVAL_BREAKER();
2790+
break;
2791+
}
2792+
27862793
case SAVE_IP:
27872794
{
27882795
frame->prev_instr = ip_offset + oparg;

Python/opcode_metadata.h

Lines changed: 28 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,7 @@ translate_bytecode_to_trace(
372372
_PyUOpInstruction *trace,
373373
int buffer_size)
374374
{
375-
#ifdef Py_DEBUG
376375
_Py_CODEUNIT *initial_instr = instr;
377-
#endif
378376
int trace_length = 0;
379377
int max_length = buffer_size;
380378

@@ -456,6 +454,19 @@ translate_bytecode_to_trace(
456454
break;
457455
}
458456

457+
case JUMP_BACKWARD:
458+
{
459+
if (instr + 2 - oparg == initial_instr
460+
&& trace_length + 3 <= max_length)
461+
{
462+
ADD_TO_TRACE(JUMP_TO_TOP, 0);
463+
}
464+
else {
465+
DPRINTF(2, "JUMP_BACKWARD not to top ends trace\n");
466+
}
467+
goto done;
468+
}
469+
459470
default:
460471
{
461472
const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode];

Tools/cases_generator/generate_cases.py

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)