FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-42246: Don't eliminate jumps to jump, if it will break PEP 626. by markshannon · Pull Request #23896 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (3) .py  (2) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
169 changes: 85 additions & 84 deletions Lib/test/test_dis.py

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions Lib/test/test_sys_settrace.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,70 @@ def func():
(6, 'line'),
(6, 'return')])

def test_break_to_continue1(self):

def func():
TRUE = 1
x = [1]
while x:
x.pop()
while TRUE:
break
continue

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(6, 'line'),
(7, 'line'),
(3, 'line'),
(3, 'return')])

def test_break_to_continue2(self):

def func():
TRUE = 1
x = [1]
while x:
x.pop()
while TRUE:
break
else:
continue

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(6, 'line'),
(3, 'line'),
(3, 'return')])

def test_break_to_break(self):

def func():
TRUE = 1
while TRUE:
while TRUE:
break
break

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(5, 'return')])


class SkipLineEventsTraceTestCase(TraceTestCase):
"""Repeat the trace tests, but with per-line events skipped"""
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jumps to jumps are not eliminated when it would break PEP 626.
135 changes: 84 additions & 51 deletions Python/compile.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1432,28 +1432,32 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
return 1;
}

static int
compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
{
struct instr *i;
int off;

if (c->c_do_not_emit_bytecode) {
return 1;
}

assert(HAS_ARG(opcode));
assert(b != NULL);
off = compiler_next_instr(c->u->u_curblock);
if (off < 0)
assert(target != NULL);

int off = compiler_next_instr(b);
struct instr *i = &b->b_instr[off];
if (off < 0) {
return 0;
i = &c->u->u_curblock->b_instr[off];
}
i->i_opcode = opcode;
i->i_target = b;
i->i_lineno = c->u->u_lineno;
i->i_target = target;
i->i_lineno = lineno;
return 1;
}

static int
compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
{
if (c->c_do_not_emit_bytecode) {
return 1;
}
return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
}

/* NEXT_BLOCK() creates an implicit jump from the current block
to the new block.

Expand Down Expand Up @@ -6067,6 +6071,27 @@ fold_tuple_on_constants(struct instr *inst,
return 0;
}


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;
if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
return -1;
}
assert (bb->b_iused >= 2);
bb->b_instr[bb->b_iused-2].i_opcode = NOP;
return 0;
}

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

Expand Down Expand Up @@ -6183,45 +6208,55 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
case JUMP_IF_FALSE_OR_POP:
switch(target->i_opcode) {
case POP_JUMP_IF_FALSE:
*inst = *target;
--i;
if (inst->i_lineno == target->i_lineno) {
*inst = *target;
i--;
}
break;
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
case JUMP_IF_FALSE_OR_POP:
if (inst->i_target != target->i_target) {
if (inst->i_lineno == target->i_lineno &&
inst->i_target != target->i_target) {
inst->i_target = target->i_target;
--i;
i--;
}
break;
case JUMP_IF_TRUE_OR_POP:
assert (inst->i_target->b_iused == 1);
inst->i_opcode = POP_JUMP_IF_FALSE;
inst->i_target = inst->i_target->b_next;
--i;
if (inst->i_lineno == target->i_lineno) {
inst->i_opcode = POP_JUMP_IF_FALSE;
inst->i_target = inst->i_target->b_next;
--i;
}
break;
}
break;

case JUMP_IF_TRUE_OR_POP:
switch(target->i_opcode) {
case POP_JUMP_IF_TRUE:
*inst = *target;
--i;
if (inst->i_lineno == target->i_lineno) {
*inst = *target;
i--;
}
break;
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
case JUMP_IF_TRUE_OR_POP:
if (inst->i_target != target->i_target) {
if (inst->i_lineno == target->i_lineno &&
inst->i_target != target->i_target) {
inst->i_target = target->i_target;
--i;
i--;
}
break;
case JUMP_IF_FALSE_OR_POP:
assert (inst->i_target->b_iused == 1);
inst->i_opcode = POP_JUMP_IF_TRUE;
inst->i_target = inst->i_target->b_next;
--i;
if (inst->i_lineno == target->i_lineno) {
inst->i_opcode = POP_JUMP_IF_TRUE;
inst->i_target = inst->i_target->b_next;
--i;
}
break;
}
break;
Expand All @@ -6230,9 +6265,9 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
switch(target->i_opcode) {
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
if (inst->i_target != target->i_target) {
if (inst->i_lineno == target->i_lineno) {
inst->i_target = target->i_target;
--i;
i--;
}
break;
}
Expand All @@ -6242,9 +6277,9 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
switch(target->i_opcode) {
case JUMP_ABSOLUTE:
case JUMP_FORWARD:
if (inst->i_target != target->i_target) {
if (inst->i_lineno == target->i_lineno) {
inst->i_target = target->i_target;
--i;
i--;
}
break;
}
Expand All @@ -6255,32 +6290,30 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
assert (i == bb->b_iused-1);
switch(target->i_opcode) {
case JUMP_FORWARD:
if (inst->i_target != target->i_target) {
inst->i_target = target->i_target;
// --i;
if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
goto error;
}
break;

case JUMP_ABSOLUTE:
if (inst->i_target != target->i_target) {
inst->i_target = target->i_target;
inst->i_opcode = target->i_opcode;
--i;
if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
goto error;
}
break;
}
if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
basicblock *to_copy = inst->i_target;
inst->i_opcode = NOP;
for (i = 0; i < to_copy->b_iused; i++) {
int index = compiler_next_instr(bb);
if (index < 0) {
return -1;
default:
if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
basicblock *to_copy = inst->i_target;
inst->i_opcode = NOP;
for (i = 0; i < to_copy->b_iused; i++) {
int index = compiler_next_instr(bb);
if (index < 0) {
return -1;
}
bb->b_instr[index] = to_copy->b_instr[i];
}
bb->b_exit = 1;
}
bb->b_instr[index] = to_copy->b_instr[i];
}
bb->b_exit = 1;
}
break;
}
}
return 0;
Expand Down
Loading

Back | FazBrowse Home | New Git URL