From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 2 Oct 2023 15:43:16 +0000 (-0700) Subject: [3.12] gh-109889: fix compiler's redundant NOP detection to look past NOPs with no... X-Git-Tag: v3.12.1~418 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b96102f29d2634a69b04cc782236cb12d8e3e56;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno (GH-109987) (#110048) gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno (GH-109987) (cherry picked from commit f580edcc6a4c528020afe46c753db713474acad6) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index b75d1c59591e..42df670fe00e 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1269,6 +1269,11 @@ class TestSpecifics(unittest.TestCase): while x: 0 if 1 else 0 + def test_remove_redundant_nop_edge_case(self): + # See gh-109889 + def f(): + a if (1 if b else c) else d + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): # Ensure that compiled code snippets have correct line and column numbers diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-27-21-35-49.gh-issue-109889.t5hIRT.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-27-21-35-49.gh-issue-109889.t5hIRT.rst new file mode 100644 index 000000000000..8be373f0f6b6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-09-27-21-35-49.gh-issue-109889.t5hIRT.rst @@ -0,0 +1,2 @@ +Fix the compiler's redundant NOP detection algorithm to skip over NOPs with +no line number when looking for the next instruction's lineno. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index f860631a6c21..afcae317403e 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -981,7 +981,17 @@ remove_redundant_nops(basicblock *bb) { } /* or if last instruction in BB and next BB has same line number */ if (next) { - if (lineno == next->b_instr[0].i_loc.lineno) { + location next_loc = NO_LOCATION; + for (int next_i=0; next_i < next->b_iused; next_i++) { + cfg_instr *instr = &next->b_instr[next_i]; + if (instr->i_opcode == NOP && instr->i_loc.lineno == NO_LOCATION.lineno) { + /* Skip over NOPs without location, they will be removed */ + continue; + } + next_loc = instr->i_loc; + break; + } + if (lineno == next_loc.lineno) { continue; } }