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-98831: "Generate" the interpreter by gvanrossum · Pull Request #98830 · python/cpython

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Oct 28, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Overview

This is the first stage of a project to generate the interpreter, in particular the cases of the main switch. The references below explain the why. This PR introduces the following:

  • Two new files in the Python subdirectory, bytecodes.c and generated_cases.c.h. (UPDATE: cases.h -> generated_cases.c.h.)

    • bytecodes.c contains instruction definitions. These almost look like the old switch cases, but instead of TARGET(INAME) { ... } they have inst(INAME, stack_effect) { ... }, and the DISPATCH() macro call at the end of the block can be omitted. (The stack effect is not yet used by the generator, and it is not always correct.) At the top and bottom of bytecodes.c is some dummy C code that makes look like valid C code to editors like VS Code.
    • generated_cases.c.h is a generated file containing the switch cases. It is currently identical to the switch cases in ceval.c.
  • A new bunch of tooling, in Tools/cases_generator. This includes:

    • A temporary script, extract_cases.py, that extracts the cases from ceval.c and writes them, in the form of instruction definitions, to bytecodes.c.
    • A script, generate_cases.py, that reads the instruction definitions from bytecodes.c and regenerates the cases, writing them to generated_cases.c.h.
    • A parser for a subset of C that turns blocks of C code into an AST, so we can do transformations at the AST level. (There is currently one example of this -- the function always_exits() determines recursively whether a block is guaranteed to exit, using a custom definition of "exit" that includes macro calls like DISPATCH() and GO_TO_INSTRUCTION().)
  • Changes to ceval.c so that it uses #include "generated_cases.c.h". For now I've left the original switch cases in ceval.c, inside #else ... #endif, so that we can compare the original switch cases to the generated switch cases to validate the toolchain. However, I plan to rip these out either once the PR is (nearly) approved, or perhaps in a quick follow-up PR.

  • A new target in Makefile.pre.in to auto-generate generated_cases.c.h from bytecodes.c, using make regen-cases.

    • Currently missing is similar tooling on Windows. For now, you can only regenerate the cases on UNIX. They are checked into the repo, so Windows should still compile.

References

  • Generating code for the ceval loop/switch faster-cpython/ideas#5
  • Super-instructions faster-cpython/ideas#16
  • Define opcodes in terms of smaller "micro-ops" faster-cpython/ideas#454
  • Change the interpreter definition DSL to be legal C syntax faster-cpython/ideas#477
  • Mechanics of interpreter generation from DSL faster-cpython/ideas#479
  • Stack effect of jumping -- how to express in DSL faster-cpython/ideas#481

Metadata

  • Issue: Generate the interpreter #98831

Sorry, something went wrong.

Uh oh!

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

All reactions
gvanrossum added 30 commits October 11, 2022 16:47
@gvanrossum
New script to generate the ceval switch
089ab0c
This doesn't work yet, just a checkpoint. I'm going to bed now.

