Skip to content

Commit c885623

Browse files
GH-98831: Auto-generate PREDICTED() macro calls (#99102)
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
1 parent e99380c commit c885623

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

Python/bytecodes.c

-16
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ dummy_func(
112112

113113
// stack effect: ( -- __0)
114114
inst(LOAD_CONST) {
115-
PREDICTED(LOAD_CONST);
116115
PyObject *value = GETITEM(consts, oparg);
117116
Py_INCREF(value);
118117
PUSH(value);
@@ -431,7 +430,6 @@ dummy_func(
431430

432431
// stack effect: (__0 -- )
433432
inst(BINARY_SUBSCR) {
434-
PREDICTED(BINARY_SUBSCR);
435433
PyObject *sub = POP();
436434
PyObject *container = TOP();
437435
PyObject *res = PyObject_GetItem(container, sub);
@@ -631,7 +629,6 @@ dummy_func(
631629

632630
// stack effect: (__0, __1, __2 -- )
633631
inst(STORE_SUBSCR) {
634-
PREDICTED(STORE_SUBSCR);
635632
PyObject *sub = TOP();
636633
PyObject *container = SECOND();
637634
PyObject *v = THIRD();
@@ -884,7 +881,6 @@ dummy_func(
884881

885882
// stack effect: ( -- )
886883
inst(GET_AWAITABLE) {
887-
PREDICTED(GET_AWAITABLE);
888884
PyObject *iterable = TOP();
889885
PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
890886

@@ -1204,7 +1200,6 @@ dummy_func(
12041200

12051201
// stack effect: (__0 -- __array[oparg])
12061202
inst(UNPACK_SEQUENCE) {
1207-
PREDICTED(UNPACK_SEQUENCE);
12081203
PyObject *seq = POP();
12091204
PyObject **top = stack_pointer + oparg;
12101205
if (!unpack_iterable(tstate, seq, oparg, -1, top)) {
@@ -1290,7 +1285,6 @@ dummy_func(
12901285

12911286
// stack effect: (__0, __1 -- )
12921287
inst(STORE_ATTR) {
1293-
PREDICTED(STORE_ATTR);
12941288
PyObject *name = GETITEM(names, oparg);
12951289
PyObject *owner = TOP();
12961290
PyObject *v = SECOND();
@@ -1407,7 +1401,6 @@ dummy_func(
14071401

14081402
// error: LOAD_GLOBAL has irregular stack effect
14091403
inst(LOAD_GLOBAL) {
1410-
PREDICTED(LOAD_GLOBAL);
14111404
int push_null = oparg & 1;
14121405
PEEK(0) = NULL;
14131406
PyObject *name = GETITEM(names, oparg>>1);
@@ -1871,7 +1864,6 @@ dummy_func(
18711864

18721865
// error: LOAD_ATTR has irregular stack effect
18731866
inst(LOAD_ATTR) {
1874-
PREDICTED(LOAD_ATTR);
18751867
PyObject *name = GETITEM(names, oparg >> 1);
18761868
PyObject *owner = TOP();
18771869
if (oparg & 1) {
@@ -2276,7 +2268,6 @@ dummy_func(
22762268

22772269
// stack effect: (__0 -- )
22782270
inst(COMPARE_OP) {
2279-
PREDICTED(COMPARE_OP);
22802271
assert(oparg <= Py_GE);
22812272
PyObject *right = POP();
22822273
PyObject *left = TOP();
@@ -2539,15 +2530,13 @@ dummy_func(
25392530

25402531
// stack effect: ( -- )
25412532
inst(JUMP_BACKWARD) {
2542-
PREDICTED(JUMP_BACKWARD);
25432533
assert(oparg < INSTR_OFFSET());
25442534
JUMPBY(-oparg);
25452535
CHECK_EVAL_BREAKER();
25462536
}
25472537

25482538
// stack effect: (__0 -- )
25492539
inst(POP_JUMP_IF_FALSE) {
2550-
PREDICTED(POP_JUMP_IF_FALSE);
25512540
PyObject *cond = POP();
25522541
if (Py_IsTrue(cond)) {
25532542
_Py_DECREF_NO_DEALLOC(cond);
@@ -2788,7 +2777,6 @@ dummy_func(
27882777

27892778
// stack effect: ( -- __0)
27902779
inst(FOR_ITER) {
2791-
PREDICTED(FOR_ITER);
27922780
/* before: [iter]; after: [iter, iter()] *or* [] */
27932781
PyObject *iter = TOP();
27942782
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
@@ -3118,7 +3106,6 @@ dummy_func(
31183106

31193107
// stack effect: (__0, __array[oparg] -- )
31203108
inst(CALL) {
3121-
PREDICTED(CALL);
31223109
int total_args, is_meth;
31233110
is_meth = is_method(stack_pointer, oparg);
31243111
PyObject *function = PEEK(oparg + 1);
@@ -3216,7 +3203,6 @@ dummy_func(
32163203

32173204
// stack effect: (__0, __array[oparg] -- )
32183205
inst(CALL_PY_EXACT_ARGS) {
3219-
PREDICTED(CALL_PY_EXACT_ARGS);
32203206
assert(call_shape.kwnames == NULL);
32213207
DEOPT_IF(tstate->interp->eval_frame, CALL);
32223208
_PyCallCache *cache = (_PyCallCache *)next_instr;
@@ -3721,7 +3707,6 @@ dummy_func(
37213707

37223708
// error: CALL_FUNCTION_EX has irregular stack effect
37233709
inst(CALL_FUNCTION_EX) {
3724-
PREDICTED(CALL_FUNCTION_EX);
37253710
PyObject *func, *callargs, *kwargs = NULL, *result;
37263711
if (oparg & 0x01) {
37273712
kwargs = POP();
@@ -3913,7 +3898,6 @@ dummy_func(
39133898

39143899
// stack effect: (__0 -- )
39153900
inst(BINARY_OP) {
3916-
PREDICTED(BINARY_OP);
39173901
PyObject *rhs = POP();
39183902
PyObject *lhs = TOP();
39193903
assert(0 <= oparg);

Tools/cases_generator/generate_cases.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import argparse
88
import io
9+
import re
910
import sys
1011

1112
import parser
@@ -61,12 +62,18 @@ def always_exits(block: parser.Block) -> bool:
6162

6263

6364
def write_cases(f: io.TextIOBase, instrs: list[InstDef]):
65+
predictions = set()
66+
for inst in instrs:
67+
for target in re.findall(r"(?:PREDICT|GO_TO_INSTRUCTION)\((\w+)\)", inst.block.text):
68+
predictions.add(target)
6469
indent = " "
6570
f.write("// This file is generated by Tools/scripts/generate_cases.py\n")
6671
f.write("// Do not edit!\n")
6772
for instr in instrs:
6873
assert isinstance(instr, InstDef)
6974
f.write(f"\n{indent}TARGET({instr.name}) {{\n")
75+
if instr.name in predictions:
76+
f.write(f"{indent} PREDICTED({instr.name});\n")
7077
# input = ", ".join(instr.inputs)
7178
# output = ", ".join(instr.outputs)
7279
# f.write(f"{indent} // {input} -- {output}\n")

0 commit comments

Comments
 (0)