Skip to content

bpo-45773: Stop "optimizing" certain jump patterns #29505

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 6 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,5 +600,12 @@ def test_bpo_42057(self):
except Exception or Exception:
pass

def test_bpo_45773_pop_jump_if_true(self):
compile("while True or spam: pass", "<test>", "exec")

def test_bpo_45773_pop_jump_if_false(self):
compile("while True or not spam: pass", "<test>", "exec")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a compiler hang when attempting to optimize certain jump patterns.
109 changes: 38 additions & 71 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8101,34 +8101,29 @@ fold_rotations(struct instr *inst, int n)
}
}

/* Maximum size of basic block that should be copied in optimizer */
#define MAX_COPY_SIZE 4

static int
eliminate_jump_to_jump(basicblock *bb, int opcode) {
assert (bb->b_iused > 0);
struct instr *inst = &bb->b_instr[bb->b_iused-1];
assert (is_jump(inst));
assert (inst->i_target->b_iused > 0);
struct instr *target = &inst->i_target->b_instr[0];
if (inst->i_target == target->i_target) {
/* Nothing to do */
return 0;
}
int lineno = target->i_lineno;
int end_lineno = target->i_end_lineno;
int col_offset = target->i_col_offset;
int end_col_offset = target->i_end_col_offset;
if (add_jump_to_block(bb, opcode, lineno, end_lineno, col_offset,
end_col_offset, target->i_target) == 0) {
return -1;
// Attempt to eliminate jumps to jumps by updating inst to jump to
// target->i_target using the provided opcode. Return whether or not the
// optimization was successful.
static bool
jump_thread(struct instr *inst, struct instr *target, int opcode)
{
assert(is_jump(inst));
assert(is_jump(target));
// bpo-45773: If inst->i_target == target->i_target, then nothing actually
// changes (and we fall into an infinite loop):
if (inst->i_lineno == target->i_lineno &&
inst->i_target != target->i_target)
{
inst->i_target = target->i_target;
inst->i_opcode = opcode;
return true;
}
assert (bb->b_iused >= 2);
bb->b_instr[bb->b_iused-2].i_opcode = NOP;
return 0;
return false;
}

/* Maximum size of basic block that should be copied in optimizer */
#define MAX_COPY_SIZE 4

/* Optimization */
static int
optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Expand Down Expand Up @@ -8250,23 +8245,19 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
case JUMP_IF_FALSE_OR_POP:
switch(target->i_opcode) {
case POP_JUMP_IF_FALSE:
if (inst->i_lineno == target->i_lineno) {
*inst = *target;
i--;
}
i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
break;
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
case JUMP_IF_FALSE_OR_POP:
if (inst->i_lineno == target->i_lineno &&
inst->i_target != target->i_target) {
inst->i_target = target->i_target;
i--;
}
i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
break;
case JUMP_IF_TRUE_OR_POP:
assert (inst->i_target->b_iused == 1);
case POP_JUMP_IF_TRUE:
if (inst->i_lineno == target->i_lineno) {
// We don't need to bother checking for loops here,
// since inst->i_target can't possibly be equal to
// inst->i_target->b_next:
inst->i_opcode = POP_JUMP_IF_FALSE;
inst->i_target = inst->i_target->b_next;
--i;
Expand All @@ -8278,23 +8269,19 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
case JUMP_IF_TRUE_OR_POP:
switch(target->i_opcode) {
case POP_JUMP_IF_TRUE:
if (inst->i_lineno == target->i_lineno) {
*inst = *target;
i--;
}
i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
break;
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
case JUMP_IF_TRUE_OR_POP:
if (inst->i_lineno == target->i_lineno &&
inst->i_target != target->i_target) {
inst->i_target = target->i_target;
i--;
}
i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
break;
case JUMP_IF_FALSE_OR_POP:
assert (inst->i_target->b_iused == 1);
case POP_JUMP_IF_FALSE:
if (inst->i_lineno == target->i_lineno) {
// We don't need to bother checking for loops here,
// since inst->i_target can't possibly be equal to
// inst->i_target->b_next.
inst->i_opcode = POP_JUMP_IF_TRUE;
inst->i_target = inst->i_target->b_next;
--i;
Expand All @@ -8307,49 +8294,29 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
switch(target->i_opcode) {
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
if (inst->i_lineno == target->i_lineno) {
inst->i_target = target->i_target;
i--;
}
break;
case JUMP_IF_FALSE_OR_POP:
i -= jump_thread(inst, target, inst->i_opcode);
}
break;

case POP_JUMP_IF_TRUE:
switch(target->i_opcode) {
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
if (inst->i_lineno == target->i_lineno) {
inst->i_target = target->i_target;
i--;
}
break;
case JUMP_IF_TRUE_OR_POP:
i -= jump_thread(inst, target, inst->i_opcode);
}
break;

case JUMP_ABSOLUTE:
case JUMP_FORWARD:
assert (i == bb->b_iused-1);
switch(target->i_opcode) {
case JUMP_FORWARD:
if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
goto error;
}
break;

case JUMP_ABSOLUTE:
if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
goto error;
}
break;
case JUMP_FORWARD:
i -= jump_thread(inst, target, JUMP_ABSOLUTE);
}
break;
case FOR_ITER:
assert (i == bb->b_iused-1);
if (target->i_opcode == JUMP_FORWARD) {
if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
goto error;
}
i -= jump_thread(inst, target, FOR_ITER);
}
break;
case ROT_N:
Expand Down