From 3278bd2b67c37227c2d5543d69f905c8ca68a1e2 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 May 2024 15:46:31 +0100 Subject: [PATCH 1/5] Reduce boilerplate needed in optimizer_bytecodes.c --- Include/internal/pycore_optimizer.h | 14 +- Lib/test/test_generated_cases.py | 2 - Python/optimizer_analysis.c | 73 +++-- Python/optimizer_bytecodes.c | 222 ++++++-------- Python/optimizer_cases.c.h | 297 +++++-------------- Python/optimizer_symbols.c | 107 +++---- Tools/cases_generator/optimizer_generator.py | 3 - 7 files changed, 259 insertions(+), 459 deletions(-) diff --git a/Include/internal/pycore_optimizer.h b/Include/internal/pycore_optimizer.h index c0a76e85350541..188cf38e1a29a6 100644 --- a/Include/internal/pycore_optimizer.h +++ b/Include/internal/pycore_optimizer.h @@ -64,7 +64,9 @@ typedef struct ty_arena { } ty_arena; struct _Py_UOpsContext { - PyObject_HEAD + char done; + char out_of_space; + bool contradiction; // The current "executing" frame. _Py_UOpsAbstractFrame *frame; _Py_UOpsAbstractFrame frames[MAX_ABSTRACT_FRAME_DEPTH]; @@ -92,16 +94,16 @@ extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *con extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx); extern bool _Py_uop_sym_has_type(_Py_UopsSymbol *sym); extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ); -extern bool _Py_uop_sym_set_null(_Py_UopsSymbol *sym); -extern bool _Py_uop_sym_set_non_null(_Py_UopsSymbol *sym); -extern bool _Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *typ); -extern bool _Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val); +extern void _Py_uop_sym_set_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym); +extern void _Py_uop_sym_set_non_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym); +extern void _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *typ); +extern void _Py_uop_sym_set_const(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyObject *const_val); extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym); extern int _Py_uop_sym_truthiness(_Py_UopsSymbol *sym); extern PyTypeObject *_Py_uop_sym_get_type(_Py_UopsSymbol *sym); -extern int _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx); +extern void _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx); extern void _Py_uop_abstractcontext_fini(_Py_UOpsContext *ctx); extern _Py_UOpsAbstractFrame *_Py_uop_frame_new( diff --git a/Lib/test/test_generated_cases.py b/Lib/test/test_generated_cases.py index 7b9dd36f85454f..fb85222fdcce74 100644 --- a/Lib/test/test_generated_cases.py +++ b/Lib/test/test_generated_cases.py @@ -909,7 +909,6 @@ def test_overridden_abstract_args(self): case OP2: { _Py_UopsSymbol *out; out = sym_new_not_null(ctx); - if (out == NULL) goto out_of_space; stack_pointer[-1] = out; break; } @@ -934,7 +933,6 @@ def test_no_overridden_case(self): case OP: { _Py_UopsSymbol *out; out = sym_new_not_null(ctx); - if (out == NULL) goto out_of_space; stack_pointer[-1] = out; break; } diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index 842b2e489239af..b9b5714f1c441c 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -297,12 +297,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, INST->oparg = ARG; \ INST->operand = OPERAND; -#define OUT_OF_SPACE_IF_NULL(EXPR) \ - do { \ - if ((EXPR) == NULL) { \ - goto out_of_space; \ - } \ - } while (0); +#define OUT_OF_SPACE_IF_NULL(EXPR) (EXPR) #define _LOAD_ATTR_NOT_NULL \ do { \ @@ -324,10 +319,10 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, #define sym_has_type _Py_uop_sym_has_type #define sym_get_type _Py_uop_sym_get_type #define sym_matches_type _Py_uop_sym_matches_type -#define sym_set_null _Py_uop_sym_set_null -#define sym_set_non_null _Py_uop_sym_set_non_null -#define sym_set_type _Py_uop_sym_set_type -#define sym_set_const _Py_uop_sym_set_const +#define sym_set_null(SYM) _Py_uop_sym_set_null(ctx, SYM) +#define sym_set_non_null(SYM) _Py_uop_sym_set_non_null(ctx, SYM) +#define sym_set_type(SYM, TYPE) _Py_uop_sym_set_type(ctx, SYM, TYPE) +#define sym_set_const(SYM, CNST) _Py_uop_sym_set_const(ctx, SYM, CNST) #define sym_is_bottom _Py_uop_sym_is_bottom #define sym_truthiness _Py_uop_sym_truthiness #define frame_new _Py_uop_frame_new @@ -408,18 +403,20 @@ optimize_uops( _PyUOpInstruction *first_valid_check_stack = NULL; _PyUOpInstruction *corresponding_check_stack = NULL; - if (_Py_uop_abstractcontext_init(ctx) < 0) { - goto out_of_space; - } + _Py_uop_abstractcontext_init(ctx); _Py_UOpsAbstractFrame *frame = _Py_uop_frame_new(ctx, co, ctx->n_consumed, 0, curr_stacklen); if (frame == NULL) { return -1; } ctx->curr_frame_depth++; ctx->frame = frame; + ctx->done = false; + ctx->out_of_space = false; + ctx->contradiction = false; _PyUOpInstruction *this_instr = NULL; - for (int i = 0; i < trace_len; i++) { + for(int i = 0; !ctx->done; i++) { + assert(i < trace_len); this_instr = &trace[i]; int oparg = this_instr->oparg; @@ -447,32 +444,22 @@ optimize_uops( ctx->frame->stack_pointer = stack_pointer; assert(STACK_LEVEL() >= 0); } - Py_UNREACHABLE(); - -out_of_space: - DPRINTF(3, "\n"); - DPRINTF(1, "Out of space in abstract interpreter\n"); - goto done; -error: - DPRINTF(3, "\n"); - DPRINTF(1, "Encountered error in abstract interpreter\n"); - if (opcode <= MAX_UOP_ID) { - OPT_ERROR_IN_OPCODE(opcode); + if (ctx->out_of_space) { + DPRINTF(3, "\n"); + DPRINTF(1, "Out of space in abstract interpreter\n"); + } + if (ctx->contradiction) { + // Attempted to push a "bottom" (contradition) symbol onto the stack. + // This means that the abstract interpreter has hit unreachable code. + // We *could* generate an _EXIT_TRACE or _FATAL_ERROR here, but hitting + // bottom indicates type instability, so we are probably better off + // retrying later. + DPRINTF(3, "\n"); + DPRINTF(1, "Hit bottom in abstract interpreter\n"); + _Py_uop_abstractcontext_fini(ctx); + return 0; } - _Py_uop_abstractcontext_fini(ctx); - return -1; -hit_bottom: - // Attempted to push a "bottom" (contradition) symbol onto the stack. - // This means that the abstract interpreter has hit unreachable code. - // We *could* generate an _EXIT_TRACE or _FATAL_ERROR here, but hitting - // bottom indicates type instability, so we are probably better off - // retrying later. - DPRINTF(3, "\n"); - DPRINTF(1, "Hit bottom in abstract interpreter\n"); - _Py_uop_abstractcontext_fini(ctx); - return 0; -done: /* Either reached the end or cannot optimize further, but there * would be no benefit in retrying later */ _Py_uop_abstractcontext_fini(ctx); @@ -485,6 +472,16 @@ optimize_uops( first_valid_check_stack->operand = max_space; } return trace_len; + +error: + DPRINTF(3, "\n"); + DPRINTF(1, "Encountered error in abstract interpreter\n"); + if (opcode <= MAX_UOP_ID) { + OPT_ERROR_IN_OPCODE(opcode); + } + _Py_uop_abstractcontext_fini(ctx); + return -1; + } diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 928bc03382b8fb..7bbd3e9e15d93a 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -23,10 +23,10 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame; #define sym_matches_type _Py_uop_sym_matches_type #define sym_get_type _Py_uop_sym_get_type #define sym_has_type _Py_uop_sym_has_type -#define sym_set_null _Py_uop_sym_set_null -#define sym_set_non_null _Py_uop_sym_set_non_null -#define sym_set_type _Py_uop_sym_set_type -#define sym_set_const _Py_uop_sym_set_const +#define sym_set_null(SYM) _Py_uop_sym_set_null(ctx, SYM) +#define sym_set_non_null(SYM) _Py_uop_sym_set_non_null(ctx, SYM) +#define sym_set_type(SYM, TYPE) _Py_uop_sym_set_type(ctx, SYM, TYPE) +#define sym_set_const(SYM, CNST) _Py_uop_sym_set_const(ctx, SYM, CNST) #define sym_is_bottom _Py_uop_sym_is_bottom #define frame_new _Py_uop_frame_new #define frame_pop _Py_uop_frame_pop @@ -72,9 +72,7 @@ dummy_func(void) { op(_LOAD_FAST_CHECK, (-- value)) { value = GETLOCAL(oparg); // We guarantee this will error - just bail and don't optimize it. - if (sym_is_null(value)) { - goto out_of_space; - } + ctx->done = true; } op(_LOAD_FAST, (-- value)) { @@ -83,8 +81,7 @@ dummy_func(void) { op(_LOAD_FAST_AND_CLEAR, (-- value)) { value = GETLOCAL(oparg); - _Py_UopsSymbol *temp; - OUT_OF_SPACE_IF_NULL(temp = sym_new_null(ctx)); + _Py_UopsSymbol *temp = sym_new_null(ctx); GETLOCAL(oparg) = temp; } @@ -94,9 +91,6 @@ dummy_func(void) { op(_PUSH_NULL, (-- res)) { res = sym_new_null(ctx); - if (res == NULL) { - goto out_of_space; - }; } op(_GUARD_BOTH_INT, (left, right -- left, right)) { @@ -113,12 +107,8 @@ dummy_func(void) { REPLACE_OP(this_instr, _GUARD_NOS_INT, 0, 0); } } - if (!sym_set_type(left, &PyLong_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyLong_Type)) { - goto hit_bottom; - } + sym_set_type(left, &PyLong_Type); + sym_set_type(right, &PyLong_Type); } op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) { @@ -135,12 +125,9 @@ dummy_func(void) { REPLACE_OP(this_instr, _GUARD_NOS_FLOAT, 0, 0); } } - if (!sym_set_type(left, &PyFloat_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyFloat_Type)) { - goto hit_bottom; - } + + sym_set_type(left, &PyFloat_Type); + sym_set_type(right, &PyFloat_Type); } op(_GUARD_BOTH_UNICODE, (left, right -- left, right)) { @@ -148,12 +135,8 @@ dummy_func(void) { sym_matches_type(right, &PyUnicode_Type)) { REPLACE_OP(this_instr, _NOP, 0 ,0); } - if (!sym_set_type(left, &PyUnicode_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyUnicode_Type)) { - goto hit_bottom; - } + sym_set_type(left, &PyUnicode_Type); + sym_set_type(left, &PyUnicode_Type); } op(_BINARY_OP, (left, right -- res)) { @@ -165,14 +148,14 @@ dummy_func(void) { if (oparg != NB_TRUE_DIVIDE && oparg != NB_INPLACE_TRUE_DIVIDE && ltype == &PyLong_Type && rtype == &PyLong_Type) { /* If both inputs are ints and the op is not division the result is an int */ - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } else { /* For any other op combining ints/floats the result is a float */ - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } } - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); } op(_BINARY_OP_ADD_INT, (left, right -- res)) { @@ -188,12 +171,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } } @@ -210,12 +192,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } } @@ -232,12 +213,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } } @@ -255,12 +235,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } } @@ -278,12 +257,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } } @@ -301,12 +279,11 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } } @@ -319,80 +296,50 @@ dummy_func(void) { } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyUnicode_Type)); + res = sym_new_type(ctx, &PyUnicode_Type); } } op(_TO_BOOL, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { + if (!optimize_to_bool(this_instr, ctx, value, &res)) { res = sym_new_type(ctx, &PyBool_Type); - OUT_OF_SPACE_IF_NULL(res); } } op(_TO_BOOL_BOOL, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyBool_Type)) { - goto hit_bottom; - } + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyBool_Type); res = value; } } op(_TO_BOOL_INT, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyLong_Type)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyLong_Type); + res = sym_new_type(ctx, &PyBool_Type); } } op(_TO_BOOL_LIST, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyList_Type)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyList_Type); + res = sym_new_type(ctx, &PyBool_Type); } } op(_TO_BOOL_NONE, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if (!sym_set_const(value, Py_None)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_const(value, Py_None); + res = sym_new_const(ctx, Py_False); } } op(_TO_BOOL_STR, (value -- res)) { - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); - if(!sym_set_type(value, &PyUnicode_Type)) { - goto hit_bottom; - } + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + res = sym_new_type(ctx, &PyBool_Type); + sym_set_type(value, &PyUnicode_Type); } } @@ -400,66 +347,66 @@ dummy_func(void) { (void)left; (void)right; if (oparg & 16) { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } else { - OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_not_null(ctx)); + res = _Py_uop_sym_new_not_null(ctx); } } op(_COMPARE_OP_INT, (left, right -- res)) { (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } op(_COMPARE_OP_FLOAT, (left, right -- res)) { (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } op(_COMPARE_OP_STR, (left, right -- res)) { (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } op(_IS_OP, (left, right -- res)) { (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } op(_CONTAINS_OP, (left, right -- res)) { (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } op(_LOAD_CONST, (-- value)) { PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg); int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE; REPLACE_OP(this_instr, opcode, 0, (uintptr_t)val); - OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, val)); + value = sym_new_const(ctx, val); } op(_LOAD_CONST_INLINE, (ptr/4 -- value)) { - OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + 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)); + 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)); + value = sym_new_const(ctx, ptr); + 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)); + value = sym_new_const(ctx, ptr); + null = sym_new_null(ctx); } op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) { @@ -496,15 +443,15 @@ dummy_func(void) { op(_LOAD_ATTR, (owner -- attr, self_or_null if (oparg & 1))) { (void)owner; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); if (oparg & 1) { - OUT_OF_SPACE_IF_NULL(self_or_null = sym_new_unknown(ctx)); + self_or_null = sym_new_unknown(ctx); } } op(_LOAD_ATTR_MODULE, (index/1, owner -- attr, null if (oparg & 1))) { (void)index; - OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); + null = sym_new_null(ctx); attr = NULL; if (this_instr[-1].opcode == _NOP) { // Preceding _CHECK_ATTR_MODULE was removed: mod is const and dict is watched. @@ -520,7 +467,7 @@ dummy_func(void) { } if (attr == NULL) { /* No conversion made. We don't know what `attr` is. */ - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); } } @@ -544,43 +491,37 @@ dummy_func(void) { op(_LOAD_ATTR_METHOD_WITH_VALUES, (descr/4, owner -- attr, self if (1))) { (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; } op(_LOAD_ATTR_METHOD_NO_DICT, (descr/4, owner -- attr, self if (1))) { (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; } op(_LOAD_ATTR_METHOD_LAZY_DICT, (descr/4, owner -- attr, self if (1))) { (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; } op(_INIT_CALL_BOUND_METHOD_EXACT_ARGS, (callable, unused, unused[oparg] -- func, self, unused[oparg])) { (void)callable; - OUT_OF_SPACE_IF_NULL(func = sym_new_not_null(ctx)); - OUT_OF_SPACE_IF_NULL(self = sym_new_not_null(ctx)); + func = sym_new_not_null(ctx); + self = sym_new_not_null(ctx); } op(_CHECK_FUNCTION_EXACT_ARGS, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) { - if (!sym_set_type(callable, &PyFunction_Type)) { - goto hit_bottom; - } + sym_set_type(callable, &PyFunction_Type); (void)self_or_null; (void)func_version; } op(_CHECK_CALL_BOUND_METHOD_EXACT_ARGS, (callable, null, unused[oparg] -- callable, null, unused[oparg])) { - if (!sym_set_null(null)) { - goto hit_bottom; - } - if (!sym_set_type(callable, &PyMethod_Type)) { - goto hit_bottom; - } + sym_set_null(null); + sym_set_type(callable, &PyMethod_Type); } op(_INIT_CALL_PY_EXACT_ARGS, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) { @@ -602,7 +543,8 @@ dummy_func(void) { if (func == NULL) { DPRINTF(3, "\n"); DPRINTF(1, "Missing function\n"); - goto done; + ctx->done = true; + break; } co = (PyCodeObject *)func->func_code; DPRINTF(3, "code=%p ", co); @@ -625,8 +567,7 @@ dummy_func(void) { localsplus_start = args; n_locals_already_filled = argcount; } - OUT_OF_SPACE_IF_NULL(new_frame = - frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); + new_frame = frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0); } op(_PY_FRAME_GENERAL, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) { @@ -635,7 +576,8 @@ dummy_func(void) { (void)self_or_null; (void)args; first_valid_check_stack = NULL; - goto done; + new_frame = NULL; + ctx->done = true; } op(_POP_FRAME, (retval -- res)) { @@ -656,7 +598,7 @@ dummy_func(void) { co = get_code(this_instr); if (co == NULL) { // might be impossible, but bailing is still safe - goto done; + ctx->done = true; } } @@ -665,7 +607,7 @@ dummy_func(void) { ctx->frame->stack_pointer = stack_pointer; frame_pop(ctx); stack_pointer = ctx->frame->stack_pointer; - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); /* Stack space handling */ assert(corresponding_check_stack == NULL); @@ -678,17 +620,17 @@ dummy_func(void) { co = get_code(this_instr); if (co == NULL) { // might be impossible, but bailing is still safe - goto done; + ctx->done = true; } } op(_YIELD_VALUE, (unused -- res)) { - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); } op(_FOR_ITER_GEN_FRAME, ( -- )) { /* We are about to hit the end of the trace */ - goto done; + ctx->done = true; } op(_CHECK_STACK_SPACE, ( --)) { @@ -712,7 +654,8 @@ dummy_func(void) { co = get_code(this_instr); if (co == NULL) { // should be about to _EXIT_TRACE anyway - goto done; + ctx->done = true; + break; } /* Stack space handling */ @@ -721,7 +664,8 @@ dummy_func(void) { curr_space += framesize; if (curr_space < 0 || curr_space > INT32_MAX) { // won't fit in signed 32-bit int - goto done; + ctx->done = true; + break; } max_space = curr_space > max_space ? curr_space : max_space; if (first_valid_check_stack == NULL) { @@ -738,7 +682,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)); + values[i] = sym_new_unknown(ctx); } } @@ -747,12 +691,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)); + values[i] = sym_new_unknown(ctx); } } op(_ITER_NEXT_RANGE, (iter -- iter, next)) { - OUT_OF_SPACE_IF_NULL(next = sym_new_type(ctx, &PyLong_Type)); + next = sym_new_type(ctx, &PyLong_Type); (void)iter; } @@ -805,11 +749,11 @@ dummy_func(void) { } op(_JUMP_TO_TOP, (--)) { - goto done; + ctx->done = true; } op(_EXIT_TRACE, (--)) { - goto done; + ctx->done = true; } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 2a4efd73d794df..5feaf8b46765eb 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -17,9 +17,7 @@ _Py_UopsSymbol *value; value = GETLOCAL(oparg); // We guarantee this will error - just bail and don't optimize it. - if (sym_is_null(value)) { - goto out_of_space; - } + ctx->done = true; stack_pointer[0] = value; stack_pointer += 1; break; @@ -36,8 +34,7 @@ case _LOAD_FAST_AND_CLEAR: { _Py_UopsSymbol *value; value = GETLOCAL(oparg); - _Py_UopsSymbol *temp; - OUT_OF_SPACE_IF_NULL(temp = sym_new_null(ctx)); + _Py_UopsSymbol *temp = sym_new_null(ctx); GETLOCAL(oparg) = temp; stack_pointer[0] = value; stack_pointer += 1; @@ -49,7 +46,7 @@ PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg); int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE; REPLACE_OP(this_instr, opcode, 0, (uintptr_t)val); - OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, val)); + value = sym_new_const(ctx, val); stack_pointer[0] = value; stack_pointer += 1; break; @@ -71,9 +68,6 @@ case _PUSH_NULL: { _Py_UopsSymbol *res; res = sym_new_null(ctx); - if (res == NULL) { - goto out_of_space; - }; stack_pointer[0] = res; stack_pointer += 1; break; @@ -82,7 +76,6 @@ case _END_SEND: { _Py_UopsSymbol *value; value = sym_new_not_null(ctx); - if (value == NULL) goto out_of_space; stack_pointer[-2] = value; stack_pointer += -1; break; @@ -91,7 +84,6 @@ case _UNARY_NEGATIVE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -99,7 +91,6 @@ case _UNARY_NOT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -108,12 +99,8 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { + if (!optimize_to_bool(this_instr, ctx, value, &res)) { res = sym_new_type(ctx, &PyBool_Type); - OUT_OF_SPACE_IF_NULL(res); } stack_pointer[-1] = res; break; @@ -123,13 +110,8 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyBool_Type)) { - goto hit_bottom; - } + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyBool_Type); res = value; } stack_pointer[-1] = res; @@ -140,14 +122,9 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyLong_Type)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyLong_Type); + res = sym_new_type(ctx, &PyBool_Type); } stack_pointer[-1] = res; break; @@ -157,14 +134,9 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if(!sym_set_type(value, &PyList_Type)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_type(value, &PyList_Type); + res = sym_new_type(ctx, &PyBool_Type); } stack_pointer[-1] = res; break; @@ -174,14 +146,9 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - if (!sym_set_const(value, Py_None)) { - goto hit_bottom; - } - OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False)); + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + sym_set_const(value, Py_None); + res = sym_new_const(ctx, Py_False); } stack_pointer[-1] = res; break; @@ -191,14 +158,9 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *res; value = stack_pointer[-1]; - if (optimize_to_bool(this_instr, ctx, value, &res)) { - OUT_OF_SPACE_IF_NULL(res); - } - else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); - if(!sym_set_type(value, &PyUnicode_Type)) { - goto hit_bottom; - } + if (!optimize_to_bool(this_instr, ctx, value, &res)) { + res = sym_new_type(ctx, &PyBool_Type); + sym_set_type(value, &PyUnicode_Type); } stack_pointer[-1] = res; break; @@ -207,7 +169,6 @@ case _REPLACE_WITH_TRUE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -215,7 +176,6 @@ case _UNARY_INVERT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -238,12 +198,8 @@ REPLACE_OP(this_instr, _GUARD_NOS_INT, 0, 0); } } - if (!sym_set_type(left, &PyLong_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyLong_Type)) { - goto hit_bottom; - } + sym_set_type(left, &PyLong_Type); + sym_set_type(right, &PyLong_Type); break; } @@ -273,12 +229,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -303,12 +258,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -333,12 +287,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and add tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -363,12 +316,8 @@ REPLACE_OP(this_instr, _GUARD_NOS_FLOAT, 0, 0); } } - if (!sym_set_type(left, &PyFloat_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyFloat_Type)) { - goto hit_bottom; - } + sym_set_type(left, &PyFloat_Type); + sym_set_type(right, &PyFloat_Type); break; } @@ -399,12 +348,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -430,12 +378,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -461,12 +408,11 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); // TODO gh-115506: // replace opcode with constant propagated one and update tests! } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -482,12 +428,8 @@ sym_matches_type(right, &PyUnicode_Type)) { REPLACE_OP(this_instr, _NOP, 0 ,0); } - if (!sym_set_type(left, &PyUnicode_Type)) { - goto hit_bottom; - } - if (!sym_set_type(right, &PyUnicode_Type)) { - goto hit_bottom; - } + sym_set_type(left, &PyUnicode_Type); + sym_set_type(left, &PyUnicode_Type); break; } @@ -505,10 +447,9 @@ } res = sym_new_const(ctx, temp); Py_DECREF(temp); - OUT_OF_SPACE_IF_NULL(res); } else { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyUnicode_Type)); + res = sym_new_type(ctx, &PyUnicode_Type); } stack_pointer[-2] = res; stack_pointer += -1; @@ -518,7 +459,6 @@ case _BINARY_SUBSCR: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -527,7 +467,6 @@ case _BINARY_SLICE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-3] = res; stack_pointer += -2; break; @@ -541,7 +480,6 @@ case _BINARY_SUBSCR_LIST_INT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -550,7 +488,6 @@ case _BINARY_SUBSCR_STR_INT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -559,7 +496,6 @@ case _BINARY_SUBSCR_TUPLE_INT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -568,7 +504,6 @@ case _BINARY_SUBSCR_DICT: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -609,7 +544,6 @@ case _CALL_INTRINSIC_1: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -617,7 +551,6 @@ case _CALL_INTRINSIC_2: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -642,7 +575,7 @@ co = get_code(this_instr); if (co == NULL) { // might be impossible, but bailing is still safe - goto done; + ctx->done = true; } stack_pointer[0] = res; stack_pointer += 1; @@ -656,7 +589,6 @@ case _GET_AITER: { _Py_UopsSymbol *iter; iter = sym_new_not_null(ctx); - if (iter == NULL) goto out_of_space; stack_pointer[-1] = iter; break; } @@ -664,7 +596,6 @@ case _GET_ANEXT: { _Py_UopsSymbol *awaitable; awaitable = sym_new_not_null(ctx); - if (awaitable == NULL) goto out_of_space; stack_pointer[0] = awaitable; stack_pointer += 1; break; @@ -673,7 +604,6 @@ case _GET_AWAITABLE: { _Py_UopsSymbol *iter; iter = sym_new_not_null(ctx); - if (iter == NULL) goto out_of_space; stack_pointer[-1] = iter; break; } @@ -686,7 +616,7 @@ case _YIELD_VALUE: { _Py_UopsSymbol *res; - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); stack_pointer[-1] = res; break; } @@ -699,7 +629,6 @@ case _LOAD_ASSERTION_ERROR: { _Py_UopsSymbol *value; value = sym_new_not_null(ctx); - if (value == NULL) goto out_of_space; stack_pointer[0] = value; stack_pointer += 1; break; @@ -708,7 +637,6 @@ case _LOAD_BUILD_CLASS: { _Py_UopsSymbol *bc; bc = sym_new_not_null(ctx); - if (bc == NULL) goto out_of_space; stack_pointer[0] = bc; stack_pointer += 1; break; @@ -731,7 +659,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)); + values[i] = sym_new_unknown(ctx); } stack_pointer += -1 + oparg; break; @@ -741,9 +669,7 @@ _Py_UopsSymbol *val1; _Py_UopsSymbol *val0; val1 = sym_new_not_null(ctx); - if (val1 == NULL) goto out_of_space; val0 = sym_new_not_null(ctx); - if (val0 == NULL) goto out_of_space; stack_pointer[-1] = val1; stack_pointer[0] = val0; stack_pointer += 1; @@ -755,7 +681,6 @@ values = &stack_pointer[-1]; for (int _i = oparg; --_i >= 0;) { values[_i] = sym_new_not_null(ctx); - if (values[_i] == NULL) goto out_of_space; } stack_pointer += -1 + oparg; break; @@ -766,7 +691,6 @@ values = &stack_pointer[-1]; for (int _i = oparg; --_i >= 0;) { values[_i] = sym_new_not_null(ctx); - if (values[_i] == NULL) goto out_of_space; } stack_pointer += -1 + oparg; break; @@ -781,7 +705,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)); + values[i] = sym_new_unknown(ctx); } stack_pointer += (oparg >> 8) + (oparg & 0xFF); break; @@ -809,7 +733,6 @@ case _LOAD_LOCALS: { _Py_UopsSymbol *locals; locals = sym_new_not_null(ctx); - if (locals == NULL) goto out_of_space; stack_pointer[0] = locals; stack_pointer += 1; break; @@ -818,7 +741,6 @@ case _LOAD_FROM_DICT_OR_GLOBALS: { _Py_UopsSymbol *v; v = sym_new_not_null(ctx); - if (v == NULL) goto out_of_space; stack_pointer[-1] = v; break; } @@ -829,9 +751,7 @@ _Py_UopsSymbol *res; _Py_UopsSymbol *null = NULL; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; null = sym_new_null(ctx); - if (null == NULL) goto out_of_space; stack_pointer[0] = res; if (oparg & 1) stack_pointer[1] = null; stack_pointer += 1 + (oparg & 1); @@ -850,9 +770,7 @@ _Py_UopsSymbol *res; _Py_UopsSymbol *null = NULL; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; null = sym_new_null(ctx); - if (null == NULL) goto out_of_space; stack_pointer[0] = res; if (oparg & 1) stack_pointer[1] = null; stack_pointer += 1 + (oparg & 1); @@ -863,9 +781,7 @@ _Py_UopsSymbol *res; _Py_UopsSymbol *null = NULL; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; null = sym_new_null(ctx); - if (null == NULL) goto out_of_space; stack_pointer[0] = res; if (oparg & 1) stack_pointer[1] = null; stack_pointer += 1 + (oparg & 1); @@ -887,7 +803,6 @@ case _LOAD_FROM_DICT_OR_DEREF: { _Py_UopsSymbol *value; value = sym_new_not_null(ctx); - if (value == NULL) goto out_of_space; stack_pointer[-1] = value; break; } @@ -895,7 +810,6 @@ case _LOAD_DEREF: { _Py_UopsSymbol *value; value = sym_new_not_null(ctx); - if (value == NULL) goto out_of_space; stack_pointer[0] = value; stack_pointer += 1; break; @@ -913,7 +827,6 @@ case _BUILD_STRING: { _Py_UopsSymbol *str; str = sym_new_not_null(ctx); - if (str == NULL) goto out_of_space; stack_pointer[-oparg] = str; stack_pointer += 1 - oparg; break; @@ -922,7 +835,6 @@ case _BUILD_TUPLE: { _Py_UopsSymbol *tup; tup = sym_new_not_null(ctx); - if (tup == NULL) goto out_of_space; stack_pointer[-oparg] = tup; stack_pointer += 1 - oparg; break; @@ -931,7 +843,6 @@ case _BUILD_LIST: { _Py_UopsSymbol *list; list = sym_new_not_null(ctx); - if (list == NULL) goto out_of_space; stack_pointer[-oparg] = list; stack_pointer += 1 - oparg; break; @@ -952,7 +863,6 @@ case _BUILD_MAP: { _Py_UopsSymbol *map; map = sym_new_not_null(ctx); - if (map == NULL) goto out_of_space; stack_pointer[-oparg*2] = map; stack_pointer += 1 - oparg*2; break; @@ -965,7 +875,6 @@ case _BUILD_CONST_KEY_MAP: { _Py_UopsSymbol *map; map = sym_new_not_null(ctx); - if (map == NULL) goto out_of_space; stack_pointer[-1 - oparg] = map; stack_pointer += -oparg; break; @@ -991,7 +900,6 @@ case _LOAD_SUPER_ATTR_ATTR: { _Py_UopsSymbol *attr; attr = sym_new_not_null(ctx); - if (attr == NULL) goto out_of_space; stack_pointer[-3] = attr; stack_pointer += -2; break; @@ -1001,9 +909,7 @@ _Py_UopsSymbol *attr; _Py_UopsSymbol *self_or_null; attr = sym_new_not_null(ctx); - if (attr == NULL) goto out_of_space; self_or_null = sym_new_not_null(ctx); - if (self_or_null == NULL) goto out_of_space; stack_pointer[-3] = attr; stack_pointer[-2] = self_or_null; stack_pointer += -1; @@ -1016,9 +922,9 @@ _Py_UopsSymbol *self_or_null = NULL; owner = stack_pointer[-1]; (void)owner; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); if (oparg & 1) { - OUT_OF_SPACE_IF_NULL(self_or_null = sym_new_unknown(ctx)); + self_or_null = sym_new_unknown(ctx); } stack_pointer[-1] = attr; if (oparg & 1) stack_pointer[0] = self_or_null; @@ -1077,7 +983,7 @@ owner = stack_pointer[-1]; uint16_t index = (uint16_t)this_instr->operand; (void)index; - OUT_OF_SPACE_IF_NULL(null = sym_new_null(ctx)); + null = sym_new_null(ctx); attr = NULL; if (this_instr[-1].opcode == _NOP) { // Preceding _CHECK_ATTR_MODULE was removed: mod is const and dict is watched. @@ -1093,7 +999,7 @@ } if (attr == NULL) { /* No conversion made. We don't know what `attr` is. */ - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); } stack_pointer[-1] = attr; if (oparg & 1) stack_pointer[0] = null; @@ -1183,10 +1089,10 @@ (void)left; (void)right; if (oparg & 16) { - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); } else { - OUT_OF_SPACE_IF_NULL(res = _Py_uop_sym_new_not_null(ctx)); + res = _Py_uop_sym_new_not_null(ctx); } stack_pointer[-2] = res; stack_pointer += -1; @@ -1201,7 +1107,7 @@ left = stack_pointer[-2]; (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1215,7 +1121,7 @@ left = stack_pointer[-2]; (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1229,7 +1135,7 @@ left = stack_pointer[-2]; (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1243,7 +1149,7 @@ left = stack_pointer[-2]; (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1257,7 +1163,7 @@ left = stack_pointer[-2]; (void)left; (void)right; - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type)); + res = sym_new_type(ctx, &PyBool_Type); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1266,7 +1172,6 @@ case _CONTAINS_OP_SET: { _Py_UopsSymbol *b; b = sym_new_not_null(ctx); - if (b == NULL) goto out_of_space; stack_pointer[-2] = b; stack_pointer += -1; break; @@ -1275,7 +1180,6 @@ case _CONTAINS_OP_DICT: { _Py_UopsSymbol *b; b = sym_new_not_null(ctx); - if (b == NULL) goto out_of_space; stack_pointer[-2] = b; stack_pointer += -1; break; @@ -1285,9 +1189,7 @@ _Py_UopsSymbol *rest; _Py_UopsSymbol *match; rest = sym_new_not_null(ctx); - if (rest == NULL) goto out_of_space; match = sym_new_not_null(ctx); - if (match == NULL) goto out_of_space; stack_pointer[-2] = rest; stack_pointer[-1] = match; break; @@ -1296,7 +1198,6 @@ case _CHECK_EXC_MATCH: { _Py_UopsSymbol *b; b = sym_new_not_null(ctx); - if (b == NULL) goto out_of_space; stack_pointer[-1] = b; break; } @@ -1308,7 +1209,6 @@ case _IS_NONE: { _Py_UopsSymbol *b; b = sym_new_not_null(ctx); - if (b == NULL) goto out_of_space; stack_pointer[-1] = b; break; } @@ -1316,7 +1216,6 @@ case _GET_LEN: { _Py_UopsSymbol *len_o; len_o = sym_new_not_null(ctx); - if (len_o == NULL) goto out_of_space; stack_pointer[0] = len_o; stack_pointer += 1; break; @@ -1325,7 +1224,6 @@ case _MATCH_CLASS: { _Py_UopsSymbol *attrs; attrs = sym_new_not_null(ctx); - if (attrs == NULL) goto out_of_space; stack_pointer[-3] = attrs; stack_pointer += -2; break; @@ -1334,7 +1232,6 @@ case _MATCH_MAPPING: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[0] = res; stack_pointer += 1; break; @@ -1343,7 +1240,6 @@ case _MATCH_SEQUENCE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[0] = res; stack_pointer += 1; break; @@ -1352,7 +1248,6 @@ case _MATCH_KEYS: { _Py_UopsSymbol *values_or_none; values_or_none = sym_new_not_null(ctx); - if (values_or_none == NULL) goto out_of_space; stack_pointer[0] = values_or_none; stack_pointer += 1; break; @@ -1361,7 +1256,6 @@ case _GET_ITER: { _Py_UopsSymbol *iter; iter = sym_new_not_null(ctx); - if (iter == NULL) goto out_of_space; stack_pointer[-1] = iter; break; } @@ -1369,7 +1263,6 @@ case _GET_YIELD_FROM_ITER: { _Py_UopsSymbol *iter; iter = sym_new_not_null(ctx); - if (iter == NULL) goto out_of_space; stack_pointer[-1] = iter; break; } @@ -1379,7 +1272,6 @@ case _FOR_ITER_TIER_TWO: { _Py_UopsSymbol *next; next = sym_new_not_null(ctx); - if (next == NULL) goto out_of_space; stack_pointer[0] = next; stack_pointer += 1; break; @@ -1400,7 +1292,6 @@ case _ITER_NEXT_LIST: { _Py_UopsSymbol *next; next = sym_new_not_null(ctx); - if (next == NULL) goto out_of_space; stack_pointer[0] = next; stack_pointer += 1; break; @@ -1419,7 +1310,6 @@ case _ITER_NEXT_TUPLE: { _Py_UopsSymbol *next; next = sym_new_not_null(ctx); - if (next == NULL) goto out_of_space; stack_pointer[0] = next; stack_pointer += 1; break; @@ -1439,7 +1329,7 @@ _Py_UopsSymbol *iter; _Py_UopsSymbol *next; iter = stack_pointer[-1]; - OUT_OF_SPACE_IF_NULL(next = sym_new_type(ctx, &PyLong_Type)); + next = sym_new_type(ctx, &PyLong_Type); (void)iter; stack_pointer[0] = next; stack_pointer += 1; @@ -1448,7 +1338,7 @@ case _FOR_ITER_GEN_FRAME: { /* We are about to hit the end of the trace */ - goto done; + ctx->done = true; break; } @@ -1459,7 +1349,6 @@ case _WITH_EXCEPT_START: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[0] = res; stack_pointer += 1; break; @@ -1469,9 +1358,7 @@ _Py_UopsSymbol *prev_exc; _Py_UopsSymbol *new_exc; prev_exc = sym_new_not_null(ctx); - if (prev_exc == NULL) goto out_of_space; new_exc = sym_new_not_null(ctx); - if (new_exc == NULL) goto out_of_space; stack_pointer[-1] = prev_exc; stack_pointer[0] = new_exc; stack_pointer += 1; @@ -1493,7 +1380,7 @@ owner = stack_pointer[-1]; PyObject *descr = (PyObject *)this_instr->operand; (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; stack_pointer[-1] = attr; stack_pointer[0] = self; @@ -1508,7 +1395,7 @@ owner = stack_pointer[-1]; PyObject *descr = (PyObject *)this_instr->operand; (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; stack_pointer[-1] = attr; stack_pointer[0] = self; @@ -1519,7 +1406,6 @@ case _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES: { _Py_UopsSymbol *attr; attr = sym_new_not_null(ctx); - if (attr == NULL) goto out_of_space; stack_pointer[-1] = attr; break; } @@ -1527,7 +1413,6 @@ case _LOAD_ATTR_NONDESCRIPTOR_NO_DICT: { _Py_UopsSymbol *attr; attr = sym_new_not_null(ctx); - if (attr == NULL) goto out_of_space; stack_pointer[-1] = attr; break; } @@ -1543,7 +1428,7 @@ owner = stack_pointer[-1]; PyObject *descr = (PyObject *)this_instr->operand; (void)descr; - OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx)); + attr = sym_new_not_null(ctx); self = owner; stack_pointer[-1] = attr; stack_pointer[0] = self; @@ -1572,7 +1457,8 @@ (void)self_or_null; (void)args; first_valid_check_stack = NULL; - goto done; + new_frame = NULL; + ctx->done = true; stack_pointer[-2 - oparg] = (_Py_UopsSymbol *)new_frame; stack_pointer += -1 - oparg; break; @@ -1590,9 +1476,7 @@ _Py_UopsSymbol *method; _Py_UopsSymbol *self; method = sym_new_not_null(ctx); - if (method == NULL) goto out_of_space; self = sym_new_not_null(ctx); - if (self == NULL) goto out_of_space; stack_pointer[-2 - oparg] = method; stack_pointer[-1 - oparg] = self; break; @@ -1605,7 +1489,6 @@ case _CALL_NON_PY_GENERAL: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1616,12 +1499,8 @@ _Py_UopsSymbol *callable; null = stack_pointer[-1 - oparg]; callable = stack_pointer[-2 - oparg]; - if (!sym_set_null(null)) { - goto hit_bottom; - } - if (!sym_set_type(callable, &PyMethod_Type)) { - goto hit_bottom; - } + sym_set_null(null); + sym_set_type(callable, &PyMethod_Type); break; } @@ -1631,8 +1510,8 @@ _Py_UopsSymbol *self; callable = stack_pointer[-2 - oparg]; (void)callable; - OUT_OF_SPACE_IF_NULL(func = sym_new_not_null(ctx)); - OUT_OF_SPACE_IF_NULL(self = sym_new_not_null(ctx)); + func = sym_new_not_null(ctx); + self = sym_new_not_null(ctx); stack_pointer[-2 - oparg] = func; stack_pointer[-1 - oparg] = self; break; @@ -1653,9 +1532,7 @@ self_or_null = stack_pointer[-1 - oparg]; callable = stack_pointer[-2 - oparg]; uint32_t func_version = (uint32_t)this_instr->operand; - if (!sym_set_type(callable, &PyFunction_Type)) { - goto hit_bottom; - } + sym_set_type(callable, &PyFunction_Type); (void)self_or_null; (void)func_version; break; @@ -1691,7 +1568,8 @@ if (func == NULL) { DPRINTF(3, "\n"); DPRINTF(1, "Missing function\n"); - goto done; + ctx->done = true; + break; } co = (PyCodeObject *)func->func_code; DPRINTF(3, "code=%p ", co); @@ -1712,8 +1590,7 @@ localsplus_start = args; n_locals_already_filled = argcount; } - OUT_OF_SPACE_IF_NULL(new_frame = - frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0)); + new_frame = frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0); stack_pointer[-2 - oparg] = (_Py_UopsSymbol *)new_frame; stack_pointer += -1 - oparg; break; @@ -1730,7 +1607,8 @@ co = get_code(this_instr); if (co == NULL) { // should be about to _EXIT_TRACE anyway - goto done; + ctx->done = true; + break; } /* Stack space handling */ int framesize = co->co_framesize; @@ -1738,7 +1616,8 @@ curr_space += framesize; if (curr_space < 0 || curr_space > INT32_MAX) { // won't fit in signed 32-bit int - goto done; + ctx->done = true; + break; } max_space = curr_space > max_space ? curr_space : max_space; if (first_valid_check_stack == NULL) { @@ -1755,7 +1634,6 @@ case _CALL_TYPE_1: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-3] = res; stack_pointer += -2; break; @@ -1764,7 +1642,6 @@ case _CALL_STR_1: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-3] = res; stack_pointer += -2; break; @@ -1773,7 +1650,6 @@ case _CALL_TUPLE_1: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-3] = res; stack_pointer += -2; break; @@ -1789,7 +1665,6 @@ case _CALL_BUILTIN_CLASS: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1798,7 +1673,6 @@ case _CALL_BUILTIN_O: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1807,7 +1681,6 @@ case _CALL_BUILTIN_FAST: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1816,7 +1689,6 @@ case _CALL_BUILTIN_FAST_WITH_KEYWORDS: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1825,7 +1697,6 @@ case _CALL_LEN: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1834,7 +1705,6 @@ case _CALL_ISINSTANCE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1843,7 +1713,6 @@ case _CALL_METHOD_DESCRIPTOR_O: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1852,7 +1721,6 @@ case _CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1861,7 +1729,6 @@ case _CALL_METHOD_DESCRIPTOR_NOARGS: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1870,7 +1737,6 @@ case _CALL_METHOD_DESCRIPTOR_FAST: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2 - oparg] = res; stack_pointer += -1 - oparg; break; @@ -1887,7 +1753,6 @@ case _MAKE_FUNCTION: { _Py_UopsSymbol *func; func = sym_new_not_null(ctx); - if (func == NULL) goto out_of_space; stack_pointer[-1] = func; break; } @@ -1895,7 +1760,6 @@ case _SET_FUNCTION_ATTRIBUTE: { _Py_UopsSymbol *func; func = sym_new_not_null(ctx); - if (func == NULL) goto out_of_space; stack_pointer[-2] = func; stack_pointer += -1; break; @@ -1906,7 +1770,7 @@ ctx->frame->stack_pointer = stack_pointer; frame_pop(ctx); stack_pointer = ctx->frame->stack_pointer; - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); /* Stack space handling */ assert(corresponding_check_stack == NULL); assert(co != NULL); @@ -1917,7 +1781,7 @@ co = get_code(this_instr); if (co == NULL) { // might be impossible, but bailing is still safe - goto done; + ctx->done = true; } stack_pointer[0] = res; stack_pointer += 1; @@ -1927,7 +1791,6 @@ case _BUILD_SLICE: { _Py_UopsSymbol *slice; slice = sym_new_not_null(ctx); - if (slice == NULL) goto out_of_space; stack_pointer[-2 - ((oparg == 3) ? 1 : 0)] = slice; stack_pointer += -1 - ((oparg == 3) ? 1 : 0); break; @@ -1936,7 +1799,6 @@ case _CONVERT_VALUE: { _Py_UopsSymbol *result; result = sym_new_not_null(ctx); - if (result == NULL) goto out_of_space; stack_pointer[-1] = result; break; } @@ -1944,7 +1806,6 @@ case _FORMAT_SIMPLE: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-1] = res; break; } @@ -1952,7 +1813,6 @@ case _FORMAT_WITH_SPEC: { _Py_UopsSymbol *res; res = sym_new_not_null(ctx); - if (res == NULL) goto out_of_space; stack_pointer[-2] = res; stack_pointer += -1; break; @@ -1983,14 +1843,14 @@ if (oparg != NB_TRUE_DIVIDE && oparg != NB_INPLACE_TRUE_DIVIDE && ltype == &PyLong_Type && rtype == &PyLong_Type) { /* If both inputs are ints and the op is not division the result is an int */ - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyLong_Type)); + res = sym_new_type(ctx, &PyLong_Type); } else { /* For any other op combining ints/floats the result is a float */ - OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyFloat_Type)); + res = sym_new_type(ctx, &PyFloat_Type); } } - OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx)); + res = sym_new_unknown(ctx); stack_pointer[-2] = res; stack_pointer += -1; break; @@ -2077,7 +1937,7 @@ } case _JUMP_TO_TOP: { - goto done; + ctx->done = true; break; } @@ -2099,7 +1959,7 @@ } case _EXIT_TRACE: { - goto done; + ctx->done = true; break; } @@ -2110,7 +1970,7 @@ case _LOAD_CONST_INLINE: { _Py_UopsSymbol *value; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + value = sym_new_const(ctx, ptr); stack_pointer[0] = value; stack_pointer += 1; break; @@ -2119,7 +1979,7 @@ case _LOAD_CONST_INLINE_BORROW: { _Py_UopsSymbol *value; PyObject *ptr = (PyObject *)this_instr->operand; - OUT_OF_SPACE_IF_NULL(value = sym_new_const(ctx, ptr)); + value = sym_new_const(ctx, ptr); stack_pointer[0] = value; stack_pointer += 1; break; @@ -2128,7 +1988,6 @@ case _POP_TOP_LOAD_CONST_INLINE_BORROW: { _Py_UopsSymbol *value; value = sym_new_not_null(ctx); - if (value == NULL) goto out_of_space; stack_pointer[-1] = value; break; } @@ -2137,8 +1996,8 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *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)); + value = sym_new_const(ctx, ptr); + null = sym_new_null(ctx); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; @@ -2149,8 +2008,8 @@ _Py_UopsSymbol *value; _Py_UopsSymbol *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)); + value = sym_new_const(ctx, ptr); + null = sym_new_null(ctx); stack_pointer[0] = value; stack_pointer[1] = null; stack_pointer += 2; diff --git a/Python/optimizer_symbols.c b/Python/optimizer_symbols.c index 4aeb04fe0405d2..5b0cdcacc3a117 100644 --- a/Python/optimizer_symbols.c +++ b/Python/optimizer_symbols.c @@ -32,6 +32,7 @@ // Flags for below. #define IS_NULL 1 << 0 #define NOT_NULL 1 << 1 +#define NO_SPACE 1 << 2 #ifdef Py_DEBUG static inline int get_lltrace(void) { @@ -48,6 +49,20 @@ static inline int get_lltrace(void) { #define DPRINTF(level, ...) #endif +static _Py_UopsSymbol NO_SPACE_SYMBOL = { + .flags = IS_NULL | NOT_NULL | NO_SPACE, + .typ = NULL, + .const_val = NULL +}; + +_Py_UopsSymbol * +out_of_space(_Py_UOpsContext *ctx) +{ + ctx->done = true; + ctx->out_of_space = true; + return &NO_SPACE_SYMBOL; +} + static _Py_UopsSymbol * sym_new(_Py_UOpsContext *ctx) { @@ -72,11 +87,13 @@ sym_set_flag(_Py_UopsSymbol *sym, int flag) } static inline void -sym_set_bottom(_Py_UopsSymbol *sym) +sym_set_bottom(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym) { sym_set_flag(sym, IS_NULL | NOT_NULL); sym->typ = NULL; Py_CLEAR(sym->const_val); + ctx->done = true; + ctx->contradiction = true; } bool @@ -115,45 +132,42 @@ _Py_uop_sym_get_const(_Py_UopsSymbol *sym) return sym->const_val; } -bool -_Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *typ) +void +_Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *typ) { assert(typ != NULL && PyType_Check(typ)); if (sym->flags & IS_NULL) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); + return; } if (sym->typ != NULL) { if (sym->typ != typ) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); + ctx->contradiction = true; + return; } } else { sym_set_flag(sym, NOT_NULL); sym->typ = typ; } - return true; } -bool -_Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val) +void +_Py_uop_sym_set_const(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyObject *const_val) { assert(const_val != NULL); if (sym->flags & IS_NULL) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); } PyTypeObject *typ = Py_TYPE(const_val); if (sym->typ != NULL && sym->typ != typ) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); } if (sym->const_val != NULL) { if (sym->const_val != const_val) { // TODO: What if they're equal? - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); } } else { @@ -161,29 +175,24 @@ _Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val) sym->typ = typ; sym->const_val = Py_NewRef(const_val); } - return true; } -bool -_Py_uop_sym_set_null(_Py_UopsSymbol *sym) +void +_Py_uop_sym_set_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym) { if (_Py_uop_sym_is_not_null(sym)) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); } sym_set_flag(sym, IS_NULL); - return true; } -bool -_Py_uop_sym_set_non_null(_Py_UopsSymbol *sym) +void +_Py_uop_sym_set_non_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym) { if (_Py_uop_sym_is_null(sym)) { - sym_set_bottom(sym); - return false; + sym_set_bottom(ctx, sym); } sym_set_flag(sym, NOT_NULL); - return true; } @@ -198,7 +207,7 @@ _Py_uop_sym_new_not_null(_Py_UOpsContext *ctx) { _Py_UopsSymbol *res = _Py_uop_sym_new_unknown(ctx); if (res == NULL) { - return NULL; + return out_of_space(ctx); } sym_set_flag(res, NOT_NULL); return res; @@ -209,9 +218,9 @@ _Py_uop_sym_new_type(_Py_UOpsContext *ctx, PyTypeObject *typ) { _Py_UopsSymbol *res = sym_new(ctx); if (res == NULL) { - return NULL; + return out_of_space(ctx); } - _Py_uop_sym_set_type(res, typ); + _Py_uop_sym_set_type(ctx, res, typ); return res; } @@ -222,9 +231,9 @@ _Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *const_val) assert(const_val != NULL); _Py_UopsSymbol *res = sym_new(ctx); if (res == NULL) { - return NULL; + return out_of_space(ctx); } - _Py_uop_sym_set_const(res, const_val); + _Py_uop_sym_set_const(ctx, res, const_val); return res; } @@ -233,9 +242,9 @@ _Py_uop_sym_new_null(_Py_UOpsContext *ctx) { _Py_UopsSymbol *null_sym = _Py_uop_sym_new_unknown(ctx); if (null_sym == NULL) { - return NULL; + return out_of_space(ctx); } - _Py_uop_sym_set_null(null_sym); + _Py_uop_sym_set_null(ctx, null_sym); return null_sym; } @@ -318,6 +327,8 @@ _Py_uop_frame_new( frame->stack_pointer = frame->stack + curr_stackentries; ctx->n_consumed = localsplus_start + (co->co_nlocalsplus + co->co_stacksize); if (ctx->n_consumed >= ctx->limit) { + ctx->done = true; + ctx->out_of_space = true; return NULL; } @@ -325,9 +336,6 @@ _Py_uop_frame_new( // Initialize with the initial state of all local variables for (int i = n_locals_already_filled; i < co->co_nlocalsplus; i++) { _Py_UopsSymbol *local = _Py_uop_sym_new_unknown(ctx); - if (local == NULL) { - return NULL; - } frame->locals[i] = local; } @@ -335,9 +343,6 @@ _Py_uop_frame_new( // Initialize the stack as well for (int i = 0; i < curr_stackentries; i++) { _Py_UopsSymbol *stackvar = _Py_uop_sym_new_unknown(ctx); - if (stackvar == NULL) { - return NULL; - } frame->stack[i] = stackvar; } @@ -357,7 +362,7 @@ _Py_uop_abstractcontext_fini(_Py_UOpsContext *ctx) } } -int +void _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx) { ctx->limit = ctx->locals_and_stack + MAX_ABSTRACT_INTERP_SIZE; @@ -374,8 +379,6 @@ _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx) // Frame setup ctx->curr_frame_depth = 0; - - return 0; } int @@ -404,8 +407,8 @@ static _Py_UopsSymbol * make_bottom(_Py_UOpsContext *ctx) { _Py_UopsSymbol *sym = _Py_uop_sym_new_unknown(ctx); - _Py_uop_sym_set_null(sym); - _Py_uop_sym_set_non_null(sym); + _Py_uop_sym_set_null(ctx, sym); + _Py_uop_sym_set_non_null(ctx, sym); return sym; } @@ -452,10 +455,10 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored)) TEST_PREDICATE(!_Py_uop_sym_is_const(sym), "int is a constant"); TEST_PREDICATE(_Py_uop_sym_get_const(sym) == NULL, "int as constant is not NULL"); - _Py_uop_sym_set_type(sym, &PyLong_Type); // Should be a no-op + _Py_uop_sym_set_type(ctx, sym, &PyLong_Type); // Should be a no-op TEST_PREDICATE(_Py_uop_sym_matches_type(sym, &PyLong_Type), "(int and int) isn't int"); - _Py_uop_sym_set_type(sym, &PyFloat_Type); // Should make it bottom + _Py_uop_sym_set_type(ctx, sym, &PyFloat_Type); // Should make it bottom TEST_PREDICATE(_Py_uop_sym_is_bottom(sym), "(int and float) isn't bottom"); val_42 = PyLong_FromLong(42); @@ -470,7 +473,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored)) if (sym == NULL) { goto fail; } - _Py_uop_sym_set_const(sym, val_42); + _Py_uop_sym_set_const(ctx, sym, val_42); TEST_PREDICATE(_Py_uop_sym_truthiness(sym) == 1, "bool(42) is not True"); TEST_PREDICATE(!_Py_uop_sym_is_null(sym), "42 is NULL"); TEST_PREDICATE(_Py_uop_sym_is_not_null(sym), "42 isn't not NULL"); @@ -480,19 +483,19 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored)) TEST_PREDICATE(_Py_uop_sym_get_const(sym) != NULL, "42 as constant is NULL"); TEST_PREDICATE(_Py_uop_sym_get_const(sym) == val_42, "42 as constant isn't 42"); - _Py_uop_sym_set_type(sym, &PyLong_Type); // Should be a no-op + _Py_uop_sym_set_type(ctx, sym, &PyLong_Type); // Should be a no-op TEST_PREDICATE(_Py_uop_sym_matches_type(sym, &PyLong_Type), "(42 and 42) isn't an int"); TEST_PREDICATE(_Py_uop_sym_get_const(sym) == val_42, "(42 and 42) as constant isn't 42"); - _Py_uop_sym_set_type(sym, &PyFloat_Type); // Should make it bottom + _Py_uop_sym_set_type(ctx, sym, &PyFloat_Type); // Should make it bottom TEST_PREDICATE(_Py_uop_sym_is_bottom(sym), "(42 and float) isn't bottom"); sym = _Py_uop_sym_new_type(ctx, &PyLong_Type); if (sym == NULL) { goto fail; } - _Py_uop_sym_set_const(sym, val_42); - _Py_uop_sym_set_const(sym, val_43); // Should make it bottom + _Py_uop_sym_set_const(ctx, sym, val_42); + _Py_uop_sym_set_const(ctx, sym, val_43); // Should make it bottom TEST_PREDICATE(_Py_uop_sym_is_bottom(sym), "(42 and 43) isn't bottom"); diff --git a/Tools/cases_generator/optimizer_generator.py b/Tools/cases_generator/optimizer_generator.py index 1c6b708e82321a..d5592672a55514 100644 --- a/Tools/cases_generator/optimizer_generator.py +++ b/Tools/cases_generator/optimizer_generator.py @@ -84,14 +84,11 @@ def emit_default(out: CWriter, uop: Uop) -> None: if var.is_array(): out.emit(f"for (int _i = {var.size}; --_i >= 0;) {{\n") out.emit(f"{var.name}[_i] = sym_new_not_null(ctx);\n") - out.emit(f"if ({var.name}[_i] == NULL) goto out_of_space;\n") out.emit("}\n") elif var.name == "null": out.emit(f"{var.name} = sym_new_null(ctx);\n") - out.emit(f"if ({var.name} == NULL) goto out_of_space;\n") else: out.emit(f"{var.name} = sym_new_not_null(ctx);\n") - out.emit(f"if ({var.name} == NULL) goto out_of_space;\n") def write_uop( From 0e08c38e61204548200c0e1695ea79cafba93841 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 May 2024 15:50:32 +0100 Subject: [PATCH 2/5] Tidy up loose ends --- Python/optimizer_analysis.c | 9 --------- Python/optimizer_bytecodes.c | 14 +++++++++----- Python/optimizer_cases.c.h | 14 +++++++++----- Python/optimizer_symbols.c | 1 - 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index b9b5714f1c441c..1b9746d7a356bd 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -297,15 +297,6 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, INST->oparg = ARG; \ INST->operand = OPERAND; -#define OUT_OF_SPACE_IF_NULL(EXPR) (EXPR) - -#define _LOAD_ATTR_NOT_NULL \ - do { \ - OUT_OF_SPACE_IF_NULL(attr = _Py_uop_sym_new_not_null(ctx)); \ - OUT_OF_SPACE_IF_NULL(null = _Py_uop_sym_new_null(ctx)); \ - } while (0); - - /* Shortened forms for convenience, used in optimizer_bytecodes.c */ #define sym_is_not_null _Py_uop_sym_is_not_null #define sym_is_const _Py_uop_sym_is_const diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 7bbd3e9e15d93a..0d43feb7221c1c 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -419,7 +419,8 @@ dummy_func(void) { } op(_LOAD_ATTR_INSTANCE_VALUE, (index/1, owner -- attr, null if (oparg & 1))) { - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)index; (void)owner; } @@ -462,7 +463,7 @@ dummy_func(void) { PyObject *res = convert_global_to_const(this_instr, dict); if (res != NULL) { this_instr[-1].opcode = _POP_TOP; - OUT_OF_SPACE_IF_NULL(attr = sym_new_const(ctx, res)); + attr = sym_new_const(ctx, res); } } if (attr == NULL) { @@ -472,19 +473,22 @@ dummy_func(void) { } op(_LOAD_ATTR_WITH_HINT, (hint/1, owner -- attr, null if (oparg & 1))) { - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)hint; (void)owner; } op(_LOAD_ATTR_SLOT, (index/1, owner -- attr, null if (oparg & 1))) { - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)index; (void)owner; } op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr, null if (oparg & 1))) { - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)descr; (void)owner; } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 5feaf8b46765eb..02bfba6e55bcad 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -946,7 +946,8 @@ _Py_UopsSymbol *null = NULL; owner = stack_pointer[-1]; uint16_t index = (uint16_t)this_instr->operand; - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)index; (void)owner; stack_pointer[-1] = attr; @@ -994,7 +995,7 @@ PyObject *res = convert_global_to_const(this_instr, dict); if (res != NULL) { this_instr[-1].opcode = _POP_TOP; - OUT_OF_SPACE_IF_NULL(attr = sym_new_const(ctx, res)); + attr = sym_new_const(ctx, res); } } if (attr == NULL) { @@ -1017,7 +1018,8 @@ _Py_UopsSymbol *null = NULL; owner = stack_pointer[-1]; uint16_t hint = (uint16_t)this_instr->operand; - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)hint; (void)owner; stack_pointer[-1] = attr; @@ -1032,7 +1034,8 @@ _Py_UopsSymbol *null = NULL; owner = stack_pointer[-1]; uint16_t index = (uint16_t)this_instr->operand; - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)index; (void)owner; stack_pointer[-1] = attr; @@ -1051,7 +1054,8 @@ _Py_UopsSymbol *null = NULL; owner = stack_pointer[-1]; PyObject *descr = (PyObject *)this_instr->operand; - _LOAD_ATTR_NOT_NULL + attr = sym_new_not_null(ctx); + null = sym_new_null(ctx); (void)descr; (void)owner; stack_pointer[-1] = attr; diff --git a/Python/optimizer_symbols.c b/Python/optimizer_symbols.c index 5b0cdcacc3a117..e546eef306eeca 100644 --- a/Python/optimizer_symbols.c +++ b/Python/optimizer_symbols.c @@ -143,7 +143,6 @@ _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *ty if (sym->typ != NULL) { if (sym->typ != typ) { sym_set_bottom(ctx, sym); - ctx->contradiction = true; return; } } From 4a6bf6373a1a5e9a71564b6cd043c61bf6625a38 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 May 2024 15:59:54 +0100 Subject: [PATCH 3/5] Whitespace --- Python/optimizer_analysis.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index 1b9746d7a356bd..437ce38800773a 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -406,7 +406,7 @@ optimize_uops( ctx->contradiction = false; _PyUOpInstruction *this_instr = NULL; - for(int i = 0; !ctx->done; i++) { + for (int i = 0; !ctx->done; i++) { assert(i < trace_len); this_instr = &trace[i]; From 9a21aacf08bcd1519acc388f11e7c7d7fd3abb7b Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 May 2024 16:03:59 +0100 Subject: [PATCH 4/5] Restore null symbol check --- Python/optimizer_bytecodes.c | 4 +++- Python/optimizer_cases.c.h | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 0d43feb7221c1c..e5c982befb2411 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -72,7 +72,9 @@ dummy_func(void) { op(_LOAD_FAST_CHECK, (-- value)) { value = GETLOCAL(oparg); // We guarantee this will error - just bail and don't optimize it. - ctx->done = true; + if (sym_is_null(value)) { + ctx->done = true; + } } op(_LOAD_FAST, (-- value)) { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 02bfba6e55bcad..cd4431d55ad908 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -17,7 +17,9 @@ _Py_UopsSymbol *value; value = GETLOCAL(oparg); // We guarantee this will error - just bail and don't optimize it. - ctx->done = true; + if (sym_is_null(value)) { + ctx->done = true; + } stack_pointer[0] = value; stack_pointer += 1; break; From 50dbf725c14b79c54ecec9cf838fc2db139c0745 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 May 2024 16:25:29 +0100 Subject: [PATCH 5/5] Update Python/optimizer_analysis.c Co-authored-by: Ken Jin --- Python/optimizer_analysis.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index 437ce38800773a..e5d3793bd4d204 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -440,7 +440,7 @@ optimize_uops( DPRINTF(1, "Out of space in abstract interpreter\n"); } if (ctx->contradiction) { - // Attempted to push a "bottom" (contradition) symbol onto the stack. + // Attempted to push a "bottom" (contradiction) symbol onto the stack. // This means that the abstract interpreter has hit unreachable code. // We *could* generate an _EXIT_TRACE or _FATAL_ERROR here, but hitting // bottom indicates type instability, so we are probably better off