Skip to content

gh-126835: make CFG optimizer skip over NOP's when looking for const sequence construction #129703

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 12 commits into from
Feb 9, 2025
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
WolframAlph committed Feb 8, 2025
commit 578d66b4d1bb233391c133c97182e435ad511443
133 changes: 132 additions & 1 deletion Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,18 @@ def test_multiple_foldings(self):
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(2,), (1, 2)])

def test_fold_tuple_of_constants_nops(self):
def test_build_empty_tuple(self):
before = [
('BUILD_TUPLE', 0, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])

def test_fold_tuple_of_constants(self):
before = [
('NOP', None, 0),
('LOAD_SMALL_INT', 1, 0),
Expand All @@ -1103,6 +1114,126 @@ def test_fold_tuple_of_constants_nops(self):
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])

# not enough consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_SMALL_INT', 2, 0),
('BUILD_TUPLE', 3, 0),
('RETURN_VALUE', None, 0)
]
self.cfg_optimization_test(same, same, consts=[])

# not all consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_NAME', 0, 0),
('LOAD_SMALL_INT', 2, 0),
('BUILD_TUPLE', 3, 0),
('RETURN_VALUE', None, 0)
]
self.cfg_optimization_test(same, same, consts=[])

def test_optimize_if_const_list(self):
before = [
('NOP', None, 0),
('LOAD_SMALL_INT', 1, 0),
('NOP', None, 0),
('LOAD_SMALL_INT', 2, 0),
('NOP', None, 0),
('NOP', None, 0),
('LOAD_SMALL_INT', 3, 0),
('NOP', None, 0),
('BUILD_LIST', 3, 0),
('RETURN_VALUE', None, 0),
]
after = [
('BUILD_LIST', 0, 0),
('LOAD_CONST', 0, 0),
('LIST_EXTEND', 1, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])

# need minimum 3 consts to optimize
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_SMALL_INT', 2, 0),
('BUILD_LIST', 2, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])

# not enough consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_SMALL_INT', 2, 0),
('LOAD_SMALL_INT', 3, 0),
('BUILD_LIST', 4, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])

# not all consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_NAME', 0, 0),
('LOAD_SMALL_INT', 3, 0),
('BUILD_LIST', 3, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])

def test_optimize_if_const_set(self):
before = [
('NOP', None, 0),
('LOAD_SMALL_INT', 1, 0),
('NOP', None, 0),
('LOAD_SMALL_INT', 2, 0),
('NOP', None, 0),
('NOP', None, 0),
('LOAD_SMALL_INT', 3, 0),
('NOP', None, 0),
('BUILD_SET', 3, 0),
('RETURN_VALUE', None, 0),
]
after = [
('BUILD_SET', 0, 0),
('LOAD_CONST', 0, 0),
('SET_UPDATE', 1, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[frozenset({1, 2, 3})])

# need minimum 3 consts to optimize
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_SMALL_INT', 2, 0),
('BUILD_SET', 2, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])

# not enough consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_SMALL_INT', 2, 0),
('LOAD_SMALL_INT', 3, 0),
('BUILD_SET', 4, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])

# not all consts
same = [
('LOAD_SMALL_INT', 1, 0),
('LOAD_NAME', 0, 0),
('LOAD_SMALL_INT', 3, 0),
('BUILD_SET', 3, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(same, same, consts=[])


def test_conditional_jump_forward_const_condition(self):
# The unreachable branch of the jump is removed, the jump
# becomes redundant and is replaced by a NOP (for the lineno)
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy