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-130415: Use boolean guards to narrow types to values in the JIT by brandtbucher · Pull Request #130659 · python/cpython

Conversation

@brandtbucher

@brandtbucher brandtbucher commented Feb 28, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This adds a symbolic "truth" type to the JIT optimizer, allowing tests like if not x: ... to narrow the value of x based on its type. Currently, it's only implemented for booleans, but I'm working with a few people to open PRs to narrow int and str values as well.

(The diff looks much worse than it is, since this requires adding ctx parameters to a couple of helper functions that are used everywhere. The "real" changes in optimizer_bytecodes.c are limited to _GUARD_IS_FALSE_POP, _GUARD_IS_NONE_POP, _GUARD_IS_TRUE_POP, _TO_BOOL, _TO_BOOL_BOOL, and _UNARY_NOT.

  • Issue: Better constant narrowing in the JIT optimizer #130415

Sorry, something went wrong.

Uh oh!

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

ZeroIntensity reacted with rocket emoji
All reactions
  • 🚀 1 reaction
brandtbucher added 8 commits February 27, 2025 08:40
@brandtbucher
Add failing regression tests
d0f236a
@brandtbucher
Add "truth" symbols
7929a57
@brandtbucher
Fix up test
6c4588e
@brandtbucher
is
1d17afe
@brandtbucher
Add creation and narrowing of truths
dcb294c
@brandtbucher
Use the new truth symbols
41e77a8
@brandtbucher
Fix tests
d0b1724
@brandtbucher
blurb add
c8214c9
@brandtbucher brandtbucher added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-JIT labels Feb 28, 2025
@brandtbucher brandtbucher self-assigned this Feb 28, 2025
@brandtbucher
brandtbucher requested review from Fidget-Spinner and markshannon as code owners February 28, 2025 01:06
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 28, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Feb 28, 2025
Better constant narrowing in the JIT optimizer #130415
Closed
13 of 17 tasks
markshannon
markshannon reviewed Feb 28, 2025
View reviewed changes

@markshannon markshannon left a comment •
edited
Loading

Uh oh!

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

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 correct, but I'm not keen on the names. Naming is hard, so I don't have much better names.

At least "truthy", rather than just "truth", would suggest Python bool(x) semantics.
Maybe "to_bool"?

Also, can you add some C tests to Python/optimizer_symbols.c for the various operations?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/optimizer_bytecodes.c Outdated

op(_UNARY_NOT, (value -- res)) {
sym_set_type(value, &PyBool_Type);
res = sym_new_truth(ctx, value, true);

@markshannon markshannon Feb 28, 2025

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.

From casual reading, this looks like an error as the true argument implies that this has the value True.

Maybe rename sym_new_truth as sym_from_truthiness and have false as the argument for NOT and true as the argument for TO_BOOL?

Sorry, something went wrong.

Uh oh!

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

All reactions

@markshannon markshannon Feb 28, 2025

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.

Or symbolic consts, like TO_BOOL and INVERT?

Sorry, something went wrong.

Uh oh!

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

All reactions

@brandtbucher brandtbucher Feb 28, 2025

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'd like to avoid a double-negative in the arg name (since inverting it from its current meaning would mean something like "not not"). What if I make it an int and use 0 and 1 instead? I feel like a big part of the friction in reading this is the use of true and false as arguments.

Sorry, something went wrong.

Uh oh!

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

All reactions

@markshannon markshannon Feb 28, 2025

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.

0 /1 or false/true doesn't matter. Good symbolic names is what is needed.
Maybe even use two different functions with different names?

Sorry, something went wrong.

Uh oh!

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

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

brandtbucher commented Feb 28, 2025

Copy link
Copy Markdown
Member Author

I agree the naming is hard. How about JitSymbolBoolOf and sym_new_bool_of? I feel like "truthy" implies that the value can't be falsey ("truth" is a noun, "truthy" is an adjective describing a value's truth).

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Feb 28, 2025

Copy link
Copy Markdown
Member Author

We can think ahead with the naming too. Soon we’ll have equality and identity as symbols, and we’ll probably want something reasonably consistent (“truth”, “equality”, and “identity” were the planned names).

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Feb 28, 2025

Copy link
Copy Markdown
Member

"truthy" is an adjective describing a value's truth

Maybe "truthiness" rather than "truthy" then?

brandtbucher reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Feb 28, 2025

Copy link
Copy Markdown
Member

I've heard "truthiness" used to describe the boolean value of a non-boolean in a test. Probably more in a javascript context, but for Python as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Feb 28, 2025

Copy link
Copy Markdown
Member Author

Sounds good, thanks for the suggestions.

All reactions

Sorry, something went wrong.

Uh oh!

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

brandtbucher added 5 commits February 28, 2025 10:46
@brandtbucher
"truth" -> "truthiness"
ffa559b
@brandtbucher
Fix some compiler warnings
cbd4569
@brandtbucher
Invert the "not" arg for clarity
9f1b271
@brandtbucher
Merge branch 'main' into jit-narrow-to-bool-bool
07697ed
@brandtbucher
Add tests for truthiness symbol
4edba18
@brandtbucher
brandtbucher merged commit 7afa476 into python:main Mar 2, 2025
@bedevere-app bedevere-app Bot removed the awaiting core review label Mar 2, 2025
@bedevere-bot

bedevere-bot commented Mar 2, 2025

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 CentOS9 NoGIL Refleaks 3.x (tier-1) has failed when building commit 7afa476.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1610/builds/946) and take a look at the build logs.
  4. Check if the failure is related to this commit (7afa476) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1610/builds/946

