From 41457c7fdb04819d04a528b8dfa72c1aa5745cc9 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Fri, 8 Mar 2024 00:21:21 +0800 Subject: [PATCH] gh-116381: Remove bad specializations, add fail stats (GH-116464) * Remove bad specializations, add fail stats --- Include/internal/pycore_list.h | 2 - Include/internal/pycore_opcode_metadata.h | 27 +------- Include/internal/pycore_tuple.h | 2 - Include/internal/pycore_uop_ids.h | 3 - Include/internal/pycore_uop_metadata.h | 6 -- Include/opcode_ids.h | 77 +++++++++++----------- Lib/_opcode_metadata.py | 80 +++++++++++------------ Objects/listobject.c | 6 +- Objects/tupleobject.c | 6 +- Python/bytecodes.c | 32 ++------- Python/executor_cases.c.h | 59 ++--------------- Python/generated_cases.c.h | 71 ++------------------ Python/opcode_targets.h | 6 +- Python/optimizer_cases.c.h | 27 -------- Python/specialize.c | 40 ++++++++---- Tools/jit/template.c | 1 + 16 files changed, 125 insertions(+), 320 deletions(-) diff --git a/Include/internal/pycore_list.h b/Include/internal/pycore_list.h index 92cab731bc2..2a82912e41d 100644 --- a/Include/internal/pycore_list.h +++ b/Include/internal/pycore_list.h @@ -56,8 +56,6 @@ typedef struct { PyAPI_FUNC(PyObject *)_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n); -PyAPI_FUNC(int) _PyList_Contains(PyObject *aa, PyObject *el); - #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 4c691d6a922..efb731f1863 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -153,14 +153,8 @@ int _PyOpcode_num_popped(int opcode, int oparg) { return 2; case CONTAINS_OP_DICT: return 2; - case CONTAINS_OP_LIST: - return 2; case CONTAINS_OP_SET: return 2; - case CONTAINS_OP_STR: - return 2; - case CONTAINS_OP_TUPLE: - return 2; case CONVERT_VALUE: return 1; case COPY: @@ -588,14 +582,8 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { return 1; case CONTAINS_OP_DICT: return 1; - case CONTAINS_OP_LIST: - return 1; case CONTAINS_OP_SET: return 1; - case CONTAINS_OP_STR: - return 1; - case CONTAINS_OP_TUPLE: - return 1; case CONVERT_VALUE: return 1; case COPY: @@ -1024,10 +1012,7 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[268] = { [COMPARE_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_DICT] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_LIST] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_SET] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_TUPLE] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONVERT_VALUE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_FLAG }, [COPY] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_PURE_FLAG }, [COPY_FREE_VARS] = { true, INSTR_FMT_IB, HAS_ARG_FLAG }, @@ -1251,10 +1236,7 @@ _PyOpcode_macro_expansion[256] = { [COMPARE_OP_STR] = { .nuops = 2, .uops = { { _GUARD_BOTH_UNICODE, 0, 0 }, { _COMPARE_OP_STR, 0, 0 } } }, [CONTAINS_OP] = { .nuops = 1, .uops = { { _CONTAINS_OP, 0, 0 } } }, [CONTAINS_OP_DICT] = { .nuops = 1, .uops = { { _CONTAINS_OP_DICT, 0, 0 } } }, - [CONTAINS_OP_LIST] = { .nuops = 1, .uops = { { _CONTAINS_OP_LIST, 0, 0 } } }, [CONTAINS_OP_SET] = { .nuops = 1, .uops = { { _CONTAINS_OP_SET, 0, 0 } } }, - [CONTAINS_OP_STR] = { .nuops = 1, .uops = { { _CONTAINS_OP_STR, 0, 0 } } }, - [CONTAINS_OP_TUPLE] = { .nuops = 1, .uops = { { _CONTAINS_OP_TUPLE, 0, 0 } } }, [CONVERT_VALUE] = { .nuops = 1, .uops = { { _CONVERT_VALUE, 0, 0 } } }, [COPY] = { .nuops = 1, .uops = { { _COPY, 0, 0 } } }, [COPY_FREE_VARS] = { .nuops = 1, .uops = { { _COPY_FREE_VARS, 0, 0 } } }, @@ -1429,10 +1411,7 @@ const char *_PyOpcode_OpName[268] = { [COMPARE_OP_STR] = "COMPARE_OP_STR", [CONTAINS_OP] = "CONTAINS_OP", [CONTAINS_OP_DICT] = "CONTAINS_OP_DICT", - [CONTAINS_OP_LIST] = "CONTAINS_OP_LIST", [CONTAINS_OP_SET] = "CONTAINS_OP_SET", - [CONTAINS_OP_STR] = "CONTAINS_OP_STR", - [CONTAINS_OP_TUPLE] = "CONTAINS_OP_TUPLE", [CONVERT_VALUE] = "CONVERT_VALUE", [COPY] = "COPY", [COPY_FREE_VARS] = "COPY_FREE_VARS", @@ -1685,10 +1664,7 @@ const uint8_t _PyOpcode_Deopt[256] = { [COMPARE_OP_STR] = COMPARE_OP, [CONTAINS_OP] = CONTAINS_OP, [CONTAINS_OP_DICT] = CONTAINS_OP, - [CONTAINS_OP_LIST] = CONTAINS_OP, [CONTAINS_OP_SET] = CONTAINS_OP, - [CONTAINS_OP_STR] = CONTAINS_OP, - [CONTAINS_OP_TUPLE] = CONTAINS_OP, [CONVERT_VALUE] = CONVERT_VALUE, [COPY] = COPY, [COPY_FREE_VARS] = COPY_FREE_VARS, @@ -1875,6 +1851,9 @@ const uint8_t _PyOpcode_Deopt[256] = { case 146: \ case 147: \ case 148: \ + case 221: \ + case 222: \ + case 223: \ case 224: \ case 225: \ case 226: \ diff --git a/Include/internal/pycore_tuple.h b/Include/internal/pycore_tuple.h index edde4049502..14a9e42c3a3 100644 --- a/Include/internal/pycore_tuple.h +++ b/Include/internal/pycore_tuple.h @@ -29,8 +29,6 @@ typedef struct { PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ } _PyTupleIterObject; -PyAPI_FUNC(int) _PyTuple_Contains(PyTupleObject *a, PyObject *el); - #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 5bf10ae6499..1209d736abe 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -76,10 +76,7 @@ extern "C" { #define _COMPARE_OP_STR 328 #define _CONTAINS_OP 329 #define _CONTAINS_OP_DICT CONTAINS_OP_DICT -#define _CONTAINS_OP_LIST CONTAINS_OP_LIST #define _CONTAINS_OP_SET CONTAINS_OP_SET -#define _CONTAINS_OP_STR CONTAINS_OP_STR -#define _CONTAINS_OP_TUPLE CONTAINS_OP_TUPLE #define _CONVERT_VALUE CONVERT_VALUE #define _COPY COPY #define _COPY_FREE_VARS COPY_FREE_VARS diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index 23aead27503..7f984d88299 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -154,11 +154,8 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = { [_COMPARE_OP_STR] = HAS_ARG_FLAG | HAS_ESCAPES_FLAG, [_IS_OP] = HAS_ARG_FLAG, [_CONTAINS_OP] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_LIST] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_SET] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_TUPLE] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_DICT] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_STR] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EG_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EXC_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_IS_NONE] = 0, @@ -314,10 +311,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = { [_COMPARE_OP_STR] = "_COMPARE_OP_STR", [_CONTAINS_OP] = "_CONTAINS_OP", [_CONTAINS_OP_DICT] = "_CONTAINS_OP_DICT", - [_CONTAINS_OP_LIST] = "_CONTAINS_OP_LIST", [_CONTAINS_OP_SET] = "_CONTAINS_OP_SET", - [_CONTAINS_OP_STR] = "_CONTAINS_OP_STR", - [_CONTAINS_OP_TUPLE] = "_CONTAINS_OP_TUPLE", [_CONVERT_VALUE] = "_CONVERT_VALUE", [_COPY] = "_COPY", [_COPY_FREE_VARS] = "_COPY_FREE_VARS", diff --git a/Include/opcode_ids.h b/Include/opcode_ids.h index b76ad8b2bf0..185205c6870 100644 --- a/Include/opcode_ids.h +++ b/Include/opcode_ids.h @@ -164,46 +164,43 @@ extern "C" { #define COMPARE_OP_INT 181 #define COMPARE_OP_STR 182 #define CONTAINS_OP_DICT 183 -#define CONTAINS_OP_LIST 184 -#define CONTAINS_OP_SET 185 -#define CONTAINS_OP_STR 186 -#define CONTAINS_OP_TUPLE 187 -#define FOR_ITER_GEN 188 -#define FOR_ITER_LIST 189 -#define FOR_ITER_RANGE 190 -#define FOR_ITER_TUPLE 191 -#define LOAD_ATTR_CLASS 192 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 193 -#define LOAD_ATTR_INSTANCE_VALUE 194 -#define LOAD_ATTR_METHOD_LAZY_DICT 195 -#define LOAD_ATTR_METHOD_NO_DICT 196 -#define LOAD_ATTR_METHOD_WITH_VALUES 197 -#define LOAD_ATTR_MODULE 198 -#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 199 -#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 200 -#define LOAD_ATTR_PROPERTY 201 -#define LOAD_ATTR_SLOT 202 -#define LOAD_ATTR_WITH_HINT 203 -#define LOAD_GLOBAL_BUILTIN 204 -#define LOAD_GLOBAL_MODULE 205 -#define LOAD_SUPER_ATTR_ATTR 206 -#define LOAD_SUPER_ATTR_METHOD 207 -#define RESUME_CHECK 208 -#define SEND_GEN 209 -#define STORE_ATTR_INSTANCE_VALUE 210 -#define STORE_ATTR_SLOT 211 -#define STORE_ATTR_WITH_HINT 212 -#define STORE_SUBSCR_DICT 213 -#define STORE_SUBSCR_LIST_INT 214 -#define TO_BOOL_ALWAYS_TRUE 215 -#define TO_BOOL_BOOL 216 -#define TO_BOOL_INT 217 -#define TO_BOOL_LIST 218 -#define TO_BOOL_NONE 219 -#define TO_BOOL_STR 220 -#define UNPACK_SEQUENCE_LIST 221 -#define UNPACK_SEQUENCE_TUPLE 222 -#define UNPACK_SEQUENCE_TWO_TUPLE 223 +#define CONTAINS_OP_SET 184 +#define FOR_ITER_GEN 185 +#define FOR_ITER_LIST 186 +#define FOR_ITER_RANGE 187 +#define FOR_ITER_TUPLE 188 +#define LOAD_ATTR_CLASS 189 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 190 +#define LOAD_ATTR_INSTANCE_VALUE 191 +#define LOAD_ATTR_METHOD_LAZY_DICT 192 +#define LOAD_ATTR_METHOD_NO_DICT 193 +#define LOAD_ATTR_METHOD_WITH_VALUES 194 +#define LOAD_ATTR_MODULE 195 +#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 196 +#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 197 +#define LOAD_ATTR_PROPERTY 198 +#define LOAD_ATTR_SLOT 199 +#define LOAD_ATTR_WITH_HINT 200 +#define LOAD_GLOBAL_BUILTIN 201 +#define LOAD_GLOBAL_MODULE 202 +#define LOAD_SUPER_ATTR_ATTR 203 +#define LOAD_SUPER_ATTR_METHOD 204 +#define RESUME_CHECK 205 +#define SEND_GEN 206 +#define STORE_ATTR_INSTANCE_VALUE 207 +#define STORE_ATTR_SLOT 208 +#define STORE_ATTR_WITH_HINT 209 +#define STORE_SUBSCR_DICT 210 +#define STORE_SUBSCR_LIST_INT 211 +#define TO_BOOL_ALWAYS_TRUE 212 +#define TO_BOOL_BOOL 213 +#define TO_BOOL_INT 214 +#define TO_BOOL_LIST 215 +#define TO_BOOL_NONE 216 +#define TO_BOOL_STR 217 +#define UNPACK_SEQUENCE_LIST 218 +#define UNPACK_SEQUENCE_TUPLE 219 +#define UNPACK_SEQUENCE_TWO_TUPLE 220 #define INSTRUMENTED_RESUME 236 #define INSTRUMENTED_END_FOR 237 #define INSTRUMENTED_END_SEND 238 diff --git a/Lib/_opcode_metadata.py b/Lib/_opcode_metadata.py index 90dfaea46ca..b5bafe6302b 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -76,11 +76,8 @@ _specializations = { "COMPARE_OP_STR", ], "CONTAINS_OP": [ - "CONTAINS_OP_LIST", "CONTAINS_OP_SET", - "CONTAINS_OP_TUPLE", "CONTAINS_OP_DICT", - "CONTAINS_OP_STR", ], "FOR_ITER": [ "FOR_ITER_LIST", @@ -146,46 +143,43 @@ _specialized_opmap = { 'COMPARE_OP_INT': 181, 'COMPARE_OP_STR': 182, 'CONTAINS_OP_DICT': 183, - 'CONTAINS_OP_LIST': 184, - 'CONTAINS_OP_SET': 185, - 'CONTAINS_OP_STR': 186, - 'CONTAINS_OP_TUPLE': 187, - 'FOR_ITER_GEN': 188, - 'FOR_ITER_LIST': 189, - 'FOR_ITER_RANGE': 190, - 'FOR_ITER_TUPLE': 191, - 'LOAD_ATTR_CLASS': 192, - 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 193, - 'LOAD_ATTR_INSTANCE_VALUE': 194, - 'LOAD_ATTR_METHOD_LAZY_DICT': 195, - 'LOAD_ATTR_METHOD_NO_DICT': 196, - 'LOAD_ATTR_METHOD_WITH_VALUES': 197, - 'LOAD_ATTR_MODULE': 198, - 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 199, - 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 200, - 'LOAD_ATTR_PROPERTY': 201, - 'LOAD_ATTR_SLOT': 202, - 'LOAD_ATTR_WITH_HINT': 203, - 'LOAD_GLOBAL_BUILTIN': 204, - 'LOAD_GLOBAL_MODULE': 205, - 'LOAD_SUPER_ATTR_ATTR': 206, - 'LOAD_SUPER_ATTR_METHOD': 207, - 'RESUME_CHECK': 208, - 'SEND_GEN': 209, - 'STORE_ATTR_INSTANCE_VALUE': 210, - 'STORE_ATTR_SLOT': 211, - 'STORE_ATTR_WITH_HINT': 212, - 'STORE_SUBSCR_DICT': 213, - 'STORE_SUBSCR_LIST_INT': 214, - 'TO_BOOL_ALWAYS_TRUE': 215, - 'TO_BOOL_BOOL': 216, - 'TO_BOOL_INT': 217, - 'TO_BOOL_LIST': 218, - 'TO_BOOL_NONE': 219, - 'TO_BOOL_STR': 220, - 'UNPACK_SEQUENCE_LIST': 221, - 'UNPACK_SEQUENCE_TUPLE': 222, - 'UNPACK_SEQUENCE_TWO_TUPLE': 223, + 'CONTAINS_OP_SET': 184, + 'FOR_ITER_GEN': 185, + 'FOR_ITER_LIST': 186, + 'FOR_ITER_RANGE': 187, + 'FOR_ITER_TUPLE': 188, + 'LOAD_ATTR_CLASS': 189, + 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 190, + 'LOAD_ATTR_INSTANCE_VALUE': 191, + 'LOAD_ATTR_METHOD_LAZY_DICT': 192, + 'LOAD_ATTR_METHOD_NO_DICT': 193, + 'LOAD_ATTR_METHOD_WITH_VALUES': 194, + 'LOAD_ATTR_MODULE': 195, + 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 196, + 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 197, + 'LOAD_ATTR_PROPERTY': 198, + 'LOAD_ATTR_SLOT': 199, + 'LOAD_ATTR_WITH_HINT': 200, + 'LOAD_GLOBAL_BUILTIN': 201, + 'LOAD_GLOBAL_MODULE': 202, + 'LOAD_SUPER_ATTR_ATTR': 203, + 'LOAD_SUPER_ATTR_METHOD': 204, + 'RESUME_CHECK': 205, + 'SEND_GEN': 206, + 'STORE_ATTR_INSTANCE_VALUE': 207, + 'STORE_ATTR_SLOT': 208, + 'STORE_ATTR_WITH_HINT': 209, + 'STORE_SUBSCR_DICT': 210, + 'STORE_SUBSCR_LIST_INT': 211, + 'TO_BOOL_ALWAYS_TRUE': 212, + 'TO_BOOL_BOOL': 213, + 'TO_BOOL_INT': 214, + 'TO_BOOL_LIST': 215, + 'TO_BOOL_NONE': 216, + 'TO_BOOL_STR': 217, + 'UNPACK_SEQUENCE_LIST': 218, + 'UNPACK_SEQUENCE_TUPLE': 219, + 'UNPACK_SEQUENCE_TWO_TUPLE': 220, } opmap = { diff --git a/Objects/listobject.c b/Objects/listobject.c index db8eedf18c5..e013383a712 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -524,8 +524,8 @@ list_length(PyObject *a) return PyList_GET_SIZE(a); } -int -_PyList_Contains(PyObject *aa, PyObject *el) +static int +list_contains(PyObject *aa, PyObject *el) { for (Py_ssize_t i = 0; ; i++) { @@ -3147,7 +3147,7 @@ static PySequenceMethods list_as_sequence = { 0, /* sq_slice */ list_ass_item, /* sq_ass_item */ 0, /* sq_ass_slice */ - _PyList_Contains, /* sq_contains */ + list_contains, /* sq_contains */ list_inplace_concat, /* sq_inplace_concat */ list_inplace_repeat, /* sq_inplace_repeat */ }; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 5bbc0279c21..d9dc00da368 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -349,8 +349,8 @@ tuplelength(PyTupleObject *a) return Py_SIZE(a); } -int -_PyTuple_Contains(PyTupleObject *a, PyObject *el) +static int +tuplecontains(PyTupleObject *a, PyObject *el) { Py_ssize_t i; int cmp; @@ -758,7 +758,7 @@ static PySequenceMethods tuple_as_sequence = { 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ - (objobjproc)_PyTuple_Contains, /* sq_contains */ + (objobjproc)tuplecontains, /* sq_contains */ }; static PyObject* diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 3276a4a9644..0397d96e3f6 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2238,11 +2238,8 @@ dummy_func( } family(CONTAINS_OP, INLINE_CACHE_ENTRIES_CONTAINS_OP) = { - CONTAINS_OP_LIST, CONTAINS_OP_SET, - CONTAINS_OP_TUPLE, CONTAINS_OP_DICT, - CONTAINS_OP_STR, }; op(_CONTAINS_OP, (left, right -- b)) { @@ -2266,46 +2263,25 @@ dummy_func( macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP; - inst(CONTAINS_OP_LIST, (unused/1, left, right -- b)) { - DEOPT_IF(!PyList_CheckExact(right)); - int res = _PyList_Contains(right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CONTAINS_OP_SET, (unused/1, left, right -- b)) { - DEOPT_IF(!PySet_CheckExact(right)); + DEOPT_IF(!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right))); + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; } - inst(CONTAINS_OP_TUPLE, (unused/1, left, right -- b)) { - DEOPT_IF(!PyTuple_CheckExact(right)); - int res = _PyTuple_Contains((PyTupleObject *)right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CONTAINS_OP_DICT, (unused/1, left, right -- b)) { DEOPT_IF(!PyDict_CheckExact(right)); + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; } - inst(CONTAINS_OP_STR, (unused/1, left, right -- b)) { - DEOPT_IF(!PyUnicode_CheckExact(right)); - int res = PyUnicode_Contains(right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) { if (_PyEval_CheckExceptStarTypeValid(tstate, match_type) < 0) { DECREF_INPUTS(); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 2e7b970b4dd..26ac1593d53 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2189,24 +2189,6 @@ break; } - case _CONTAINS_OP_LIST: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (!PyList_CheckExact(right)) goto deoptimize; - int res = _PyList_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_SET: { PyObject *right; PyObject *left; @@ -2214,7 +2196,9 @@ oparg = CURRENT_OPARG(); right = stack_pointer[-1]; left = stack_pointer[-2]; - if (!PySet_CheckExact(right)) goto deoptimize; + if (!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right))) goto deoptimize; + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); Py_DECREF(left); Py_DECREF(right); @@ -2225,24 +2209,6 @@ break; } - case _CONTAINS_OP_TUPLE: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (!PyTuple_CheckExact(right)) goto deoptimize; - int res = _PyTuple_Contains((PyTupleObject *)right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_DICT: { PyObject *right; PyObject *left; @@ -2251,6 +2217,7 @@ right = stack_pointer[-1]; left = stack_pointer[-2]; if (!PyDict_CheckExact(right)) goto deoptimize; + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); Py_DECREF(left); Py_DECREF(right); @@ -2261,24 +2228,6 @@ break; } - case _CONTAINS_OP_STR: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (!PyUnicode_CheckExact(right)) goto deoptimize; - int res = PyUnicode_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CHECK_EG_MATCH: { PyObject *match_type; PyObject *exc_value; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 54c48617fd4..f5d125c63b1 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2176,6 +2176,7 @@ right = stack_pointer[-1]; left = stack_pointer[-2]; DEOPT_IF(!PyDict_CheckExact(right), CONTAINS_OP); + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); Py_DECREF(left); Py_DECREF(right); @@ -2186,28 +2187,6 @@ DISPATCH(); } - TARGET(CONTAINS_OP_LIST) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_LIST); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(!PyList_CheckExact(right), CONTAINS_OP); - int res = _PyList_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - TARGET(CONTAINS_OP_SET) { frame->instr_ptr = next_instr; next_instr += 2; @@ -2219,7 +2198,9 @@ /* Skip 1 cache entry */ right = stack_pointer[-1]; left = stack_pointer[-2]; - DEOPT_IF(!PySet_CheckExact(right), CONTAINS_OP); + DEOPT_IF(!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right)), CONTAINS_OP); + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); Py_DECREF(left); Py_DECREF(right); @@ -2230,50 +2211,6 @@ DISPATCH(); } - TARGET(CONTAINS_OP_STR) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_STR); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(!PyUnicode_CheckExact(right), CONTAINS_OP); - int res = PyUnicode_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - - TARGET(CONTAINS_OP_TUPLE) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_TUPLE); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(!PyTuple_CheckExact(right), CONTAINS_OP); - int res = _PyTuple_Contains((PyTupleObject *)right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - TARGET(CONVERT_VALUE) { frame->instr_ptr = next_instr; next_instr += 1; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 6b3846d4629..4061ba33cea 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -183,10 +183,7 @@ static void *opcode_targets[256] = { &&TARGET_COMPARE_OP_INT, &&TARGET_COMPARE_OP_STR, &&TARGET_CONTAINS_OP_DICT, - &&TARGET_CONTAINS_OP_LIST, &&TARGET_CONTAINS_OP_SET, - &&TARGET_CONTAINS_OP_STR, - &&TARGET_CONTAINS_OP_TUPLE, &&TARGET_FOR_ITER_GEN, &&TARGET_FOR_ITER_LIST, &&TARGET_FOR_ITER_RANGE, @@ -235,6 +232,9 @@ static void *opcode_targets[256] = { &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, + &&_unknown_opcode, + &&_unknown_opcode, + &&_unknown_opcode, &&TARGET_INSTRUMENTED_RESUME, &&TARGET_INSTRUMENTED_END_FOR, &&TARGET_INSTRUMENTED_END_SEND, diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index e4384975643..c88b25132c0 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1218,15 +1218,6 @@ break; } - case _CONTAINS_OP_LIST: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_SET: { _Py_UopsSymbol *b; b = sym_new_unknown(ctx); @@ -1236,15 +1227,6 @@ break; } - case _CONTAINS_OP_TUPLE: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_DICT: { _Py_UopsSymbol *b; b = sym_new_unknown(ctx); @@ -1254,15 +1236,6 @@ break; } - case _CONTAINS_OP_STR: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CHECK_EG_MATCH: { _Py_UopsSymbol *rest; _Py_UopsSymbol *match; diff --git a/Python/specialize.c b/Python/specialize.c index 5d339b8a476..b1f9eb756c3 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -600,6 +600,12 @@ _PyCode_Quicken(PyCodeObject *code) #define SPEC_FAIL_TO_BOOL_SET 17 #define SPEC_FAIL_TO_BOOL_TUPLE 18 +// CONTAINS_OP +#define SPEC_FAIL_CONTAINS_OP_STR 9 +#define SPEC_FAIL_CONTAINS_OP_TUPLE 10 +#define SPEC_FAIL_CONTAINS_OP_LIST 11 +#define SPEC_FAIL_CONTAINS_OP_USER_CLASS 12 + static int function_kind(PyCodeObject *code); static bool function_check_args(PyObject *o, int expected_argcount, int opcode); static uint32_t function_get_version(PyObject *o, int opcode); @@ -2562,34 +2568,40 @@ success: cache->counter = adaptive_counter_cooldown(); } +#ifdef Py_STATS +static int containsop_fail_kind(PyObject *value) { + if (PyUnicode_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_STR; + } + if (PyList_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_LIST; + } + if (PyTuple_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_TUPLE; + } + if (PyType_Check(value)) { + return SPEC_FAIL_CONTAINS_OP_USER_CLASS; + } + return SPEC_FAIL_OTHER; +} +#endif // Py_STATS + void _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr) { assert(ENABLE_SPECIALIZATION); assert(_PyOpcode_Caches[CONTAINS_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP); _PyContainsOpCache *cache = (_PyContainsOpCache *)(instr + 1); - if (PyUnicode_CheckExact(value)) { - instr->op.code = CONTAINS_OP_STR; - goto success; - } - if (PyList_CheckExact(value)) { - instr->op.code = CONTAINS_OP_LIST; - goto success; - } - if (PyTuple_CheckExact(value)) { - instr->op.code = CONTAINS_OP_TUPLE; - goto success; - } if (PyDict_CheckExact(value)) { instr->op.code = CONTAINS_OP_DICT; goto success; } - if (PySet_CheckExact(value)) { + if (PySet_CheckExact(value) || PyFrozenSet_CheckExact(value)) { instr->op.code = CONTAINS_OP_SET; goto success; } - + SPECIALIZATION_FAIL(CONTAINS_OP, containsop_fail_kind(value)); STAT_INC(CONTAINS_OP, failure); instr->op.code = CONTAINS_OP; cache->counter = adaptive_counter_backoff(cache->counter); diff --git a/Tools/jit/template.c b/Tools/jit/template.c index 8aaf4581de3..504e6c87552 100644 --- a/Tools/jit/template.c +++ b/Tools/jit/template.c @@ -13,6 +13,7 @@ #include "pycore_range.h" #include "pycore_setobject.h" #include "pycore_sliceobject.h" +#include "pycore_descrobject.h" #include "ceval_macros.h"