Skip to content

Commit b854557

Browse files
authored
bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. (pythonGH-27656)
1 parent 2b496e7 commit b854557

File tree

6 files changed

+6720
-6705
lines changed

6 files changed

+6720
-6705
lines changed

Lib/test/test_dis.py

Lines changed: 59 additions & 60 deletions
Large diffs are not rendered by default.

Lib/test/test_sys_settrace.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,29 @@ def func():
894894
(4, 'line'),
895895
(4, 'return')])
896896

897+
def test_nested_ifs_with_and(self):
898+
899+
def func():
900+
if A:
901+
if B:
902+
if C:
903+
if D:
904+
return False
905+
else:
906+
return False
907+
elif E and F:
908+
return True
909+
910+
A = B = True
911+
C = False
912+
913+
self.run_and_compare(func,
914+
[(0, 'call'),
915+
(1, 'line'),
916+
(2, 'line'),
917+
(3, 'line'),
918+
(3, 'return')])
919+
897920
def test_nested_try_if(self):
898921

899922
def func():

Python/compile.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ compiler_copy_block(struct compiler *c, basicblock *block)
890890
* a block can only have one fallthrough predecessor.
891891
*/
892892
assert(block->b_nofallthrough);
893-
basicblock *result = compiler_next_block(c);
893+
basicblock *result = compiler_new_block(c);
894894
if (result == NULL) {
895895
return NULL;
896896
}
@@ -7567,8 +7567,9 @@ normalize_basic_block(basicblock *bb);
75677567
static int
75687568
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
75697569

7570+
/* Duplicates exit BBs, so that line numbers can be propagated to them */
75707571
static int
7571-
ensure_exits_have_lineno(struct compiler *c);
7572+
duplicate_exits_without_lineno(struct compiler *c);
75727573

75737574
static int
75747575
extend_block(basicblock *bb);
@@ -7710,10 +7711,10 @@ guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
77107711
}
77117712
struct instr *last = &b->b_instr[b->b_iused-1];
77127713
if (last->i_lineno < 0) {
7713-
if (last->i_opcode == RETURN_VALUE)
7714-
{
7714+
if (last->i_opcode == RETURN_VALUE) {
77157715
for (int i = 0; i < b->b_iused; i++) {
77167716
assert(b->b_instr[i].i_lineno < 0);
7717+
77177718
b->b_instr[i].i_lineno = lineno;
77187719
}
77197720
}
@@ -7769,6 +7770,9 @@ fix_cell_offsets(struct compiler *c, basicblock *entryblock, int *fixedmap)
77697770
return numdropped;
77707771
}
77717772

7773+
static void
7774+
propagate_line_numbers(struct assembler *a);
7775+
77727776
static PyCodeObject *
77737777
assemble(struct compiler *c, int addNone)
77747778
{
@@ -7801,10 +7805,6 @@ assemble(struct compiler *c, int addNone)
78017805
}
78027806
}
78037807

7804-
if (ensure_exits_have_lineno(c)) {
7805-
return NULL;
7806-
}
7807-
78087808
nblocks = 0;
78097809
entryblock = NULL;
78107810
for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
@@ -7861,8 +7861,11 @@ assemble(struct compiler *c, int addNone)
78617861
if (optimize_cfg(c, &a, consts)) {
78627862
goto error;
78637863
}
7864+
if (duplicate_exits_without_lineno(c)) {
7865+
return NULL;
7866+
}
7867+
propagate_line_numbers(&a);
78647868
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
7865-
78667869
int maxdepth = stackdepth(c);
78677870
if (maxdepth < 0) {
78687871
goto error;
@@ -8365,6 +8368,7 @@ clean_basic_block(basicblock *bb) {
83658368
}
83668369
}
83678370
}
8371+
83688372
}
83698373
if (dest != src) {
83708374
bb->b_instr[dest] = bb->b_instr[src];
@@ -8479,7 +8483,7 @@ eliminate_empty_basic_blocks(basicblock *entry) {
84798483
* but has no impact on the generated line number events.
84808484
*/
84818485
static void
8482-
propogate_line_numbers(struct assembler *a) {
8486+
propagate_line_numbers(struct assembler *a) {
84838487
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
84848488
if (b->b_iused == 0) {
84858489
continue;
@@ -8544,6 +8548,11 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
85448548
clean_basic_block(b);
85458549
assert(b->b_predecessors == 0);
85468550
}
8551+
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
8552+
if (extend_block(b)) {
8553+
return -1;
8554+
}
8555+
}
85478556
if (mark_reachable(a)) {
85488557
return -1;
85498558
}
@@ -8581,7 +8590,6 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
85818590
if (maybe_empty_blocks) {
85828591
eliminate_empty_basic_blocks(a->a_entry);
85838592
}
8584-
propogate_line_numbers(a);
85858593
return 0;
85868594
}
85878595

@@ -8600,7 +8608,7 @@ is_exit_without_lineno(basicblock *b) {
86008608
* copy the line number from the sole predecessor block.
86018609
*/
86028610
static int
8603-
ensure_exits_have_lineno(struct compiler *c)
8611+
duplicate_exits_without_lineno(struct compiler *c)
86048612
{
86058613
basicblock *entry = NULL;
86068614
/* Copy all exit blocks without line number that are targets of a jump.
@@ -8616,20 +8624,21 @@ ensure_exits_have_lineno(struct compiler *c)
86168624
continue;
86178625
}
86188626
basicblock *target = b->b_instr[b->b_iused-1].i_target;
8619-
if (is_exit_without_lineno(target)) {
8627+
if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
86208628
basicblock *new_target = compiler_copy_block(c, target);
86218629
if (new_target == NULL) {
86228630
return -1;
86238631
}
86248632
COPY_INSTR_LOC(b->b_instr[b->b_iused-1], new_target->b_instr[0]);
86258633
b->b_instr[b->b_iused-1].i_target = new_target;
8634+
target->b_predecessors--;
8635+
new_target->b_predecessors = 1;
8636+
new_target->b_next = target->b_next;
8637+
target->b_next = new_target;
86268638
}
86278639
}
86288640
}
86298641
assert(entry != NULL);
8630-
if (is_exit_without_lineno(entry)) {
8631-
entry->b_instr[0].i_lineno = c->u->u_firstlineno;
8632-
}
86338642
/* Eliminate empty blocks */
86348643
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
86358644
while (b->b_next && b->b_next->b_iused == 0) {

0 commit comments

Comments
 (0)