]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-92228: disable the compiler's 'small exit block inlining' optimization...
authorChristian Heimes <christian@python.org>
Thu, 7 Jul 2022 10:10:32 +0000 (12:10 +0200)
committerGitHub <noreply@github.com>
Thu, 7 Jul 2022 10:10:32 +0000 (03:10 -0700)
Inlining of code that corresponds to source code lines, can make it hard to distinguish later between code which is only reachable from except handlers, and that which is reachable in normal control flow. This caused problems with the debugger's jump feature.

This PR turns off the inlining optimisation for code which has line numbers. We still inline things like the implicit "return None"..
(cherry picked from commit bde06e1b8381f140b296a397ddd1deb1c784ff8e)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/test/test_dis.py
Lib/test/test_peepholer.py
Lib/test/test_sys_settrace.py
Misc/NEWS.d/next/Core and Builtins/2022-07-06-14-02-26.gh-issue-92228.44Cbly.rst [new file with mode: 0644]
Python/compile.c

index 4283c30e0a1c1975747c7675ea7739a2efb70c1c..b00f329e0068b692721e790c2fc9a812e2144a37 100644 (file)
@@ -366,14 +366,12 @@ dis_traceback = """\
            LOAD_CONST               2 (0)
     -->    BINARY_OP               11 (/)
            POP_TOP
-
-%3d        LOAD_FAST                1 (tb)
-           RETURN_VALUE
+           JUMP_FORWARD            30 (to 76)
         >> PUSH_EXC_INFO
 
 %3d        LOAD_GLOBAL              0 (Exception)
            CHECK_EXC_MATCH
-           POP_JUMP_FORWARD_IF_FALSE    18 (to 72)
+           POP_JUMP_FORWARD_IF_FALSE    17 (to 68)
            STORE_FAST               0 (e)
 
 %3d        LOAD_FAST                0 (e)
@@ -383,9 +381,7 @@ dis_traceback = """\
            LOAD_CONST               0 (None)
            STORE_FAST               0 (e)
            DELETE_FAST              0 (e)
-
-%3d        LOAD_FAST                1 (tb)
-           RETURN_VALUE
+           JUMP_FORWARD             8 (to 76)
         >> LOAD_CONST               0 (None)
            STORE_FAST               0 (e)
            DELETE_FAST              0 (e)
@@ -395,15 +391,17 @@ dis_traceback = """\
         >> COPY                     3
            POP_EXCEPT
            RERAISE                  1
+
+%3d     >> LOAD_FAST                1 (tb)
+           RETURN_VALUE
 ExceptionTable:
 """ % (TRACEBACK_CODE.co_firstlineno,
        TRACEBACK_CODE.co_firstlineno + 1,
        TRACEBACK_CODE.co_firstlineno + 2,
-       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)
+       TRACEBACK_CODE.co_firstlineno + 3,
+       TRACEBACK_CODE.co_firstlineno + 5)
 
 def _fstring(a, b, c, d):
     return f'{a} {b:4} {c!r} {d!r:4}'
index 6a2f550d67052a0a3b236521890667a0ef663d17..241644ad7d2ff067e717d62fb28824d766cf193b 100644 (file)
@@ -344,6 +344,8 @@ class TestTranforms(BytecodeTestCase):
         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):
index 7ec290dbf04ad50a88d30325b3b069e593345d52..f03b03e19a2528b3ffc1472cd30d91c223fba1df 100644 (file)
@@ -2042,6 +2042,15 @@ class JumpTestCase(unittest.TestCase):
             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)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-06-14-02-26.gh-issue-92228.44Cbly.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-06-14-02-26.gh-issue-92228.44Cbly.rst
new file mode 100644 (file)
index 0000000..458ad89
--- /dev/null
@@ -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.
index cfe4b6ec04f9ed69c031c7c72e639a48dfa66af6..a71e7d31eecd28e09d1a6000c007079dce94d2e8 100644 (file)
@@ -8976,6 +8976,16 @@ error:
     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_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.
  */
@@ -8992,6 +9002,10 @@ extend_block(basicblock *bb) {
     }
     if (last->i_target->b_exit && 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 = compiler_next_instr(bb);