gh-101799: implement PREP_RERAISE_STAR as an intrinsic function (#101800)

This commit is contained in:
Irit Katriel 2023-02-14 11:54:13 +00:00 committed by GitHub
parent 3690688149
commit 81e3aa835c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 107 additions and 73 deletions

View File

@ -768,16 +768,6 @@ iterations of the loop.
.. versionadded:: 3.11
.. opcode:: PREP_RERAISE_STAR
Combines the raised and reraised exceptions list from ``STACK[-1]``, into an
exception group to propagate from a try-except* block. Uses the original exception
group from ``STACK[-2]`` to reconstruct the structure of reraised exceptions. Pops
two items from the stack and pushes the exception to reraise or ``None``
if there isn't one.
.. versionadded:: 3.11
.. opcode:: WITH_EXCEPT_START
Calls the function in position 4 on the stack with arguments (type, val, tb)
@ -1515,7 +1505,8 @@ iterations of the loop.
.. opcode:: CALL_INTRINSIC_1
Calls an intrinsic function with one argument. Passes ``STACK[-1]`` as the
argument and sets ``STACK[-1]`` to the result. Used to implement functionality that is necessary but not performance critical.
argument and sets ``STACK[-1]`` to the result. Used to implement
functionality that is necessary but not performance critical.
The operand determines which intrinsic function is called:
@ -1529,6 +1520,19 @@ iterations of the loop.
.. versionadded:: 3.12
.. opcode:: CALL_INTRINSIC_2
Calls an intrinsic function with two arguments. Passes ``STACK[-2]``, ``STACK[-1]`` as the
arguments and sets ``STACK[-1]`` to the result. Used to implement functionality that is
necessary but not performance critical.
The operand determines which intrinsic function is called:
* ``0`` Not valid
* ``1`` Calculates the :exc:`ExceptionGroup` to raise from a ``try-except*``.
.. versionadded:: 3.12
**Pseudo-instructions**

View File

@ -1,4 +1,6 @@
/* Unary Functions: */
#define INTRINSIC_PRINT 1
#define INTRINSIC_IMPORT_STAR 2
#define INTRINSIC_STOPITERATION_ERROR 3
@ -8,6 +10,17 @@
#define MAX_INTRINSIC_1 6
/* Binary Functions: */
#define INTRINSIC_PREP_RERAISE_STAR 1
#define MAX_INTRINSIC_2 1
typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);
typedef PyObject *(*instrinsic_func2)(PyThreadState* tstate, PyObject *value1, PyObject *value2);
extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[];
extern instrinsic_func2 _PyIntrinsics_BinaryFunctions[];

View File

