Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
04b4a84
move binop folding to cfg
WolframAlph Feb 14, 2025
a6babab
update tests
WolframAlph Feb 14, 2025
0fa7c4c
add match case folding tests
WolframAlph Feb 14, 2025
27b1a09
add peepholer tests
WolframAlph Feb 15, 2025
d3df8c8
polish ast code
WolframAlph Feb 15, 2025
e82841a
add assertions
WolframAlph Feb 15, 2025
cd6fbd7
use IS_NUMERIC_CONST_EXPR
WolframAlph Feb 15, 2025
300976e
bring back tests
WolframAlph Feb 15, 2025
3240d8e
move unaryop to cfg
WolframAlph Feb 15, 2025
74d2275
add tests
WolframAlph Feb 15, 2025
8358e28
cancel out unary not
WolframAlph Feb 15, 2025
c1a1be5
fix optimize_if_const_unaryop
WolframAlph Feb 15, 2025
ef9221a
add test_optimize_unary_not
WolframAlph Feb 15, 2025
598adce
add optimize_unary_not_non_const
WolframAlph Feb 16, 2025
bb60d4c
add tests
WolframAlph Feb 16, 2025
eb99870
add tests
WolframAlph Feb 16, 2025
c93627e
add static
WolframAlph Feb 16, 2025
f0f044a
polish
WolframAlph Feb 16, 2025
a02ac66
add unaryop folding tests
WolframAlph Feb 16, 2025
2739fa0
add not not test to catch misoptimized case
WolframAlph Feb 17, 2025
258a5b6
revert old unarynot handing, add contains/is + unarynot folding
WolframAlph Feb 17, 2025
59ee897
address reviews
WolframAlph Feb 17, 2025
91ea2fa
address reviews
WolframAlph Feb 17, 2025
2c5ee86
simplify optimize_if_const_unaryop
WolframAlph Feb 17, 2025
0399fce
address reviews
WolframAlph Feb 19, 2025
3c53923
simplify instr_make_load_const
WolframAlph Feb 19, 2025
a6a06c8
address review for tests
WolframAlph Feb 20, 2025
2ea8425
replace macros
WolframAlph Feb 20, 2025
7c9c69b
update peepholer test
WolframAlph Feb 20, 2025
099afba
define is_unarynegative_const_complex_expr & is_allowed_match_case_bi…
WolframAlph Feb 20, 2025
0dad7c1
try to fold match case expression without checks
WolframAlph Feb 20, 2025
1e8b552
simplify folding
WolframAlph Feb 20, 2025
f4e9a42
address review
WolframAlph Feb 20, 2025
2dba23f
minor adjustments
WolframAlph Feb 21, 2025
cafbc61
Merge branch 'main' into fold-binop-cfg
iritkatriel Feb 21, 2025
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
Prev Previous commit
Next Next commit
add test_optimize_unary_not
  • Loading branch information
WolframAlph committed Feb 15, 2025
commit ef9221a0dc58d13a3da20e559d82aa1b118ce9e7
173 changes: 158 additions & 15 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,31 +1637,57 @@ def test_optimize_literal_set_contains(self):
]
self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])

def test_optimize_unary_not_to_bool(self):
def test_optimize_unary_not(self):
# test folding
before = [
('LOAD_SMALL_INT', 1, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[False])

# test cancel out
before = [
('LOAD_SMALL_INT', 1, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_SMALL_INT', 1, 0),
('RETURN_VALUE', None, 0),
Comment thread
iritkatriel marked this conversation as resolved.
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test eliminate to bool
before = [
('LOAD_NAME', 0, 0),
('UNARY_NOT', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('UNARY_NOT', None, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test folding & elimitnate to bool
Comment thread
WolframAlph marked this conversation as resolved.
Outdated
before = [
('LOAD_SMALL_INT', 1, 0),
('UNARY_NOT', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
Expand All @@ -1670,8 +1696,43 @@ def test_optimize_unary_not_to_bool(self):
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[False])

is_ = 0
isnot = 1
# test cancel out & eliminate to bool (to bool stays)
before = [
('LOAD_SMALL_INT', 1, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_SMALL_INT', 1, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

is_ = in_ = 0
isnot = notin = 1

# test is/isnot
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('IS_OP', is_, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('IS_OP', isnot, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test is/isnot eliminate to bool
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
Expand All @@ -1680,20 +1741,85 @@ def test_optimize_unary_not_to_bool(self):
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('IS_OP', isnot, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

in_ = 0
notin = 1
# test in/notin
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', notin, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test in/notin cancel out
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test in/notin
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('UNARY_NOT', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', notin, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test in/notin cancel out eliminate to bool (to bool stays) (maybe optimize later ???)
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test in/notin eliminate to bool
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
Expand All @@ -1702,17 +1828,34 @@ def test_optimize_unary_not_to_bool(self):
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('TO_BOOL', None, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', notin, 0),
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# test in/notin cancel out eliminate to bool (to bool stays) (maybe optimize later ???)
before = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('UNARY_NOT', None, 0),
('UNARY_NOT', None, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_NAME', 0, 0),
('LOAD_NAME', 1, 0),
('CONTAINS_OP', in_, 0),
('TO_BOOL', None, 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

def test_optimize_if_const_unaryop(self):
pass
Expand Down