gh-98831: Finish the UNPACK_SEQUENCE family (#101666)

New generator feature: Generate useful glue for output arrays, so you can just write values to the output array (no bounds checking). Rewrote UNPACK_SEQUENCE_TWO_TUPLE to use this, and also UNPACK_SEQUENCE_{TUPLE,LIST}.
This commit is contained in:
Guido van Rossum 2023-02-07 15:44:37 -08:00 committed by GitHub
parent 753fc8a5d6
commit aacbdb0c65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 63 deletions

View file

@ -876,6 +876,13 @@ dummy_func(
}
}
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
UNPACK_SEQUENCE,
UNPACK_SEQUENCE_TWO_TUPLE,
UNPACK_SEQUENCE_TUPLE,
UNPACK_SEQUENCE_LIST,
};
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
#if ENABLE_SPECIALIZATION
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
@ -894,43 +901,36 @@ dummy_func(
ERROR_IF(res == 0, error);
}
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- v1, v0)) {
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- values[oparg])) {
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
assert(oparg == 2);
STAT_INC(UNPACK_SEQUENCE, hit);
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
Py_DECREF(seq);
}
// stack effect: (__0 -- __array[oparg])
inst(UNPACK_SEQUENCE_TUPLE) {
PyObject *seq = TOP();
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyTuple_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyTuple_ITEMS(seq);
while (oparg--) {
PUSH(Py_NewRef(items[oparg]));
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
}
// stack effect: (__0 -- __array[oparg])
inst(UNPACK_SEQUENCE_LIST) {
PyObject *seq = TOP();
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
DEOPT_IF(!PyList_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyList_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyList_ITEMS(seq);
while (oparg--) {
PUSH(Py_NewRef(items[oparg]));
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
}
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
@ -3185,6 +3185,3 @@ family(call, INLINE_CACHE_ENTRIES_CALL) = {
CALL_NO_KW_METHOD_DESCRIPTOR_O, CALL_NO_KW_STR_1, CALL_NO_KW_TUPLE_1,
CALL_NO_KW_TYPE_1 };
family(store_fast) = { STORE_FAST, STORE_FAST__LOAD_FAST, STORE_FAST__STORE_FAST };
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
UNPACK_SEQUENCE, UNPACK_SEQUENCE_LIST,
UNPACK_SEQUENCE_TUPLE, UNPACK_SEQUENCE_TWO_TUPLE };

View file

@ -1125,6 +1125,7 @@
TARGET(UNPACK_SEQUENCE) {
PREDICTED(UNPACK_SEQUENCE);
static_assert(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE == 1, "incorrect cache size");
PyObject *seq = PEEK(1);
#if ENABLE_SPECIALIZATION
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
@ -1149,48 +1150,51 @@
TARGET(UNPACK_SEQUENCE_TWO_TUPLE) {
PyObject *seq = PEEK(1);
PyObject *v1;
PyObject *v0;
PyObject **values = stack_pointer - (1);
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
assert(oparg == 2);
STAT_INC(UNPACK_SEQUENCE, hit);
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
Py_DECREF(seq);
STACK_GROW(1);
POKE(1, v0);
POKE(2, v1);
STACK_SHRINK(1);
STACK_GROW(oparg);
JUMPBY(1);
DISPATCH();
}
TARGET(UNPACK_SEQUENCE_TUPLE) {
PyObject *seq = TOP();
PyObject *seq = PEEK(1);
PyObject **values = stack_pointer - (1);
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyTuple_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyTuple_ITEMS(seq);
while (oparg--) {
PUSH(Py_NewRef(items[oparg]));
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
STACK_SHRINK(1);
STACK_GROW(oparg);
JUMPBY(1);
DISPATCH();
}
TARGET(UNPACK_SEQUENCE_LIST) {
PyObject *seq = TOP();
PyObject *seq = PEEK(1);
PyObject **values = stack_pointer - (1);
DEOPT_IF(!PyList_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyList_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyList_ITEMS(seq);
while (oparg--) {
PUSH(Py_NewRef(items[oparg]));
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
STACK_SHRINK(1);
STACK_GROW(oparg);
JUMPBY(1);
DISPATCH();
}

View file

@ -127,9 +127,9 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case UNPACK_SEQUENCE_TWO_TUPLE:
return 1;
case UNPACK_SEQUENCE_TUPLE:
return -1;
return 1;
case UNPACK_SEQUENCE_LIST:
return -1;
return 1;
case UNPACK_EX:
return 1;
case STORE_ATTR:
@ -473,11 +473,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case UNPACK_SEQUENCE:
return oparg;
case UNPACK_SEQUENCE_TWO_TUPLE:
return 2;
return oparg;
case UNPACK_SEQUENCE_TUPLE:
return -1;
return oparg;
case UNPACK_SEQUENCE_LIST:
return -1;
return oparg;
case UNPACK_EX:
return (oparg & 0xFF) + (oparg >> 8) + 1;
case STORE_ATTR:
@ -765,9 +765,9 @@ struct opcode_metadata {
[STORE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[DELETE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[UNPACK_SEQUENCE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
[UNPACK_SEQUENCE_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[UNPACK_SEQUENCE_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[UNPACK_SEQUENCE_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[UNPACK_SEQUENCE_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[UNPACK_EX] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[STORE_ATTR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
[DELETE_ATTR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },