[Python-checkins] GH-98831: Auto-generate PREDICTED() macro calls (#99102)

gvanrossum webhook-mailer at python.org
Fri Nov 4 18:30:26 EDT 2022


https://github.com/python/cpython/commit/c885623e9fa596cbe4fd359ea4aedfbde0ae2482
commit: c885623e9fa596cbe4fd359ea4aedfbde0ae2482
branch: main
author: Guido van Rossum <guido at python.org>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-11-04T15:30:17-07:00
summary:

GH-98831: Auto-generate PREDICTED() macro calls (#99102)

Co-authored-by: Brandt Bucher <brandtbucher at gmail.com>

files:
M Python/bytecodes.c
M Tools/cases_generator/generate_cases.py

diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index b0d56279e043..072eee42f29e 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -112,7 +112,6 @@ dummy_func(
 
         // stack effect: ( -- __0)
         inst(LOAD_CONST) {
-            PREDICTED(LOAD_CONST);
             PyObject *value = GETITEM(consts, oparg);
             Py_INCREF(value);
             PUSH(value);
@@ -431,7 +430,6 @@ dummy_func(
 
         // stack effect: (__0 -- )
         inst(BINARY_SUBSCR) {
-            PREDICTED(BINARY_SUBSCR);
             PyObject *sub = POP();
             PyObject *container = TOP();
             PyObject *res = PyObject_GetItem(container, sub);
@@ -631,7 +629,6 @@ dummy_func(
 
         // stack effect: (__0, __1, __2 -- )
         inst(STORE_SUBSCR) {
-            PREDICTED(STORE_SUBSCR);
             PyObject *sub = TOP();
             PyObject *container = SECOND();
             PyObject *v = THIRD();
@@ -884,7 +881,6 @@ dummy_func(
 
         // stack effect: ( -- )
         inst(GET_AWAITABLE) {
-            PREDICTED(GET_AWAITABLE);
             PyObject *iterable = TOP();
             PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
 
@@ -1204,7 +1200,6 @@ dummy_func(
 
         // stack effect: (__0 -- __array[oparg])
         inst(UNPACK_SEQUENCE) {
-            PREDICTED(UNPACK_SEQUENCE);
             PyObject *seq = POP();
             PyObject **top = stack_pointer + oparg;
             if (!unpack_iterable(tstate, seq, oparg, -1, top)) {
@@ -1290,7 +1285,6 @@ dummy_func(
 
         // stack effect: (__0, __1 -- )
         inst(STORE_ATTR) {
-            PREDICTED(STORE_ATTR);
             PyObject *name = GETITEM(names, oparg);
             PyObject *owner = TOP();
             PyObject *v = SECOND();
@@ -1407,7 +1401,6 @@ dummy_func(
 
         // error: LOAD_GLOBAL has irregular stack effect
         inst(LOAD_GLOBAL) {
-            PREDICTED(LOAD_GLOBAL);
             int push_null = oparg & 1;
             PEEK(0) = NULL;
             PyObject *name = GETITEM(names, oparg>>1);
@@ -1871,7 +1864,6 @@ dummy_func(
 
         // error: LOAD_ATTR has irregular stack effect
         inst(LOAD_ATTR) {
-            PREDICTED(LOAD_ATTR);
             PyObject *name = GETITEM(names, oparg >> 1);
             PyObject *owner = TOP();
             if (oparg & 1) {
@@ -2276,7 +2268,6 @@ dummy_func(
 
         // stack effect: (__0 -- )
         inst(COMPARE_OP) {
-            PREDICTED(COMPARE_OP);
             assert(oparg <= Py_GE);
             PyObject *right = POP();
             PyObject *left = TOP();
@@ -2539,7 +2530,6 @@ dummy_func(
 
         // stack effect: ( -- )
         inst(JUMP_BACKWARD) {
-            PREDICTED(JUMP_BACKWARD);
             assert(oparg < INSTR_OFFSET());
             JUMPBY(-oparg);
             CHECK_EVAL_BREAKER();
@@ -2547,7 +2537,6 @@ dummy_func(
 
         // stack effect: (__0 -- )
         inst(POP_JUMP_IF_FALSE) {
-            PREDICTED(POP_JUMP_IF_FALSE);
             PyObject *cond = POP();
             if (Py_IsTrue(cond)) {
                 _Py_DECREF_NO_DEALLOC(cond);
@@ -2788,7 +2777,6 @@ dummy_func(
 
         // stack effect: ( -- __0)
         inst(FOR_ITER) {
-            PREDICTED(FOR_ITER);
             /* before: [iter]; after: [iter, iter()] *or* [] */
             PyObject *iter = TOP();
             PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
@@ -3118,7 +3106,6 @@ dummy_func(
 
         // stack effect: (__0, __array[oparg] -- )
         inst(CALL) {
-            PREDICTED(CALL);
             int total_args, is_meth;
             is_meth = is_method(stack_pointer, oparg);
             PyObject *function = PEEK(oparg + 1);
@@ -3216,7 +3203,6 @@ dummy_func(
 
         // stack effect: (__0, __array[oparg] -- )
         inst(CALL_PY_EXACT_ARGS) {
-            PREDICTED(CALL_PY_EXACT_ARGS);
             assert(call_shape.kwnames == NULL);
             DEOPT_IF(tstate->interp->eval_frame, CALL);
             _PyCallCache *cache = (_PyCallCache *)next_instr;
@@ -3721,7 +3707,6 @@ dummy_func(
 
         // error: CALL_FUNCTION_EX has irregular stack effect
         inst(CALL_FUNCTION_EX) {
-            PREDICTED(CALL_FUNCTION_EX);
             PyObject *func, *callargs, *kwargs = NULL, *result;
             if (oparg & 0x01) {
                 kwargs = POP();
@@ -3913,7 +3898,6 @@ dummy_func(
 
         // stack effect: (__0 -- )
         inst(BINARY_OP) {
-            PREDICTED(BINARY_OP);
             PyObject *rhs = POP();
             PyObject *lhs = TOP();
             assert(0 <= oparg);
diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py
index 305ffdad2401..82e58017d648 100644
--- a/Tools/cases_generator/generate_cases.py
+++ b/Tools/cases_generator/generate_cases.py
@@ -6,6 +6,7 @@
 
 import argparse
 import io
+import re
 import sys
 
 import parser
@@ -61,12 +62,18 @@ def always_exits(block: parser.Block) -> bool:
 
 
 def write_cases(f: io.TextIOBase, instrs: list[InstDef]):
+    predictions = set()
+    for inst in instrs:
+        for target in re.findall(r"(?:PREDICT|GO_TO_INSTRUCTION)\((\w+)\)", inst.block.text):
+            predictions.add(target)
     indent = "        "
     f.write("// This file is generated by Tools/scripts/generate_cases.py\n")
     f.write("// Do not edit!\n")
     for instr in instrs:
         assert isinstance(instr, InstDef)
         f.write(f"\n{indent}TARGET({instr.name}) {{\n")
+        if instr.name in predictions:
+            f.write(f"{indent}    PREDICTED({instr.name});\n")
         # input = ", ".join(instr.inputs)
         # output = ", ".join(instr.outputs)
         # f.write(f"{indent}    // {input} -- {output}\n")



More information about the Python-checkins mailing list