Skip to content

Commit a86f7da

Browse files
authored
bpo-44626: Merge basic blocks earlier to enable better handling of exit blocks without line numbers (GH-27138)
1 parent 5007a4f commit a86f7da

File tree

5 files changed

+4411
-4337
lines changed

5 files changed

+4411
-4337
lines changed

Lib/test/test_dis.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def _prepare_test_cases():
10921092
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False, positions=None),
10931093
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=False, positions=None),
10941094
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False, positions=None),
1095-
Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=109, argval=218, argrepr='to 218', offset=122, starts_line=None, is_jump_target=False, positions=None),
1095+
Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=124, argval=248, argrepr='to 248', offset=122, starts_line=None, is_jump_target=False, positions=None),
10961096
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False, positions=None),
10971097
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False, positions=None),
10981098
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False, positions=None),
@@ -1101,7 +1101,7 @@ def _prepare_test_cases():
11011101
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False, positions=None),
11021102
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False, positions=None),
11031103
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False, positions=None),
1104-
Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=192, argrepr='to 192', offset=140, starts_line=None, is_jump_target=False, positions=None),
1104+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=234, argrepr='to 234', offset=140, starts_line=None, is_jump_target=False, positions=None),
11051105
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=False, positions=None),
11061106
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True, positions=None),
11071107
Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False, positions=None),
@@ -1115,7 +1115,7 @@ def _prepare_test_cases():
11151115
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False, positions=None),
11161116
Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=164, starts_line=None, is_jump_target=False, positions=None),
11171117
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False, positions=None),
1118-
Instruction(opname='JUMP_FORWARD', opcode=110, arg=11, argval=192, argrepr='to 192', offset=168, starts_line=None, is_jump_target=False, positions=None),
1118+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=220, argrepr='to 220', offset=168, starts_line=None, is_jump_target=False, positions=None),
11191119
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False, positions=None),
11201120
Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False, positions=None),
11211121
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=90, argval=180, argrepr='to 180', offset=174, starts_line=None, is_jump_target=False, positions=None),
@@ -1127,20 +1127,35 @@ def _prepare_test_cases():
11271127
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False, positions=None),
11281128
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False, positions=None),
11291129
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=190, starts_line=None, is_jump_target=False, positions=None),
1130-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=192, starts_line=28, is_jump_target=True, positions=None),
1131-
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=194, starts_line=None, is_jump_target=False, positions=None),
1132-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=196, starts_line=None, is_jump_target=False, positions=None),
1133-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, starts_line=None, is_jump_target=False, positions=None),
1134-
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=200, starts_line=None, is_jump_target=False, positions=None),
1135-
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=202, starts_line=None, is_jump_target=False, positions=None),
1136-
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False, positions=None),
1137-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=206, starts_line=None, is_jump_target=False, positions=None),
1138-
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=208, starts_line=None, is_jump_target=False, positions=None),
1139-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=210, starts_line=None, is_jump_target=False, positions=None),
1140-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False, positions=None),
1141-
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=214, starts_line=None, is_jump_target=False, positions=None),
1142-
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=216, starts_line=None, is_jump_target=False, positions=None),
1143-
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=218, starts_line=22, is_jump_target=True, positions=None),
1130+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False, positions=None),
1131+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=194, starts_line=28, is_jump_target=False, positions=None),
1132+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=196, starts_line=None, is_jump_target=False, positions=None),
1133+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=198, starts_line=None, is_jump_target=False, positions=None),
1134+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=200, starts_line=None, is_jump_target=False, positions=None),
1135+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=202, starts_line=None, is_jump_target=False, positions=None),
1136+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False, positions=None),
1137+
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False, positions=None),
1138+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=208, starts_line=None, is_jump_target=False, positions=None),
1139+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=210, starts_line=None, is_jump_target=False, positions=None),
1140+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=212, starts_line=None, is_jump_target=False, positions=None),
1141+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=False, positions=None),
1142+
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=216, starts_line=None, is_jump_target=False, positions=None),
1143+
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False, positions=None),
1144+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=220, starts_line=25, is_jump_target=True, positions=None),
1145+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=222, starts_line=28, is_jump_target=False, positions=None),
1146+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=224, starts_line=None, is_jump_target=False, positions=None),
1147+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=226, starts_line=None, is_jump_target=False, positions=None),
1148+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=228, starts_line=None, is_jump_target=False, positions=None),
1149+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=230, starts_line=None, is_jump_target=False, positions=None),
1150+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=232, starts_line=None, is_jump_target=False, positions=None),
1151+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=234, starts_line=23, is_jump_target=True, positions=None),
1152+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=236, starts_line=28, is_jump_target=False, positions=None),
1153+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=238, starts_line=None, is_jump_target=False, positions=None),
1154+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=240, starts_line=None, is_jump_target=False, positions=None),
1155+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=242, starts_line=None, is_jump_target=False, positions=None),
1156+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=244, starts_line=None, is_jump_target=False, positions=None),
1157+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=246, starts_line=None, is_jump_target=False, positions=None),
1158+
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=248, starts_line=22, is_jump_target=True, positions=None),
11441159
]
11451160

