]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-125912: Teach the JIT's optimizer about _BINARY_OP_INPLACE_ADD_UNICODE (GH-125935)
authorBrandt Bucher <brandtbucher@microsoft.com>
Mon, 28 Oct 2024 21:37:16 +0000 (14:37 -0700)
committerGitHub <noreply@github.com>
Mon, 28 Oct 2024 21:37:16 +0000 (14:37 -0700)
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index d71b55cbe1c68ddb6651a6196a311603adc5d936..f40ad1e5744fd14d4753ed65ef9cabe5a845afa3 100644 (file)
@@ -331,6 +331,24 @@ dummy_func(void) {
         }
     }
 
+    op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right -- )) {
+        _Py_UopsSymbol *res;
+        if (sym_is_const(left) && sym_is_const(right) &&
+            sym_matches_type(left, &PyUnicode_Type) && sym_matches_type(right, &PyUnicode_Type)) {
+            PyObject *temp = PyUnicode_Concat(sym_get_const(left), sym_get_const(right));
+            if (temp == NULL) {
+                goto error;
+            }
+            res = sym_new_const(ctx, temp);
+            Py_DECREF(temp);
+        }
+        else {
+            res = sym_new_type(ctx, &PyUnicode_Type);
+        }
+        // _STORE_FAST:
+        GETLOCAL(this_instr->operand) = res;
+    }
+
     op(_BINARY_SUBSCR_INIT_CALL, (container, sub -- new_frame: _Py_UOpsAbstractFrame *)) {
         (void)container;
         (void)sub;
index 6ec9e69d1dbc44947a7193d95e5488779d51fa20..243b3efa41b2d0c93d82caa708da9e15928ee5cc 100644 (file)
         }
 
         case _BINARY_OP_INPLACE_ADD_UNICODE: {
+            _Py_UopsSymbol *right;
+            _Py_UopsSymbol *left;
+            right = stack_pointer[-1];
+            left = stack_pointer[-2];
+            _Py_UopsSymbol *res;
+            if (sym_is_const(left) && sym_is_const(right) &&
+                sym_matches_type(left, &PyUnicode_Type) && sym_matches_type(right, &PyUnicode_Type)) {
+                PyObject *temp = PyUnicode_Concat(sym_get_const(left), sym_get_const(right));
+                if (temp == NULL) {
+                    goto error;
+                }
+                res = sym_new_const(ctx, temp);
+                Py_DECREF(temp);
+            }
+            else {
+                res = sym_new_type(ctx, &PyUnicode_Type);
+            }
+            // _STORE_FAST:
+            GETLOCAL(this_instr->operand) = res;
             stack_pointer += -2;
             assert(WITHIN_STACK_BOUNDS());
             break;