It's going to be a long slog.
@gvanrossum
Add 'End regular instructions' marker
bba5b94
@gvanrossum
Extract to a format closer to interpreter_definition.md
99b0058
@gvanrossum
Switch to generating new DSL syntax
2939830
@gvanrossum
Indent output; Always generate stack effect syntax; use argparse
65abfc4
@gvanrossum
Check jump flag; tweak begin and end of the script a bit
51dbe28
@gvanrossum
Fix incorrect output for ??--??
a238f69
@gvanrossum
Script to generate cases.h from bytecodes.inst
2d5b1b5
@gvanrossum
Write bytecodes.inst into Python directory by default
ae042be
@gvanrossum
Initial bytecodes.inst
4315621
@gvanrossum
Initial cases.h
8c80bfd
@gvanrossum
Changes to ceval.c to use cases.h
7301203
@gvanrossum
Changes to Makefile.pre.in to generate cases.h
d9ac40b
@gvanrossum
Move #endif around
a5bff0c
@gvanrossum
Write families to .inst file
8c13c55
(Don't do anything with them yet.)
@gvanrossum
Derive stack effect for super-instructions and specializations
34c84bf
@gvanrossum
Regenerated bytecodes.inst with fewer errors
ff70895
@gvanrossum
Support array stack effects
78f8591
@gvanrossum
Array stack effects are now in bytecodes.inst
a29b815
@gvanrossum
Add Mark's C lexer
688bb5d
@gvanrossum
Start writing a real parser
7def42b
(This is a checkpoint, doesn't work yet.)
@gvanrossum
Move cases infrastructure to its own directory
8fae259
@gvanrossum
Pristine copy of lexer from Mark's gist
b998a97
https://gist.github.com/markshannon/db7ab649440b5af765451bb77c7dba34#gistcomment-4334862
@gvanrossum
Fix tiny bits in lexer.py
c44adad
@gvanrossum
Move c_lexer.c to lexer.c
d0c4398
@gvanrossum
Complete parser (not using it yet):
06a3afa
@gvanrossum
Complete the parser and use it
33540af
@gvanrossum
Use a dataclass for Token; move stuff around to fix tokens
1990b61
@gvanrossum
Make a nice syntax error in lexer.py
690d4c5
@gvanrossum
More handy stuff for raising syntax errors
b3cd440
@gvanrossum
Merge remote-tracking branch 'origin/main' into generate-ceval-switch
8a312ca
gvanrossum
gvanrossum commented Nov 2, 2022
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'm pushing the new version in a few minutes, after merging two tough commits from main. The pushed version will once again have the cases in ceval.c, I'll remove those later (right before merging).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/bytecodes.c Outdated
#define SET_TOP(v) (stack_pointer[-1] = (v))
#define GETLOCAL(i) (frame->localsplus[i])

#define inst(name, stack_effect) case name:

@gvanrossum gvanrossum Nov 2, 2022

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.

Good point. I added some more dummy arguments and labels, fixed the inst() macro, and I'm just copying all #includes from ceval.c...

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/bytecodes.c
#define GETLOCAL(i) (frame->localsplus[i])

#define inst(name, stack_effect) case name:
#define family(name) static int family_##name

@gvanrossum gvanrossum Nov 2, 2022

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.

Ah, that was a last-minute change, I had a + separator there first. I'll put it inside curlies and make the macro define an unsized array.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Tools/cases_generator/parser.py Resolved

Uh oh!

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

Comment thread Tools/cases_generator/generate_cases.py Resolved

Uh oh!

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

@gvanrossum
gvanrossum requested a review from brandtbucher November 2, 2022 23:50
@gvanrossum

gvanrossum commented Nov 2, 2022

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Nov 2, 2022
@bedevere-bot

bedevere-bot commented Nov 2, 2022

Copy link
Copy Markdown

Thanks for making the requested changes!

@brandtbucher: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Nov 2, 2022

Copy link
Copy Markdown
Member Author

(Wait, I haven't pushed yet. In a minute.)

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
Update generated files after merging from main
7a327aa
@gvanrossum
gvanrossum force-pushed the generate-ceval-switch branch from 8c273bd to 7a327aa Compare November 2, 2022 23:52
@gvanrossum

gvanrossum commented Nov 2, 2022

Copy link
Copy Markdown
Member Author

Now it's ready.

All reactions

Sorry, something went wrong.

Uh oh!

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

brandtbucher
brandtbucher approved these changes Nov 3, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Nov 3, 2022
@gvanrossum gvanrossum added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 3, 2022
@bedevere-bot

bedevere-bot commented Nov 3, 2022

Copy link
Copy Markdown

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

If you want to schedule another build, you need to add the ":hammer: 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 3, 2022
@gvanrossum
Remove the redundant cases from ceval.c
fddc08c
If you get a merge conflict, talk to Guido
@gvanrossum

gvanrossum commented Nov 3, 2022

Copy link
Copy Markdown
Member Author

I have one more commit (removing the cases from ceval.c) and then I'll merge. If you get a merge conflict after that, re-apply your changes to the instructions in ceval.c to bytecodes.c, and re-run make regen-cases.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum gvanrossum added the DO-NOT-MERGE label Nov 3, 2022
@gvanrossum

gvanrossum commented Nov 3, 2022

Copy link
Copy Markdown
Member Author

All buildbots passed except the three s390 ones. I'm not going to wait for those, I'm pushing the final commit (fddc08c) and once the regular CI passes I'll merge. Hopefully before midnight.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
gvanrossum merged commit 41bc101 into python:main Nov 3, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Nov 3, 2022
This was referenced Nov 3, 2022
Make sure regen-cases are in regen-all #99048
Closed
gh-99048: add regen-cases to regen-all in Makefile #99049
Closed
@gvanrossum gvanrossum mentioned this pull request Nov 3, 2022
GH-98831: Remove redundant extract_cases.py script #99065
Merged
@gvanrossum gvanrossum removed the DO-NOT-MERGE label Nov 3, 2022
gvanrossum added a commit that referenced this pull request Nov 3, 2022
@gvanrossum
GH-98831: Remove redundant extract_cases.py script (GH-99065)
916af11
Debt I owe from PR GH-98830.
@gvanrossum
gvanrossum deleted the generate-ceval-switch branch December 8, 2022 22:43
@appleparan appleparan mentioned this pull request Aug 16, 2026
docs: align lectures 02-04 with PyTorch 2.13 PyTorchKR/pytorch-internals-course#26
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 approved these changes

Uh oh!

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

Assignees

@gvanrossum gvanrossum

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