]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131798: Optimize `_UNARY_NEGATIVE` (GH-135223)
authorNoam Cohen <noam@noam.me>
Mon, 23 Jun 2025 19:42:09 +0000 (22:42 +0300)
committerGitHub <noreply@github.com>
Mon, 23 Jun 2025 19:42:09 +0000 (03:42 +0800)
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-19-17-22.gh-issue-131798.XoV8Eb.rst [new file with mode: 0644]
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 0a85b19e06f15805761fe1ca09ab5f09378b0c18..e4c9a463855a69c24dfd905b3241fdaad2bf38d6 100644 (file)
@@ -2432,6 +2432,24 @@ class TestUopsOptimization(unittest.TestCase):
         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):
     return x
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-19-17-22.gh-issue-131798.XoV8Eb.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-19-17-22.gh-issue-131798.XoV8Eb.rst
new file mode 100644 (file)
index 0000000..6a9d9c6
--- /dev/null
@@ -0,0 +1 @@
+Optimize ``_UNARY_NEGATIVE`` in JIT-compiled code.
index f8fbaf232ffa2e2208b1c8453ef8469e048c24bb..f8a0484bdc2b04b0a6d836b575a4093ee1e7441a 100644 (file)
@@ -452,7 +452,13 @@ dummy_func(void) {
             res = sym_new_compact_int(ctx);
         }
         else {
-            res = sym_new_not_null(ctx);
+            PyTypeObject *type = sym_get_type(value);
+            if (type == &PyLong_Type || type == &PyFloat_Type) {
+                res = sym_new_type(ctx, type);
+            }
+            else {
+                res = sym_new_not_null(ctx);
+            }
         }
     }
 
index 1e581afadc9569ca50c8fa10c4ecea8924f470ce..10767ccdbd57f5b845ab9891305af776d06006c0 100644 (file)
                 res = sym_new_compact_int(ctx);
             }
             else {
-                res = sym_new_not_null(ctx);
+                PyTypeObject *type = sym_get_type(value);
+                if (type == &PyLong_Type || type == &PyFloat_Type) {
+                    res = sym_new_type(ctx, type);
+                }
+                else {
+                    res = sym_new_not_null(ctx);
+                }
             }
             stack_pointer[-1] = res;
             break;