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-106529: Make FOR_ITER a viable uop by gvanrossum · Pull Request #112134 · python/cpython

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Nov 15, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Also clean up a few nits in the code generator, and add back an important Make dependency for ceval.o.

  • Issue: Branching design for Tier 2 (uops) interpreter #106529

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 15, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Nov 15, 2023
Branching design for Tier 2 (uops) interpreter #106529
Closed
7 of 11 tasks
@gvanrossum
gvanrossum marked this pull request as draft November 15, 2023 23:44
@bedevere-app bedevere-app Bot removed the awaiting core review label Nov 15, 2023
@gvanrossum

gvanrossum commented Nov 15, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Hold on, need to remove merge conflicts from commit history.

All reactions

Sorry, something went wrong.

Uh oh!

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

gvanrossum added 5 commits November 15, 2023 15:54
@gvanrossum
Add executor_cases.c.h dependency for ceval.o
a08909d
@gvanrossum
Clean up flags.py
4c2914b
@gvanrossum
Clean up parsing.py
053a0a2
@gvanrossum
Add back printing optimized uops
b838435
@gvanrossum
Hacky way to make FOR_ITER a viable uop
b28effa
@gvanrossum
gvanrossum force-pushed the for-iter-uop branch from e909a83 to b28effa Compare November 15, 2023 23:58
@gvanrossum
gvanrossum marked this pull request as ready for review November 15, 2023 23:58
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 15, 2023
gvanrossum
gvanrossum commented Nov 16, 2023
View reviewed changes
Comment thread Tools/cases_generator/flags.py
skipping = False
for i, token in enumerate(node.tokens):
if token.kind == "MACRO":
if token.kind == "CMACRO":

@gvanrossum gvanrossum Nov 16, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

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.

NOTE: This fix resulted in _SPECIALIZE_UNPACK_SEQUENCE becoming a viable uop. It was missing a TIER_ONE_ONLY marker; I've added it back. (The fix is needed to restore the feature that this function doesn't look inside #if TIER_ONE.)

Sorry, something went wrong.

Uh oh!

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

All reactions
@gvanrossum
_SPECIALIZE_UNPACK_SEQUENCE is TIER_ONE_ONLY
de8f199
@gvanrossum

gvanrossum commented Nov 16, 2023

Copy link
Copy Markdown
Member Author

@brandtbucher Let me know how bad this interferes with the JIT branch. The stuff I put in _FOR_ITER_TIER_TWO probably shouldn't be compiled literally into the template. Maybe I should make part of the code at the deoptimize label into a macro? (But the setting of frame->instr_ptr must differ -- we can't use target since that's also used by error exits, since GH-112065.)

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
NEWS
5c5d8bd
@gvanrossum

gvanrossum commented Nov 16, 2023

Copy link
Copy Markdown
Member Author

Benchmark results:
https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20231115-3.13.0a1+-5c5d8bd-PYTHON_UOPS

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
Double max trace length to 256
36e9ada
@brandtbucher

brandtbucher commented Nov 16, 2023

Copy link
Copy Markdown
Member

@brandtbucher Let me know how bad this interferes with the JIT branch. The stuff I put in _FOR_ITER_TIER_TWO probably shouldn't be compiled literally into the template. Maybe I should make part of the code at the deoptimize label into a macro? (But the setting of frame->instr_ptr must differ -- we can't use target since that's also used by error exits, since GH-112065.)

So, this doesn't actually look too bad. I think with a tiny bit of reworking, this could be merged into the JIT branch with no pain:

  • Move the line that sets frame->instr_ptr from the exit_trace label into the _EXIT_TRACE instruction, using a macro (like CURRENT_TARGET() or something) to access the target member.
  • Remove the stuff from _FOR_ITER_TIER_TWO that updates the stats, saves the stack pointer, and decrefs the executor, and just goto exit_trace; instead after updating frame->instr_ptr.
All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
Move stuff around to suit the JIT branch
def1830
@gvanrossum
gvanrossum requested a review from markshannon as a code owner November 16, 2023 23:40
@gvanrossum

gvanrossum commented Nov 16, 2023

Copy link
Copy Markdown
Member Author

Okay, I think I managed to do that. I can now merge into the justin branch with only a single trivial merge conflict (we both added something to the end of ceval_macros.h). Next up of course, how would I redefine CURRENT_TARGET() in templace.c?

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Nov 16, 2023

