GH-131798: Optimize away type(x) in the JIT when the result is known (GH-135194)

This commit is contained in:
Tomas R. 2025-06-07 01:44:43 +02:00 committed by GitHub
parent f00512db20
commit 46151648ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 9 deletions

View file

@ -1778,7 +1778,23 @@ class TestUopsOptimization(unittest.TestCase):
self.assertNotIn("_GUARD_TOS_UNICODE", uops)
self.assertIn("_BINARY_OP_ADD_UNICODE", uops)
def test_call_type_1(self):
def test_call_type_1_guards_removed(self):
def testfunc(n):
x = 0
for _ in range(n):
foo = eval('42')
x += type(foo) is int
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_TYPE_1", uops)
self.assertNotIn("_GUARD_NOS_NULL", uops)
self.assertNotIn("_GUARD_CALLABLE_TYPE_1", uops)
def test_call_type_1_known_type(self):
def testfunc(n):
x = 0
for _ in range(n):
@ -1789,9 +1805,13 @@ class TestUopsOptimization(unittest.TestCase):
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_CALL_TYPE_1", uops)
self.assertNotIn("_GUARD_NOS_NULL", uops)
self.assertNotIn("_GUARD_CALLABLE_TYPE_1", uops)
# When the result of type(...) is known, _CALL_TYPE_1 is replaced with
# _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW which is optimized away in
# remove_unneeded_uops.
self.assertNotIn("_CALL_TYPE_1", uops)
self.assertNotIn("_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_POP_CALL_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
def test_call_type_1_result_is_const(self):
def testfunc(n):
@ -1806,7 +1826,6 @@ class TestUopsOptimization(unittest.TestCase):
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_CALL_TYPE_1", uops)
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops)
def test_call_str_1(self):

View file

@ -0,0 +1,2 @@
Optimize away ``_CALL_TYPE_1`` in the JIT when the return type is known.
Patch by Tomas Roun

View file

@ -937,8 +937,11 @@ dummy_func(void) {
}
op(_CALL_TYPE_1, (unused, unused, arg -- res)) {
if (sym_has_type(arg)) {
res = sym_new_const(ctx, (PyObject *)sym_get_type(arg));
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
}
else {
res = sym_new_not_null(ctx);

View file

@ -2056,8 +2056,11 @@
JitOptSymbol *arg;
JitOptSymbol *res;
arg = stack_pointer[-1];
if (sym_has_type(arg)) {
res = sym_new_const(ctx, (PyObject *)sym_get_type(arg));
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
}
else {
res = sym_new_not_null(ctx);