GH-99005: More intrinsics (GH-100774)

* Remove UNARY_POSITIVE, LIST_TO_TUPLE and ASYNC_GEN_WRAP, replacing them with intrinsics.
This commit is contained in:
Mark Shannon 2023-01-06 14:47:57 +00:00 committed by GitHub
parent 659c2607f5
commit 78068126a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 151 additions and 204 deletions

View file

@ -173,12 +173,6 @@ dummy_func(
macro(END_FOR) = POP_TOP + POP_TOP;
inst(UNARY_POSITIVE, (value -- res)) {
res = PyNumber_Positive(value);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}
inst(UNARY_NEGATIVE, (value -- res)) {
res = PyNumber_Negative(value);
DECREF_INPUTS();
@ -757,13 +751,6 @@ dummy_func(
}
}
inst(ASYNC_GEN_WRAP, (v -- w)) {
assert(frame->f_code->co_flags & CO_ASYNC_GENERATOR);
w = _PyAsyncGenValueWrapperNew(v);
DECREF_INPUTS();
ERROR_IF(w == NULL, error);
}
inst(YIELD_VALUE, (retval -- unused)) {
// NOTE: It's important that YIELD_VALUE never raises an exception!
// The compiler treats any exception raised here as a failed close()
@ -1348,12 +1335,6 @@ dummy_func(
PUSH(list);
}
inst(LIST_TO_TUPLE, (list -- tuple)) {
tuple = PyList_AsTuple(list);
DECREF_INPUTS();
ERROR_IF(tuple == NULL, error);
}
inst(LIST_EXTEND, (iterable -- )) {
PyObject *list = PEEK(oparg + 1); // iterable is still on the stack
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);

View file

@ -1090,7 +1090,6 @@ stack_effect(int opcode, int oparg, int jump)
return -2;
/* Unary operators */
case UNARY_POSITIVE:
case UNARY_NEGATIVE:
case UNARY_NOT:
case UNARY_INVERT:
@ -1123,7 +1122,6 @@ stack_effect(int opcode, int oparg, int jump)
return -1;
case SETUP_ANNOTATIONS:
return 0;
case ASYNC_GEN_WRAP:
case YIELD_VALUE:
return 0;
case POP_BLOCK:
@ -1296,8 +1294,6 @@ stack_effect(int opcode, int oparg, int jump)
return 1;
case LOAD_ASSERTION_ERROR:
return 1;
case LIST_TO_TUPLE:
return 0;
case LIST_EXTEND:
case SET_UPDATE:
case DICT_MERGE:
@ -4122,8 +4118,6 @@ unaryop(unaryop_ty op)
return UNARY_INVERT;
case Not:
return UNARY_NOT;
case UAdd:
return UNARY_POSITIVE;
case USub:
return UNARY_NEGATIVE;
default:
@ -4191,7 +4185,7 @@ addop_binary(struct compiler *c, location loc, operator_ty binop,
static int
addop_yield(struct compiler *c, location loc) {
if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
ADDOP(c, loc, ASYNC_GEN_WRAP);
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_ASYNC_GEN_WRAP);
}
ADDOP_I(c, loc, YIELD_VALUE, 0);
ADDOP_I(c, loc, RESUME, 1);
@ -4358,7 +4352,7 @@ starunpack_helper(struct compiler *c, location loc,
ADDOP_LOAD_CONST_NEW(c, loc, folded);
ADDOP_I(c, loc, extend, 1);
if (tuple) {
ADDOP(c, loc, LIST_TO_TUPLE);
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
}
}
return SUCCESS;
@ -4409,7 +4403,7 @@ starunpack_helper(struct compiler *c, location loc,
}
assert(sequence_built);
if (tuple) {
ADDOP(c, loc, LIST_TO_TUPLE);
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
}
return SUCCESS;
}
@ -5784,7 +5778,12 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
break;
case UnaryOp_kind:
VISIT(c, expr, e->v.UnaryOp.operand);
ADDOP(c, loc, unaryop(e->v.UnaryOp.op));
if (e->v.UnaryOp.op == UAdd) {
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_UNARY_POSITIVE);
}
else {
ADDOP(c, loc, unaryop(e->v.UnaryOp.op));
}
break;
case Lambda_kind:
return compiler_lambda(c, e);

View file

