Skip to content

gh-143732: Add specialization for TO_BOOL - #148113

Closed
eendebakpt wants to merge 9 commits into
python:mainfrom
eendebakpt:to_bool_generic
Closed

gh-143732: Add specialization for TO_BOOL#148113
eendebakpt wants to merge 9 commits into
python:mainfrom
eendebakpt:to_bool_generic

Conversation

@eendebakpt

@eendebakpteendebakpt commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

We add TO_BOOL_GENERIC as 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.

eendebakptand others added 5 commits March 25, 2026 22:53
- 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>
self.assert_specialized(to_bool_str, "TO_BOOL_STR")
self.assert_no_opcode(to_bool_str, "TO_BOOL")

def to_bool_generic_dict():

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

// 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

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has as side effect the list.append needs less checks.

@eendebakpteendebakpt changed the title Draft: gh-143732: Add specialization for TO_BOOLgh-143732: Add specialization for TO_BOOLApr 5, 2026
@markshannon

Copy link
Copy Markdown
Member

I don't think we want TO_BOOL_GENERIC as it is basically duplicating TO_BOOL. The "generic" form is only useful if we also add a _PY form that inlines calls to __bool__() for classes where __bool__() is implemented in Python.
In that case we need the "generic" form to guard against __bool__() being implemented in Python, so we always inline the call.
I'm not sure if this is worth it for __bool__ as much as for other __dunder__ functions.

What you can do is add the _RECORD_TOS_TYPE uop to TO_BOOL and use that to implement the _TO_BOOL_DICT optimization.

You might also want to add _TO_BOOL_SIZED and apply it to any class where __bool__() is equivalent to Py_SIZE(obj) != 0. We could then specialize TO_BOOL for tuples, bytes, bytearrays and others, in the JIT.

@eendebakpt

Copy link
Copy Markdown
ContributorAuthor

@markshannon It seems I misinterpreted the comments at the issue. I though the TO_BOOL_GENERIC/TO_BOOL_PY pair would cover most (all types) and allow tier2 specialization.

I am having some trouble adding the _RECORD_TOS_TYPE to TO_BOOL as that has _SPECIALIZE_TO_BOOL and I cannot combine the two. I created a separate PR with just the tier2 opcodes. (they work in the jit, but could benefit from more type information from either a _RECORD_TOS_TYPE or tier1 opcodes).

@markshannon

Copy link
Copy Markdown
Member

#148285

@markshannon

Copy link
Copy Markdown
Member

The issue with _SPECIALIZE_TO_BOOL preventing recording has been fixed, if you want to pick this up again

@eendebakpt

Copy link
Copy Markdown
ContributorAuthor

@markshannon I am closing this in favor of #148271. That PR only currently adds the tier2 opcodes (not yet the recording uop). I will add the recording uop on top of the PR or a followup PR depending how hard it is.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@eendebakpt@markshannon