]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120619: Optimize through `_Py_FRAME_GENERAL` (GH-124518)
authorKen Jin <kenjin@python.org>
Wed, 2 Oct 2024 17:10:51 +0000 (01:10 +0800)
committerGitHub <noreply@github.com>
Wed, 2 Oct 2024 17:10:51 +0000 (01:10 +0800)
* Optimize through _Py_FRAME_GENERAL

* refactor

Python/optimizer_analysis.c
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index f30e873605d8588630fab249e1aa5b8c2b3494fe..06826ff942a761638d9ef1929b0030bf37c82e4d 100644 (file)
@@ -385,6 +385,30 @@ get_code(_PyUOpInstruction *op)
     return co;
 }
 
+static PyCodeObject *
+get_code_with_logging(_PyUOpInstruction *op)
+{
+    PyCodeObject *co = NULL;
+    uint64_t push_operand = op->operand;
+    if (push_operand & 1) {
+        co = (PyCodeObject *)(push_operand & ~1);
+        DPRINTF(3, "code=%p ", co);
+        assert(PyCode_Check(co));
+    }
+    else {
+        PyFunctionObject *func = (PyFunctionObject *)push_operand;
+        DPRINTF(3, "func=%p ", func);
+        if (func == NULL) {
+            DPRINTF(3, "\n");
+            DPRINTF(1, "Missing function\n");
+            return NULL;
+        }
+        co = (PyCodeObject *)func->func_code;
+        DPRINTF(3, "code=%p ", co);
+    }
+    return co;
+}
+
 /* 1 for success, 0 for not ready, cannot error at the moment. */
 static int
 optimize_uops(
index 9a1b9da52f4bb5e7c87a3f221b3d1e9146964a68..bf8f0753f800c0e807aaf407a05bb6234e1d49ea 100644 (file)
@@ -575,25 +575,13 @@ dummy_func(void) {
 
         PyCodeObject *co = NULL;
         assert((this_instr + 2)->opcode == _PUSH_FRAME);
-        uint64_t push_operand = (this_instr + 2)->operand;
-        if (push_operand & 1) {
-            co = (PyCodeObject *)(push_operand & ~1);
-            DPRINTF(3, "code=%p ", co);
-            assert(PyCode_Check(co));
-        }
-        else {
-            PyFunctionObject *func = (PyFunctionObject *)push_operand;
-            DPRINTF(3, "func=%p ", func);
-            if (func == NULL) {
-                DPRINTF(3, "\n");
-                DPRINTF(1, "Missing function\n");
-                ctx->done = true;
-                break;
-            }
-            co = (PyCodeObject *)func->func_code;
-            DPRINTF(3, "code=%p ", co);
+        co = get_code_with_logging((this_instr + 2));
+        if (co == NULL) {
+            ctx->done = true;
+            break;
         }
 
+
         assert(self_or_null != NULL);
         assert(args != NULL);
         if (sym_is_not_null(self_or_null)) {
@@ -619,12 +607,17 @@ dummy_func(void) {
     }
 
     op(_PY_FRAME_GENERAL, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) {
-        /* The _Py_UOpsAbstractFrame design assumes that we can copy arguments across directly */
-        (void)callable;
-        (void)self_or_null;
-        (void)args;
-        new_frame = NULL;
-        ctx->done = true;
+        (void)(self_or_null);
+        (void)(callable);
+        PyCodeObject *co = NULL;
+        assert((this_instr + 2)->opcode == _PUSH_FRAME);
+        co = get_code_with_logging((this_instr + 2));
+        if (co == NULL) {
+            ctx->done = true;
+            break;
+        }
+
+        new_frame = frame_new(ctx, co, 0, NULL, 0);
     }
 
     op(_PY_FRAME_KW, (callable, self_or_null, args[oparg], kwnames -- new_frame: _Py_UOpsAbstractFrame *)) {
index 4d172e3c7627045a34837cac00ea796937d57b5f..fc0c0eff01d4c1c9d6a4f292ec87011d5e72b3d4 100644 (file)
             _Py_UopsSymbol *self_or_null;
             _Py_UopsSymbol *callable;
             _Py_UOpsAbstractFrame *new_frame;
-            args = &stack_pointer[-oparg];
             self_or_null = stack_pointer[-1 - oparg];
             callable = stack_pointer[-2 - oparg];
-            /* The _Py_UOpsAbstractFrame design assumes that we can copy arguments across directly */
-            (void)callable;
-            (void)self_or_null;
-            (void)args;
-            new_frame = NULL;
-            ctx->done = true;
+            (void)(self_or_null);
+            (void)(callable);
+            PyCodeObject *co = NULL;
+            assert((this_instr + 2)->opcode == _PUSH_FRAME);
+            co = get_code_with_logging((this_instr + 2));
+            if (co == NULL) {
+                ctx->done = true;
+                break;
+            }
+            new_frame = frame_new(ctx, co, 0, NULL, 0);
             stack_pointer[-2 - oparg] = (_Py_UopsSymbol *)new_frame;
             stack_pointer += -1 - oparg;
             assert(WITHIN_STACK_BOUNDS());
             (void)callable;
             PyCodeObject *co = NULL;
             assert((this_instr + 2)->opcode == _PUSH_FRAME);
-            uint64_t push_operand = (this_instr + 2)->operand;
-            if (push_operand & 1) {
-                co = (PyCodeObject *)(push_operand & ~1);
-                DPRINTF(3, "code=%p ", co);
-                assert(PyCode_Check(co));
-            }
-            else {
-                PyFunctionObject *func = (PyFunctionObject *)push_operand;
-                DPRINTF(3, "func=%p ", func);
-                if (func == NULL) {
-                    DPRINTF(3, "\n");
-                    DPRINTF(1, "Missing function\n");
-                    ctx->done = true;
-                    break;
-                }
-                co = (PyCodeObject *)func->func_code;
-                DPRINTF(3, "code=%p ", co);
+            co = get_code_with_logging((this_instr + 2));
+            if (co == NULL) {
+                ctx->done = true;
+                break;
             }
             assert(self_or_null != NULL);
             assert(args != NULL);