Copy link
Copy Markdown
Member

#define CURRENT_TARGET() (target)

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Nov 16, 2023

Copy link
Copy Markdown
Member Author

#define CURRENT_TARGET() (target)

Yup, and I also needed to move the exit_trace: label one line down (to avoid setting frame->instr_ptr twice).

It seems to work, but unsure how to prove it (it compiles and passes tests, but it would too if the JIT was never invoked).

Anyway, we should probably wait until we've decided that making Tier 2 5% slower is a good idea.

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Nov 17, 2023

Copy link
Copy Markdown
Member

There seem to be a few unrelated changes in this PR.
Could you make another PR for the increase to the uop buffer size, extra debugging and change to UNPACK_SEQUENCE?

All reactions

Sorry, something went wrong.

Uh oh!

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

markshannon
markshannon reviewed Nov 17, 2023
View reviewed changes
Comment thread Python/bytecodes.c Outdated
/* iterator ended normally */
Py_DECREF(iter);
STACK_SHRINK(1);
/* HACK: Emulate DEOPT_IF to jump over END_FOR */

@markshannon markshannon Nov 17, 2023

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.

No hacks, please 🙂

The code should look like this:

if (next == NULL) {
    if (_PyErr_Occurred(tstate)) {
        if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
            GOTO_ERROR(error);
        }
        _PyErr_Clear(tstate);
    }
    /* iterator ended normally */
    Py_DECREF(iter);
    STACK_SHRINK(1);
    DEOPT_IF(true);
}

The trace generator can adjust the target, so it points after the END_FOR.

Sorry, something went wrong.

Uh oh!

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

gvanrossum reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@gvanrossum

gvanrossum commented Nov 17, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Could you make another PR for the increase to the uop buffer size, extra debugging and change to UNPACK_SEQUENCE?

Sure, see gh-112214. I've merged that into main, since the tests pass, next I'll merge it into this PR and do the other thing you requested.

All reactions

Sorry, something went wrong.

Uh oh!

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

