Skip to content
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

bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. #27656

Merged
merged 3 commits into from
Aug 9, 2021
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
119 changes: 59 additions & 60 deletions Lib/test/test_dis.py

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,29 @@ def func():
(4, 'line'),
(4, 'return')])

def test_nested_ifs_with_and(self):

def func():
if A:
if B:
if C:
if D:
return False
else:
return False
elif E and F:
return True

A = B = True
C = False

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

def test_nested_try_if(self):

def func():
Expand Down
41 changes: 25 additions & 16 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ compiler_copy_block(struct compiler *c, basicblock *block)
* a block can only have one fallthrough predecessor.
*/
assert(block->b_nofallthrough);
basicblock *result = compiler_next_block(c);
basicblock *result = compiler_new_block(c);
if (result == NULL) {
return NULL;
}
Expand Down Expand Up @@ -7567,8 +7567,9 @@ normalize_basic_block(basicblock *bb);
static int
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);

/* Duplicates exit BBs, so that line numbers can be propagated to them */
static int
ensure_exits_have_lineno(struct compiler *c);
duplicate_exits_without_lineno(struct compiler *c);

static int
extend_block(basicblock *bb);
Expand Down Expand Up @@ -7710,10 +7711,10 @@ guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
}
struct instr *last = &b->b_instr[b->b_iused-1];
if (last->i_lineno < 0) {
if (last->i_opcode == RETURN_VALUE)
{
if (last->i_opcode == RETURN_VALUE) {
for (int i = 0; i < b->b_iused; i++) {
assert(b->b_instr[i].i_lineno < 0);

b->b_instr[i].i_lineno = lineno;
}
}
Expand Down Expand Up @@ -7769,6 +7770,9 @@ fix_cell_offsets(struct compiler *c, basicblock *entryblock, int *fixedmap)
return numdropped;
}

static void
propagate_line_numbers(struct assembler *a);

static PyCodeObject *
assemble(struct compiler *c, int addNone)
{
Expand Down Expand Up @@ -7801,10 +7805,6 @@ assemble(struct compiler *c, int addNone)
}
}

if (ensure_exits_have_lineno(c)) {
return NULL;
}

nblocks = 0;
entryblock = NULL;
for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Expand Down Expand Up @@ -7861,8 +7861,11 @@ assemble(struct compiler *c, int addNone)
if (optimize_cfg(c, &a, consts)) {
goto error;
}
if (duplicate_exits_without_lineno(c)) {
return NULL;
}
propagate_line_numbers(&a);
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);

int maxdepth = stackdepth(c);
if (maxdepth < 0) {
goto error;
Expand Down Expand Up @@ -8365,6 +8368,7 @@ clean_basic_block(basicblock *bb) {
}
}
}

}
if (dest != src) {
bb->b_instr[dest] = bb->b_instr[src];
Expand Down Expand Up @@ -8479,7 +8483,7 @@ eliminate_empty_basic_blocks(basicblock *entry) {
* but has no impact on the generated line number events.
*/
static void
propogate_line_numbers(struct assembler *a) {
propagate_line_numbers(struct assembler *a) {
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
if (b->b_iused == 0) {
continue;
Expand Down Expand Up @@ -8544,6 +8548,11 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
clean_basic_block(b);
assert(b->b_predecessors == 0);
}
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
if (extend_block(b)) {
return -1;
}
}
if (mark_reachable(a)) {
return -1;
}
Expand Down Expand Up @@ -8581,7 +8590,6 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
if (maybe_empty_blocks) {
eliminate_empty_basic_blocks(a->a_entry);
}
propogate_line_numbers(a);
return 0;
}

Expand All @@ -8600,7 +8608,7 @@ is_exit_without_lineno(basicblock *b) {
* copy the line number from the sole predecessor block.
*/
static int
ensure_exits_have_lineno(struct compiler *c)
duplicate_exits_without_lineno(struct compiler *c)
{
basicblock *entry = NULL;
/* Copy all exit blocks without line number that are targets of a jump.
Expand All @@ -8616,20 +8624,21 @@ ensure_exits_have_lineno(struct compiler *c)
continue;
}
basicblock *target = b->b_instr[b->b_iused-1].i_target;
if (is_exit_without_lineno(target)) {
if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
basicblock *new_target = compiler_copy_block(c, target);
if (new_target == NULL) {
return -1;
}
COPY_INSTR_LOC(b->b_instr[b->b_iused-1], new_target->b_instr[0]);
b->b_instr[b->b_iused-1].i_target = new_target;
target->b_predecessors--;
new_target->b_predecessors = 1;
new_target->b_next = target->b_next;
target->b_next = new_target;
}
}
}
assert(entry != NULL);
if (is_exit_without_lineno(entry)) {
entry->b_instr[0].i_lineno = c->u->u_firstlineno;
}
/* Eliminate empty blocks */
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
while (b->b_next && b->b_next->b_iused == 0) {
Expand Down
Loading