Title: Better constant narrowing in the JIT optimizer · Issue #130415 · python/cpython · GitHub
Open Graph Title: Better constant narrowing in the JIT optimizer · Issue #130415 · python/cpython
X Title: Better constant narrowing in the JIT optimizer · Issue #130415 · python/cpython
Description: Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend. Currently, the JIT optimizer uses the _COMPARE_OP family, _CONTAINS_OP, _IS_OP, and the _TO_BOOL family to narrow the types of t...
Open Graph Description: Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend. Currently, the JIT optimizer uses the _COMPARE_OP family, _CONTAINS_OP, _IS_OP, and the ...
X Description: Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend. Currently, the JIT optimizer uses the _COMPARE_OP family, _CONTAINS_OP, _IS_OP, ...
Opengraph URL: https://github.com/python/cpython/issues/130415
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Better constant narrowing in the JIT optimizer","articleBody":"**Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend.**\n\nCurrently, the JIT optimizer uses the `_COMPARE_OP` family, `_CONTAINS_OP`, `_IS_OP`, and the `_TO_BOOL` family to narrow the *types* of the input values and the type of the output value.\n\nHowever, by \"peeking ahead\" and seeing how a value will be used, we can narrow these types to constants as well. As a simple example, consider `_TO_BOOL_INT + _GUARD_IS_FALSE_CHECK` on an unknown value. After the `_TO_BOOL_INT`, it can be narrowed to a known class, `int` (we do this today). However, after the `_GUARD_IS_FALSE_CHECK`, we can actually narrow it to a constant value, `0`.\n\nAn example implementation of this idea for `_TO_BOOL_BOOL` is here: https://github.com/python/cpython/compare/main...brandtbucher:cpython:hack-night-to-bool-bool\n \nI've divided this work into 3 \"waves\" of increasing complexity. Tasks in **bold** are probably a bit harder, tasks in *italics* are probably a bit easier.\n\n## Narrow types to constants in branches involving truthiness:\n\n- [x] **`_TO_BOOL + _GUARD_IS_*_POP`** [gh-130659](https://github.com/python/cpython/pull/130659)\n- [x] *`_TO_BOOL_BOOL + _GUARD_IS_*_POP`* [gh-130659](https://github.com/python/cpython/pull/130659)\n- [x] `_TO_BOOL_INT + _GUARD_IS_*_POP` [gh-130772](https://github.com/python/cpython/pull/130772)\n- [ ] ~**`_TO_BOOL_LIST + _GUARD_IS_*_POP`**~\n- [x] `_TO_BOOL_STR + _GUARD_IS_*_POP` [gh-130476](https://github.com/python/cpython/pull/130476)\n\n## Narrow types to constants in branches involving comparisons with a constant:\n\n- [ ] **`_COMPARE_OP + _GUARD_IS_*_POP` (`==`, `!=`)**\n- [ ] `_COMPARE_OP_FLOAT + _GUARD_IS_*_POP` (`==`, `!=`)\n- [ ] `_COMPARE_OP_INT + _GUARD_IS_*_POP` (`==`, `!=`)\n- [ ] `_COMPARE_OP_STR + _GUARD_IS_*_POP` (`==`, `!=`)\n- [ ] **`_CONTAINS_OP + _GUARD_IS_*_POP` (`in`, `not in`)**\n- [ ] *`_IS_OP + _GUARD_IS_*_POP` (`is`, `is not`)*\n\n## Evaluate comparisons involving two constants:\n\nThis is related, but a bit more involved, since we need a way to pop two values from the stack and push a constant (`_POP_TWO_LOAD_CONST_INLINE_BORROW`). We should also teach `remove_unneeded_uops` about this new instruction.\n\n- [x] **`_COMPARE_OP`** (`==`, `!=`, `\u003c`, `\u003e`, `\u003c=`, `\u003e=`)\n- [x] `_COMPARE_OP_FLOAT` (`==`, `!=`, `\u003c`, `\u003e`, `\u003c=`, `\u003e=`) [gh-137062](https://github.com/python/cpython/pull/137062)\n- [x] `_COMPARE_OP_INT` (`==`, `!=`, `\u003c`, `\u003e`, `\u003c=`, `\u003e=`) [gh-131489](https://github.com/python/cpython/pull/131489) \n- [x] `_COMPARE_OP_STR` (`==`, `!=`, `\u003c`, `\u003e`, `\u003c=`, `\u003e=`) [gh-137062](https://github.com/python/cpython/pull/137062)\n- [x] **`_CONTAINS_OP` (`in`, `not in`)**\n- [ ] ~*`_IS_OP` (`is`, `is not`)*~\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-130476\n* gh-130477\n* gh-130659\n* gh-130772\n* gh-131489\n* gh-131778\n* gh-132333\n* gh-132708\n* gh-134241\n* gh-137062\n* gh-143895\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/brandtbucher","@type":"Person","name":"brandtbucher"},"datePublished":"2025-02-21T17:23:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/130415/cpython/issues/130415"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d80c82bb-7669-92dd-adfc-9e8984f97ab8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C816:2ABBC9:27869FB:34FE1B6:696EDF85 |
| html-safe-nonce | d4488f266729cfebda2964405b50162885d35492ded8996a0f3d328611715510 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODE2OjJBQkJDOToyNzg2OUZCOjM0RkUxQjY6Njk2RURGODUiLCJ2aXNpdG9yX2lkIjoiMTg1Nzg0MDI1MDg1MTYxNDU5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ef3282a334584677a910116334fb6e37d052d28c1583679700f9218cd539b04f |
| hovercard-subject-tag | issue:2869613568 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/130415/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8961c24ad29b7386524a86aa934d38131215dc4e4407fb380d2ec49c652e6bb3/python/cpython/issues/130415 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8961c24ad29b7386524a86aa934d38131215dc4e4407fb380d2ec49c652e6bb3/python/cpython/issues/130415 |
| og:image:alt | Please don't work on this. I'm planning on sprinting on this with new contributors at an event this weekend. Currently, the JIT optimizer uses the _COMPARE_OP family, _CONTAINS_OP, _IS_OP, and the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | brandtbucher |
| hostname | github.com |
| expected-hostname | github.com |
| None | b278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 39aed5006635ab6f45e6b77d23e73b08a00272a3 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width