gh-131798: Optimize _UNARY_NEGATIVE (GH-135223)

This commit is contained in:
Noam Cohen 2025-06-23 22:42:09 +03:00 committed by GitHub
parent 569fc6870f
commit bda121862e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 2 deletions

View file

@ -2432,6 +2432,24 @@ def testfunc(n):
self.assertIn("_POP_TOP_FLOAT", uops) self.assertIn("_POP_TOP_FLOAT", uops)
def test_unary_negative_long_float_type(self):
def testfunc(n):
for _ in range(n):
a = 9397
f = 9397.0
x = -a + -a
y = -f + -f
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)
self.assertNotIn("_GUARD_TOS_FLOAT", uops)
self.assertNotIn("_GUARD_NOS_FLOAT", uops)
def global_identity(x): def global_identity(x):
return x return x

View file

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

View file

@ -451,10 +451,16 @@ dummy_func(void) {
if (sym_is_compact_int(value)) { if (sym_is_compact_int(value)) {
res = sym_new_compact_int(ctx); res = sym_new_compact_int(ctx);
} }
else {
PyTypeObject *type = sym_get_type(value);
if (type == &PyLong_Type || type == &PyFloat_Type) {
res = sym_new_type(ctx, type);
}
else { else {
res = sym_new_not_null(ctx); res = sym_new_not_null(ctx);
} }
} }
}
op(_UNARY_INVERT, (value -- res)) { op(_UNARY_INVERT, (value -- res)) {
if (sym_matches_type(value, &PyLong_Type)) { if (sym_matches_type(value, &PyLong_Type)) {

View file

@ -189,9 +189,15 @@
if (sym_is_compact_int(value)) { if (sym_is_compact_int(value)) {
res = sym_new_compact_int(ctx); res = sym_new_compact_int(ctx);
} }
else {
PyTypeObject *type = sym_get_type(value);
if (type == &PyLong_Type || type == &PyFloat_Type) {
res = sym_new_type(ctx, type);
}
else { else {
res = sym_new_not_null(ctx); res = sym_new_not_null(ctx);
} }
}
stack_pointer[-1] = res; stack_pointer[-1] = res;
break; break;
} }