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-111520: Integrate the Tier 2 interpreter in the Tier 1 interpreter by gvanrossum · Pull Request #111428 · python/cpython

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Oct 28, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

See also faster-cpython/ideas#631

Beware! This changes the env vars to enable uops and their debugging to PYTHON_UOPS and PYTHON_LLTRACE.

  • Issue: Merge Tier 1 and 2 interpreters into a single function #111520

Sorry, something went wrong.

Uh oh!

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

All reactions
gvanrossum added 5 commits October 27, 2023 13:24
@gvanrossum
Make all labels in _PyUopExecute end in _tier_two
97984d3
Use `GOTO_ERROR(error)` instead of `goto error`.
This macro is defined differently in the tier two interpreter.
@gvanrossum
Control lltrace via PYTHON_LLTRACE=N
157a450
This replaces PYTHONUOPSDEBUG=N. The meaning of N is the same (for now):

  0: no tracing
  1: print when tier 2 trace created
  2: print contents of tier 2 trace
  3: print every uop executed
  4: print optimization attempts and details
  5: print tier 1 instructions and stack
@gvanrossum
Rename PYTHONUOPS to PYTHON_UOPS for consistency
d1b9c1b
@gvanrossum
Integrate Tier 2 into _PyEval_EvalFrameDefault
d805312
@gvanrossum
DO NOT MERGE: Always use -Xuops
e0e60ce
@gvanrossum

gvanrossum commented Oct 29, 2023

Copy link
Copy Markdown
Member Author

With uops always enabled, only test_embed fails, but that's the same on main (see GH-111339). I added that to benchmark Tier 2 perf (who knows, the speedier Tier switching may make things faster there).

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Oct 29, 2023

Copy link
Copy Markdown
Member Author

@markshannon Can you review this? If you think this is roughly going in the right direction I will clean it up.

All reactions

Sorry, something went wrong.

Uh oh!

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

markshannon
markshannon reviewed Oct 30, 2023
View reviewed changes

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

Looks promising.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/bytecodes.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Outdated

OPT_STAT_INC(traces_executed);
_Py_CODEUNIT *ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive;
_PyUOpInstruction *next_uop = self->trace;

@markshannon markshannon Oct 30, 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.

This should have been set by ENTER_EXECUTOR.

Sorry, something went wrong.

Uh oh!

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

All reactions

@gvanrossum gvanrossum Oct 31, 2023

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.

Yes and no. It would mean that next_uop would have to be a local in the top-level function scope. I am trying to reduce the number of locals there. For now let me keep it this way. I expect that it will be fine even when we start stitching, because each trace can only be entered at the top and we need the executor (so that we can decref it upon exiting the trace). The initial next_uop is just the executor plus a fixed offset anyway.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c
);
goto error_tier_two;

pop_4_error_tier_two:

@markshannon markshannon Oct 30, 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.

Can't this be shared with tier 1? Unwinding should work the same.

Sorry, something went wrong.

Uh oh!

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

All reactions

@gvanrossum gvanrossum Oct 31, 2023

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.

Alas, not quite. The debug output is different, the stats collection is different, but most importantly, we need to DECREF(self) here before jumping to error.

Sorry, something went wrong.

Uh oh!

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

All reactions

@gvanrossum gvanrossum Oct 31, 2023

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.

Also we need to set next_instr = frame->instr_ptr.

Sorry, something went wrong.

Uh oh!

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

