Skip to content

Commit f6eafe1

Browse files
authored
Normalize jumps in compiler. All forward jumps to use JUMP_FORWARD. (pythonGH-28755)
1 parent c379bc5 commit f6eafe1

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

Lib/test/test_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def _prepare_test_cases():
10461046
Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False, positions=None),
10471047
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False, positions=None),
10481048
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False, positions=None),
1049-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False, positions=None),
1049+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=5, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False, positions=None),
10501050
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True, positions=None),
10511051
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True, positions=None),
10521052
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False, positions=None),
@@ -1071,7 +1071,7 @@ def _prepare_test_cases():
10711071
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False, positions=None),
10721072
Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False, positions=None),
10731073
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False, positions=None),
1074-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False, positions=None),
1074+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=6, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False, positions=None),
10751075
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True, positions=None),
10761076
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False, positions=None),
10771077
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True, positions=None),

Python/ceval.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,19 +4051,18 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
40514051

40524052
TARGET(JUMP_ABSOLUTE) {
40534053
PREDICTED(JUMP_ABSOLUTE);
4054-
if (oparg < INSTR_OFFSET()) {
4055-
/* Increment the warmup counter and quicken if warm enough
4056-
* _Py_Quicken is idempotent so we don't worry about overflow */
4057-
if (!PyCodeObject_IsWarmedUp(co)) {
4058-
PyCodeObject_IncrementWarmup(co);
4059-
if (PyCodeObject_IsWarmedUp(co)) {
4060-
if (_Py_Quicken(co)) {
4061-
goto error;
4062-
}
4063-
int nexti = INSTR_OFFSET();
4064-
first_instr = co->co_firstinstr;
4065-
next_instr = first_instr + nexti;
4054+
assert(oparg < INSTR_OFFSET());
4055+
/* Increment the warmup counter and quicken if warm enough
4056+
* _Py_Quicken is idempotent so we don't worry about overflow */
4057+
if (!PyCodeObject_IsWarmedUp(co)) {
4058+
PyCodeObject_IncrementWarmup(co);
4059+
if (PyCodeObject_IsWarmedUp(co)) {
4060+
if (_Py_Quicken(co)) {
4061+
goto error;
40664062
}
4063+
int nexti = INSTR_OFFSET();
4064+
first_instr = co->co_firstinstr;
4065+
next_instr = first_instr + nexti;
40674066
}
40684067
}
40694068
JUMPTO(oparg);
@@ -4072,6 +4071,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
40724071
}
40734072

40744073
TARGET(JUMP_ABSOLUTE_QUICK) {
4074+
assert(oparg < INSTR_OFFSET());
40754075
JUMPTO(oparg);
40764076
CHECK_EVAL_BREAKER();
40774077
DISPATCH();

Python/compile.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7220,6 +7220,26 @@ assemble_emit(struct assembler *a, struct instr *i)
72207220
return 1;
72217221
}
72227222

7223+
static void
7224+
normalize_jumps(struct assembler *a)
7225+
{
7226+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7227+
b->b_visited = 0;
7228+
}
7229+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7230+
b->b_visited = 1;
7231+
if (b->b_iused == 0) {
7232+
continue;
7233+
}
7234+
struct instr *last = &b->b_instr[b->b_iused-1];
7235+
if (last->i_opcode == JUMP_ABSOLUTE &&
7236+
last->i_target->b_visited == 0
7237+
) {
7238+
last->i_opcode = JUMP_FORWARD;
7239+
}
7240+
}
7241+
}
7242+
72237243
static void
72247244
assemble_jump_offsets(struct assembler *a, struct compiler *c)
72257245
{
@@ -7897,6 +7917,9 @@ assemble(struct compiler *c, int addNone)
78977917
clean_basic_block(b);
78987918
}
78997919

7920+
/* Order of basic blocks must have been determined by now */
7921+
normalize_jumps(&a);
7922+
79007923
/* Can't modify the bytecode after computing jump offsets. */
79017924
assemble_jump_offsets(&a, c);
79027925

0 commit comments

Comments
 (0)