summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-02-07 20:37:43 (GMT)
committerGitHub <noreply@github.com>2023-02-07 20:37:43 (GMT)
commitdec1ab03879e959f7efb910a723caf4a9ce453cf (patch)
tree4339eb2f4fc3f4fd383ef83fde76e8ef3fc59ab3
parentf87f6e23964d7a4c38b655089cda65538a24ec36 (diff)
downloadcpython-dec1ab03879e959f7efb910a723caf4a9ce453cf.zip
cpython-dec1ab03879e959f7efb910a723caf4a9ce453cf.tar.gz
cpython-dec1ab03879e959f7efb910a723caf4a9ce453cf.tar.bz2
gh-98831: rewrite UNPACK_EX, UNPACK_SEQUENCE, UNPACK_SEQUENCE_TWO_TUPLE in the instruction definition DSL (#101641)
-rw-r--r--Python/bytecodes.c37
-rw-r--r--Python/generated_cases.c.h41
-rw-r--r--Python/opcode_metadata.h16
3 files changed, 40 insertions, 54 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index ec0439a..b43625f 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -859,13 +859,11 @@ 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();
@@ -873,27 +871,19 @@ dummy_func(
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])
@@ -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) = {
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 4e511f4..ab19f43 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -1108,11 +1108,11 @@
TARGET(UNPACK_SEQUENCE) {
PREDICTED(UNPACK_SEQUENCE);
+ PyObject *seq = PEEK(1);
#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();
@@ -1120,27 +1120,30 @@
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);
+ if (res == 0) goto pop_1_error;
+ STACK_SHRINK(1);
+ STACK_GROW(oparg);
+ JUMPBY(1);
DISPATCH();
}
TARGET(UNPACK_SEQUENCE_TWO_TUPLE) {
- PyObject *seq = TOP();
+ PyObject *seq = PEEK(1);
+ PyObject *v1;
+ PyObject *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_GROW(1);
+ POKE(1, v0);
+ POKE(2, v1);
+ JUMPBY(1);
DISPATCH();
}
@@ -1175,15 +1178,13 @@
}
TARGET(UNPACK_EX) {
+ PyObject *seq = PEEK(1);
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);
+ if (res == 0) goto pop_1_error;
+ STACK_GROW((oparg & 0xFF) + (oparg >> 8));
DISPATCH();
}
diff --git a/Python/opcode_metadata.h b/Python/opcode_metadata.h
index ed26ff0..d258535 100644
--- a/Python/opcode_metadata.h
+++ b/Python/opcode_metadata.h
@@ -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:
@@ -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:
@@ -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 },