Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Loading
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 83 additions & 3 deletions 86 Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2105,17 +2105,76 @@ 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)
Comment on lines +2124 to +2125
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.


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(self):
def testfunc(n):
x = 0
for _ in range(n):
# One of the classes is unknown, but we can still
# narrow to True
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.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_unknown_not_narrowed(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)
Expand All @@ -2124,6 +2183,27 @@ def testfunc(n):
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_metaclass(self):
class EvenNumberMeta(type):
def __instancecheck__(self, number):
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.
48 changes: 46 additions & 2 deletions 48 Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.


// the result is always a bool, but sometimes we can
// narrow it down to True or False
res = sym_new_type(ctx, &PyBool_Type);
Expand All @@ -947,14 +950,55 @@ 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);
bool all_items_known = true;
PyObject *out = NULL;
if (length >= 0) {
// We cannot do anything about tuples with unknown (length == -1)

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;
}
Comment on lines +974 to +979
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.

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 -- )) {
Expand Down
34 changes: 33 additions & 1 deletion 34 Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.