-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
1d582e8
97aca36
467bcb9
d2e339f
0465c9a
088ccd8
fb28e06
1e9b95f
a09b860
17701d3
cf71d8b
25ae15f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -938,9 +938,6 @@ 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)) | ||||||
|
|
||||||
| // the result is always a bool, but sometimes we can | ||||||
| // narrow it down to True or False | ||||||
| res = sym_new_type(ctx, &PyBool_Type); | ||||||
|
|
@@ -951,7 +948,7 @@ dummy_func(void) { | |||||
| // known types, meaning we can deduce either True or False | ||||||
|
|
||||||
| PyObject *out = Py_False; | ||||||
| if (sym_IS_SUBTYPE(inst_type, cls_o)) { | ||||||
| if (inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o)) { | ||||||
| out = Py_True; | ||||||
| } | ||||||
| sym_set_const(res, out); | ||||||
|
|
@@ -972,23 +969,24 @@ dummy_func(void) { | |||||
| 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. | ||||||
| // There is an unknown item in the tuple. | ||||||
| // It could potentially define its own __instancecheck__ | ||||||
| // method so we can only deduce bool. | ||||||
| all_items_known = false; | ||||||
| continue; | ||||||
| break; | ||||||
|
Member
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. Could this be
Suggested change
Member
Author
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. Yup, see #134543 (comment). I just needed to add some extra bookkeeping to know when we can replace the call and when not. |
||||||
| } | ||||||
|
tomasr8 marked this conversation as resolved.
|
||||||
| PyTypeObject *cls_o = (PyTypeObject *)sym_get_const(ctx, item); | ||||||
| if (cls_o && | ||||||
| sym_matches_type(item, &PyType_Type) && | ||||||
|
Member
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. Maybe add a comment explaining that this is to protect against metaclasses definine
Member
Author
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. How would you formulate it? I don't think of it as specifically a guard for
Member
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. We're not only checking that the object is a subclass of
Member
Author
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. Thanks for the clarification, I added a comment :)
Member
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. Wait, wouldn't this only work on something that is
Member
Author
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. Hmm I don't believe so. If
Member
Author
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. For example something like this seems to work: JitOptRef ref_type = _Py_uop_sym_new_const(ctx, (PyObject *)&PyLong_Type);
TEST_PREDICATE(_Py_uop_sym_matches_type(ref_type, &PyType_Type), "int is not a type");
Member
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. That''s kinda strange. The code for so it's a pointer comparison, not a subclass check.
Member
Author
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. The subclass check is done after with |
||||||
| sym_IS_SUBTYPE(inst_type, cls_o)) | ||||||
| (inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o))) | ||||||
| { | ||||||
| out = Py_True; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if (!out && all_items_known) { | ||||||
| if (out == NULL && all_items_known) { | ||||||
| // We haven't deduced True, but all items in the tuple are known | ||||||
| // so we can deduce False | ||||||
| // so we can deduce False. | ||||||
| out = Py_False; | ||||||
| } | ||||||
| if (out) { | ||||||
|
|
@@ -997,7 +995,6 @@ dummy_func(void) { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
| #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.
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.
I'm confused, why can't we narrow to
True? We can't remove the call, but the result is known.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.
Yep, that was a brainfart 😄 I somehow conflated narrowing to True/False with replacing the op, but we can obviously just narrow without removing the call as you said!