Skip to content

Commit a99305e

Browse files
authored
[3.12] gh-112356: LOAD_GLOBAL can only include one PUSH_NULL (#112566)
1 parent 4c9da4c commit a99305e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Lib/test/test_peepholer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,5 +1075,26 @@ def test_no_unsafe_static_swap(self):
10751075
]
10761076
self.cfg_optimization_test(insts, insts, consts=list(range(3)), nlocals=1)
10771077

1078+
def test_only_one_push_null_per_load_global(self):
1079+
# When optimizing func()(), a second pass shouldn't
1080+
# let the LOAD_GLOBAL absorb another PUSH_NULL.
1081+
before = [
1082+
('PUSH_NULL', 0, 1),
1083+
('PUSH_NULL', 0, 1),
1084+
('LOAD_GLOBAL', 0, 1),
1085+
('CALL', 0, 1),
1086+
('CALL', 0, 1),
1087+
('RETURN_VALUE', 0, 1),
1088+
]
1089+
after = [
1090+
('PUSH_NULL', 0, 1),
1091+
('LOAD_GLOBAL', 1, 1),
1092+
('CALL', 0, 1),
1093+
('CALL', 0, 1),
1094+
('RETURN_VALUE', 0, 1),
1095+
]
1096+
self.cfg_optimization_test(before, expected_insts=after, consts=[])
1097+
self.cfg_optimization_test(after, expected_insts=after, consts=[])
1098+
10781099
if __name__ == "__main__":
10791100
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stopped erroneously deleting a ``LOAD_NULL`` bytecode instruction when optimized twice.

Python/flowgraph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,9 +1554,9 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
15541554
case KW_NAMES:
15551555
break;
15561556
case PUSH_NULL:
1557-
if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0) {
1557+
if (nextop == LOAD_GLOBAL && (bb->b_instr[i+1].i_oparg & 1) == 0) {
15581558
INSTR_SET_OP0(inst, NOP);
1559-
inst[1].i_oparg |= 1;
1559+
bb->b_instr[i+1].i_oparg |= 1;
15601560
}
15611561
break;
15621562
default:

0 commit comments

Comments
 (0)