Skip to content

gh-92228: disable the compiler's 'small exit block inlining' optimization for blocks that have a line number #94592

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 7 commits into from
Jul 7, 2022
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
27 changes: 7 additions & 20 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ def bug42562():
--> BINARY_OP 11 (/)
POP_TOP

%3d LOAD_FAST_CHECK 1 (tb)
%3d >> LOAD_FAST_CHECK 1 (tb)
RETURN_VALUE
>> PUSH_EXC_INFO

%3d LOAD_GLOBAL 0 (Exception)
CHECK_EXC_MATCH
POP_JUMP_FORWARD_IF_FALSE 23 (to 82)
POP_JUMP_FORWARD_IF_FALSE 22 (to 80)
STORE_FAST 0 (e)

%3d LOAD_FAST 0 (e)
Expand All @@ -376,9 +376,7 @@ def bug42562():
LOAD_CONST 0 (None)
STORE_FAST 0 (e)
DELETE_FAST 0 (e)

%3d LOAD_FAST 1 (tb)
RETURN_VALUE
JUMP_BACKWARD 29 (to 14)
>> LOAD_CONST 0 (None)
STORE_FAST 0 (e)
DELETE_FAST 0 (e)
Expand All @@ -396,7 +394,6 @@ def bug42562():
TRACEBACK_CODE.co_firstlineno + 5,
TRACEBACK_CODE.co_firstlineno + 3,
TRACEBACK_CODE.co_firstlineno + 4,
TRACEBACK_CODE.co_firstlineno + 5,
TRACEBACK_CODE.co_firstlineno + 3)

def _fstring(a, b, c, d):
Expand Down Expand Up @@ -443,7 +440,7 @@ def _with(c):
CALL 2
POP_TOP

%3d LOAD_CONST 2 (2)
%3d >> LOAD_CONST 2 (2)
STORE_FAST 2 (y)
LOAD_CONST 0 (None)
RETURN_VALUE
Expand All @@ -456,11 +453,7 @@ def _with(c):
POP_EXCEPT
POP_TOP
POP_TOP

%3d LOAD_CONST 2 (2)
STORE_FAST 2 (y)
LOAD_CONST 0 (None)
RETURN_VALUE
JUMP_BACKWARD 13 (to 30)
>> COPY 3
POP_EXCEPT
RERAISE 1
Expand All @@ -472,7 +465,6 @@ def _with(c):
_with.__code__.co_firstlineno + 1,
_with.__code__.co_firstlineno + 3,
_with.__code__.co_firstlineno + 1,
_with.__code__.co_firstlineno + 3,
)

async def _asyncwith(c):
Expand Down Expand Up @@ -510,7 +502,7 @@ async def _asyncwith(c):
JUMP_BACKWARD_NO_INTERRUPT 4 (to 48)
>> POP_TOP

%3d LOAD_CONST 2 (2)
%3d >> LOAD_CONST 2 (2)
STORE_FAST 2 (y)
LOAD_CONST 0 (None)
RETURN_VALUE
Expand All @@ -529,11 +521,7 @@ async def _asyncwith(c):
POP_EXCEPT
POP_TOP
POP_TOP

%3d LOAD_CONST 2 (2)
STORE_FAST 2 (y)
LOAD_CONST 0 (None)
RETURN_VALUE
JUMP_BACKWARD 19 (to 58)
>> COPY 3
POP_EXCEPT
RERAISE 1
Expand All @@ -545,7 +533,6 @@ async def _asyncwith(c):
_asyncwith.__code__.co_firstlineno + 1,
_asyncwith.__code__.co_firstlineno + 3,
_asyncwith.__code__.co_firstlineno + 1,
_asyncwith.__code__.co_firstlineno + 3,
)


Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ def f(x):
self.assertEqual(len(returns), 1)
self.check_lnotab(f)

@unittest.skip("Following gh-92228 the return has two predecessors "
"and that prevents jump elimination.")
def test_elim_jump_to_return(self):
# JUMP_FORWARD to RETURN --> RETURN
def f(cond, true_value, false_value):
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,15 @@ def test_no_jump_within_except_block(output):
output.append(6)
output.append(7)

@jump_test(6, 1, [1, 5, 1, 5])
def test_jump_over_try_except(output):
output.append(1)
try:
1 / 0
except ZeroDivisionError as e:
output.append(5)
x = 42 # has to be a two-instruction block

@jump_test(2, 4, [1, 4, 5, -4])
def test_jump_across_with(output):
output.append(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disable the compiler's inline-small-exit-blocks optimization for exit blocks that are associated with source code lines. This fixes a bug where the debugger cannot tell where an exception handler ends and the following code block begins.
14 changes: 14 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9224,6 +9224,16 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
return -1;
}

static bool
basicblock_has_lineno(const basicblock *bb) {
for (int i = 0; i < bb->b_iused; i++) {
if (bb->b_instr[i].i_loc.lineno > 0) {
return true;
}
}
return false;
}

/* If this block ends with an unconditional jump to an exit block,
* then remove the jump and extend this block with the target.
*/
Expand All @@ -9240,6 +9250,10 @@ extend_block(basicblock *bb) {
}
if (basicblock_exits_scope(last->i_target) && last->i_target->b_iused <= MAX_COPY_SIZE) {
basicblock *to_copy = last->i_target;
if (basicblock_has_lineno(to_copy)) {
/* copy only blocks without line number (like implicit 'return None's) */
return 0;
}
last->i_opcode = NOP;
for (int i = 0; i < to_copy->b_iused; i++) {
int index = basicblock_next_instr(bb);
Expand Down