https://github.com/python/cpython/commit/bda121862e7d7f4684d9f0281f7d1f408c0c740c
commit: bda121862e7d7f4684d9f0281f7d1f408c0c740c
branch: main
author: Noam Cohen <n...@noam.me>
committer: Fidget-Spinner <kenjin4...@gmail.com>
date: 2025-06-24T03:42:09+08:00
summary:

gh-131798: Optimize `_UNARY_NEGATIVE` (GH-135223)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-19-17-22.gh-issue-131798.XoV8Eb.rst
M Lib/test/test_capi/test_opt.py
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h

diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 0a85b19e06f158..e4c9a463855a69 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -2432,6 +2432,24 @@ def testfunc(n):
         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
index 00000000000000..6a9d9c683f9a8c
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-19-17-22.gh-issue-131798.XoV8Eb.rst
@@ -0,0 +1 @@
+Optimize ``_UNARY_NEGATIVE`` in JIT-compiled code.
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index f8fbaf232ffa2e..f8a0484bdc2b04 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -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);
+            }
         }
     }
 
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 1e581afadc9569..10767ccdbd57f5 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -190,7 +190,13 @@
                 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;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to