10000 gh-126835: Move constant unaryop & binop folding to CFG by WolframAlph · Pull Request #129550 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126835: Move constant unaryop & binop folding to CFG #129550

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 35 commits into from
Feb 21, 2025
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 match case folding tests
  • Loading branch information
WolframAlph committed Feb 14, 2025
commit 0fa7c4cb92aedb8dfa2900d36445932c7cbaa26f
83 changes: 68 additions & 15 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3081,21 +3081,6 @@ def test_cli_file_input(self):


class ASTOptimiziationTests(unittest.TestCase):
binop = {
"+": ast.Add(),
"-": ast.Sub(),
"*": ast.Mult(),
"/": ast.Div(),
"%": ast.Mod(),
"<<": ast.LShift(),
">>": ast.RShift(),
"|": ast.BitOr(),
"^": ast.BitXor(),
"&": ast.BitAnd(),
"//": ast.FloorDiv(),
"**": ast.Pow(),
}

unaryop = {
"~": ast.Invert(),
"+": ast.UAdd(),
Expand Down Expand Up @@ -3298,6 +3283,74 @@ def test_folding_type_param_in_type_alias(self):
)
self.assert_ast(result_code, non_optimized_target, optimized_target)

def test_folding_match_case_allowed_expressions(self):
source = textwrap.dedent("""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also want to check for invalid expressions like duplicate mapping keys? For example:

match x:
    case {0j: y, 0j: z} ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They won't trigger error here, this is only ast parsing phase. Error is emitted in codegen.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but it's not tested anywhere currently and since we're changing the logic around match, it feels safer to make sure we don't change something accidentally. Though I don't insist on adding it in this PR, it's more of a general comment

Copy link
Contributor Author
@WolframAlph WolframAlph Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test that, but I am not sure if in this PR as it only touches AST related logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add tests covering various aspects of the feature you're touching.

match 0:
case -0: pass
case -0.1: pass
case -0j: pass
case 1 + 2j: pass
case 1 - 2j: pass
case 1.1 + 2.1j: pass
case 1.1 - 2.1j: pass
case -0 + 1j: pass
case -0 - 1j: pass
case -0.1 + 1.1j: pass
case -0.1 - 1.1j: pass
case {-0: 0}: pass
case {-0.1: 0}: pass
case {-0j: 0}: pass
case {1 + 2j: 0}: pass
case {1 - 2j: 0}: pass
case {1.1 + 2.1j: 0}: pass
case {1.1 - 2.1j: 0}: pass
case {-0 + 1j: 0}: pass
case {-0 - 1j: 0}: pass
case {-0.1 + 1.1j: 0}: pass
case {-0.1 - 1.1j: 0}: pass
case {-0: 0, 0 + 1j: 0, 0.1 + 1j: 0}: pass
""")
expected_constants = (
0,
-0.1,
complex(0, -0),
complex(1, 2),
complex(1, -2),
complex(1.1, 2.1),
complex(1.1, -2.1),
complex(-0, 1),
complex(-0, -1),
complex(-0.1, 1.1),
complex(-0.1, -1.1),
(0, ),
(-0.1, ),
(complex(0, -0), ),
(complex(1, 2), ),
(complex(1, -2), ),
(complex(1.1, 2.1), ),
(complex(1.1, -2.1), ),
(complex(-0, 1), ),
(complex(-0, -1), ),
(complex(-0.1, 1.1), ),
(complex(-0.1, -1.1), ),
(0, complex(0, 1), complex(0.1, 1))
)
consts = iter(expected_constants)
tree = ast.parse(source, optimize=1)
match_stmt = tree.body[0]
for case in match_stmt.cases:
pattern = case.pattern
if isinstance(pattern, ast.MatchValue):
self.assertIsInstance(pattern.value, ast.Constant)
self.assertEqual(pattern.value.value, next(consts))
elif isinstance(pattern, ast.MatchMapping):
keys = iter(next(consts))
for key in pattern.keys:
self.assertIsInstance(key, ast.Constant)
self.assertEqual(key.value, next(keys))
else:
self.fail(f"Expected ast.MatchValue or ast.MatchMapping, found: {type(pattern)}")


if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
Expand Down
Loading
0