https://github.com/python/cpython/commit/71009cb835f9fa37647b0f2f7dcc0d6ca8aa36b6
commit: 71009cb835f9fa37647b0f2f7dcc0d6ca8aa36b6
branch: main
author: Tomas R. <tomas.ro...@gmail.com>
committer: brandtbucher <brandtbuc...@gmail.com>
date: 2025-04-08T08:22:54-07:00
summary:

GH-131798: Narrow the result type of _BINARY_OP_SUBSCR_STR_INT to str in the 
JIT (GH-132153)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-13-17-10.gh-issue-131798.uMrfha.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 0e13799ad47381..3ade7cb7a2b6af 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -1646,6 +1646,26 @@ def f(n):
         self.assertIn("_TO_BOOL_STR", uops)
         self.assertNotIn("_GUARD_TOS_UNICODE", uops)
 
+    def test_binary_subcsr_str_int_narrows_to_str(self):
+        def testfunc(n):
+            x = []
+            s = "foo"
+            for _ in range(n):
+                y = s[0]       # _BINARY_OP_SUBSCR_STR_INT
+                z = "bar" + y  # (_GUARD_TOS_UNICODE) + _BINARY_OP_ADD_UNICODE
+                x.append(z)
+            return x
+
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, ["barf"] * TIER2_THRESHOLD)
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+        self.assertIn("_BINARY_OP_SUBSCR_STR_INT", uops)
+        # _BINARY_OP_SUBSCR_STR_INT narrows the result to 'str' so
+        # the unicode guard before _BINARY_OP_ADD_UNICODE is removed.
+        self.assertNotIn("_GUARD_TOS_UNICODE", uops)
+        self.assertIn("_BINARY_OP_ADD_UNICODE", uops)
+
 
 def global_identity(x):
     return x
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-13-17-10.gh-issue-131798.uMrfha.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-13-17-10.gh-issue-131798.uMrfha.rst
new file mode 100644
index 00000000000000..5ea5fcecc33517
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-13-17-10.gh-issue-131798.uMrfha.rst
@@ -0,0 +1,2 @@
+Allow the JIT to remove unicode guards after ``_BINARY_OP_SUBSCR_STR_INT``
+by setting the return type to string.
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index d7b3564db1b90a..72dc2bbd44e71c 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -366,6 +366,10 @@ dummy_func(void) {
         ctx->done = true;
     }
 
+    op(_BINARY_OP_SUBSCR_STR_INT, (left, right -- res)) {
+        res = sym_new_type(ctx, &PyUnicode_Type);
+    }
+
     op(_TO_BOOL, (value -- res)) {
         int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
         if (!already_bool) {
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 870c32d74ac913..160d09ca7c5580 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -569,7 +569,7 @@
 
         case _BINARY_OP_SUBSCR_STR_INT: {
             JitOptSymbol *res;
-            res = sym_new_not_null(ctx);
+            res = sym_new_type(ctx, &PyUnicode_Type);
             stack_pointer[-2] = res;
             stack_pointer += -1;
             assert(WITHIN_STACK_BOUNDS());

_______________________________________________
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