| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -2383,17 +2383,154 @@ def testfunc(n): | |
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertIn("_GUARD_IS_TRUE_POP", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes(self): | ||
| def test_call_isinstance_tuple_of_classes_is_true(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # A tuple of classes is currently not optimized, | ||
| # so this is only narrowed to bool: | ||
| y = isinstance(42, (int, str)) | ||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertNotIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertNotIn("_GUARD_IS_TRUE_POP", uops) | ||
| self.assertIn("_BUILD_TUPLE", uops) | ||
| self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes_is_false(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| y = isinstance(42, (bool, str)) | ||
| if not y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertNotIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertNotIn("_GUARD_IS_FALSE_POP", uops) | ||
| self.assertIn("_BUILD_TUPLE", uops) | ||
| self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes_true_unknown_1(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # One of the classes is unknown, but it comes | ||
| # after a known class, so we can narrow to True and | ||
| # remove the isinstance call. | ||
| y = isinstance(42, (int, eval('str'))) | ||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertNotIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertNotIn("_GUARD_IS_TRUE_POP", uops) | ||
| self.assertIn("_BUILD_TUPLE", uops) | ||
| self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes_true_unknown_2(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # We can narrow to True, but since the unknown class comes | ||
| # first and could potentially trigger an __instancecheck__, | ||
| # we can't remove the isinstance call. | ||
| y = isinstance(42, (eval('str'), int)) | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'm confused, why can't we narrow to True? We can't remove the call, but the result is known.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityYep, 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!
Sorry, something went wrong.
All reactions
|
||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertNotIn("_GUARD_IS_TRUE_POP", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes_true_unknown_3(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # We can only narrow to bool here | ||
| y = isinstance(42, (str, eval('int'))) | ||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertIn("_GUARD_IS_TRUE_POP", uops) | ||
|
|
||
| def test_call_isinstance_tuple_of_classes_true_unknown_4(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # We can only narrow to bool here | ||
| y = isinstance(42, (eval('int'), str)) | ||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertIn("_GUARD_IS_TRUE_POP", uops) | ||
|
|
||
| def test_call_isinstance_empty_tuple(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| y = isinstance(42, ()) | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI had to pause and think for a second to figure out what this would even do. Nice edge case.
Sorry, something went wrong.
tomasr8 reacted with laugh emoji
All reactions
|
||
| if not y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| uops = get_opnames(ex) | ||
| self.assertNotIn("_CALL_ISINSTANCE", uops) | ||
| self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
| self.assertNotIn("_GUARD_IS_FALSE_POP", uops) | ||
| self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops) | ||
| self.assertNotIn("_POP_CALL_LOAD_CONST_INLINE_BORROW", uops) | ||
| self.assertNotIn("_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW", uops) | ||
| self.assertNotIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops) | ||
|
|
||
| def test_call_isinstance_tuple_unknown_length(self): | ||
| def testfunc(n): | ||
| x = 0 | ||
| for _ in range(n): | ||
| # tuple with an unknown length, we only narrow to bool | ||
| tup = tuple(eval('(int, str)')) | ||
| y = isinstance(42, tup) | ||
| if y: | ||
| x += 1 | ||
| return x | ||
|
|
||
| res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) | ||
| self.assertEqual(res, TIER2_THRESHOLD) | ||
| self.assertIsNotNone(ex) | ||
| Expand Down | ||
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -1025,14 +1025,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)) { | ||
| 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 can_replace_op = true; | ||
| PyObject *out = Py_False; | ||
| for (int i = 0; i < length; i++) { | ||
| JitOptRef item = sym_tuple_getitem(ctx, cls, i); | ||
| if (!sym_has_type(item)) { | ||
| // There is an unknown item in the tuple. | ||
| // It could potentially define its own __instancecheck__ | ||
| // so it is no longer possible to replace the op with a const load. | ||
| out = NULL; | ||
| can_replace_op = false; | ||
| continue; | ||
| } | ||
| PyTypeObject *cls_o = (PyTypeObject *)sym_get_const(ctx, item); | ||
| if (cls_o && | ||
| // Ensure that item is an exact instance of `type` ensuring that | ||
| // there is no __instancecheck__ defined. | ||
| sym_matches_type(item, &PyType_Type) && | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityMaybe add a comment explaining that this is to protect against metaclasses definine __instancecheck__.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHow would you formulate it? I don't think of it as specifically a guard for __instancecheck__ but basically PyObject_TypeCheck adapted to the JIT optimizer.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWe're not only checking that the object is a subclass of type, we're also checking that it is an exact instance of type itself. We care about this second condition because it guarantees that __instancecheck__ doesn't exist (otherwise we would need to look it up to check if it exists or not).
Sorry, something went wrong.
tomasr8 reacted with thumbs up emoji
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThanks for the clarification, I added a comment :)
Sorry, something went wrong.
All reactions
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWait, wouldn't this only work on something that is isinstance(x, type), like the actual type itself, not any type like int? Sorry, I'm confused!
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHmm I don't believe so. If item is e.g. int then sym_matches_type(item, &PyType_Type) should return true
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityFor 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");
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThat''s kinda strange. The code for _Py_uop_sym_matches_type is: _Py_uop_sym_get_type(sym) == typ so it's a pointer comparison, not a subclass check.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThe subclass check is done after with PyType_IsSubtype if that's what you mean? The current version of _CALL_ISINSTANCE (which does not handle tuples) works the same way though. @brandtbucher, what do you think?
Sorry, something went wrong.
All reactions
|
||
| (inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o))) | ||
| { | ||
| out = Py_True; | ||
| break; | ||
| } | ||
| } | ||
| if (out) { | ||
| sym_set_const(res, out); | ||
| if (can_replace_op) { | ||
| REPLACE_OP(this_instr, _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| op(_GUARD_IS_TRUE_POP, (flag -- )) { | ||
| Expand Down | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality_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 in remove_unneeded_uops.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityGot it, might be worth looking into next if you're up for it! Could be tricky, though.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.