Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
1d582e8
97aca36
467bcb9
d2e339f
0465c9a
088ccd8
fb28e06
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2191,17 +2191,154 @@ def testfunc(n): | ||
self.assertNotIn("_TO_BOOL_BOOL", uops) | ||
self.assertIn("_GUARD_IS_TRUE_POP", uops) | ||
deftest_call_isinstance_tuple_of_classes_is_true(self): | ||
def testfunc(n): | ||
x = 0 | ||
for _ in range(n): | ||
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) | ||
Comment on lines +2210 to +2211 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.
LOAD_CONSTLOAD_CONST_BUILD_TUPLE_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW To optimize this, we'd need some special handling for | ||
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 | ||
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_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_2(self): | ||
def testfunc(n): | ||
x = 0 | ||
for _ in range(n): | ||
# One of the classes is unknown, so we can't narrow | ||
# to True or False, only bool | ||
y = isinstance(42, (eval('str'), 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_3(self): | ||
def testfunc(n): | ||
x = 0 | ||
for _ in range(n): | ||
# One of the classes is unknown, so we can't narrow | ||
# to True or False, only bool | ||
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): | ||
# One of the classes is unknown, so we can't narrow | ||
# to True or False, only bool | ||
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, ()) | ||
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) | ||
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. |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.