Skip to content

bpo-42615: Delete redundant jump instructions that only bypass empty blocks #23733

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 8 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,34 @@ def test_big_dict_literal(self):
the_dict = "{" + ",".join(f"{x}:{x}" for x in range(dict_size)) + "}"
self.assertEqual(len(eval(the_dict)), dict_size)

def test_redundant_jump_in_if_else_break(self):
# Check if bytecode containing jumps that simply point to the next line
# is generated around if-else-break style structures. See bpo-42615.

def if_else_break():
val = 1
while True:
if val > 0:
val -= 1
else:
break
val = -1

INSTR_SIZE = 2
HANDLED_JUMPS = (
'POP_JUMP_IF_FALSE',
'POP_JUMP_IF_TRUE',
'JUMP_ABSOLUTE',
'JUMP_FORWARD',
)

for line, instr in enumerate(dis.Bytecode(if_else_break)):
if instr.opname == 'JUMP_FORWARD':
self.assertNotEqual(instr.arg, 0)
elif instr.opname in HANDLED_JUMPS:
self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)


class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object
# stays within reasonable bounds (see issue #21523 for an example
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ Grzegorz Grzywacz
Thomas Guettler
Yuyang Guo
Anuj Gupta
Om Gupta
Michael Guravage
Lars Gustäbel
Thomas Güttler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove jump commands made redundant by the deletion of unreachable bytecode
blocks
42 changes: 42 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6437,8 +6437,50 @@ optimize_cfg(struct assembler *a, PyObject *consts)
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
if (b->b_reachable == 0) {
b->b_iused = 0;
b->b_nofallthrough = 0;
}
}
/* Delete jump instructions made redundant by previous step. If a non-empty
block ends with a jump instruction, check if the next non-empty block
reached through normal flow control is the target of that jump. If it
is, then the jump instruction is redundant and can be deleted.
*/
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];
if (b_last_instr->i_opcode == POP_JUMP_IF_FALSE ||
b_last_instr->i_opcode == POP_JUMP_IF_TRUE ||
b_last_instr->i_opcode == JUMP_ABSOLUTE ||
b_last_instr->i_opcode == JUMP_FORWARD) {
basicblock *b_next_act = b->b_next;
while (b_next_act != NULL && b_next_act->b_iused == 0) {
b_next_act = b_next_act->b_next;
}
if (b_last_instr->i_target == b_next_act) {
b->b_nofallthrough = 0;
switch(b_last_instr->i_opcode) {
case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
b_last_instr->i_opcode = POP_TOP;
b_last_instr->i_target = NULL;
b_last_instr->i_oparg = 0;
break;
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
b_last_instr->i_opcode = NOP;
clean_basic_block(b);
break;
}
/* The blocks after this one are now reachable through it */
b_next_act = b->b_next;
while (b_next_act != NULL && b_next_act->b_iused == 0) {
b_next_act->b_reachable = 1;
b_next_act = b_next_act->b_next;
}
}
}
}
}
minimize_lineno_table(a);
return 0;
}
Expand Down
202 changes: 101 additions & 101 deletions Python/importlib.h

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