@ -87,6 +87,7 @@ const uint8_t _PyOpcode_Deopt[256] = {
[CALL_BUILTIN_FAST_WITH_KEYWORDS] = CALL,
[CALL_FUNCTION_EX] = CALL_FUNCTION_EX,
[CALL_INTRINSIC_1] = CALL_INTRINSIC_1,
[CALL_INTRINSIC_2] = CALL_INTRINSIC_2,
[CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = CALL,
[CALL_NO_KW_BUILTIN_FAST] = CALL,
[CALL_NO_KW_BUILTIN_O] = CALL,
@ -187,7 +188,6 @@ const uint8_t _PyOpcode_Deopt[256] = {
[POP_JUMP_IF_NOT_NONE] = POP_JUMP_IF_NOT_NONE,
[POP_JUMP_IF_TRUE] = POP_JUMP_IF_TRUE,
[POP_TOP] = POP_TOP,
[PREP_RERAISE_STAR] = PREP_RERAISE_STAR,
[PUSH_EXC_INFO] = PUSH_EXC_INFO,
[PUSH_NULL] = PUSH_NULL,
[RAISE_VARARGS] = RAISE_VARARGS,
@ -319,7 +319,7 @@ static const char *const _PyOpcode_OpName[263] = {
[SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS",
[STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT",
[PREP_RERAISE_STAR] = "PREP_RERAISE_STAR",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[POP_EXCEPT] = "POP_EXCEPT",
[STORE_NAME] = "STORE_NAME",
[DELETE_NAME] = "DELETE_NAME",
@ -344,7 +344,7 @@ static const char *const _PyOpcode_OpName[263] = {
[JUMP_FORWARD] = "JUMP_FORWARD",
[JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP",
[JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST",
[POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE",
[POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE",
[LOAD_GLOBAL] = "LOAD_GLOBAL",
@ -374,7 +374,7 @@ static const char *const _PyOpcode_OpName[263] = {
[JUMP_BACKWARD] = "JUMP_BACKWARD",
[COMPARE_AND_BRANCH] = "COMPARE_AND_BRANCH",
[CALL_FUNCTION_EX] = "CALL_FUNCTION_EX",
[STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST",
[STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST",
[EXTENDED_ARG] = "EXTENDED_ARG",
[LIST_APPEND] = "LIST_APPEND",
[SET_ADD] = "SET_ADD",
@ -384,20 +384,20 @@ static const char *const _PyOpcode_OpName[263] = {
[YIELD_VALUE] = "YIELD_VALUE",
[RESUME] = "RESUME",
[MATCH_CLASS] = "MATCH_CLASS",
[STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST",
[STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
[FORMAT_VALUE] = "FORMAT_VALUE",
[BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP",
[BUILD_STRING] = "BUILD_STRING",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
[UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST",
[UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE",
[UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE",
[SEND_GEN] = "SEND_GEN",
[LIST_EXTEND] = "LIST_EXTEND",
[SET_UPDATE] = "SET_UPDATE",
[DICT_MERGE] = "DICT_MERGE",
[DICT_UPDATE] = "DICT_UPDATE",
[SEND_GEN] = "SEND_GEN",
[166] = "<166>",
[167] = "<167>",
[168] = "<168>",
[169] = "<169>",
@ -405,7 +405,7 @@ static const char *const _PyOpcode_OpName[263] = {
[CALL] = "CALL",
[KW_NAMES] = "KW_NAMES",
[CALL_INTRINSIC_1] = "CALL_INTRINSIC_1",
[174] = "<174>",
[CALL_INTRINSIC_2] = "CALL_INTRINSIC_2",
[175] = "<175>",
[176] = "<176>",
[177] = "<177>",
@ -498,11 +498,11 @@ static const char *const _PyOpcode_OpName[263] = {
#endif
#define EXTRA_CASES \
case 166: \
case 167: \
case 168: \
case 169: \
case 170: \
case 174: \
case 175: \
case 176: \
case 177: \

20
Include/opcode.h generated
View File

@ -43,7 +43,6 @@ extern "C" {
#define RETURN_GENERATOR 75
#define RETURN_VALUE 83
#define SETUP_ANNOTATIONS 85
#define PREP_RERAISE_STAR 88
#define POP_EXCEPT 89
#define HAVE_ARGUMENT 90
#define STORE_NAME 90
@ -117,6 +116,7 @@ extern "C" {
#define CALL 171
#define KW_NAMES 172
#define CALL_INTRINSIC_1 173
#define CALL_INTRINSIC_2 174
#define MIN_PSEUDO_OPCODE 256
#define SETUP_FINALLY 256
#define SETUP_CLEANUP 257
@ -179,15 +179,15 @@ extern "C" {
#define LOAD_GLOBAL_MODULE 84
#define STORE_ATTR_INSTANCE_VALUE 86
#define STORE_ATTR_SLOT 87
#define STORE_ATTR_WITH_HINT 113
#define STORE_FAST__LOAD_FAST 143
#define STORE_FAST__STORE_FAST 153
#define STORE_SUBSCR_DICT 154
#define STORE_SUBSCR_LIST_INT 158
#define UNPACK_SEQUENCE_LIST 159
#define UNPACK_SEQUENCE_TUPLE 160
#define UNPACK_SEQUENCE_TWO_TUPLE 161
#define SEND_GEN 166
#define STORE_ATTR_WITH_HINT 88
#define STORE_FAST__LOAD_FAST 113
#define STORE_FAST__STORE_FAST 143
#define STORE_SUBSCR_DICT 153
#define STORE_SUBSCR_LIST_INT 154
#define UNPACK_SEQUENCE_LIST 158
#define UNPACK_SEQUENCE_TUPLE 159
#define UNPACK_SEQUENCE_TWO_TUPLE 160
#define SEND_GEN 161
#define DO_TRACING 255
#define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\

View File

@ -433,6 +433,7 @@ _code_type = type(_write_atomic.__code__)
# Python 3.12a5 3517 (Change YIELD_VALUE oparg to exception block depth)
# Python 3.12a5 3518 (Add RETURN_CONST instruction)
# Python 3.12a5 3519 (Modify SEND instruction)
# Python 3.12a5 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
# Python 3.13 will start with 3550
@ -445,7 +446,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.
MAGIC_NUMBER = (3519).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3520).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

View File

@ -127,7 +127,6 @@ def_op('RETURN_VALUE', 83)
def_op('SETUP_ANNOTATIONS', 85)
def_op('PREP_RERAISE_STAR', 88)
def_op('POP_EXCEPT', 89)
HAVE_ARGUMENT = 90 # real opcodes from here have an argument:
@ -224,6 +223,7 @@ def_op('CALL', 171)
def_op('KW_NAMES', 172)
hasconst.append(172)
def_op('CALL_INTRINSIC_1', 173)
def_op('CALL_INTRINSIC_2', 174)
hasarg.extend([op for op in opmap.values() if op >= HAVE_ARGUMENT])

View File

@ -0,0 +1,2 @@
Add :opcode:`CALL_INTRINSIC_2` and use it instead of
:opcode:`PREP_RERAISE_STAR`.

View File

@ -501,7 +501,14 @@ dummy_func(
inst(CALL_INTRINSIC_1, (value -- res)) {
assert(oparg <= MAX_INTRINSIC_1);
res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value);
Py_DECREF(value);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}
inst(CALL_INTRINSIC_2, (value2, value1 -- res)) {
assert(oparg <= MAX_INTRINSIC_2);
res = _PyIntrinsics_BinaryFunctions[oparg](tstate, value2, value1);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}
@ -788,15 +795,6 @@ dummy_func(
goto exception_unwind;
}
inst(PREP_RERAISE_STAR, (orig, excs -- val)) {
assert(PyList_Check(excs));
val = _PyExc_PrepReraiseStar(orig, excs);
DECREF_INPUTS();
ERROR_IF(val == NULL, error);
}
inst(END_ASYNC_FOR, (awaitable, exc -- )) {
assert(exc && PyExceptionInstance_Check(exc));
if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
@ -2383,7 +2381,7 @@ dummy_func(
}
// Cache layout: counter/1, func_version/2, min_args/1
// Neither CALL_INTRINSIC_1 nor CALL_FUNCTION_EX are members!
// Neither CALL_INTRINSIC_1/2 nor CALL_FUNCTION_EX are members!
family(call, INLINE_CACHE_ENTRIES_CALL) = {
CALL,
CALL_BOUND_METHOD_EXACT_ARGS,

View File

@ -3431,7 +3431,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
[orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
[orig, res] PREP_RERAISE_STAR
[orig, res] CALL_INTRINSIC_2 PREP_RERAISE_STAR
[exc] COPY 1
[exc, exc] POP_JUMP_IF_NOT_NONE RER
[exc] POP_TOP
@ -3580,7 +3580,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
NEW_JUMP_TARGET_LABEL(c, reraise);
USE_LABEL(c, reraise_star);
ADDOP(c, NO_LOCATION, PREP_RERAISE_STAR);
ADDOP_I(c, NO_LOCATION, CALL_INTRINSIC_2, INTRINSIC_PREP_RERAISE_STAR);
ADDOP_I(c, NO_LOCATION, COPY, 1);
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise);

View File

@ -689,6 +689,20 @@
DISPATCH();
}
TARGET(CALL_INTRINSIC_2) {
PyObject *value1 = PEEK(1);
PyObject *value2 = PEEK(2);
PyObject *res;
assert(oparg <= MAX_INTRINSIC_2);
res = _PyIntrinsics_BinaryFunctions[oparg](tstate, value2, value1);
Py_DECREF(value2);
Py_DECREF(value1);
if (res == NULL) goto pop_2_error;
STACK_SHRINK(1);
POKE(1, res);
DISPATCH();
}
TARGET(RAISE_VARARGS) {
PyObject **args = &PEEK(oparg);
PyObject *cause = NULL, *exc = NULL;
@ -999,22 +1013,6 @@
goto exception_unwind;
}
TARGET(PREP_RERAISE_STAR) {
PyObject *excs = PEEK(1);
PyObject *orig = PEEK(2);
PyObject *val;
assert(PyList_Check(excs));
val = _PyExc_PrepReraiseStar(orig, excs);
Py_DECREF(orig);
Py_DECREF(excs);
if (val == NULL) goto pop_2_error;
STACK_SHRINK(1);
POKE(1, val);
DISPATCH();
}
TARGET(END_ASYNC_FOR) {
PyObject *exc = PEEK(1);
PyObject *awaitable = PEEK(2);

View File

@ -9,6 +9,7 @@
#include "pycore_pyerrors.h"
/******** Unary functions ********/
static PyObject *
no_intrinsic(PyThreadState* tstate, PyObject *unused)
@ -208,3 +209,20 @@ _PyIntrinsics_UnaryFunctions[] = {
[INTRINSIC_UNARY_POSITIVE] = unary_pos,
[INTRINSIC_LIST_TO_TUPLE] = list_to_tuple,
};
/******** Binary functions ********/
static PyObject *
prep_reraise_star(PyThreadState* unused, PyObject *orig, PyObject *excs)
{
assert(PyList_Check(excs));
return _PyExc_PrepReraiseStar(orig, excs);
}
instrinsic_func2
_PyIntrinsics_BinaryFunctions[] = {
[INTRINSIC_PREP_RERAISE_STAR] = prep_reraise_star,
};

View File

@ -88,6 +88,8 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
return 2;
case CALL_INTRINSIC_1:
return 1;
case CALL_INTRINSIC_2:
return 2;
case RAISE_VARARGS:
return oparg;
case INTERPRETER_EXIT:
@ -112,8 +114,6 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
return 1;
case RERAISE:
return oparg + 1;
case PREP_RERAISE_STAR:
return 2;
case END_ASYNC_FOR:
return 2;
case CLEANUP_THROW:
@ -440,6 +440,8 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
return 0;
case CALL_INTRINSIC_1:
return 1;
case CALL_INTRINSIC_2:
return 1;
case RAISE_VARARGS:
return 0;
case INTERPRETER_EXIT:
@ -464,8 +466,6 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
return 0;
case RERAISE:
return oparg;
case PREP_RERAISE_STAR:
return 1;
case END_ASYNC_FOR:
return 0;
case CLEANUP_THROW:
@ -760,6 +760,7 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
[STORE_SUBSCR_DICT] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
[DELETE_SUBSCR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[CALL_INTRINSIC_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CALL_INTRINSIC_2] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[RAISE_VARARGS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[INTERPRETER_EXIT] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[RETURN_VALUE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
@ -772,7 +773,6 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
[YIELD_VALUE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[POP_EXCEPT] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[RERAISE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[PREP_RERAISE_STAR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[END_ASYNC_FOR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[CLEANUP_THROW] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[LOAD_ASSERTION_ERROR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },

View File

@ -87,7 +87,7 @@ static void *opcode_targets[256] = {
&&TARGET_SETUP_ANNOTATIONS,
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_PREP_RERAISE_STAR,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_POP_EXCEPT,
&&TARGET_STORE_NAME,
&&TARGET_DELETE_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_STORE_ATTR_WITH_HINT,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_POP_JUMP_IF_FALSE,
&&TARGET_POP_JUMP_IF_TRUE,
&&TARGET_LOAD_GLOBAL,
@ -142,7 +142,7 @@ static void *opcode_targets[256] = {
&&TARGET_JUMP_BACKWARD,
&&TARGET_COMPARE_AND_BRANCH,
&&TARGET_CALL_FUNCTION_EX,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_STORE_FAST__STORE_FAST,
&&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__STORE_FAST,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_FORMAT_VALUE,
&&TARGET_BUILD_CONST_KEY_MAP,
&&TARGET_BUILD_STRING,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_UNPACK_SEQUENCE_LIST,
&&TARGET_UNPACK_SEQUENCE_TUPLE,
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
&&TARGET_SEND_GEN,
&&TARGET_LIST_EXTEND,
&&TARGET_SET_UPDATE,
&&TARGET_DICT_MERGE,
&&TARGET_DICT_UPDATE,
&&TARGET_SEND_GEN,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,
@ -173,7 +173,7 @@ static void *opcode_targets[256] = {
&&TARGET_CALL,
&&TARGET_KW_NAMES,
&&TARGET_CALL_INTRINSIC_1,
&&_unknown_opcode,
&&TARGET_CALL_INTRINSIC_2,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,