Failed tests:

  • test_free_threading

Summary of the results of the build (if available):

==

Click to see traceback logs
remote: Enumerating objects: 19, done.        
remote: Counting objects:   7% (1/14)        
remote: Counting objects:  14% (2/14)        
remote: Counting objects:  21% (3/14)        
remote: Counting objects:  28% (4/14)        
remote: Counting objects:  35% (5/14)        
remote: Counting objects:  42% (6/14)        
remote: Counting objects:  50% (7/14)        
remote: Counting objects:  57% (8/14)        
remote: Counting objects:  64% (9/14)        
remote: Counting objects:  71% (10/14)        
remote: Counting objects:  78% (11/14)        
remote: Counting objects:  85% (12/14)        
remote: Counting objects:  92% (13/14)        
remote: Counting objects: 100% (14/14)        
remote: Counting objects: 100% (14/14), done.        
remote: Compressing objects:   9% (1/11)        
remote: Compressing objects:  18% (2/11)        
remote: Compressing objects:  27% (3/11)        
remote: Compressing objects:  36% (4/11)        
remote: Compressing objects:  45% (5/11)        
remote: Compressing objects:  54% (6/11)        
remote: Compressing objects:  63% (7/11)        
remote: Compressing objects:  72% (8/11)        
remote: Compressing objects:  81% (9/11)        
remote: Compressing objects:  90% (10/11)        
remote: Compressing objects: 100% (11/11)        
remote: Compressing objects: 100% (11/11), done.        
remote: Total 19 (delta 3), reused 3 (delta 3), pack-reused 5 (from 1)        
From https://github.com/python/cpython
 * branch                    main       -> FETCH_HEAD
Note: switching to '7afa476874b9a432ad6dbe9fb3e65d62f2999f88'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 7afa476874b GH-130415: Use boolean guards to narrow types to values in the JIT (GH-130659)
Switched to and reset branch 'main'

configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15)

make: *** [Makefile:2325: buildbottest] Error 2
All reactions

Sorry, something went wrong.

Uh oh!

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

@tim-stephenson tim-stephenson mentioned this pull request Mar 16, 2025
Cannot include cpython/Include/internal/pycore_optimizer.h in C++ extension due to use of protected C++ keyword "not" #131331
Closed
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
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner Fidget-Spinner is a code owner

Uh oh!

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

Assignees

@brandtbucher brandtbucher

Labels

interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-JIT

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

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