Skip to content
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

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

Merged
merged 35 commits into from
Feb 21, 2025

Conversation

WolframAlph
Copy link
Contributor

@WolframAlph WolframAlph commented Feb 1, 2025

This PR migrates:

  1. Unary ops optimizations from AST to CFG.
  2. All binary ops optimizations from AST to CFG except for string formatting. FTR: Move const folding to the peephole optimizer #126835 (comment)

cc @Eclips4 @tomasr8

@WolframAlph WolframAlph changed the title gh-126835: [WIP]: move binop folding to cfg gh-126835: Move constant binop folding to CFG Feb 15, 2025
@WolframAlph WolframAlph marked this pull request as ready for review February 15, 2025 08:28
value=ast.Constant(value=1),
)
)
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.

@WolframAlph
Copy link
Contributor Author

I've addressed comments & updated ast_opt.c to not use macros for node attribute access.

@WolframAlph
Copy link
Contributor Author

@iritkatriel CI reports unused functions, but we need them in assertions. So either mark them as used or convert them to macros I guess?

@iritkatriel
Copy link
Member

@iritkatriel CI reports unused functions, but we need them in assertions. So either mark them as used or convert them to macros I guess?

Put them under #ifndef NDEBUG.

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Feb 20, 2025

I've addressed review & simplifed match pattern folding. One question remaining whether we should add tests with hand crafted ast with invalid expressions in match statement case.

Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

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

Looks good!

@iritkatriel
Copy link
Member

This is looking good. I made some final minor comments.

Are you still planning to add tests for invalid hand-crafted ASTs?

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Feb 21, 2025

Done. Honestly I don't think we need them. We already test folding in match cases quite extensively, and current folding is the same as previous, just for limited expressions. We could add them for completeness, but probably in another PR as this one is getting quite big. But if you think we need them, I could add to this.

@iritkatriel
Copy link
Member

Sure.

@iritkatriel iritkatriel added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 21, 2025
@iritkatriel
Copy link
Member

It would be good to add smoke tests to make sure we don't crash on invalid ast input. Can be in another pr.

@WolframAlph
Copy link
Contributor Author

Thanks @iritkatriel for guidance & patience.

@iritkatriel iritkatriel added 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section and removed 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Feb 21, 2025
@iritkatriel
Copy link
Member

!buildbot refleak

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @iritkatriel for commit cafbc61 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F129550%2Fmerge

The command will test the builders whose names match following regular expression: refleak

The builders matched are:

  • aarch64 RHEL8 Refleaks PR
  • s390x RHEL8 Refleaks PR
  • AMD64 RHEL8 Refleaks PR
  • AMD64 FreeBSD Refleaks PR
  • PPC64LE Fedora Stable Refleaks PR
  • PPC64LE CentOS9 Refleaks PR
  • AMD64 CentOS9 NoGIL Refleaks PR
  • AMD64 Fedora Stable Refleaks PR
  • AMD64 Fedora Rawhide NoGIL refleaks PR
  • aarch64 Fedora Stable Refleaks PR
  • PPC64LE Fedora Rawhide Refleaks PR
  • AMD64 CentOS9 Refleaks PR
  • PPC64LE RHEL8 Refleaks PR
  • s390x RHEL9 Refleaks PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • AMD64 Windows11 Refleaks PR
  • PPC64LE Fedora Rawhide NoGIL refleaks PR
  • AMD64 Fedora Rawhide Refleaks PR
  • aarch64 Fedora Rawhide Refleaks PR
  • aarch64 CentOS9 Refleaks PR
  • aarch64 Fedora Rawhide NoGIL refleaks PR

@iritkatriel iritkatriel merged commit 38642bf into python:main Feb 21, 2025
61 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants