Skip to content

Commit aacbdb0

Browse files
authored
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}.
1 parent 753fc8a commit aacbdb0

File tree

5 files changed

+72
-63
lines changed

5 files changed

+72
-63
lines changed

Python/bytecodes.c

+17-20
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,13 @@ dummy_func(
876876
}
877877
}
878878

879+
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
880+
UNPACK_SEQUENCE,
881+
UNPACK_SEQUENCE_TWO_TUPLE,
882+
UNPACK_SEQUENCE_TUPLE,
883+
UNPACK_SEQUENCE_LIST,
884+
};
885+
879886
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
880887
#if ENABLE_SPECIALIZATION
881888
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
@@ -894,43 +901,36 @@ dummy_func(
894901
ERROR_IF(res == 0, error);
895902
}
896903

897-
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- v1, v0)) {
904+
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- values[oparg])) {
898905
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
899906
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
907+
assert(oparg == 2);
900908
STAT_INC(UNPACK_SEQUENCE, hit);
901-
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
902-
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
909+
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
910+
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
903911
Py_DECREF(seq);
904912
}
905913

906-
// stack effect: (__0 -- __array[oparg])
907-
inst(UNPACK_SEQUENCE_TUPLE) {
908-
PyObject *seq = TOP();
914+
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
909915
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
910916
DEOPT_IF(PyTuple_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
911917
STAT_INC(UNPACK_SEQUENCE, hit);
912-
STACK_SHRINK(1);
913918
PyObject **items = _PyTuple_ITEMS(seq);
914-
while (oparg--) {
915-
PUSH(Py_NewRef(items[oparg]));
919+
for (int i = oparg; --i >= 0; ) {
920+
*values++ = Py_NewRef(items[i]);
916921
}
917922
Py_DECREF(seq);
918-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
919923
}
920924

921-
// stack effect: (__0 -- __array[oparg])
922-
inst(UNPACK_SEQUENCE_LIST) {
923-
PyObject *seq = TOP();
925+
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
924926
DEOPT_IF(!PyList_CheckExact(seq), UNPACK_SEQUENCE);
925927
DEOPT_IF(PyList_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
926928
STAT_INC(UNPACK_SEQUENCE, hit);
927-
STACK_SHRINK(1);
928929
PyObject **items = _PyList_ITEMS(seq);
929-
while (oparg--) {
930-
PUSH(Py_NewRef(items[oparg]));
930+
for (int i = oparg; --i >= 0; ) {
931+
*values++ = Py_NewRef(items[i]);
931932
}
932933
Py_DECREF(seq);
933-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
934934
}
935935

936936
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
@@ -3185,6 +3185,3 @@ family(call, INLINE_CACHE_ENTRIES_CALL) = {
31853185
CALL_NO_KW_METHOD_DESCRIPTOR_O, CALL_NO_KW_STR_1, CALL_NO_KW_TUPLE_1,
31863186
CALL_NO_KW_TYPE_1 };
31873187
family(store_fast) = { STORE_FAST, STORE_FAST__LOAD_FAST, STORE_FAST__STORE_FAST };
3188-
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
3189-
UNPACK_SEQUENCE, UNPACK_SEQUENCE_LIST,
3190-
UNPACK_SEQUENCE_TUPLE, UNPACK_SEQUENCE_TWO_TUPLE };

Python/generated_cases.c.h

+21-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_metadata.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
127127
case UNPACK_SEQUENCE_TWO_TUPLE:
128128
return 1;
129129
case UNPACK_SEQUENCE_TUPLE:
130-
return -1;
130+
return 1;
131131
case UNPACK_SEQUENCE_LIST:
132-
return -1;
132+
return 1;
133133
case UNPACK_EX:
134134
return 1;
135135
case STORE_ATTR:
@@ -473,11 +473,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
473473
case UNPACK_SEQUENCE:
474474
return oparg;
475475
case UNPACK_SEQUENCE_TWO_TUPLE:
476-
return 2;
476+
return oparg;
477477
case UNPACK_SEQUENCE_TUPLE:
478-
return -1;
478+
return oparg;
479479
case UNPACK_SEQUENCE_LIST:
480-
return -1;
480+
return oparg;
481481
case UNPACK_EX:
482482
return (oparg & 0xFF) + (oparg >> 8) + 1;
483483
case STORE_ATTR:
@@ -765,9 +765,9 @@ struct opcode_metadata {
765765
[STORE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
766766
[DELETE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
767767
[UNPACK_SEQUENCE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
768-
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
769-
[UNPACK_SEQUENCE_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
770-
[UNPACK_SEQUENCE_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
768+
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
769+
[UNPACK_SEQUENCE_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
770+
[UNPACK_SEQUENCE_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
771771
[UNPACK_EX] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
772772
[STORE_ATTR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
773773
[DELETE_ATTR] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },

Tools/cases_generator/generate_cases.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,8 @@ def assign(self, dst: StackEffect, src: StackEffect):
180180
stmt = f"if ({src.cond}) {{ {stmt} }}"
181181
self.emit(stmt)
182182
elif m := re.match(r"^&PEEK\(.*\)$", dst.name):
183-
# NOTE: MOVE_ITEMS() does not actually exist.
184-
# The only supported output array forms are:
185-
# - unused[...]
186-
# - X[...] where X[...] matches an input array exactly
187-
self.emit(f"MOVE_ITEMS({dst.name}, {src.name}, {src.size});")
183+
# The user code is responsible for writing to the output array.
184+
pass
188185
elif m := re.match(r"^REG\(oparg(\d+)\)$", dst.name):
189186
self.emit(f"Py_XSETREF({dst.name}, {cast}{src.name});")
190187
else:
@@ -309,10 +306,24 @@ def write(self, out: Formatter) -> None:
309306
out.declare(ieffect, src)
310307

311308
# Write output stack effect variable declarations
309+
isize = string_effect_size(list_effect_size(self.input_effects))
312310
input_names = {ieffect.name for ieffect in self.input_effects}
313-
for oeffect in self.output_effects:
311+
for i, oeffect in enumerate(self.output_effects):
314312
if oeffect.name not in input_names:
315-
out.declare(oeffect, None)
313+
if oeffect.size:
314+
osize = string_effect_size(
315+
list_effect_size([oeff for oeff in self.output_effects[:i]])
316+
)
317+
offset = "stack_pointer"
318+
if isize != osize:
319+
if isize != "0":
320+
offset += f" - ({isize})"
321+
if osize != "0":
322+
offset += f" + {osize}"
323+
src = StackEffect(offset, "PyObject **")
324+
out.declare(oeffect, src)
325+
else:
326+
out.declare(oeffect, None)
316327

317328
# out.emit(f"JUMPBY(OPSIZE({self.inst.name}) - 1);")
318329

Tools/cases_generator/test_generator.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,18 @@ def test_array_input():
424424

425425
def test_array_output():
426426
input = """
427-
inst(OP, (-- below, values[oparg*3], above)) {
428-
spam();
427+
inst(OP, (unused, unused -- below, values[oparg*3], above)) {
428+
spam(values, oparg);
429429
}
430430
"""
431431
output = """
432432
TARGET(OP) {
433433
PyObject *below;
434-
PyObject **values;
434+
PyObject **values = stack_pointer - (2) + 1;
435435
PyObject *above;
436-
spam();
437-
STACK_GROW(2);
436+
spam(values, oparg);
438437
STACK_GROW(oparg*3);
439438
POKE(1, above);
440-
MOVE_ITEMS(&PEEK(1 + oparg*3), values, oparg*3);
441439
POKE(2 + oparg*3, below);
442440
DISPATCH();
443441
}
@@ -446,18 +444,17 @@ def test_array_output():
446444

447445
def test_array_input_output():
448446
input = """
449-
inst(OP, (below, values[oparg] -- values[oparg], above)) {
450-
spam();
447+
inst(OP, (values[oparg] -- values[oparg], above)) {
448+
spam(values, oparg);
451449
}
452450
"""
453451
output = """
454452
TARGET(OP) {
455453
PyObject **values = &PEEK(oparg);
456-
PyObject *below = PEEK(1 + oparg);
457454
PyObject *above;
458-
spam();
455+
spam(values, oparg);
456+
STACK_GROW(1);
459457
POKE(1, above);
460-
MOVE_ITEMS(&PEEK(1 + oparg), values, oparg);
461458
DISPATCH();
462459
}
463460
"""

0 commit comments

Comments
 (0)