Skip to content

Commit

Permalink
LOAD_GLOBAL can only include one PUSH_NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
sweeneyde committed Nov 30, 2023
1 parent 4c9da4c commit 5b08f80
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,5 +1075,28 @@ def test_no_unsafe_static_swap(self):
]
self.cfg_optimization_test(insts, insts, consts=list(range(3)), nlocals=1)

def test_only_one_push_null_per_load_global(self):
# When optimizing func()(), a second pass shouldn't
# let the LOAD_GLOBAL absorb another PUSH_NULL.
before = [
('PUSH_NULL', 0, 1),
('PUSH_NULL', 0, 1),
('LOAD_GLOBAL', 0, 1),
('CALL', 0, 1),
('CALL', 0, 1),
('RETURN_VALUE', 0, 1),
]
after = [
('PUSH_NULL', 0, 1),
('LOAD_GLOBAL', 1, 1),
('CALL', 0, 1),
('CALL', 0, 1),
('RETURN_VALUE', 0, 1),
]
self.cfg_optimization_test(before, expected_insts=after, consts=[])
self.cfg_optimization_test(after, expected_insts=after, consts=[])



if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
case KW_NAMES:
break;
case PUSH_NULL:
if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0) {
if (nextop == LOAD_GLOBAL && (inst[1].i_oparg & 1) == 0) {
INSTR_SET_OP0(inst, NOP);
inst[1].i_oparg |= 1;
}
Expand Down

0 comments on commit 5b08f80

Please sign in to comment.