Skip to content

GH-98831: Implement array support in cases generator #100912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4b17476
Tweak 'This file is generated...' header
gvanrossum Jan 5, 2023
69f04e4
Start on array stack effects
gvanrossum Jan 6, 2023
b9bd7e3
Simple input array effects work
gvanrossum Jan 7, 2023
bb53c44
Ignore Context when comparing Nodes
gvanrossum Jan 7, 2023
4a15c53
Improve stack effect counting (TODO: use everywhere)
gvanrossum Jan 7, 2023
0a1e9a2
Fix stack effect counting in most places
gvanrossum Jan 8, 2023
b023236
Fix TODO
gvanrossum Jan 9, 2023
9ec91e1
Fix opcode metadata for array stack effects
gvanrossum Jan 9, 2023
af785de
Array-ize LIST_APPEND
gvanrossum Jan 10, 2023
4fddf2b
Array-ize SET_ADD
gvanrossum Jan 10, 2023
ac22115
Array-ize LIST_EXTEND
gvanrossum Jan 10, 2023
6c83be0
Array-ize SET_UPDATE
gvanrossum Jan 10, 2023
c7999dd
Array-ize BUILD_TUPLE
gvanrossum Jan 10, 2023
a7aa425
Array-ize BUILD_LIST
gvanrossum Jan 10, 2023
11f6a58
Use ERROR_IF() in BUILD_STRING
gvanrossum Jan 10, 2023
597fd69
Merge remote-tracking branch 'origin/main' into cases-array
gvanrossum Jan 10, 2023
abff4ae
Apply most suggestions from code review
gvanrossum Jan 12, 2023
a02a9eb
Accept more general dimensions
gvanrossum Jan 12, 2023
2bcd90f
Fix LIST_EXTEND
gvanrossum Jan 12, 2023
c579a40
Fix LIST_APPEND, SET_ADD, SET_UPDATE
gvanrossum Jan 12, 2023
21ac256
Merge remote-tracking branch 'origin/main' into cases-array
gvanrossum Jan 14, 2023
70e983d
Array-ize BUILD_SET
gvanrossum Jan 14, 2023
827481e
Array-ize BUILD_MAP
gvanrossum Jan 14, 2023
aa69c55
Array-ize BUILD_CONST_KEY_MAP
gvanrossum Jan 14, 2023
6cbabc7
Add comment warning that RAISE_VARARGS needs to stay legacy
gvanrossum Jan 14, 2023
c7cfe9a
Look for 'oparg' in the whole instruction
gvanrossum Jan 17, 2023
a1a4688
Add docstring for effect_size()
gvanrossum Jan 17, 2023
16a2d99
Parenthesize certain symbolic effects
gvanrossum Jan 17, 2023
1ef085d
Fix typo in comment
gvanrossum Jan 17, 2023
1bf42ce
Swap args of imaginary MOVE_ITEMS() macro
gvanrossum Jan 17, 2023
c2be1ab
Tune for loops
gvanrossum Jan 17, 2023
c7edea2
Disallow empty dimension
gvanrossum Jan 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 36 additions & 70 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,12 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}

