Skip to content

gh-98831: rewrite UNPACK_EX, UNPACK_SEQUENCE, UNPACK_SEQUENCE_TWO_TUPLE in the instruction definition DSL #101641

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 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 11 additions & 26 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,41 +859,31 @@ dummy_func(
}
}

// stack effect: (__0 -- __array[oparg])
inst(UNPACK_SEQUENCE) {
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
#if ENABLE_SPECIALIZATION
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
assert(cframe.use_tracing == 0);
PyObject *seq = TOP();
next_instr--;
_Py_Specialize_UnpackSequence(seq, next_instr, oparg);
DISPATCH_SAME_OPARG();
}
STAT_INC(UNPACK_SEQUENCE, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
PyObject *seq = POP();
PyObject **top = stack_pointer + oparg;
if (!unpack_iterable(tstate, seq, oparg, -1, top)) {
Py_DECREF(seq);
goto error;
}
STACK_GROW(oparg);
PyObject **top = stack_pointer + oparg - 1;
int res = unpack_iterable(tstate, seq, oparg, -1, top);
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
ERROR_IF(res == 0, error);
}

// stack effect: (__0 -- __array[oparg])
inst(UNPACK_SEQUENCE_TWO_TUPLE) {
PyObject *seq = TOP();
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- v1, v0)) {
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
SET_TOP(Py_NewRef(PyTuple_GET_ITEM(seq, 1)));
PUSH(Py_NewRef(PyTuple_GET_ITEM(seq, 0)));
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
Py_DECREF(seq);
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
}

// stack effect: (__0 -- __array[oparg])
Expand Down Expand Up @@ -926,17 +916,12 @@ dummy_func(
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
}

// error: UNPACK_EX has irregular stack effect
inst(UNPACK_EX) {
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
PyObject *seq = POP();
PyObject **top = stack_pointer + totalargs;
if (!unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top)) {
Py_DECREF(seq);
goto error;
}
STACK_GROW(totalargs);
PyObject **top = stack_pointer + totalargs - 1;
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
Py_DECREF(seq);
ERROR_IF(res == 0, error);
}

family(store_attr, INLINE_CACHE_ENTRIES_STORE_ATTR) = {
Expand Down
41 changes: 21 additions & 20 deletions Python/generated_cases.c.h

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

16 changes: 8 additions & 8 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case DELETE_NAME:
return 0;
case UNPACK_SEQUENCE:
return -1;
return 1;
case UNPACK_SEQUENCE_TWO_TUPLE:
return -1;
return 1;
case UNPACK_SEQUENCE_TUPLE:
return -1;
case UNPACK_SEQUENCE_LIST:
return -1;
case UNPACK_EX:
return -1;
return 1;
case STORE_ATTR:
return 2;
case DELETE_ATTR:
Expand Down Expand Up @@ -467,15 +467,15 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case DELETE_NAME:
return 0;
case UNPACK_SEQUENCE:
return -1;
return oparg;
case UNPACK_SEQUENCE_TWO_TUPLE:
return -1;
return 2;
case UNPACK_SEQUENCE_TUPLE:
return -1;
case UNPACK_SEQUENCE_LIST:
return -1;
case UNPACK_EX:
return -1;
return (oparg & 0xFF) + (oparg >> 8) + 1;
case STORE_ATTR:
return 0;
case DELETE_ATTR:
Expand Down Expand Up @@ -759,8 +759,8 @@ struct opcode_metadata {
[LOAD_BUILD_CLASS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[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_IB },
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[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_EX] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down