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-131798: JIT: Optimize `_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW` by tomasr8 · Pull Request #134369 · python/cpython

Conversation

@tomasr8

@tomasr8 tomasr8 commented May 20, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Optimize _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW

This requires adding some additional ops:

  • _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW
  • _POP_CALL_LOAD_CONST_INLINE_BORROW
  • _POP_THREE
  • _POP_TWO

We can now optimize:

_LOAD_CONST_INLINE_BORROW
_LOAD_CONST_INLINE_BORROW
_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW

into just

_POP_CALL_LOAD_CONST_INLINE_BORROW

i.e. we save two stack pushes and two stack pops.

  • Issue: Better uop coverage in the JIT optimizer #131798

Sorry, something went wrong.

Uh oh!

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

All reactions
@tomasr8
tomasr8 requested a review from brandtbucher May 20, 2025 17:47
@tomasr8
tomasr8 requested review from Fidget-Spinner and markshannon as code owners May 20, 2025 17:47
@bedevere-app bedevere-app Bot mentioned this pull request May 20, 2025
Better uop coverage in the JIT optimizer #131798
Open
@bedevere-app bedevere-app Bot added the awaiting review label May 20, 2025
@tomasr8
tomasr8 force-pushed the jit-optimize-pop-call-two branch from f37b9c3 to 82e0a17 Compare May 20, 2025 18:10
@tomasr8
tomasr8 marked this pull request as draft May 20, 2025 20:34
@bedevere-app bedevere-app Bot removed the awaiting review label May 20, 2025
Fidget-Spinner
Fidget-Spinner reviewed May 21, 2025
View reviewed changes
Comment thread Python/bytecodes.c Outdated
PyStackRef_CLOSE(pop1);
}

tier2 pure op(_POP_CALL_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null -- value)) {

@Fidget-Spinner Fidget-Spinner May 21, 2025

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.

Can you remove the pure annotations from these please? They don't get optimized to anything so there's not really a use for them.

Sorry, something went wrong.

Uh oh!

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

tomasr8 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Fidget-Spinner
Fidget-Spinner requested changes May 21, 2025
View reviewed changes

@Fidget-Spinner Fidget-Spinner left a comment •
edited
Loading

Uh oh!

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

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.

Thanks for the PR. I think @brandtbucher brought this up before, but I think we should be making the uop buffer add-to rather than in-place. What I mean is that instead of adding a pop variant for every optimization we want to do:

...
_POP_X_LOAD_CONST_INLINE_BORROW

We should add:

...
_POP_TOP
_POP_TOP
...
_LOAD_CONST_INLINE_BORROW

This way:

  1. We don't have to add a new special pop uop for every single op. This saves on mental overhead, because a special pop variant of the uop for every uop we want to optimize is really tough to reason about.
  2. In the optimizer, we can just generalize it to POP_TOP X times, which is easier to write code to optimize for, rather than add a new entry to remove_unneeded_uops. As you probably noticed already, it's really tedious to do this.

This would require allowing us to add to the uop buffer, rather than overwriting it in-place. It might be a bigger change, but if you're up for it, it would greatly simplify all future optimizations, and I'd be grateful for someone cleaning that part up. It would best be a separate PR.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app

bedevere-app Bot commented May 21, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot added the awaiting changes label May 21, 2025
@tomasr8

tomasr8 commented May 21, 2025

Copy link
Copy Markdown
Member Author

Thanks for the PR. I think @brandtbucher brought this up before, but I think we should be making the uop buffer add-to rather than in-place.

We actually discussed this w/ Brandt yesterday and I totally agree with that. Brandt also mentioned that it is ok to add this for now but I don't mind closing this and focusing on changing the uop buffer right away.

Note that Brandt greatly simplified remove_unneeded_uops yesterday in #134373. Once I rebase this PR, it should end up being much simpler.

cc @brandtbucher

Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented May 21, 2025

Copy link
Copy Markdown
Member

Yeah, 90% of the reason why I felt the urgency to move away from in-place modification was because the logic in the pass that removes matching pushes and pops was getting completely out-of-hand. That's not a concern anymore, so I think we can keep the momentum going on the current in-place optimizer work for the time being.

Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@Fidget-Spinner

Fidget-Spinner commented May 21, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Yeah, 90% of the reason why I felt the urgency to move away from in-place modification was because the logic in the pass that removes matching pushes and pops was getting completely out-of-hand. That's not a concern anymore, so I think we can keep the momentum going on the current in-place optimizer work for the time being.

Ok. Can we please make sure to move away from the in-place optimizer after this PR for any new pop tops load const inline X instructions? The pushes and pops are no longer an issue, but the number of new uops variants we're adding is getting out of hand. I have no clue what some of these uops do unless I specifically look up their emission in the optimizer. We don't need this many uops and fewer uops is generally a better thing (less maintainer burden, less human error ...). If we can generalize to just a few replicate(7) POP_TOP_X instructions, that would be awesome.

brandtbucher and tomasr8 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented May 21, 2025

Copy link
Copy Markdown
Member

Yep, I agree. It also allows us to specialize individual pops for specific known types, immortal values, non-escaping pops, etc. After the sprint ends tomorrow, we can start with larger cleanup efforts (which I agree we need, in the tracer too).

tomasr8 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@Fidget-Spinner

Fidget-Spinner commented May 21, 2025

Copy link
Copy Markdown
Member

Great! Feel free to dismiss my review then in the GH UI.

All reactions

Sorry, something went wrong.

Uh oh!

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

@tomasr8
tomasr8 force-pushed the jit-optimize-pop-call-two branch 4 times, most recently from 3d71a8e to 47995d0 Compare May 22, 2025 15:47
@tomasr8
Optimize _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
90d38d7
@tomasr8
tomasr8 force-pushed the jit-optimize-pop-call-two branch from 47995d0 to 90d38d7 Compare May 22, 2025 15:48
@tomasr8
Add news entry
0e66db4
@tomasr8
tomasr8 marked this pull request as ready for review May 22, 2025 15:50
@bedevere-app bedevere-app Bot added awaiting review and removed awaiting changes labels May 22, 2025
tomasr8 added 3 commits May 22, 2025 17:54
@tomasr8
Trailing whitespace
0e1030b
@tomasr8
Simplify logic
af88646
@tomasr8
Wrap line
3ed47d2
tomasr8 added 4 commits May 22, 2025 18:06
@tomasr8
PEP7
bf27867
@tomasr8
PEP7 vol. 2
2f90609
@tomasr8
Simplify logic
5db29d8
@tomasr8
Style fixes
efbded1
brandtbucher
brandtbucher approved these changes May 22, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels May 22, 2025
@brandtbucher

brandtbucher commented May 22, 2025

Copy link
Copy Markdown
Member

When I get back, I'll start working on making the optimizer operate on a copy of the stream. This is good for now though. Really cool to see entire calls start disappearing.

tomasr8 reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

YvesDup pushed a commit to YvesDup/cpython that referenced this pull request May 22, 2025
@tomasr8
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
484e003
…134369)
@brandtbucher
brandtbucher merged commit d1ea8ed into python:main May 22, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label May 22, 2025
@brandtbucher