// Alternative: (list, unused[oparg], v -- list, unused[oparg])
inst(LIST_APPEND, (v --)) {
PyObject *list = PEEK(oparg + 1); // +1 to account for v staying on stack
inst(LIST_APPEND, (list, unused[oparg-1], v -- list, unused[oparg-1])) {
ERROR_IF(_PyList_AppendTakeRef((PyListObject *)list, v) < 0, error);
PREDICT(JUMP_BACKWARD);
}

// Alternative: (set, unused[oparg], v -- set, unused[oparg])
inst(SET_ADD, (v --)) {
PyObject *set = PEEK(oparg + 1); // +1 to account for v staying on stack
inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
int err = PySet_Add(set, v);
Py_DECREF(v);
ERROR_IF(err, error);
Expand Down Expand Up @@ -537,7 +533,7 @@ dummy_func(
ERROR_IF(res == NULL, error);
}

// stack effect: (__array[oparg] -- )
// This should remain a legacy instruction.
inst(RAISE_VARARGS) {
PyObject *cause = NULL, *exc = NULL;
switch (oparg) {
Expand Down Expand Up @@ -1300,40 +1296,25 @@ dummy_func(
}
}

// stack effect: (__array[oparg] -- __0)
inst(BUILD_STRING) {
PyObject *str;
str = _PyUnicode_JoinArray(&_Py_STR(empty),
stack_pointer - oparg, oparg);
if (str == NULL)
goto error;
while (--oparg >= 0) {
PyObject *item = POP();
Py_DECREF(item);
inst(BUILD_STRING, (pieces[oparg] -- str)) {
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
for (int i = 0; i < oparg; i++) {
Py_DECREF(pieces[i]);
}
PUSH(str);
ERROR_IF(str == NULL, error);
}

// stack effect: (__array[oparg] -- __0)
inst(BUILD_TUPLE) {
STACK_SHRINK(oparg);
PyObject *tup = _PyTuple_FromArraySteal(stack_pointer, oparg);
if (tup == NULL)
goto error;
PUSH(tup);
inst(BUILD_TUPLE, (values[oparg] -- tup)) {
tup = _PyTuple_FromArraySteal(values, oparg);
ERROR_IF(tup == NULL, error);
}

// stack effect: (__array[oparg] -- __0)
inst(BUILD_LIST) {
STACK_SHRINK(oparg);
PyObject *list = _PyList_FromArraySteal(stack_pointer, oparg);
if (list == NULL)
goto error;
PUSH(list);
inst(BUILD_LIST, (values[oparg] -- list)) {
list = _PyList_FromArraySteal(values, oparg);
ERROR_IF(list == NULL, error);
}

inst(LIST_EXTEND, (iterable -- )) {
PyObject *list = PEEK(oparg + 1); // iterable is still on the stack
inst(LIST_EXTEND, (list, unused[oparg-1], iterable -- list, unused[oparg-1])) {
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
if (none_val == NULL) {
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Expand All @@ -1351,48 +1332,40 @@ dummy_func(
DECREF_INPUTS();
}

inst(SET_UPDATE, (iterable --)) {
PyObject *set = PEEK(oparg + 1); // iterable is still on the stack
inst(SET_UPDATE, (set, unused[oparg-1], iterable -- set, unused[oparg-1])) {
int err = _PySet_Update(set, iterable);
DECREF_INPUTS();
ERROR_IF(err < 0, error);
}

// stack effect: (__array[oparg] -- __0)
inst(BUILD_SET) {
PyObject *set = PySet_New(NULL);
inst(BUILD_SET, (values[oparg] -- set)) {
set = PySet_New(NULL);
int err = 0;
int i;
if (set == NULL)
goto error;
for (i = oparg; i > 0; i--) {
PyObject *item = PEEK(i);
for (int i = 0; i < oparg; i++) {
PyObject *item = values[i];
if (err == 0)
err = PySet_Add(set, item);
Py_DECREF(item);
}
STACK_SHRINK(oparg);
if (err != 0) {
Py_DECREF(set);
goto error;
ERROR_IF(true, error);
}
PUSH(set);
}

// stack effect: (__array[oparg*2] -- __0)
inst(BUILD_MAP) {
PyObject *map = _PyDict_FromItems(
&PEEK(2*oparg), 2,
&PEEK(2*oparg - 1), 2,
inst(BUILD_MAP, (values[oparg*2] -- map)) {
map = _PyDict_FromItems(
values, 2,
values+1, 2,
oparg);
if (map == NULL)
goto error;

while (oparg--) {
Py_DECREF(POP());
Py_DECREF(POP());
for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i*2]);
Py_DECREF(values[i*2+1]);
}
PUSH(map);
ERROR_IF(map == NULL, error);
}

inst(SETUP_ANNOTATIONS, (--)) {
Expand Down Expand Up @@ -1437,28 +1410,21 @@ dummy_func(
}
}

// stack effect: (__array[oparg] -- )
inst(BUILD_CONST_KEY_MAP) {
PyObject *map;
PyObject *keys = TOP();
inst(BUILD_CONST_KEY_MAP, (values[oparg], keys -- map)) {
if (!PyTuple_CheckExact(keys) ||
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
_PyErr_SetString(tstate, PyExc_SystemError,
"bad BUILD_CONST_KEY_MAP keys argument");
goto error;
goto error; // Pop the keys and values.
}
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
&PEEK(oparg + 1), 1, oparg);
if (map == NULL) {
goto error;
}

Py_DECREF(POP());
while (oparg--) {
Py_DECREF(POP());
values, 1, oparg);
Py_DECREF(keys);
for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i]);
}
PUSH(map);
ERROR_IF(map == NULL, error);
}

inst(DICT_UPDATE, (update --)) {
Expand Down
102 changes: 55 additions & 47 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ static const struct {
[BINARY_SUBSCR_TUPLE_INT] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_DICT] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_GETITEM] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC000 },
[LIST_APPEND] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[SET_ADD] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LIST_APPEND] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[SET_ADD] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[STORE_SUBSCR] = { 3, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
[STORE_SUBSCR_LIST_INT] = { 3, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
[STORE_SUBSCR_DICT] = { 3, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
Expand Down Expand Up @@ -92,8 +92,8 @@ static const struct {
[BUILD_STRING] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[BUILD_TUPLE] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[BUILD_LIST] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LIST_EXTEND] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[SET_UPDATE] = { 1, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LIST_EXTEND] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[SET_UPDATE] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[BUILD_SET] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[BUILD_MAP] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[SETUP_ANNOTATIONS] = { 0, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
Expand Down
Loading