@ -202,16 +202,6 @@
DISPATCH();
}
TARGET(UNARY_POSITIVE) {
PyObject *value = PEEK(1);
PyObject *res;
res = PyNumber_Positive(value);
Py_DECREF(value);
if (res == NULL) goto pop_1_error;
POKE(1, res);
DISPATCH();
}
TARGET(UNARY_NEGATIVE) {
PyObject *value = PEEK(1);
PyObject *res;
@ -921,17 +911,6 @@
DISPATCH();
}
TARGET(ASYNC_GEN_WRAP) {
PyObject *v = PEEK(1);
PyObject *w;
assert(frame->f_code->co_flags & CO_ASYNC_GENERATOR);
w = _PyAsyncGenValueWrapperNew(v);
Py_DECREF(v);
if (w == NULL) goto pop_1_error;
POKE(1, w);
DISPATCH();
}
TARGET(YIELD_VALUE) {
PyObject *retval = PEEK(1);
// NOTE: It's important that YIELD_VALUE never raises an exception!
@ -1566,16 +1545,6 @@
DISPATCH();
}
TARGET(LIST_TO_TUPLE) {
PyObject *list = PEEK(1);
PyObject *tuple;
tuple = PyList_AsTuple(list);
Py_DECREF(list);
if (tuple == NULL) goto pop_1_error;
POKE(1, tuple);
DISPATCH();
}
TARGET(LIST_EXTEND) {
PyObject *iterable = PEEK(1);
PyObject *list = PEEK(oparg + 1); // iterable is still on the stack

View file

@ -185,10 +185,26 @@ stopiteration_error(PyThreadState* tstate, PyObject *exc)
return Py_NewRef(exc);
}
static PyObject *
unary_pos(PyThreadState* unused, PyObject *value)
{
return PyNumber_Positive(value);
}
static PyObject *
list_to_tuple(PyThreadState* unused, PyObject *v)
{
assert(PyList_Check(v));
return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
}
instrinsic_func1
_PyIntrinsics_UnaryFunctions[] = {
[0] = no_intrinsic,
[INTRINSIC_PRINT] = print_expr,
[INTRINSIC_IMPORT_STAR] = import_star,
[INTRINSIC_STOPITERATION_ERROR] = stopiteration_error,
[INTRINSIC_ASYNC_GEN_WRAP] = _PyAsyncGenValueWrapperNew,
[INTRINSIC_UNARY_POSITIVE] = unary_pos,
[INTRINSIC_LIST_TO_TUPLE] = list_to_tuple,
};

View file