brandtbucher commented May 22, 2025

Copy link
Copy Markdown
Member

Looks great. @tomasr8: natural follow-ups would be support for tuples of types in isinstance calls, as well as extending this optimization to the other calls we specialize (and seeing if we should possibly specialize more of them).

All reactions

Sorry, something went wrong.

Uh oh!

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

@tomasr8
tomasr8 deleted the jit-optimize-pop-call-two branch May 22, 2025 18:06
@tomasr8

tomasr8 commented May 22, 2025

Copy link
Copy Markdown
Member Author

Looks great. @tomasr8: natural follow-ups would be support for tuples of types in isinstance calls, as well as extending this optimization to the other calls we specialize (and seeing if we should possibly specialize more of them).

on it!

All reactions

Sorry, something went wrong.

Uh oh!

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

lkollar pushed a commit to lkollar/cpython that referenced this pull request May 26, 2025
@tomasr8 @lkollar
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
72fe669
…134369)
lkollar pushed a commit to lkollar/cpython that referenced this pull request May 26, 2025
@tomasr8 @lkollar
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
9ba8acd
…134369)
lkollar pushed a commit to lkollar/cpython that referenced this pull request May 26, 2025
@tomasr8 @lkollar
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
458988c
…134369)
lkollar pushed a commit to lkollar/cpython that referenced this pull request May 30, 2025
@tomasr8 @lkollar
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
fa9a151
…134369)
lkollar pushed a commit to lkollar/cpython that referenced this pull request May 31, 2025
@tomasr8 @lkollar
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
2a45348
…134369)
@tomasr8 tomasr8 mentioned this pull request Jun 5, 2025
gh-131798: JIT: Optimize _CALL_TYPE_1 when the result is known #135194
Merged
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@tomasr8 @Pranjal095
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
6b9964d
…134369)
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@tomasr8 @Pranjal095
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
305d517
…134369)
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@tomasr8 @Pranjal095
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
60e6243
…134369)
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@tomasr8 @taegyunkim
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
4167454
…134369)
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@tomasr8 @taegyunkim
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
b2091bd
…134369)
gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
@tomasr8 @gostak-dd
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
6bb5112
…134369)
gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
@tomasr8 @gostak-dd
pythonGH-131798: Optimize away isinstance calls in the JIT (pythonGH-…
55fbc43
…134369)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner requested changes
@brandtbucher brandtbucher brandtbucher approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

3 participants

@tomasr8 @brandtbucher @Fidget-Spinner
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 01402503 — 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.