From e6a0f2a3ab2e8b6e59cbfecd879c64db5a20c806 Mon Sep 17 00:00:00 2001 From: Sacul0457Deve <183588943+Sacul0457@users.noreply.github.com.> Date: Wed, 4 Feb 2026 19:35:06 +0800 Subject: [PATCH 1/6] add _GUARD_TOS_SLICE to optimizer_bytecodes --- Lib/test/test_capi/test_opt.py | 17 +++++++++++++++++ Python/optimizer_bytecodes.c | 7 +++++++ Python/optimizer_cases.c.h | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 437cc340fc90e3..02866b5341586f 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2045,6 +2045,23 @@ def f(n): self.assertNotIn("_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS", uops) self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) + def test_remove_guard_for_known_type_slice(self): + def f(n): + x = 0 + for _ in range(n): + l = [1, 2, 3] + slice_obj = slice(0, 1) + x += l[slice_obj][0] # guarded + x += l[slice_obj][0] # unguarded + return x + res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) + self.assertEqual(res, TIER2_THRESHOLD * 2) + uops = get_opnames(ex) + + count = count_ops(ex, "_GUARD_TOS_SLICE") + self.assertEqual(count, 1) + self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops) + def test_binary_subcsr_str_int_narrows_to_str(self): def testfunc(n): x = [] diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 89c6707160326c..93ae5555a8e8cf 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1416,6 +1416,13 @@ dummy_func(void) { ADD_OP(_NOP, 0, 0); } } + op(_GUARD_TOS_SLICE, (tos -- tos)){ + if (sym_matches_type(tos, &PySlice_Type)){ + ADD_OP(_NOP, 0, 0); + } + sym_set_type(tos, &PySlice_Type); + } + op(_GUARD_NOS_NULL, (null, unused -- null, unused)) { if (sym_is_null(null)) { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 61a30314c21789..259cf2a1c32dc7 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -370,6 +370,12 @@ } case _GUARD_TOS_SLICE: { + JitOptRef tos; + tos = stack_pointer[-1]; + if (sym_matches_type(tos, &PySlice_Type)){ + ADD_OP(_NOP, 0, 0); + } + sym_set_type(tos, &PySlice_Type); break; } From d2e98d715121e102d8b3a9ee6dc4c361a8be32ed Mon Sep 17 00:00:00 2001 From: Sacul0457Deve <183588943+Sacul0457@users.noreply.github.com.> Date: Wed, 4 Feb 2026 19:37:45 +0800 Subject: [PATCH 2/6] shift test function up --- Lib/test/test_capi/test_opt.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 02866b5341586f..9463e5d5594d47 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2030,21 +2030,6 @@ def f(n): self.assertNotIn("_GUARD_NOS_TUPLE", uops) self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) - def test_remove_guard_for_tuple_bounds_check(self): - def f(n): - x = 0 - for _ in range(n): - t = (1, 2, 3) - x += t[0] - return x - - res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) - self.assertEqual(res, TIER2_THRESHOLD) - self.assertIsNotNone(ex) - uops = get_opnames(ex) - self.assertNotIn("_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS", uops) - self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) - def test_remove_guard_for_known_type_slice(self): def f(n): x = 0 @@ -2062,6 +2047,21 @@ def f(n): self.assertEqual(count, 1) self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops) + def test_remove_guard_for_tuple_bounds_check(self): + def f(n): + x = 0 + for _ in range(n): + t = (1, 2, 3) + x += t[0] + return x + + res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) + self.assertEqual(res, TIER2_THRESHOLD) + self.assertIsNotNone(ex) + uops = get_opnames(ex) + self.assertNotIn("_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS", uops) + self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) + def test_binary_subcsr_str_int_narrows_to_str(self): def testfunc(n): x = [] From 41544e78122889f93241b1549ef81b1395fd6d2a Mon Sep 17 00:00:00 2001 From: Sacul0457Deve <183588943+Sacul0457@users.noreply.github.com.> Date: Wed, 4 Feb 2026 20:11:49 +0800 Subject: [PATCH 3/6] add spacing between ops --- Python/optimizer_bytecodes.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 93ae5555a8e8cf..f27fcb6d86f9ba 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1416,6 +1416,8 @@ dummy_func(void) { ADD_OP(_NOP, 0, 0); } } + + op(_GUARD_TOS_SLICE, (tos -- tos)){ if (sym_matches_type(tos, &PySlice_Type)){ ADD_OP(_NOP, 0, 0); From 8fe5b1213178361737e0eaee87d88bbb131b2edf Mon Sep 17 00:00:00 2001 From: Sacul0457Deve <183588943+Sacul0457@users.noreply.github.com.> Date: Wed, 4 Feb 2026 20:13:13 +0800 Subject: [PATCH 4/6] resize spacing between ops again --- Python/optimizer_bytecodes.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index f27fcb6d86f9ba..3a2af7d9198175 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1417,7 +1417,6 @@ dummy_func(void) { } } - op(_GUARD_TOS_SLICE, (tos -- tos)){ if (sym_matches_type(tos, &PySlice_Type)){ ADD_OP(_NOP, 0, 0); @@ -1425,7 +1424,6 @@ dummy_func(void) { sym_set_type(tos, &PySlice_Type); } - op(_GUARD_NOS_NULL, (null, unused -- null, unused)) { if (sym_is_null(null)) { ADD_OP(_NOP, 0, 0); From 1216b7cfaa9ac0219d91dc0e36c4404514b125ad Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 4 Feb 2026 12:19:50 +0000 Subject: [PATCH 5/6] =?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 --- .../2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst new file mode 100644 index 00000000000000..f14f870d582c30 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst @@ -0,0 +1 @@ +Optimise `_GUARD_TOS_SLICE` in the JIT. From 5887eb8fbab5f29c2bbf192b21287ac08110bc62 Mon Sep 17 00:00:00 2001 From: Sacul <183588943+Sacul0457@users.noreply.github.com> Date: Wed, 4 Feb 2026 20:28:22 +0800 Subject: [PATCH 6/6] Fix formatting of _GUARD_TOS_SLICE in NEWS entry --- .../2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst index f14f870d582c30..849889f81fc323 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst @@ -1 +1 @@ -Optimise `_GUARD_TOS_SLICE` in the JIT. +Optimise ``_GUARD_TOS_SLICE`` in the JIT.