Skip to content

Commit b78eb39

Browse files
committed
Don't optimize LOAD_FAST instructions whose local is killed by DEL_FAST
In certain cases it's possible for locals loaded by `LOAD_FAST` instructions to be on the stack when the local is killed by `DEL_FAST`. These `LOAD_FAST` instructions should not be optimized into `LOAD_FAST_BORROW` as the strong reference in the frame is killed while there is still a reference on the stack.
1 parent 7cb86c5 commit b78eb39

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,13 @@ def test_unoptimized_if_support_killed(self):
24722472
]
24732473
self.check(insts, insts)
24742474

2475+
insts = [
2476+
("LOAD_FAST", 0, 1),
2477+
("DELETE_FAST", 0, 2),
2478+
("POP_TOP", None, 3),
2479+
]
2480+
self.check(insts, insts)
2481+
24752482
def test_unoptimized_if_aliased(self):
24762483
insts = [
24772484
("LOAD_FAST", 0, 1),

Python/flowgraph.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,11 @@ optimize_load_fast(cfg_builder *g)
27952795
assert(opcode != EXTENDED_ARG);
27962796
switch (opcode) {
27972797
// Opcodes that load and store locals
2798+
case DELETE_FAST: {
2799+
kill_local(instr_flags, &refs, oparg);
2800+
break;
2801+
}
2802+
27982803
case LOAD_FAST: {
27992804
PUSH_REF(i, oparg);
28002805
break;

0 commit comments

Comments
 (0)