Skip to content

bpo-47120: make JUMP_NO_INTERRUPT relative #32221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 5, 2022
16 changes: 8 additions & 8 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,14 @@ iterations of the loop.

.. opcode:: JUMP_BACKWARD (delta)

Decrements bytecode counter by *delta*.
Decrements bytecode counter by *delta*. Checks for interrupts.

.. versionadded:: 3.11


.. opcode:: JUMP_BACKWARD_NO_INTERRUPT (delta)

Decrements bytecode counter by *delta*. Does not check for interrupts.

.. versionadded:: 3.11

Expand Down Expand Up @@ -974,13 +981,6 @@ iterations of the loop.
.. versionadded:: 3.1


.. opcode:: JUMP_NO_INTERRUPT (target)

Set bytecode counter to *target*. Do not check for interrupts.

.. versionadded:: 3.11


.. opcode:: FOR_ITER (delta)

TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ CPython bytecode changes

* Replaced :opcode:`JUMP_ABSOLUTE` by the relative :opcode:`JUMP_BACKWARD`.

* Added :opcode:`JUMP_BACKWARD_NO_INTERRUPT`, which is used in certain loops where it is undesirable to handle interrupts.

Deprecated
==========

Expand Down
6 changes: 3 additions & 3 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a6 3488 (LOAD_GLOBAL can push additional NULL)
# Python 3.11a6 3489 (Add JUMP_BACKWARD, remove JUMP_ABSOLUTE)
# Python 3.11a6 3490 (remove JUMP_IF_NOT_EXC_MATCH, add CHECK_EXC_MATCH)
# Python 3.11a6 3491 (remove JUMP_IF_NOT_EG_MATCH, add CHECK_EG_MATCH)
# Python 3.11a6 3491 (remove JUMP_IF_NOT_EG_MATCH, add CHECK_EG_MATCH,
# add JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual)

# Python 3.12 will start with magic number 3500

Expand Down
2 changes: 1 addition & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def jabs_op(name, op, entries=0):
def_op('GET_AWAITABLE', 131)
def_op('MAKE_FUNCTION', 132) # Flags
def_op('BUILD_SLICE', 133) # Number of items
jabs_op('JUMP_NO_INTERRUPT', 134) # Target byte offset from beginning of code
jrel_op('JUMP_BACKWARD_NO_INTERRUPT', 134) # Number of words to skip (backwards)
def_op('MAKE_CELL', 135)
hasfree.append(135)
def_op('LOAD_CLOSURE', 136)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ def test_boundaries(self):
def test_widths(self):
for opcode, opname in enumerate(dis.opname):
if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
'BUILD_TUPLE_UNPACK_WITH_CALL'):
'BUILD_TUPLE_UNPACK_WITH_CALL',
'JUMP_BACKWARD_NO_INTERRUPT'):
continue
with self.subTest(opname=opname):
width = dis._OPNAME_WIDTH
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace the absolute jump opcode :opcode:`JUMP_NO_INTERRUPT` by the relative :opcode:`JUMP_BACKWARD_NO_INTERRUPT`.
14 changes: 5 additions & 9 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,6 @@ mark_stacks(PyCodeObject *code_obj, int len)
stacks[i+1] = next_stack;
break;
}
case JUMP_NO_INTERRUPT:
j = get_arg(code, i);
assert(j < len);
if (stacks[j] == UNINITIALIZED && j < i) {
todo = 1;
}
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
case POP_EXCEPT:
next_stack = pop_value(pop_value(pop_value(next_stack)));
stacks[i+1] = next_stack;
Expand All @@ -256,8 +247,13 @@ mark_stacks(PyCodeObject *code_obj, int len)
stacks[j] = next_stack;
break;
case JUMP_BACKWARD:
case JUMP_BACKWARD_NO_INTERRUPT:
j = i + 1 - get_arg(code, i);
assert(j >= 0);
assert(j < len);
if (stacks[j] == UNINITIALIZED && j < i) {
todo = 1;
}
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4045,13 +4045,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
DISPATCH();
}

