gh-131798: Optimize _UNARY_INVERT (GH-135222)

This commit is contained in:
Noam Cohen 2025-06-09 13:33:18 +03:00 committed by GitHub
parent c19e36cc4e
commit b150b6aca7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 1 deletions

View File

@ -2275,6 +2275,21 @@ class TestUopsOptimization(unittest.TestCase):
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
self.assertNotIn("_GUARD_TOS_TUPLE", uops)
def test_unary_invert_long_type(self):
def testfunc(n):
for _ in range(n):
a = 9397
x = ~a + ~a
testfunc(TIER2_THRESHOLD)
ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_GUARD_TOS_INT", uops)
self.assertNotIn("_GUARD_NOS_INT", uops)
def global_identity(x):
return x

View File

@ -0,0 +1 @@
Optimize ``_UNARY_INVERT`` in JIT-compiled code.

View File

@ -467,6 +467,15 @@ dummy_func(void) {
res = sym_new_truthiness(ctx, value, false);
}
op(_UNARY_INVERT, (value -- res)) {
if (sym_matches_type(value, &PyLong_Type)) {
res = sym_new_type(ctx, &PyLong_Type);
}
else {
res = sym_new_not_null(ctx);
}
}
op(_COMPARE_OP, (left, right -- res)) {
if (oparg & 16) {
res = sym_new_type(ctx, &PyBool_Type);

View File

@ -285,8 +285,15 @@
}
case _UNARY_INVERT: {
JitOptSymbol *value;
JitOptSymbol *res;
value = stack_pointer[-1];
if (sym_matches_type(value, &PyLong_Type)) {
res = sym_new_type(ctx, &PyLong_Type);
}
else {
res = sym_new_not_null(ctx);
}
stack_pointer[-1] = res;
break;
}