GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-126835: Move constant tuple folding to CFG by WolframAlph · Pull Request #130769 · python/cpython

Conversation

@WolframAlph

@WolframAlph WolframAlph commented Mar 2, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: Move const folding to the peephole optimizer #126835

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@WolframAlph
WolframAlph requested review from Eclips4, iritkatriel, isidentical and markshannon as code owners March 2, 2025 19:44
@bedevere-app bedevere-app Bot added the awaiting review label Mar 2, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Mar 2, 2025
Move const folding to the peephole optimizer #126835
Open
@WolframAlph
WolframAlph requested a review from ericsnowcurrently as a code owner March 2, 2025 19:52
picnixz
picnixz reviewed Mar 2, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Small style comments. It's always better to split assertions into two when possible to know which condition failed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz added the skip news label Mar 2, 2025
@WolframAlph
WolframAlph requested a review from picnixz March 3, 2025 09:07
picnixz
picnixz reviewed Mar 3, 2025
View reviewed changes
Comment thread Lib/test/test_builtin.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/codegen.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Mar 3, 2025
View reviewed changes
Comment thread Lib/test/test_ast/test_ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_opcache.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Mar 3, 2025
View reviewed changes
Comment thread Python/codegen.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 4, 2025

Copy link
Copy Markdown
Contributor Author

After moving folding to codegen (70ce2c8) I can see couple refleaks after running test suite in refleak hunter mode. After some debugging I narrowed down the problem. Long tuple defined directly does not report refleaks:

x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)

but if it is run via compile, we get refleaks:

compile("x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)", "", "single")

no idea yet why this is happening. I will be debugging further, but maybe someone has some insight what could be the problem?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 4, 2025

Copy link
Copy Markdown
Member

Does it leak with compile with other modes (exec, eval)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 4, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Does it leak with compile with other modes (exec, eval)?

Yes. I wonder if it could be some form of false positive.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 4, 2025

Copy link
Copy Markdown
Member

No idea.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Problem found. Turns out codegen_addop_load_const internally creates new reference to the passed constant object.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 5, 2025

Copy link
Copy Markdown
Member

Why does it not happen at the prompt?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

No idea. Maybe refleak checking flaw, but not sure. Maybe someone who knows more about it could tell us?

Back to this PR - if we fold long tuples at codegen stage, we won't be able to fold long tuples that have constant expressions inside, for instance:

(1, 2, 1 + 2, 4, ... 100)  # long constant tuple

will not be folded, unless we fold it in peepholer as written initially. Drawback of having optimizations that have to work together split between different stages.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 5, 2025

Copy link
Copy Markdown
Member

I tend to agree. So I guess you're saying we should choose the second option from #130769 (comment).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Second options assumes that if we have constant tuple, we can skip STACK_USE_GUIDLINE limitation and emit long tuple bytecode relying on peepholer to optimize. However, with constant expressions inside (like in my previous example), at codegen stage we don't know if it is constant or not. And we cannot emit long tuple bytecode, unless we are sure it will be optimized by peepholer. I'm saying it looks like we should stick with optimizing in the peepholer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Second options assumes that if we have constant tuple, we can skip STACK_USE_GUIDLINE limitation and emit long tuple bytecode relying on peepholer to optimize. However, with constant expressions inside (like in my previous example), at codegen stage we don't know if it is constant or not. And we cannot emit long tuple bytecode, unless we are sure it will be optimized by peepholer. I'm saying it looks like we should stick with optimizing in the peepholer.

We could check recursively for constant expressions and emit long tuple bytecode if we know all subexpressions are constant, but I believe it would complicate code more than optimizing it in the peepholer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 5, 2025

Copy link
Copy Markdown
Member

I understand. Ok, let's revert back to the peephole situation then.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Mar 5, 2025

Copy link
Copy Markdown
Member

Another idea (not in this PR) would be to move all the STACK_USE_GUIDELINE stuff to the peephole. So codegen emits code without considering whether it's big or not, and later (after optimisations) we have a pass that finds big stack use and reduces. I don't know how this compares to what we have now in terms of code complexity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

I will make a note and investigate later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph

WolframAlph commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

I've reverted to the peepholer optimization and addressed previous review, please take a look.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph
WolframAlph requested a review from iritkatriel March 5, 2025 20:30
iritkatriel
iritkatriel reviewed Mar 5, 2025
View reviewed changes

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think it's a bit simpler if we grab two instructions in each iteration.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/flowgraph.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@WolframAlph
WolframAlph requested a review from iritkatriel March 6, 2025 08:53
WolframAlph added 16 commits March 18, 2025 13:30
@WolframAlph
update comment
f150c15
@WolframAlph
fold in codegen instead of cfg
4331a08
@WolframAlph
update const tuple creation
70c4388
@WolframAlph
Revert "update const tuple creation"
4a44c02
This reverts commit dfad198.
@WolframAlph
Revert "fold in codegen instead of cfg"
d0cd2a5
This reverts commit 70ce2c8.
@WolframAlph
remove comment
e4d9164
@WolframAlph
replace typeparam tests from test_ast in test_compile
75a2762
@WolframAlph
process 2 instructions per iteration
f0429c8
@WolframAlph
make 2 passes to avoid unnecessary allocations
939fbee
@WolframAlph
add more tests
57e6f07
@WolframAlph
improve styling
4ae59f8
@WolframAlph
address reivew
bffc456
@WolframAlph
get rid of label
e8e70e0
@WolframAlph
inline noping out
a7dbe98
@WolframAlph
add more tests
836c0ee
@WolframAlph
resolve conflicts
72a3e65
@WolframAlph
WolframAlph force-pushed the tuple-fold-cfg branch from 577fb71 to 72a3e65 Compare March 18, 2025 12:34
@WolframAlph
use new nop_out
967a18e
@WolframAlph

WolframAlph commented Mar 19, 2025

Copy link
Copy Markdown
Contributor Author

@iritkatriel can you review again when you have time?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel approved these changes Mar 19, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Mar 19, 2025
@iritkatriel
Merge branch 'main' into tuple-fold-cfg
4b11cf8
@iritkatriel

iritkatriel commented Mar 19, 2025

Copy link
Copy Markdown
Member

!buildbot refleak

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Mar 19, 2025

Copy link
Copy Markdown

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

Results will be shown at:

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

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

The builders matched are:

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel
iritkatriel merged commit 75103d9 into python:main Mar 19, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 19, 2025
@WolframAlph WolframAlph mentioned this pull request Mar 19, 2025
gh-126835: Disable tuple folding in the AST optimizer #128802
Closed
This was referenced Mar 26, 2026
Performance regression: O(N²) compile time for large constant tuples after const folding moved to CFG #146455
Closed
gh-146455: Fix O(N²) in add_const() after constant folding moved to CFG #146456
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz left review comments
@iritkatriel iritkatriel iritkatriel approved these changes
@Eclips4 Eclips4 Awaiting requested review from Eclips4 Eclips4 is a code owner
@isidentical isidentical Awaiting requested review from isidentical
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@WolframAlph @iritkatriel @bedevere-bot @picnixz @Eclips4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.