All reactions
@gvanrossum gvanrossum changed the title Integrate the Tier 2 interpreter in the Tier 1 interpreter gh-111520: Integrate the Tier 2 interpreter in the Tier 1 interpreter Oct 30, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Oct 30, 2023
Merge Tier 1 and 2 interpreters into a single function #111520
Closed
gvanrossum added 4 commits October 30, 2023 13:22
@gvanrossum
Merge branch 'main' into mix-tiers
a720f1a
@gvanrossum
Merge branch 'main' into mix-tiers
b808f6d
@gvanrossum
Get rid of separate executor.c file
a0aed59
@gvanrossum
Most suggestions from Mark's code review
75605c7
- Remove CHECK_EVAL_BREAKER() from top of Tier 2 loop
- Make Tier 2 default case Py_UNREACHABLE() in non-debug mode
- GOTO_TIER_TWO() macro instead of `goto enter_tier_two`
- Inline ip_offset (Tier 2 LOAD_IP() is now a no-op)
- Move #define/#under TIER_ONE/TIER_TWO into generated .c.h files
- ceval_macros.h defines Tier 1 macros, Tier 2 is inlined in ceval.c
gvanrossum
gvanrossum commented Oct 31, 2023
View reviewed changes
Comment thread Python/bytecodes.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Outdated

OPT_STAT_INC(traces_executed);
_Py_CODEUNIT *ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive;
_PyUOpInstruction *next_uop = self->trace;

@gvanrossum gvanrossum Oct 31, 2023

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.

Yes and no. It would mean that next_uop would have to be a local in the top-level function scope. I am trying to reduce the number of locals there. For now let me keep it this way. I expect that it will be fine even when we start stitching, because each trace can only be entered at the top and we need the executor (so that we can decref it upon exiting the trace). The initial next_uop is just the executor plus a fixed offset anyway.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c
);
goto error_tier_two;

pop_4_error_tier_two:

@gvanrossum gvanrossum Oct 31, 2023

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.

Alas, not quite. The debug output is different, the stats collection is different, but most importantly, we need to DECREF(self) here before jumping to error.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/ceval.c
Comment on lines +1048 to +1051
frame->return_offset = 0; // Don't leave this random
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
goto resume_with_error;

@gvanrossum gvanrossum Oct 31, 2023

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.

