-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
tomasr8
wants to merge
3
commits into
python:main
Choose a base branch
from
tomasr8:jit-tuple-isinstance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2025-05-22-22-19-56.gh-issue-131798.kxRt1-.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Optimize ``_CALL_ISINSTANCE`` in the JIT when the second argument is a tuple | ||
of classes. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
// the result is always a bool, but sometimes we can | ||
// narrow it down to True or False | ||
res = sym_new_type(ctx, &PyBool_Type); | ||
|
@@ -947,14 +950,54 @@ dummy_func(void) { | |
// isinstance(inst, cls) where both inst and cls have | ||
// known types, meaning we can deduce either True or False | ||
|
||
// The below check is equivalent to PyObject_TypeCheck(inst, cls) | ||
PyObject *out = Py_False; | ||
if (inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o)) { | ||
if (sym_IS_SUBTYPE(inst_type, cls_o)) { | ||
out = Py_True; | ||
} | ||
sym_set_const(res, out); | ||
REPLACE_OP(this_instr, _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out); | ||
} | ||
else if (inst_type && sym_matches_type(cls, &PyTuple_Type)) { | ||
// isinstance(inst, tup) where inst has a known type and tup is a tuple. | ||
// We can deduce True if inst is an instance of at least one of | ||
// the items in the tuple. | ||
// We can deduce False if all items in the tuple have known types and | ||
// inst is not an instance of any of them. | ||
|
||
int length = sym_tuple_length(cls); | ||
if (length != -1) { | ||
// We cannot do anything about tuples with unknown length | ||
bool all_items_known = true; | ||
PyObject *out = NULL; | ||
for (int i = 0; i < length; i++) { | ||
JitOptSymbol *item = sym_tuple_getitem(ctx, cls, i); | ||
if (!sym_has_type(item)) { | ||
// There is an unknown item in the tuple, | ||
// we can no longer deduce False. | ||
all_items_known = false; | ||
continue; | ||
} | ||
PyTypeObject *cls_o = (PyTypeObject *)sym_get_const(ctx, item); | ||
if (cls_o && | ||
sym_matches_type(item, &PyType_Type) && | ||
sym_IS_SUBTYPE(inst_type, cls_o)) | ||
{ | ||
out = Py_True; | ||
break; | ||
} | ||
} | ||
if (!out && all_items_known) { | ||
// We haven't deduced True, but all items in the tuple are known | ||
// so we can deduce False | ||
out = Py_False; | ||
} | ||
if (out) { | ||
sym_set_const(res, out); | ||
REPLACE_OP(this_instr, _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out); | ||
} | ||
} | ||
} | ||
#undef sym_IS_SUBTYPE | ||
} | ||
|
||
op(_GUARD_IS_TRUE_POP, (flag -- )) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
_BUILD_TUPLE
is preventing us from optimizing out_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
.The bytecode is basically:
To optimize this, we'd need some special handling for
_BUILD_TUPLE
inremove_unneeded_uops
.