René's URL Explorer Experiment


Title: Branching design for Tier 2 (uops) interpreter · Issue #106529 · python/cpython · GitHub

Open Graph Title: Branching design for Tier 2 (uops) interpreter · Issue #106529 · python/cpython

X Title: Branching design for Tier 2 (uops) interpreter · Issue #106529 · python/cpython

Description: This issue is part of the larger epic of gh-104584. In PR gh-106393 I tried to implement branching, but it was premature. Here's a better design, following @markshannon's guidance. We have the following jump instructions (not counting th...

Open Graph Description: This issue is part of the larger epic of gh-104584. In PR gh-106393 I tried to implement branching, but it was premature. Here's a better design, following @markshannon's guidance. We have the foll...

X Description: This issue is part of the larger epic of gh-104584. In PR gh-106393 I tried to implement branching, but it was premature. Here's a better design, following @markshannon's guidance. We have ...

Opengraph URL: https://github.com/python/cpython/issues/106529

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Branching design for Tier 2 (uops) interpreter","articleBody":"This issue is part of the larger epic of gh-104584. In PR gh-106393 I tried to implement branching, but it was premature. Here's a better design, following @markshannon's [guidance](https://github.com/python/cpython/pull/106393#discussion_r1252794047).\r\n\r\nWe have the following jump instructions (not counting the instrumented versions):\r\n\r\nUnconditional jumps:\r\n\r\n- [x] JUMP_BACKWARD\r\n- [ ] JUMP_BACKWARD_NO_INTERRUPT\r\n- [x] JUMP_FORWARD\r\n\r\nBranches, a.k.a. conditional jumps:\r\n\r\n- [x] POP_JUMP_IF_FALSE, POP_JUMP_IF_TRUE, ~POP_JUMP_IF_NONE, POP_JUMP_IF_NOT_NONE~\r\n- [x] FOR_ITER's specializations:\r\n  - [x] FOR_ITER_LIST\r\n  - [x] FOR_ITER_TUPLE\r\n  - [x] FOR_ITER_RANGE\r\n- [ ] FOR_ITER_GEN\r\n- [ ] SEND\r\n\r\n- [ ] Add counters to to POP_JUMP_IF_{FALSE,TRUE} to determine likeliness\r\n\r\nThe translation strategies could be as follows:\r\n\r\n## Unconditional jumps\r\n\r\n### **JUMP_BACKWARD**\r\n\r\n- If this jumps to exactly the top of the current trace, emit a Tier 2 JUMP_TO_TOP uop, and stop projecting (i.e., exit the trace generation loop). The JUMP_TO_TOP uop implementation should include a CHECK_EVAL_BREAKER call.\r\n- If this jumps anywhere else, emit a SAVE_IP uop with the destination of the jump, followed by an EXIT_TRACE uop, and stop projecting.\r\n\r\n### **JUMP_BACKWARD_NO_INTERRUPT**\r\n\r\n- Since this is typically only used in special circumstances, just emit a SAVE_IP instruction with the destination and an EXIT_TRACE uop, and stop projecting.\r\n- Alternatively, we could make CHECK_EVAL_BREAKER a separate UOP that is inserted for JUMP_BACKWARD but not for JUMP_BACKWARD_NO_INTERRUPT, and otherwise treat the two backward jumps the same.\r\n\r\n### **JUMP_FORWARD**\r\n\r\n- Emit a SAVE_IP uop with the destination of the jump, and continue projecting from there (i.e. set `instr` to the destination of the jump).\r\n\r\n## Conditional jumps (branches)\r\n\r\n### **POP_JUMP_IF_FALSE** and friends\r\n\r\nConsider the following Python code:\r\n```py\r\nif cond:\r\n    block\r\nrest\r\n```\r\nThis translates roughly to the following Tier 1 bytecode (using B1, B2, ... to label Tier 1 instructions, and `\u003ccond\u003e`, `\u003cblock\u003e` etc. to represent code blocks that evaluate or execute the corresponding Python fragments):\r\n```\r\nB1: \u003ccond\u003e\r\nB2: POP_JUMP_IF_FALSE B4\r\nB3: \u003cblock\u003e\r\nB4: \u003crest\u003e\r\nB5:\r\n```\r\nI propose the following translation into Tier 2 uops, assuming the branch is \"unlikely\":\r\n```\r\n    SAVE_IP B1\r\n    \u003ccond\u003e\r\n    SAVE_IP B2\r\n    JUMP_IF_FALSE stub\r\n    POP_TOP\r\n    SAVE_IP B3\r\n    \u003cblock\u003e\r\n    SAVE_IP B4\r\n    EXIT_TRACE\r\n\r\nstub:\r\n    POP_TOP\r\n    SAVE_IP B4\r\n    EXIT_TRACE\r\n```\r\nWhere JUMP_IF_FALSE inspects the top of stack but doesn't pop it, and has an argument that executes a jump in the Tier 2 uop instruction sequence.\r\n\r\nIf the branch is \"likely\", we do this instead:\r\n```\r\n    SAVE_IP B1\r\n    \u003ccond\u003e\r\n    SAVE_IP B2\r\n    JUMP_IF_TRUE stub\r\n    POP_TOP\r\n    SAVE_IP B4\r\n    \u003crest\u003e\r\n    SAVE_IP B5\r\n    EXIT_TRACE\r\n\r\nstub:\r\n    POP_TOP\r\n    SAVE_IP B3\r\n    EXIT_TRACE\r\n```\r\nNote how in this case, `\u003crest\u003e` is projected as part of the trace, while `\u003cblock\u003e` is not, since the likely case is that we jump over `\u003cblock\u003e` to `\u003crest\u003e`.\r\n\r\nFor the other simple conditional jumps (POP_JUMP_IF_TRUE, ~POP_JUMP_IF_NONE, POP_JUMP_IF_NOT_NONE~) we do the same: if the jump is unlikely, emit a JUMP_IF_XXX uop and a stub; if the jump is likely, emit the inverse JUMP_IF_NOT_XXX uop and a different stub, and continue projecting at the destination of the original jump bytecode.\r\n\r\nI propose to have hand-written cases both in the superblock generator and in the Tier 2 interpreter for these, since the translations are too irregular to fit easily in the macro expansion data structure. The stub generation will require additional logic and data structures in `translate_bytecode_to_trace()` to keep track of the stubs required so far, the available space for those, and the back-patching required to set the operands for the JUMP_IF_XXX uops.\r\n\r\n### **FOR_ITER** and (especially) its specializations\r\n\r\nThe common case for these is not to jump. The bytecode definitions are too complex to duplicate in hand-written Tier 2 uops. My proposal is to change these in bytecodes.c so that, instead of using the `JUMPBY(n)` macro, they use `JUMPBY_POP_DISPATCH(n)`, which in Tier 1 translates into just `JUMPBY(n)`, but in Tier 2 translates into roughly\r\n```cc\r\nframe-\u003eprev_instr += (x);\r\nPY_DECREF(stack_pointer[-1]);\r\nstack_pointer -= 1;\r\ngoto exit;\r\n```\r\nthereby exiting the trace when the corresponding for-loop terminates.\r\n\r\nI am assuming here that most loops have several iterations. I don't think it's worth special-casing the occasional for-loop that almost always immediately terminates.\r\n\r\n### **SEND**\r\n\r\nPossibly we could treat this the same as FOR_ITER. But right now I propose to just punt here, and when we encounter it, stop projecting, as we do with any other unsupported bytecode instruction.\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-106542\r\n* gh-106543\r\n* gh-106551\r\n* gh-106599\r\n* gh-106613\r\n* gh-106638\r\n* gh-106651\r\n* gh-106696\r\n* gh-106756\n* gh-106796\n* gh-112134\n* gh-112214\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2023-07-07T18:47:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":22},"url":"https://github.com/106529/cpython/issues/106529"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f97761ee-50f4-c7a4-a7fa-b2d80172c904
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idED76:1E1351:CC096:11DAB4:696A6AB0
html-safe-nonce8f5936c14cba706c82a3637d331160d62cc64ffc5bb8729af295502968f41b56
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDc2OjFFMTM1MTpDQzA5NjoxMURBQjQ6Njk2QTZBQjAiLCJ2aXNpdG9yX2lkIjoiNTUzNjk3OTUyMzc0NDcyMTU4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb0cde5f91ddaa1008246ed527604b264e74ab1d07cf5c43f10d0f7f7d0f0d741
hovercard-subject-tagissue:1794053965
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/106529/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8ea8b12950891de4a5b4b2a59f46e86cff4e6512c8d05932807aaffd8d973b9f/python/cpython/issues/106529
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8ea8b12950891de4a5b4b2a59f46e86cff4e6512c8d05932807aaffd8d973b9f/python/cpython/issues/106529
og:image:altThis issue is part of the larger epic of gh-104584. In PR gh-106393 I tried to implement branching, but it was premature. Here's a better design, following @markshannon's guidance. We have the foll...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegvanrossum
hostnamegithub.com
expected-hostnamegithub.com
None6fea32d5b7276b841b7a803796d9715bc6cfb31ed549fdf9de2948ac25d12ba6
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef2d9f6432a5a115ec709295ae70623f33bb80aee
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/106529#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F106529
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F106529
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/106529
Reloadhttps://github.com/python/cpython/issues/106529
Reloadhttps://github.com/python/cpython/issues/106529
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/106529
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/106529
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/106529
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/106529
Branching design for Tier 2 (uops) interpreterhttps://github.com/python/cpython/issues/106529#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
https://github.com/gvanrossum
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jul 7, 2023https://github.com/python/cpython/issues/106529#issue-1794053965
gh-104584https://github.com/python/cpython/issues/104584
gh-106393https://github.com/python/cpython/pull/106393
@markshannonhttps://github.com/markshannon
guidancehttps://github.com/python/cpython/pull/106393#discussion_r1252794047
gh-106529: Support FOR_ITER specializations as uops #106542https://github.com/python/cpython/pull/106542
gh-106529: Support JUMP_BACKWARD #106543https://github.com/python/cpython/pull/106543
gh-106529: Implement POP_JUMP_IF_XXX uops #106551https://github.com/python/cpython/pull/106551
GH-106529: Define POP_JUMP_IF_NONE in terms of POP_JUMP_IF_TRUE #106599https://github.com/python/cpython/pull/106599
gh-106529: Silence compiler warning in jump target patching #106613https://github.com/python/cpython/pull/106613
gh-106529: Split FOR_ITER_RANGE into uops #106638https://github.com/python/cpython/pull/106638
gh-106529: Implement JUMP_FORWARD in uops (with test) #106651https://github.com/python/cpython/pull/106651
gh-106529: Split FOR_ITER_{LIST,TUPLE} into uops #106696https://github.com/python/cpython/pull/106696
gh-106529: Fix subtle Tier 2 edge case with list iterator #106756https://github.com/python/cpython/pull/106756
gh-106529: Generate uops for POP_JUMP_IF_[NOT_]NONE #106796https://github.com/python/cpython/pull/106796
gh-106529: Make FOR_ITER a viable uop #112134https://github.com/python/cpython/pull/112134
gh-106529: Cleanups split off gh-112134 #112214https://github.com/python/cpython/pull/112214
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.