]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131798: JIT: Narrow the return type of _CALL_LEN to int (#132940)
authorDiego Russo <diego.russo@arm.com>
Fri, 25 Apr 2025 18:57:42 +0000 (19:57 +0100)
committerGitHub <noreply@github.com>
Fri, 25 Apr 2025 18:57:42 +0000 (02:57 +0800)
Reduce unnecessary guards whenever `len()` is called and used
after.

Co-authored-by: Max Bernstein <tekknolagi@gmail.com>
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst [new file with mode: 0644]
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 0fb2d78a87ecf1907dd89ff1da02e0c81422ecf7..0047306ae422dbc2d4cb68bb9b2417913fe4fe41 100644 (file)
@@ -1911,6 +1911,18 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertNotIn("_COMPARE_OP_INT", uops)
         self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
 
+    def test_call_len(self):
+        def testfunc(n):
+            a = [1, 2, 3, 4]
+            for _ in range(n):
+                _ = len(a) - 1
+
+        _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        uops = get_opnames(ex)
+        self.assertNotIn("_GUARD_NOS_INT", uops)
+        self.assertNotIn("_GUARD_TOS_INT", uops)
+        self.assertIn("_CALL_LEN", uops)
+
 
 def global_identity(x):
     return x
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst
new file mode 100644 (file)
index 0000000..8214870
--- /dev/null
@@ -0,0 +1 @@
+Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to int. Patch by Diego Russo
index ff2830d3003c61358e2adb50d8ca7a419b70a296..040e54479b722a90d71715d24e8cd4b555d4563c 100644 (file)
@@ -1055,6 +1055,10 @@ dummy_func(void) {
         sym_set_const(callable, (PyObject *)&PyUnicode_Type);
     }
 
+    op(_CALL_LEN, (callable[1], self_or_null[1], args[oparg] -- res)) {
+        res = sym_new_type(ctx, &PyLong_Type);
+    }
+
 // END BYTECODES //
 
 }
index 303e402b759530c1c2d8c8ab5eaf7ca68c480d64..9a5a362ec199a992dc47edd47594d3ec4ba984e2 100644 (file)
 
         case _CALL_LEN: {
             JitOptSymbol *res;
-            res = sym_new_not_null(ctx);
+            res = sym_new_type(ctx, &PyLong_Type);
             stack_pointer[-2 - oparg] = res;
             stack_pointer += -1 - oparg;
             assert(WITHIN_STACK_BOUNDS());