gvanrossum added a commit that referenced this pull request Nov 17, 2023
@gvanrossum
gh-106529: Cleanups split off gh-112134 (#112214)
be0bd54
- Double max trace size to 256
- Add a dependency on executor_cases.c.h for ceval.o
- Mark `_SPECIALIZE_UNPACK_SEQUENCE` as `TIER_ONE_ONLY`
- Add debug output back showing the optimized trace
- Bunch of cleanups to Tools/cases_generator/
@gvanrossum
Merge remote-tracking branch 'origin/main' into for-iter-uop
ce19637
gvanrossum added 2 commits November 17, 2023 12:59
@gvanrossum
Clean up _FOR_ITER_TIER_TWO using DEOPT_IF(true)
7096818
@gvanrossum
Add test
5852105
@gvanrossum

gvanrossum commented Nov 17, 2023

Copy link
Copy Markdown
Member Author

Okay, here's the new version. @markshannon Want to review one more time?

All reactions

Sorry, something went wrong.

Uh oh!

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

brandtbucher
brandtbucher reviewed Nov 17, 2023
View reviewed changes
Comment thread Python/bytecodes.c Resolved

Uh oh!

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

brandtbucher
brandtbucher reviewed Nov 17, 2023
View reviewed changes
Comment thread Python/optimizer.c Resolved

Uh oh!

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

brandtbucher
brandtbucher reviewed Nov 17, 2023
View reviewed changes
Comment thread Tools/cases_generator/instructions.py Resolved

Uh oh!

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

gvanrossum added 3 commits November 17, 2023 14:06
@gvanrossum
Revert debug change to is_viable_uop()
4ac68b3
@gvanrossum
Avoid debug-only local variable 'word'
95b1a01
@gvanrossum
Revert changes to _EXIT_TRACE logic
4c72028
@gvanrossum

gvanrossum commented Nov 17, 2023

Copy link
Copy Markdown
Member Author

I've applied Brandt's suggestions.

I've also split off some more cleanups and debug improvements into gh-112218 and gh-112220. Once those land I'll merge main once more and this should shrink a bit more (the cases_generator tweaks will be separated out).

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
Merge remote-tracking branch 'origin/main' into for-iter-uop
14aea56
@gvanrossum

gvanrossum commented Nov 18, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Benchmark says 4% slower (with Tier 2):
https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20231117-3.13.0a1+-14aea56-PYTHON_UOPS

  • For spectral_norm, one out of five traces is still too long (that must be one hell of a trace :-)
  • Also, spectral_norm no longer uses any unsupported opcodes (nice!)
  • Pystats diff show about 8% more traces executed, 8% more uops executed
  • Overall, FOR_ITER_GEN is now by far the most-used unsupported opcode (74k, vs. 9k for the next most-used, CALL)
All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
Merge branch 'main' into for-iter-uop
88c1701
@gvanrossum

gvanrossum commented Nov 19, 2023

Copy link
Copy Markdown
Member Author

FWIW, I have a branch on top of this that makes the uop interpreter 3% faster. The approach is to let the code generator generate code that extracts oparg and operand from the instruction stream as needed (i.e., only for opcodes that use them) rather than the interpreter loop preemptively extracting them for every instruction. I'm not completely happy with it, and it probably would make life a little harder for the JIT template, which doesn't like directly referencing next_uop -- this should probably wait until we're ready to generate the JIT template code separately from the Tier 2 interpreter code.

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Nov 20, 2023

Copy link
Copy Markdown
Member

Honestly, as long as it’s done in macros, it shouldn’t be too bad. Macros are a great escape hatch in the short term before we start generating bespoke “JIT cases”.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Nov 20, 2023

Copy link
Copy Markdown
Member Author

Honestly, as long as it’s done in macros, it shouldn’t be too bad. Macros are a great escape hatch in the short term before we start generating bespoke “JIT cases”.

So it could generate something like

case _FOO_UOP: {
    oparg = CURRENT_OPARG();
    operand = CURRENT_OPERAND();
    ...
}

where the default definitions for those (in ceval_macros.h) would be

#define CURRENT_OPARG() next_uop[-1].oparg
#define CURRENT_OPERAND() next_uop[-1].operand
All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Nov 20, 2023

Copy link
Copy Markdown
Member

Yup!

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
gvanrossum merged commit 1995955 into python:main Nov 20, 2023
@bedevere-app bedevere-app Bot removed the awaiting core review label Nov 20, 2023
@gvanrossum
gvanrossum deleted the for-iter-uop branch November 20, 2023 18:09
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@gvanrossum @aisk
pythongh-106529: Cleanups split off pythongh-112134 (python#112214)
2074f35
- Double max trace size to 256
- Add a dependency on executor_cases.c.h for ceval.o
- Mark `_SPECIALIZE_UNPACK_SEQUENCE` as `TIER_ONE_ONLY`
- Add debug output back showing the optimized trace
- Bunch of cleanups to Tools/cases_generator/
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@gvanrossum @aisk
pythongh-106529: Make FOR_ITER a viable uop (python#112134)
1a18d22
This uses the new mechanism whereby certain uops
are replaced by others during translation,
using the `_PyUop_Replacements` table.
We further special-case the `_FOR_ITER_TIER_TWO` uop
to update the deoptimization target to point
just past the corresponding `END_FOR` opcode.

Two tiny code cleanups are also part of this PR.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@gvanrossum @Glyphack
pythongh-106529: Cleanups split off pythongh-112134 (python#112214)
4b85885
- Double max trace size to 256
- Add a dependency on executor_cases.c.h for ceval.o
- Mark `_SPECIALIZE_UNPACK_SEQUENCE` as `TIER_ONE_ONLY`
- Add debug output back showing the optimized trace
- Bunch of cleanups to Tools/cases_generator/
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@gvanrossum @Glyphack
pythongh-106529: Make FOR_ITER a viable uop (python#112134)
e235eb1
This uses the new mechanism whereby certain uops
are replaced by others during translation,
using the `_PyUop_Replacements` table.
We further special-case the `_FOR_ITER_TIER_TWO` uop
to update the deoptimization target to point
just past the corresponding `END_FOR` opcode.

Two tiny code cleanups are also part of this PR.
@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
majit: open the retrace path, and detect a stale LLBC before it ships wrong field offsets pyre-lang/pyre#1062
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon markshannon left review comments
@brandtbucher brandtbucher brandtbucher left review comments

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

@gvanrossum @brandtbucher @markshannon
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.