From d64092e139ebf50392b96a4ecf41956a8120154b Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 14 Dec 2023 20:09:50 +0000 Subject: [PATCH 1/4] gh-113054: Compiler no longer replaces a redundant jump with no line number by a NOP --- Lib/test/test_compile.py | 4 ++++ .../2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst | 2 ++ Python/flowgraph.c | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index f681d125db7d7a..65c76a046cf238 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -444,6 +444,10 @@ def f(): self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames) self.assertIn("__package__", A.f.__code__.co_varnames) + def test_condition_expression_with_dead_blocks_compiles(self): + # See gh-113054 + compile('if 5 if 5 else T:y', '', 'exec') + def test_compile_invalid_namedexpr(self): # gh-109351 m = ast.Module( diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst new file mode 100644 index 00000000000000..fea2eaeb30106a --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst @@ -0,0 +1,2 @@ +Fixed bug where a redindant NOP is not removed, causing an assertion to fail +in the compiler in debug mode. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index fe632082d5a66c..86f439fe324f6e 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1110,7 +1110,10 @@ remove_redundant_jumps(cfg_builder *g) { * of that jump. If it is, then the jump instruction is redundant and * can be deleted. */ + assert(no_empty_basic_blocks(g)); + + bool remove_empty_blocks = false; for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) { cfg_instr *last = basicblock_last_instr(b); assert(last != NULL); @@ -1123,9 +1126,19 @@ remove_redundant_jumps(cfg_builder *g) { if (last->i_target == b->b_next) { assert(b->b_next->b_iused); INSTR_SET_OP0(last, NOP); + if (last->i_loc.lineno == NO_LOCATION.lineno) { + b->b_iused--; + if (b->b_iused == 0) { + remove_empty_blocks = true; + } + } } } } + if (remove_empty_blocks) { + eliminate_empty_basic_blocks(g); + } + assert(no_empty_basic_blocks(g)); return SUCCESS; } From eff04b2f93e6b900058fd86cc25da559ca140e0c Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 14 Dec 2023 20:21:17 +0000 Subject: [PATCH 2/4] maybe this is clearer --- Python/flowgraph.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 86f439fe324f6e..d2e3a7ae441c7f 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1125,13 +1125,15 @@ remove_redundant_jumps(cfg_builder *g) { } if (last->i_target == b->b_next) { assert(b->b_next->b_iused); - INSTR_SET_OP0(last, NOP); if (last->i_loc.lineno == NO_LOCATION.lineno) { b->b_iused--; if (b->b_iused == 0) { remove_empty_blocks = true; } } + else { + INSTR_SET_OP0(last, NOP); + } } } } From 8910217af856663687a3943007469bdccb2c9616 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 14 Dec 2023 23:18:00 +0000 Subject: [PATCH 3/4] parens in test Co-authored-by: Alex Waygood --- Lib/test/test_compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 65c76a046cf238..906e16cc9437fb 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -446,7 +446,7 @@ def f(): def test_condition_expression_with_dead_blocks_compiles(self): # See gh-113054 - compile('if 5 if 5 else T:y', '', 'exec') + compile('if (5 if 5 else T): 0', '', 'exec') def test_compile_invalid_namedexpr(self): # gh-109351 From aa31aa96cf5bde18ff02ec25b8fa8a9974873204 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 15 Dec 2023 09:05:59 +0000 Subject: [PATCH 4/4] Fix docs typo Co-authored-by: chris-eibl <138194463+chris-eibl@users.noreply.github.com> --- .../2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst index fea2eaeb30106a..d0729f9c44754c 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-14-20-08-35.gh-issue-113054.e20CtM.rst @@ -1,2 +1,2 @@ -Fixed bug where a redindant NOP is not removed, causing an assertion to fail +Fixed bug where a redundant NOP is not removed, causing an assertion to fail in the compiler in debug mode.