From ee947e337ba96cf56a767145623731067debcbf8 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Fri, 25 Apr 2025 14:41:27 +0100 Subject: [PATCH 1/3] gh-131798: JIT: Narrow the return type of _CALL_LEN to int Reduce unnecessary guards whenever `len()` is called and used in arithmetic operations. --- Lib/test/test_capi/test_opt.py | 12 ++++++++++++ Python/optimizer_bytecodes.c | 4 ++++ Python/optimizer_cases.c.h | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 0fb2d78a87ecf1..0047306ae422db 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -1911,6 +1911,18 @@ def testfunc(n): 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/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index ff2830d3003c61..040e54479b722a 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -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 // } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 303e402b759530..9a5a362ec199a9 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -2014,7 +2014,7 @@ 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()); From 705bf8b666fa4ef62795153df565f4a964a784de Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:56:49 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst 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 index 00000000000000..9917a16f65c5dc --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst @@ -0,0 +1 @@ +Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to string. Patch by Diego Russo From a017e9378d16ea02264298bc60c50999b3c57bb1 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Fri, 25 Apr 2025 19:23:54 +0100 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Core_and_Builtins/2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst Co-authored-by: Max Bernstein --- .../2025-04-25-14-56-45.gh-issue-131798.NpcKub.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9917a16f65c5dc..8214870284ed93 100644 --- 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 @@ -1 +1 @@ -Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to string. Patch by Diego Russo +Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to int. Patch by Diego Russo