11461161
# One last piece of inspect fodder to check the default line number handling

Lib/test/test_sys_settrace.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -986,14 +986,29 @@ def func():
986986
except Exception:
987987
pass
988988

989-
# This doesn't conform to PEP 626
990989
self.run_and_compare(func,
991990
[(0, 'call'),
992991
(1, 'line'),
993992
(2, 'line'),
994993
(3, 'line'),
995-
(5, 'line'),
996-
(5, 'return')])
994+
(3, 'return')])
995+
996+
def test_if_in_if_in_if(self):
997+
def func(a=0, p=1, z=1):
998+
if p:
999+
if a:
1000+
if z:
1001+
pass
1002+
else:
1003+
pass
1004+
else:
1005+
pass
1006+
1007+
self.run_and_compare(func,
1008+
[(0, 'call'),
1009+
(1, 'line'),
1010+
(2, 'line'),
1011+
(2, 'return')])
9971012

9981013
def test_early_exit_with(self):
9991014

Python/compile.c

+48-16
Original file line numberDiff line numberDiff line change
@@ -7521,10 +7521,11 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
75217521
/* For debugging purposes only */
75227522
#if 0
75237523
static void
7524-
dump_instr(const struct instr *i)
7524+
dump_instr(struct instr *i)
75257525
{
7526-
const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
7527-
const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
7526+
const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
7527+
const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
7528+
75287529
char arg[128];
75297530

75307531
*arg = '\0';
@@ -7561,6 +7562,9 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
75617562
static int
75627563
ensure_exits_have_lineno(struct compiler *c);
75637564

7565+
static int
7566+
extend_block(basicblock *bb);
7567+
75647568
static int *
75657569
build_cellfixedoffsets(struct compiler *c)
75667570
{
@@ -7783,6 +7787,12 @@ assemble(struct compiler *c, int addNone)
77837787
}
77847788
}
77857789

7790+
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7791+
if (extend_block(b)) {
7792+
return NULL;
7793+
}
7794+
}
7795+
77867796
if (ensure_exits_have_lineno(c)) {
77877797
return NULL;
77887798
}
@@ -7839,6 +7849,7 @@ assemble(struct compiler *c, int addNone)
78397849
if (consts == NULL) {
78407850
goto error;
78417851
}
7852+
78427853
if (optimize_cfg(c, &a, consts)) {
78437854
goto error;
78447855
}
@@ -8246,19 +8257,14 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
82468257
goto error;
82478258
}
82488259
break;
8249-
default:
8250-
if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
8251-
basicblock *to_copy = inst->i_target;
8252-
inst->i_opcode = NOP;
8253-
for (i = 0; i < to_copy->b_iused; i++) {
8254-
int index = compiler_next_instr(bb);
8255-
if (index < 0) {
8256-
return -1;
8257-
}
8258-
bb->b_instr[index] = to_copy->b_instr[i];
8259-
}
8260-
bb->b_exit = 1;
8261-
}
8260+
}
8261+
break;
8262+
case FOR_ITER:
8263+
assert (i == bb->b_iused-1);
8264+
if (target->i_opcode == JUMP_FORWARD) {
8265+
if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
8266+
goto error;
8267+
}
82628268
}
82638269
break;
82648270
case ROT_N:
@@ -8288,6 +8294,32 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
82888294
return -1;
82898295
}
82908296

8297+
/* If this block ends with an unconditional jump to an exit block,
8298+
* then remove the jump and extend this block with the target.
8299+
*/
8300+
static int
8301+
extend_block(basicblock *bb) {
8302+
if (bb->b_iused == 0) {
8303+
return 0;
8304+
}
8305+
struct instr *last = &bb->b_instr[bb->b_iused-1];
8306+
if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
8307+
return 0;
8308+
}
8309+
if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
8310+
basicblock *to_copy = last->i_target;
8311+
last->i_opcode = NOP;
8312+
for (int i = 0; i < to_copy->b_iused; i++) {
8313+
int index = compiler_next_instr(bb);
8314+
if (index < 0) {
8315+
return -1;
8316+
}
8317+
bb->b_instr[index] = to_copy->b_instr[i];
8318+
}
8319+
bb->b_exit = 1;
8320+
}
8321+
return 0;
8322+
}
82918323

82928324
static void
82938325
clean_basic_block(basicblock *bb) {

0 commit comments

Comments
 (0)