From f579971c9463178237fc9efba96cac13e0d3b2ac Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:14:34 +0800 Subject: [PATCH 1/4] Minor fixups in int constant propagation --- Python/tier2_redundancy_eliminator_bytecodes.c | 18 +++++++++++++++--- Python/tier2_redundancy_eliminator_cases.c.h | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Python/tier2_redundancy_eliminator_bytecodes.c b/Python/tier2_redundancy_eliminator_bytecodes.c index 39ea0eef627632..0471e9bf39d551 100644 --- a/Python/tier2_redundancy_eliminator_bytecodes.c +++ b/Python/tier2_redundancy_eliminator_bytecodes.c @@ -90,7 +90,11 @@ dummy_func(void) { goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); @@ -110,7 +114,11 @@ dummy_func(void) { goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); @@ -130,7 +138,11 @@ dummy_func(void) { goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); diff --git a/Python/tier2_redundancy_eliminator_cases.c.h b/Python/tier2_redundancy_eliminator_cases.c.h index a9617f51ef4615..66edb8cb483e58 100644 --- a/Python/tier2_redundancy_eliminator_cases.c.h +++ b/Python/tier2_redundancy_eliminator_cases.c.h @@ -194,7 +194,11 @@ goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); @@ -222,7 +226,11 @@ goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); @@ -250,7 +258,11 @@ goto error; } res = sym_new_const(ctx, temp); - // TODO replace opcode with constant propagated one and add tests! + if (res == NULL) { + goto out_of_space; + } + // TODO gh-115506: + // replace opcode with constant propagated one and add tests! } else { res = sym_new_known_type(ctx, &PyLong_Type); From fb5f3a8cc3c7838514a12d2ea09e67aa39e9ade1 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:48:43 +0800 Subject: [PATCH 2/4] Address Mark's comments --- Python/optimizer_analysis.c | 16 ++-- .../tier2_redundancy_eliminator_bytecodes.c | 87 ++++--------------- Python/tier2_redundancy_eliminator_cases.c.h | 87 ++++--------------- 3 files changed, 46 insertions(+), 144 deletions(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index d73bc310345f41..b6289426063135 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -588,16 +588,16 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, INST->oparg = ARG; \ INST->operand = OPERAND; +#define OUT_OF_SPACE_IF_NULL(RES, EXPR) \ + RES = (EXPR); \ + if (RES == NULL) { \ + goto out_of_space; \ + } + #define _LOAD_ATTR_NOT_NULL \ do { \ - attr = sym_new_known_notnull(ctx); \ - if (attr == NULL) { \ - goto error; \ - } \ - null = sym_new_null(ctx); \ - if (null == NULL) { \ - goto error; \ - } \ + OUT_OF_SPACE_IF_NULL(attr, sym_new_known_notnull(ctx)); \ + OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); \ } while (0); diff --git a/Python/tier2_redundancy_eliminator_bytecodes.c b/Python/tier2_redundancy_eliminator_bytecodes.c index 0471e9bf39d551..a0ba9331ad2a37 100644 --- a/Python/tier2_redundancy_eliminator_bytecodes.c +++ b/Python/tier2_redundancy_eliminator_bytecodes.c @@ -43,10 +43,8 @@ dummy_func(void) { op(_LOAD_FAST_AND_CLEAR, (-- value)) { value = GETLOCAL(oparg); - _Py_UOpsSymType *temp = sym_new_null(ctx); - if (temp == NULL) { - goto out_of_space; - } + _Py_UOpsSymType *temp; + OUT_OF_SPACE_IF_NULL(temp, sym_new_null(ctx)); GETLOCAL(oparg) = temp; } @@ -89,18 +87,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } } @@ -113,18 +105,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } } @@ -137,18 +123,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } } @@ -159,39 +139,21 @@ dummy_func(void) { } op(_LOAD_CONST_INLINE, (ptr/4 -- value)) { - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); } op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) { - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); } op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) { - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } - null = sym_new_null(ctx); - if (null == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); } op(_LOAD_CONST_INLINE_BORROW_WITH_NULL, (ptr/4 -- value, null)) { - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } - null = sym_new_null(ctx); - if (null == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); } @@ -273,10 +235,8 @@ dummy_func(void) { localsplus_start = args; n_locals_already_filled = argcount; } - new_frame = ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0); - if (new_frame == NULL){ - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(new_frame, + ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); } op(_POP_FRAME, (retval -- res)) { @@ -299,10 +259,7 @@ dummy_func(void) { /* This has to be done manually */ (void)seq; for (int i = 0; i < oparg; i++) { - values[i] = sym_new_unknown(ctx); - if (values[i] == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); } } @@ -311,18 +268,12 @@ dummy_func(void) { (void)seq; int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1; for (int i = 0; i < totalargs; i++) { - values[i] = sym_new_unknown(ctx); - if (values[i] == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); } } op(_ITER_NEXT_RANGE, (iter -- iter, next)) { - next = sym_new_known_type(ctx, &PyLong_Type); - if (next == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(next, sym_new_known_type(ctx, &PyLong_Type)); (void)iter; } diff --git a/Python/tier2_redundancy_eliminator_cases.c.h b/Python/tier2_redundancy_eliminator_cases.c.h index 66edb8cb483e58..936f11c307b46a 100644 --- a/Python/tier2_redundancy_eliminator_cases.c.h +++ b/Python/tier2_redundancy_eliminator_cases.c.h @@ -36,10 +36,8 @@ case _LOAD_FAST_AND_CLEAR: { _Py_UOpsSymType *value; value = GETLOCAL(oparg); - _Py_UOpsSymType *temp = sym_new_null(ctx); - if (temp == NULL) { - goto out_of_space; - } + _Py_UOpsSymType *temp; + OUT_OF_SPACE_IF_NULL(temp, sym_new_null(ctx)); GETLOCAL(oparg) = temp; stack_pointer[0] = value; stack_pointer += 1; @@ -193,18 +191,12 @@ if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -225,18 +217,12 @@ if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -257,18 +243,12 @@ if (temp == NULL) { goto error; } - res = sym_new_const(ctx, temp); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -526,10 +506,7 @@ /* This has to be done manually */ (void)seq; for (int i = 0; i < oparg; i++) { - values[i] = sym_new_unknown(ctx); - if (values[i] == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); } stack_pointer += -1 + oparg; break; @@ -577,10 +554,7 @@ (void)seq; int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1; for (int i = 0; i < totalargs; i++) { - values[i] = sym_new_unknown(ctx); - if (values[i] == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); } stack_pointer += (oparg >> 8) + (oparg & 0xFF); break; @@ -1165,10 +1139,7 @@ _Py_UOpsSymType *iter; _Py_UOpsSymType *next; iter = stack_pointer[-1]; - next = sym_new_known_type(ctx, &PyLong_Type); - if (next == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(next, sym_new_known_type(ctx, &PyLong_Type)); (void)iter; stack_pointer[0] = next; stack_pointer += 1; @@ -1371,10 +1342,8 @@ localsplus_start = args; n_locals_already_filled = argcount; } - new_frame = ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0); - if (new_frame == NULL){ - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(new_frame, + ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); stack_pointer[-2 - oparg] = (_Py_UOpsSymType *)new_frame; stack_pointer += -1 - oparg; break; @@ -1664,10 +1633,7 @@ case _LOAD_CONST_INLINE: { _Py_UOpsSymType *value; PyObject *ptr = (PyObject *)this_instr->operand; - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); stack_pointer[0] = value; stack_pointer += 1; break; @@ -1676,10 +1642,7 @@ case _LOAD_CONST_INLINE_BORROW: { _Py_UOpsSymType *value; PyObject *ptr = (PyObject *)this_instr->operand; - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); stack_pointer[0] = value; stack_pointer += 1; break; @@ -1689,14 +1652,8 @@ _Py_UOpsSymType *value; _Py_UOpsSymType *null; PyObject *ptr = (PyObject *)this_instr->operand; - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } - null = sym_new_null(ctx); - if (null == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; @@ -1707,14 +1664,8 @@ _Py_UOpsSymType *value; _Py_UOpsSymType *null; PyObject *ptr = (PyObject *)this_instr->operand; - value = sym_new_const(ctx, ptr); - if (value == NULL) { - goto out_of_space; - } - null = sym_new_null(ctx); - if (null == NULL) { - goto out_of_space; - } + OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; From c48c8226b2de52e89891df362be4f2e950ab68e0 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Fri, 16 Feb 2024 01:50:27 +0800 Subject: [PATCH 3/4] apply suggestions --- Python/optimizer_analysis.c | 11 +++--- .../tier2_redundancy_eliminator_bytecodes.c | 34 +++++++++---------- Python/tier2_redundancy_eliminator_cases.c.h | 34 +++++++++---------- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index b6289426063135..a1b71ee7e6310a 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -588,16 +588,15 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, INST->oparg = ARG; \ INST->operand = OPERAND; -#define OUT_OF_SPACE_IF_NULL(RES, EXPR) \ - RES = (EXPR); \ - if (RES == NULL) { \ - goto out_of_space; \ +#define OUT_OF_SPACE_IF_NULL(EXPR) \ + if ((EXPR) == NULL) { \ + goto out_of_space; \ } #define _LOAD_ATTR_NOT_NULL \ do { \ - OUT_OF_SPACE_IF_NULL(attr, sym_new_known_notnull(ctx)); \ - OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); \ + OUT_OF_SPACE_IF_NULL(attr = sym_new_known_notnull(ctx)); \ + OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); \ } while (0); diff --git a/Python/tier2_redundancy_eliminator_bytecodes.c b/Python/tier2_redundancy_eliminator_bytecodes.c index a0ba9331ad2a37..6aae590a8e51e4 100644 --- a/Python/tier2_redundancy_eliminator_bytecodes.c +++ b/Python/tier2_redundancy_eliminator_bytecodes.c @@ -44,7 +44,7 @@ dummy_func(void) { op(_LOAD_FAST_AND_CLEAR, (-- value)) { value = GETLOCAL(oparg); _Py_UOpsSymType *temp; - OUT_OF_SPACE_IF_NULL(temp, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(temp = sym_new_null(ctx)); GETLOCAL(oparg) = temp; } @@ -87,12 +87,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } } @@ -105,12 +105,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } } @@ -123,12 +123,12 @@ dummy_func(void) { if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } } @@ -139,21 +139,21 @@ dummy_func(void) { } op(_LOAD_CONST_INLINE, (ptr/4 -- value)) { - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); } op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) { - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); } op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) { - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); - OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); } op(_LOAD_CONST_INLINE_BORROW_WITH_NULL, (ptr/4 -- value, null)) { - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); - OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); } @@ -235,7 +235,7 @@ dummy_func(void) { localsplus_start = args; n_locals_already_filled = argcount; } - OUT_OF_SPACE_IF_NULL(new_frame, + OUT_OF_SPACE_IF_NULL(new_frame = ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); } @@ -259,7 +259,7 @@ dummy_func(void) { /* This has to be done manually */ (void)seq; for (int i = 0; i < oparg; i++) { - OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); + OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx)); } } @@ -268,12 +268,12 @@ dummy_func(void) { (void)seq; int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1; for (int i = 0; i < totalargs; i++) { - OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); + OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx)); } } op(_ITER_NEXT_RANGE, (iter -- iter, next)) { - OUT_OF_SPACE_IF_NULL(next, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(next = sym_new_known_type(ctx, &PyLong_Type)); (void)iter; } diff --git a/Python/tier2_redundancy_eliminator_cases.c.h b/Python/tier2_redundancy_eliminator_cases.c.h index 936f11c307b46a..d1301ff29ac593 100644 --- a/Python/tier2_redundancy_eliminator_cases.c.h +++ b/Python/tier2_redundancy_eliminator_cases.c.h @@ -37,7 +37,7 @@ _Py_UOpsSymType *value; value = GETLOCAL(oparg); _Py_UOpsSymType *temp; - OUT_OF_SPACE_IF_NULL(temp, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(temp = sym_new_null(ctx)); GETLOCAL(oparg) = temp; stack_pointer[0] = value; stack_pointer += 1; @@ -191,12 +191,12 @@ if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -217,12 +217,12 @@ if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -243,12 +243,12 @@ if (temp == NULL) { goto error; } - OUT_OF_SPACE_IF_NULL(res, sym_new_const(ctx, temp)); + OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp)); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyLong_Type)); } stack_pointer[-2] = res; stack_pointer += -1; @@ -506,7 +506,7 @@ /* This has to be done manually */ (void)seq; for (int i = 0; i < oparg; i++) { - OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); + OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx)); } stack_pointer += -1 + oparg; break; @@ -554,7 +554,7 @@ (void)seq; int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1; for (int i = 0; i < totalargs; i++) { - OUT_OF_SPACE_IF_NULL(values[i], sym_new_unknown(ctx)); + OUT_OF_SPACE_IF_NULL(values[i] = sym_new_unknown(ctx)); } stack_pointer += (oparg >> 8) + (oparg & 0xFF); break; @@ -1139,7 +1139,7 @@ _Py_UOpsSymType *iter; _Py_UOpsSymType *next; iter = stack_pointer[-1]; - OUT_OF_SPACE_IF_NULL(next, sym_new_known_type(ctx, &PyLong_Type)); + OUT_OF_SPACE_IF_NULL(next = sym_new_known_type(ctx, &PyLong_Type)); (void)iter; stack_pointer[0] = next; stack_pointer += 1; @@ -1342,7 +1342,7 @@ localsplus_start = args; n_locals_already_filled = argcount; } - OUT_OF_SPACE_IF_NULL(new_frame, + OUT_OF_SPACE_IF_NULL(new_frame = ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); stack_pointer[-2 - oparg] = (_Py_UOpsSymType *)new_frame; stack_pointer += -1 - oparg; @@ -1633,7 +1633,7 @@ case _LOAD_CONST_INLINE: { _Py_UOpsSymType *value; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); stack_pointer[0] = value; stack_pointer += 1; break; @@ -1642,7 +1642,7 @@ case _LOAD_CONST_INLINE_BORROW: { _Py_UOpsSymType *value; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); stack_pointer[0] = value; stack_pointer += 1; break; @@ -1652,8 +1652,8 @@ _Py_UOpsSymType *value; _Py_UOpsSymType *null; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); - OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; @@ -1664,8 +1664,8 @@ _Py_UOpsSymType *value; _Py_UOpsSymType *null; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value, sym_new_const(ctx, ptr)); - OUT_OF_SPACE_IF_NULL(null, sym_new_null(ctx)); + OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; From f9972d60d23c73c1dd95e908ff445eb34674f2ae Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:53:25 +0800 Subject: [PATCH 4/4] apply Mark's suggestions --- Python/optimizer_analysis.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index a1b71ee7e6310a..b104d2fa7baec9 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -588,10 +588,12 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, INST->oparg = ARG; \ INST->operand = OPERAND; -#define OUT_OF_SPACE_IF_NULL(EXPR) \ - if ((EXPR) == NULL) { \ - goto out_of_space; \ - } +#define OUT_OF_SPACE_IF_NULL(EXPR) \ + do { \ + if ((EXPR) == NULL) { \ + goto out_of_space; \ + } \ + } while (0); #define _LOAD_ATTR_NOT_NULL \ do { \