gh-143732: Add specialization for TO_BOOL#148113
Open
eendebakpt wants to merge 6 commits intopython:mainfrom
Open
gh-143732: Add specialization for TO_BOOL#148113eendebakpt wants to merge 6 commits intopython:mainfrom
eendebakpt wants to merge 6 commits intopython:mainfrom
Conversation
- Add TO_BOOL_GENERIC: a catch-all specialization for types not covered by existing TO_BOOL variants (dict, tuple, float, set, bytes, frozenset, etc. and heap types with __bool__/__len__). Records type info for the JIT. - Add _TO_BOOL_DICT: a tier2-only uop that checks dict.ma_used directly instead of calling PyObject_IsTrue(). The JIT optimizer replaces _TO_BOOL with _TO_BOOL_DICT when the type is known to be dict or frozendict. - Fix _GUARD_TYPE_VERSION optimizer handler to resolve types from recorded type info even when the type version cache has a collision. This enables the optimizer to eliminate redundant type guards (e.g. _GUARD_NOS_LIST) in more cases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
eendebakpt
commented
Apr 5, 2026
| self.assert_specialized(to_bool_str, "TO_BOOL_STR") | ||
| self.assert_no_opcode(to_bool_str, "TO_BOOL") | ||
|
|
||
| def to_bool_generic_dict(): |
Contributor
Author
There was a problem hiding this comment.
There is quite some redundancy in the added tests (and also the existing tests). We could refactor this, but it is a bit tricky as re-using the same method (with different arguments) makes it more difficult to reason about the specialization that takes place.
If we want this, I suggest we open a separate PR.
eendebakpt
commented
Apr 5, 2026
| // already added one earlier. | ||
| if (sym_set_type_version(owner, type_version)) { | ||
| // sym_set_type_version can resolve the type from recorded type info | ||
| // even when the version cache has a collision |
Contributor
Author
There was a problem hiding this comment.
This change has as side effect the list.append needs less checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We add
TO_BOOL_GENERICas suggested in #143732 (comment). This adds type information for several builtin classes. We also add_TO_BOOL_DICT(tier 2). In the jit conversion of a dict to bool is about 30% faster.