We may not need to reset return_offset; we can also skip syncing stack_pointer; so we could make this slightly faster as follows (but it doesn't matter since errors are presumed to be on the slow path):

Suggested change
frame->return_offset = 0; // Don't leave this random
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
goto resume_with_error;
Py_DECREF(self);
next_instr = frame->instr_ptr;
goto error;

Sorry, something went wrong.

Uh oh!

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

All reactions
gvanrossum added 2 commits October 31, 2023 16:41
@gvanrossum
Fix test_generated_cases.py by stripping preprocessor prefix/suffix
5e84476
@gvanrossum
Eradicate executors.c from Windows build files
917b7a2
gvanrossum
gvanrossum commented Nov 1, 2023
View reviewed changes

@gvanrossum gvanrossum left a comment

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.

I consider this sufficiently cleaned up to undraft it, and if @markshannon doesn't comment I'll merge tomorrow.

I could noodle endlessly with the code for dropping from tier 2 back into tier 1, with and without errors; but it may be better to put that off until a more careful review.

Sorry, something went wrong.

Uh oh!

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

All reactions
@gvanrossum
gvanrossum marked this pull request as ready for review November 1, 2023 00:23
@gvanrossum
gvanrossum requested a review from a team as a code owner November 1, 2023 00:23
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 1, 2023
@gvanrossum
Rename deoptimize_tier_two back to deoptimize (for Justin)
9067eb0
@gvanrossum
gvanrossum marked this pull request as draft November 1, 2023 00:42
@bedevere-app bedevere-app Bot removed the awaiting core review label Nov 1, 2023
@gvanrossum

gvanrossum commented Nov 1, 2023

Copy link
Copy Markdown
Member Author

Whoops, back to draft, I accidentally left in the uops-forever cherrypick. Will fix later.

All reactions

Sorry, something went wrong.

Uh oh!

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

gvanrossum added 3 commits October 31, 2023 17:43
@gvanrossum
Fix whitespace
6a4e495
@gvanrossum
Revert "DO NOT MERGE: Always use -Xuops"
81f1883
(I accidentally kept this commit after pushing it experimentally.
This means I've been testing with uops on all the time, which is
actually pretty amazing.)

This reverts commit e0e60ce.
@gvanrossum
Add blurb
ee27e73
@gvanrossum
gvanrossum marked this pull request as ready for review November 1, 2023 04:35
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 1, 2023
gvanrossum added 3 commits November 1, 2023 08:04
@gvanrossum
Eliminate 'operand' local variable
a96ac7f
@gvanrossum
Rename self -> current_executor (TODO: eliminate it?)
e02409d
@gvanrossum
Move _EXIT_TRACE logic to a separate label
fdf1a2f
Using `GOTO_TIER_ONE()` macro. This should make things simpler
for Justin.
@zooba

zooba commented Nov 1, 2023

Copy link
Copy Markdown
Member

Alas, we still run out of stack space on the recursion tests on Windows. I'll keep working on this.

The tests run against debug builds, which will have very different stack usage patterns compared to optimised builds. I wouldn't worry too much about changing anything other than the default recursion limit (which is different for debug vs. release), and then do a buildbot run to check the optimised builds.

Otherwise, the best way to reduce stack usage will be to reduce the size of local variables, possibly by refactoring into separate functions so that the variables are only allocated temporarily and don't remain on the stack as the Python code recurses.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Nov 1, 2023 •
edited by AlexWaygood
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Otherwise, the best way to reduce stack usage will be to reduce the size of local variables, possibly by refactoring into separate functions so that the variables are only allocated temporarily and don't remain on the stack as the Python code recurses.

But the whole exercise is to merge two functions in one because we want to be able to transfer back and forth using goto rather than calls!

I will see by how much the debug recursion limit needs to be adjusted to make the tests pass; I think I have only two extra local variables left.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum gvanrossum added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 1, 2023
@bedevere-bot

bedevere-bot commented Nov 1, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @gvanrossum for commit fdf1a2f 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 1, 2023
gvanrossum added 4 commits November 1, 2023 11:06
@gvanrossum
Limit infinite recursion in test_typing
2a6450c
@gvanrossum
Limit infinite recursion in test_fileio
4783de3
@gvanrossum
Limit infinite recursion in test_xml_etree
b9516a1
@gvanrossum
Limit infinite recursion in test_call
33c3fae
@gvanrossum
gvanrossum requested review from AlexWaygood and JelleZijlstra as code owners November 1, 2023 18:28
@AlexWaygood
AlexWaygood removed their request for review November 1, 2023 18:30
@gvanrossum

gvanrossum commented Nov 1, 2023

Copy link
Copy Markdown
Member Author

If this now passes the tests and the buildbots don't freak out I am going to merge this. However, @markshannon, could you have a look at how I fixed test_call? This feels like a band-aid.

All reactions

Sorry, something went wrong.

Uh oh!

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

@zooba

zooba commented Nov 1, 2023

Copy link
Copy Markdown
Member

Another fix would be to increase the value on this line:

cpython/PCbuild/python.vcxproj

Line 98 in 5d6db16

<StackReserveSize Condition="$(Configuration) == 'Debug'">8000000</StackReserveSize>

That's what determines how much stack is available, and running out is what causes the hard crash. As you can see, it's already 4x larger for debug builds than releases, but it's still only 8MB and so making it larger isn't really going to hurt anyone much.

(I'll note that I haven't looked through the whole PR, so I don't know exactly what is causing it or whether the existing recursion limit comes into play at all. I'm just throwing out helpful pointers and trusting that you guys know what's actually going on :) )

All reactions

Sorry, something went wrong.

Uh oh!

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

gvanrossum added 3 commits November 1, 2023 11:57
@gvanrossum
Fix test_call better: adjust Py_C_RECURSION_LIMIT in pystate.h
998e054
@gvanrossum
Revert unnecessary fixes to recursive tests
19d9d40
@gvanrossum
Even better fix -- increase stack space on Windows in debug mode
03de1bf
@gvanrossum

gvanrossum commented Nov 1, 2023

Copy link
Copy Markdown
Member Author

Another fix would be to increase the value on this line:

Ah, thanks. That makes more sense. I don't know exactly what's going on either, but I know that I've added some local variables to the function containing the main interpreter loop, and that causes hard crashes in a bunch of tests that recurse deeply (at the C level). I'm now applying your fix instead of my previous hacks -- except I'm keeping the bit in test.support that dynamically pulls the value of Py_C_RECURSION_LIMIT1 from _testcapi (if it exists) rather than hardcoding a value that must be kept in sync with something defined in pystate.h. (It's technically still the case, but only if _testcapi can't be imported.)

I've just pushed that and if tests still pass I'll merge.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
gvanrossum merged commit 7e135a4 into python:main Nov 1, 2023
@bedevere-app bedevere-app Bot removed the awaiting core review label Nov 1, 2023
@gvanrossum
gvanrossum deleted the mix-tiers branch November 1, 2023 20:13
FullteaR pushed a commit to FullteaR/cpython that referenced this pull request Nov 3, 2023
@gvanrossum @FullteaR
pythongh-111520: Integrate the Tier 2 interpreter in the Tier 1 inter…
43fc9d4
…preter (python#111428)

- There is no longer a separate Python/executor.c file.
- Conventions in Python/bytecodes.c are slightly different -- don't use `goto error`,
  you must use `GOTO_ERROR(error)` (same for others like `unused_local_error`).
- The `TIER_ONE` and `TIER_TWO` symbols are only valid in the generated (.c.h) files.
- In Lib/test/support/__init__.py, `Py_C_RECURSION_LIMIT` is imported from `_testcapi`.
- On Windows, in debug mode, stack allocation grows from 8MiB to 12MiB.
- **Beware!** This changes the env vars to enable uops and their debugging
  to `PYTHON_UOPS` and `PYTHON_LLTRACE`.
@mdboom mdboom mentioned this pull request Nov 6, 2023
PGO build broken on Windows #111786
Closed
@brandtbucher brandtbucher mentioned this pull request Nov 7, 2023
GH-111520: Add back the operand local #111813
Merged
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@gvanrossum @aisk
pythongh-111520: Integrate the Tier 2 interpreter in the Tier 1 inter…
724f139
…preter (python#111428)

- There is no longer a separate Python/executor.c file.
- Conventions in Python/bytecodes.c are slightly different -- don't use `goto error`,
  you must use `GOTO_ERROR(error)` (same for others like `unused_local_error`).
- The `TIER_ONE` and `TIER_TWO` symbols are only valid in the generated (.c.h) files.
- In Lib/test/support/__init__.py, `Py_C_RECURSION_LIMIT` is imported from `_testcapi`.
- On Windows, in debug mode, stack allocation grows from 8MiB to 12MiB.
- **Beware!** This changes the env vars to enable uops and their debugging
  to `PYTHON_UOPS` and `PYTHON_LLTRACE`.
@gvanrossum gvanrossum mentioned this pull request Mar 18, 2024
Move Tier 2 interpreter out of _PyEval_EvalFrameDefault #116970
Open
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@gvanrossum @Glyphack
pythongh-111520: Integrate the Tier 2 interpreter in the Tier 1 inter…
bbf5063
…preter (python#111428)

- There is no longer a separate Python/executor.c file.
- Conventions in Python/bytecodes.c are slightly different -- don't use `goto error`,
  you must use `GOTO_ERROR(error)` (same for others like `unused_local_error`).
- The `TIER_ONE` and `TIER_TWO` symbols are only valid in the generated (.c.h) files.
- In Lib/test/support/__init__.py, `Py_C_RECURSION_LIMIT` is imported from `_testcapi`.
- On Windows, in debug mode, stack allocation grows from 8MiB to 12MiB.
- **Beware!** This changes the env vars to enable uops and their debugging
  to `PYTHON_UOPS` and `PYTHON_LLTRACE`.
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
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra

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.

4 participants

@gvanrossum @zooba @bedevere-bot @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.