TARGET(JUMP_NO_INTERRUPT) {
TARGET(JUMP_BACKWARD_NO_INTERRUPT) {
/* This bytecode is used in the `yield from` or `await` loop.
* If there is an interrupt, we want it handled in the innermost
* generator or coroutine, so we deliberately do not check it here.
* (see bpo-30039).
*/
JUMPTO(oparg);
JUMPBY(-oparg);
DISPATCH();
}

Expand Down
49 changes: 29 additions & 20 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,23 @@
#define SETUP_WITH -3
#define POP_BLOCK -4
#define JUMP -5
#define JUMP_NO_INTERRUPT -6

#define MIN_VIRTUAL_OPCODE -5
#define MIN_VIRTUAL_OPCODE -6
#define MAX_ALLOWED_OPCODE 254

#define IS_WITHIN_OPCODE_RANGE(opcode) \
((opcode) >= MIN_VIRTUAL_OPCODE && (opcode) <= MAX_ALLOWED_OPCODE)

#define IS_VIRTUAL_OPCODE(opcode) ((opcode) < 0)

/* opcodes which are not emitted in codegen stage, only by the assembler */
#define IS_ASSEMBLER_OPCODE(opcode) \
((opcode) == JUMP_FORWARD || \
(opcode) == JUMP_BACKWARD || \
(opcode) == JUMP_BACKWARD_NO_INTERRUPT)


#define IS_TOP_LEVEL_AWAIT(c) ( \
(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
&& (c->u->u_ste->ste_type == ModuleBlock))
Expand Down Expand Up @@ -1011,6 +1019,7 @@ stack_effect(int opcode, int oparg, int jump)
case JUMP_FORWARD:
case JUMP_BACKWARD:
case JUMP:
case JUMP_BACKWARD_NO_INTERRUPT:
case JUMP_NO_INTERRUPT:
return 0;

Expand Down Expand Up @@ -1199,6 +1208,7 @@ compiler_addop_line(struct compiler *c, int opcode, int line,
int end_line, int col_offset, int end_col_offset)
{
assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
assert(!HAS_ARG(opcode) || IS_ARTIFICIAL(opcode));

if (compiler_use_new_implicit_block_if_needed(c) < 0) {
Expand Down Expand Up @@ -1442,6 +1452,7 @@ compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg,
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */

assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
assert(HAS_ARG(opcode));
assert(0 <= oparg && oparg <= 2147483647);

Expand Down Expand Up @@ -1486,6 +1497,7 @@ static int add_jump_to_block(struct compiler *c, int opcode,
basicblock *target)
{
assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
assert(HAS_ARG(opcode) || IS_VIRTUAL_OPCODE(opcode));
assert(target != NULL);

Expand Down Expand Up @@ -7089,8 +7101,7 @@ stackdepth(struct compiler *c)
stackdepth_push(&sp, instr->i_target, target_depth);
}
depth = new_depth;
assert(instr->i_opcode != JUMP_FORWARD);
assert(instr->i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(instr->i_opcode));
if (instr->i_opcode == JUMP_NO_INTERRUPT ||
instr->i_opcode == JUMP ||
instr->i_opcode == RETURN_VALUE ||
Expand Down Expand Up @@ -7597,15 +7608,15 @@ normalize_jumps(struct assembler *a)
continue;
}
struct instr *last = &b->b_instr[b->b_iused-1];
assert(last->i_opcode != JUMP_FORWARD);
assert(last->i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
if (last->i_opcode == JUMP) {
if (last->i_target->b_visited == 0) {
last->i_opcode = JUMP_FORWARD;
}
else {
last->i_opcode = JUMP_BACKWARD;
}
bool is_forward = last->i_target->b_visited == 0;
last->i_opcode = is_forward ? JUMP_FORWARD : JUMP_BACKWARD;
}
if (last->i_opcode == JUMP_NO_INTERRUPT) {
bool is_forward = last->i_target->b_visited == 0;
last->i_opcode = is_forward ?
JUMP_FORWARD : JUMP_BACKWARD_NO_INTERRUPT;
}
}
}
Expand Down Expand Up @@ -7641,11 +7652,13 @@ assemble_jump_offsets(struct assembler *a, struct compiler *c)
instr->i_oparg = instr->i_target->b_offset;
if (is_relative_jump(instr)) {
if (instr->i_oparg < bsize) {
assert(instr->i_opcode == JUMP_BACKWARD);
assert(instr->i_opcode == JUMP_BACKWARD ||
instr->i_opcode == JUMP_BACKWARD_NO_INTERRUPT);
instr->i_oparg = bsize - instr->i_oparg;
}
else {
assert(instr->i_opcode != JUMP_BACKWARD);
assert(instr->i_opcode != JUMP_BACKWARD_NO_INTERRUPT);
instr->i_oparg -= bsize;
}
}
Expand Down Expand Up @@ -8667,14 +8680,12 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
inst->i_target = inst->i_target->b_next;
}
target = &inst->i_target->b_instr[0];
assert(target->i_opcode != JUMP_FORWARD);
assert(target->i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(target->i_opcode));
}
else {
target = &nop;
}
assert(inst->i_opcode != JUMP_FORWARD);
assert(inst->i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(inst->i_opcode));
switch (inst->i_opcode) {
/* Remove LOAD_CONST const; conditional jump */
case LOAD_CONST:
Expand Down Expand Up @@ -8975,8 +8986,7 @@ normalize_basic_block(basicblock *bb) {
/* Mark blocks as exit and/or nofallthrough.
Raise SystemError if CFG is malformed. */
for (int i = 0; i < bb->b_iused; i++) {
assert(bb->b_instr[i].i_opcode != JUMP_FORWARD);
assert(bb->b_instr[i].i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(bb->b_instr[i].i_opcode));
switch(bb->b_instr[i].i_opcode) {
case RETURN_VALUE:
case RAISE_VARARGS:
Expand Down Expand Up @@ -9163,8 +9173,7 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
if (b->b_iused > 0) {
struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
assert(b_last_instr->i_opcode != JUMP_FORWARD);
assert(b_last_instr->i_opcode != JUMP_BACKWARD);
assert(!IS_ASSEMBLER_OPCODE(b_last_instr->i_opcode));
if (b_last_instr->i_opcode == JUMP ||
b_last_instr->i_opcode == JUMP_NO_INTERRUPT) {
if (b_last_instr->i_target == b->b_next) {
Expand Down
2 changes: 1 addition & 1 deletion Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.