gh-131798: JIT: Further optimize `_CALL_ISINSTANCE` for class tuples by tomasr8 · Pull Request #134543 · python/cpython · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

gh-131798: JIT: Further optimize _CALL_ISINSTANCE for class tuples #134543

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented May 22, 2025

We can already const eval isinstance(inst, cls) calls when both arguments are known, but only if cls is a single class (e.g. int).
This PR adds support for isinstance(inst, (cls1, cls2, ..., clsN)). This allows us to optimize for example:

  • isinstance(42, (int, str)) (to True)
  • isinstance(42, (bool, str)) (to False)

We can narrow to True even when only some of the classes are known, as long as inst is an instance of one of the known classes.

@tomasr8 tomasr8 changed the title gh-134369: JIT: Further optimize _CALL_ISINSTANCE for class tuples gh-131798: JIT: Further optimize _CALL_ISINSTANCE for class tuples May 22, 2025
Comment on lines +2124 to +2125
self.assertIn("_BUILD_TUPLE", uops)
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
Copy link
Member Author

Choose a reason for hiding this comment

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

_BUILD_TUPLE is preventing us from optimizing out _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW.
The bytecode is basically:

LOAD_CONST
LOAD_CONST
_BUILD_TUPLE
_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW

To optimize this, we'd need some special handling for _BUILD_TUPLE in remove_unneeded_uops.

@@ -938,6 +938,9 @@ dummy_func(void) {
}

op(_CALL_ISINSTANCE, (unused, unused, instance, cls -- res)) {
// The below define is equivalent to PyObject_TypeCheck(inst, cls)
#define sym_IS_SUBTYPE(inst, cls) ((inst) == (cls) || PyType_IsSubtype(inst, cls))
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'm not sure about this define, maybe it's fine to duplicate this logic?

Copy link
Member

Choose a reason for hiding this comment

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

I'll let you choose. We should probably either duplicate it, or just make it a proper function in optimizer_symbols.c.

@tomasr8 tomasr8 marked this pull request as ready for review May 22, 2025 22:34
@tomasr8 tomasr8 requested a review from Fidget-Spinner as a code owner May 22, 2025 22:34
@tomasr8 tomasr8 requested a review from brandtbucher May 22, 2025 22:34
Comment on lines +974 to +979
if (!sym_has_type(item)) {
// There is an unknown item in the tuple,
// we can no longer deduce False.
all_items_known = false;
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

We can't infer anything in this case, since the class could do anything in its __instancecheck__ or whatever, so it's no longer side-effect free. Need to bail on the whole optimization at this point.

So I don't think we need all_items_known, either. We can either:

  • Break early on our first True, like you do below, and infer True.
  • Loop over everything and infer False.
  • Bail on an unknown thing and infer bool.

Copy link
Member

Choose a reason for hiding this comment

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

I guess we could still do something like this if we know sym_get_type(item) == &PyType_Type, so it's guaranteed side-effect-free, we just don't know the result of the test. But that seems like a rare case (knowing something is a type, but not which type it actually is).

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't infer anything in this case, since the class could do anything in its instancecheck or whatever, so it's no longer side-effect free. Need to bail on the whole optimization at this point.

Just to check if I understood this point: even if we have something like isinstance(42, (unknown, int)) we can't infer True because if unknown defines its own __instancecheck__, it would change the semantics of the program if we infer True. This is because unknown.__instancecheck__ would no longer be called, right?

So basically what you said, once we see an item with an unknown type, we must stop.

Copy link
Member

@brandtbucher brandtbucher Jul 3, 2025

Choose a reason for hiding this comment

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

Basically, yeah. It's a bit more subtle, though: we can infer the result to be True, but we can't remove the isinstance call itself.

So we can set a replace_op = false flag or something to keep the inferred value but avoid the REPLACE_OP call. That would at least allow us to remove the following branch in if isinstance(42, (has_metaclass, int)): ..., even if the isinstance call itself remains.

Copy link
Member Author

Choose a reason for hiding this comment

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

Basically, yeah. It's a bit more subtle, though: we can infer the result to be True, but we can't remove the isinstance call itself.

Awesome, that was pretty much my understanding as well :)

I'll fix the PR hopefully this weekend.

@@ -938,6 +938,9 @@ dummy_func(void) {
}

op(_CALL_ISINSTANCE, (unused, unused, instance, cls -- res)) {
// The below define is equivalent to PyObject_TypeCheck(inst, cls)
#define sym_IS_SUBTYPE(inst, cls) ((inst) == (cls) || PyType_IsSubtype(inst, cls))
Copy link
Member

Choose a reason for hiding this comment

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

I'll let you choose. We should probably either duplicate it, or just make it a proper function in optimizer_symbols.c.

@bedevere-app
Copy link

bedevere-app bot commented Jul 1, 2025

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to 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

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.