@ -26,7 +26,6 @@ static const struct {
[POP_TOP] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[PUSH_NULL] = { 0, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[END_FOR] = { 2, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[UNARY_POSITIVE] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[UNARY_NEGATIVE] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[UNARY_NOT] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[UNARY_INVERT] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
@ -59,7 +58,6 @@ static const struct {
[GET_ANEXT] = { 1, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[GET_AWAITABLE] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[SEND] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[ASYNC_GEN_WRAP] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[YIELD_VALUE] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[POP_EXCEPT] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[RERAISE] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
@ -93,7 +91,6 @@ static const struct {
[BUILD_STRING] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[BUILD_TUPLE] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[BUILD_LIST] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[LIST_TO_TUPLE] = { 1, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[LIST_EXTEND] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[SET_UPDATE] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },
[BUILD_SET] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, "IB" },

View file

@ -9,13 +9,12 @@ static void *opcode_targets[256] = {
&&TARGET_BINARY_OP_ADD_UNICODE,
&&TARGET_BINARY_OP_INPLACE_ADD_UNICODE,
&&TARGET_NOP,
&&TARGET_UNARY_POSITIVE,
&&TARGET_BINARY_OP_MULTIPLY_FLOAT,
&&TARGET_UNARY_NEGATIVE,
&&TARGET_UNARY_NOT,
&&TARGET_BINARY_OP_MULTIPLY_FLOAT,
&&TARGET_BINARY_OP_MULTIPLY_INT,
&&TARGET_UNARY_INVERT,
&&TARGET_BINARY_OP_SUBTRACT_FLOAT,
&&TARGET_UNARY_INVERT,
&&TARGET_BINARY_OP_SUBTRACT_INT,
&&TARGET_BINARY_SUBSCR_DICT,
&&TARGET_BINARY_SUBSCR_GETITEM,
@ -24,20 +23,20 @@ static void *opcode_targets[256] = {
&&TARGET_CALL_PY_EXACT_ARGS,
&&TARGET_CALL_PY_WITH_DEFAULTS,
&&TARGET_CALL_BOUND_METHOD_EXACT_ARGS,
&&TARGET_CALL_BUILTIN_CLASS,
&&TARGET_BINARY_SUBSCR,
&&TARGET_BINARY_SLICE,
&&TARGET_STORE_SLICE,
&&TARGET_CALL_BUILTIN_CLASS,
&&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS,
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
&&TARGET_GET_LEN,
&&TARGET_MATCH_MAPPING,
&&TARGET_MATCH_SEQUENCE,
&&TARGET_MATCH_KEYS,
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
&&TARGET_CALL_NO_KW_BUILTIN_FAST,
&&TARGET_PUSH_EXC_INFO,
&&TARGET_CHECK_EXC_MATCH,
&&TARGET_CHECK_EG_MATCH,
&&TARGET_CALL_NO_KW_BUILTIN_FAST,
&&TARGET_CALL_NO_KW_BUILTIN_O,
&&TARGET_CALL_NO_KW_ISINSTANCE,
&&TARGET_CALL_NO_KW_LEN,
@ -48,6 +47,7 @@ static void *opcode_targets[256] = {
&&TARGET_CALL_NO_KW_STR_1,
&&TARGET_CALL_NO_KW_TUPLE_1,
&&TARGET_CALL_NO_KW_TYPE_1,
&&TARGET_COMPARE_OP_FLOAT_JUMP,
&&TARGET_WITH_EXCEPT_START,
&&TARGET_GET_AITER,
&&TARGET_GET_ANEXT,
@ -55,38 +55,38 @@ static void *opcode_targets[256] = {
&&TARGET_BEFORE_WITH,
&&TARGET_END_ASYNC_FOR,
&&TARGET_CLEANUP_THROW,
&&TARGET_COMPARE_OP_FLOAT_JUMP,
&&TARGET_COMPARE_OP_INT_JUMP,
&&TARGET_COMPARE_OP_STR_JUMP,
&&TARGET_FOR_ITER_LIST,
&&TARGET_FOR_ITER_TUPLE,
&&TARGET_STORE_SUBSCR,
&&TARGET_DELETE_SUBSCR,
&&TARGET_FOR_ITER_TUPLE,
&&TARGET_FOR_ITER_RANGE,
&&TARGET_FOR_ITER_GEN,
&&TARGET_LOAD_ATTR_CLASS,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_ATTR_INSTANCE_VALUE,
&&TARGET_LOAD_ATTR_MODULE,
&&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_LOAD_ATTR_MODULE,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_ATTR_PROPERTY,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_ATTR_SLOT,
&&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_RETURN_GENERATOR,
&&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_LOAD_ATTR_METHOD_NO_DICT,
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
&&TARGET_LOAD_CONST__LOAD_FAST,
&&TARGET_LOAD_FAST__LOAD_CONST,
&&TARGET_LIST_TO_TUPLE,
&&TARGET_RETURN_VALUE,
&&TARGET_LOAD_FAST__LOAD_FAST,
&&TARGET_SETUP_ANNOTATIONS,
&&TARGET_LOAD_GLOBAL_BUILTIN,
&&TARGET_ASYNC_GEN_WRAP,
&&TARGET_RETURN_VALUE,
&&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_SETUP_ANNOTATIONS,
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_PREP_RERAISE_STAR,
&&TARGET_POP_EXCEPT,
&&TARGET_STORE_NAME,
@ -112,7 +112,7 @@ static void *opcode_targets[256] = {
&&TARGET_JUMP_FORWARD,
&&TARGET_JUMP_IF_FALSE_OR_POP,
&&TARGET_JUMP_IF_TRUE_OR_POP,
&&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_POP_JUMP_IF_FALSE,
&&TARGET_POP_JUMP_IF_TRUE,
&&TARGET_LOAD_GLOBAL,
@ -120,7 +120,7 @@ static void *opcode_targets[256] = {
&&TARGET_CONTAINS_OP,
&&TARGET_RERAISE,
&&TARGET_COPY,
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_BINARY_OP,
&&TARGET_SEND,
&&TARGET_LOAD_FAST,
@ -140,9 +140,9 @@ static void *opcode_targets[256] = {
&&TARGET_STORE_DEREF,
&&TARGET_DELETE_DEREF,
&&TARGET_JUMP_BACKWARD,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_STORE_FAST__STORE_FAST,
&&TARGET_CALL_FUNCTION_EX,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_EXTENDED_ARG,
&&TARGET_LIST_APPEND,
&&TARGET_SET_ADD,
@ -152,20 +152,20 @@ static void *opcode_targets[256] = {
&&TARGET_YIELD_VALUE,
&&TARGET_RESUME,
&&TARGET_MATCH_CLASS,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_STORE_FAST__STORE_FAST,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_UNPACK_SEQUENCE_LIST,
&&TARGET_FORMAT_VALUE,
&&TARGET_BUILD_CONST_KEY_MAP,
&&TARGET_BUILD_STRING,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_UNPACK_SEQUENCE_LIST,
&&TARGET_UNPACK_SEQUENCE_TUPLE,
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
&&_unknown_opcode,
&&_unknown_opcode,
&&TARGET_LIST_EXTEND,
&&TARGET_SET_UPDATE,
&&TARGET_DICT_MERGE,
&&TARGET_DICT_UPDATE,
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,