Skip to content

gh-133371: Don't optimize LOAD_FAST instructions whose local is killed by DELETE_FAST #133383

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 4 commits into from
May 5, 2025
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
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Known values:
Python 3.14a7 3621 (Optimize LOAD_FAST opcodes into LOAD_FAST_BORROW)
Python 3.14a7 3622 (Store annotations in different class dict keys)
Python 3.14a7 3623 (Add BUILD_INTERPOLATION & BUILD_TEMPLATE opcodes)
Python 3.14b1 3624 (Don't optimize LOAD_FAST when local is killed by DELETE_FAST)

Python 3.15 will start with 3650

Expand All @@ -288,7 +289,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3623
#define PYC_MAGIC_NUMBER 3624
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dis
import gc
from itertools import combinations, product
import opcode
import sys
Expand Down Expand Up @@ -2472,6 +2473,13 @@ def test_unoptimized_if_support_killed(self):
]
self.check(insts, insts)

insts = [
("LOAD_FAST", 0, 1),
("DELETE_FAST", 0, 2),
("POP_TOP", None, 3),
]
self.check(insts, insts)

def test_unoptimized_if_aliased(self):
insts = [
("LOAD_FAST", 0, 1),
Expand Down Expand Up @@ -2606,6 +2614,22 @@ def test_send(self):
]
self.cfg_optimization_test(insts, expected, consts=[None])

def test_del_in_finally(self):
# This loads `obj` onto the stack, executes `del obj`, then returns the
# `obj` from the stack. See gh-133371 for more details.
def create_obj():
obj = [42]
try:
return obj
finally:
del obj

obj = create_obj()
# The crash in the linked issue happens while running GC during
# interpreter finalization, so run it here manually.
gc.collect()
self.assertEqual(obj, [42])



if __name__ == "__main__":
Expand Down
5 changes: 5 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,11 @@ optimize_load_fast(cfg_builder *g)
assert(opcode != EXTENDED_ARG);
switch (opcode) {
// Opcodes that load and store locals
case DELETE_FAST: {
kill_local(instr_flags, &refs, oparg);
break;
}

Comment on lines +2798 to +2802
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: This change by itself is not enough to make the crash in test_asyncio go away.

case LOAD_FAST: {
PUSH_REF(i, oparg);
break;
Expand Down
Loading