Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-130415: Use boolean guards to narrow types to values in the JIT #130659

Merged
merged 13 commits into from
Mar 2, 2025

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Feb 28, 2025

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.

@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
Copy link
Member

@markshannon markshannon left a 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?

@@ -430,6 +431,11 @@ dummy_func(void) {
}
}

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

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?

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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?

@brandtbucher
Copy link
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).

@brandtbucher
Copy link
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).

@markshannon
Copy link
Member

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

Maybe "truthiness" rather than "truthy" then?

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

@brandtbucher
Copy link
Member Author

Sounds good, thanks for the suggestions.

@brandtbucher brandtbucher merged commit 7afa476 into python:main Mar 2, 2025
58 checks passed
@bedevere-bot
Copy link

⚠️⚠️⚠️ 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-JIT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants