From 7114cf20c015b99123b32c1ba4f5475b7a6c3a13 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Thu, 7 Mar 2024 03:30:11 +0800 Subject: [PATCH] gh-116381: Specialize CONTAINS_OP (GH-116385) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Specialize CONTAINS_OP * 📜🤖 Added by blurb_it. * Add PyAPI_FUNC for JIT --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> --- Include/internal/pycore_code.h | 7 + Include/internal/pycore_list.h | 2 + Include/internal/pycore_opcode_metadata.h | 48 ++++- Include/internal/pycore_setobject.h | 2 + Include/internal/pycore_tuple.h | 2 + Include/internal/pycore_uop_ids.h | 199 +++++++++--------- Include/internal/pycore_uop_metadata.h | 10 + Include/opcode_ids.h | 77 +++---- Lib/_opcode_metadata.py | 84 ++++---- Lib/importlib/_bootstrap_external.py | 3 +- Lib/opcode.py | 3 + ...-03-05-22-00-58.gh-issue-116381.0Nq9iO.rst | 1 + Objects/listobject.c | 6 +- Objects/setobject.c | 8 +- Objects/tupleobject.c | 6 +- Python/bytecodes.c | 64 +++++- Python/executor_cases.c.h | 90 ++++++++ Python/generated_cases.c.h | 134 +++++++++++- Python/opcode_targets.h | 10 +- Python/optimizer_cases.c.h | 45 ++++ Python/specialize.c | 38 ++++ 21 files changed, 645 insertions(+), 194 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-03-05-22-00-58.gh-issue-116381.0Nq9iO.rst diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index a4e64825743ada..8eabd49a18afa9 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -121,6 +121,12 @@ typedef struct { #define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache) +typedef struct { + uint16_t counter; +} _PyContainsOpCache; + +#define INLINE_CACHE_ENTRIES_CONTAINS_OP CACHE_ENTRIES(_PyContainsOpCache) + // Borrowed references to common callables: struct callable_cache { PyObject *isinstance; @@ -277,6 +283,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr, extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg); extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr); extern void _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr); +extern void _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr); /* Finalizer function for static codeobjects used in deepfreeze.py */ extern void _PyStaticCode_Fini(PyCodeObject *co); diff --git a/Include/internal/pycore_list.h b/Include/internal/pycore_list.h index 2a82912e41d557..92cab731bc253b 100644 --- a/Include/internal/pycore_list.h +++ b/Include/internal/pycore_list.h @@ -56,6 +56,8 @@ 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 f355a496cf2e70..4c691d6a922862 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -151,6 +151,16 @@ int _PyOpcode_num_popped(int opcode, int oparg) { return 2; case CONTAINS_OP: 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: @@ -576,6 +586,16 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { return 1; case CONTAINS_OP: 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: @@ -1002,7 +1022,12 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[268] = { [COMPARE_OP_FLOAT] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [COMPARE_OP_INT] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [COMPARE_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_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 }, @@ -1225,6 +1250,11 @@ _PyOpcode_macro_expansion[256] = { [COMPARE_OP_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _COMPARE_OP_INT, 0, 0 } } }, [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 } } }, @@ -1398,6 +1428,11 @@ const char *_PyOpcode_OpName[268] = { [COMPARE_OP_INT] = "COMPARE_OP_INT", [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", @@ -1578,6 +1613,7 @@ const uint8_t _PyOpcode_Caches[256] = { [LOAD_SUPER_ATTR] = 1, [LOAD_ATTR] = 9, [COMPARE_OP] = 1, + [CONTAINS_OP] = 1, [POP_JUMP_IF_TRUE] = 1, [POP_JUMP_IF_FALSE] = 1, [POP_JUMP_IF_NONE] = 1, @@ -1648,6 +1684,11 @@ const uint8_t _PyOpcode_Deopt[256] = { [COMPARE_OP_INT] = COMPARE_OP, [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, @@ -1834,11 +1875,6 @@ const uint8_t _PyOpcode_Deopt[256] = { case 146: \ case 147: \ case 148: \ - case 219: \ - case 220: \ - case 221: \ - case 222: \ - case 223: \ case 224: \ case 225: \ case 226: \ diff --git a/Include/internal/pycore_setobject.h b/Include/internal/pycore_setobject.h index 34a00e6d45fe69..c4ec3ceb17eba6 100644 --- a/Include/internal/pycore_setobject.h +++ b/Include/internal/pycore_setobject.h @@ -21,6 +21,8 @@ PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); // Export for the gdb plugin's (python-gdb.py) benefit PyAPI_DATA(PyObject *) _PySet_Dummy; +PyAPI_FUNC(int) _PySet_Contains(PySetObject *so, PyObject *key); + #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_tuple.h b/Include/internal/pycore_tuple.h index 14a9e42c3a324c..edde40495021f1 100644 --- a/Include/internal/pycore_tuple.h +++ b/Include/internal/pycore_tuple.h @@ -29,6 +29,8 @@ 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 856f340f07c8bb..5bf10ae6499e69 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -74,7 +74,12 @@ extern "C" { #define _COMPARE_OP_FLOAT 326 #define _COMPARE_OP_INT 327 #define _COMPARE_OP_STR 328 -#define _CONTAINS_OP CONTAINS_OP +#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 @@ -88,41 +93,41 @@ extern "C" { #define _DICT_UPDATE DICT_UPDATE #define _END_SEND END_SEND #define _EXIT_INIT_CHECK EXIT_INIT_CHECK -#define _FATAL_ERROR 329 +#define _FATAL_ERROR 330 #define _FORMAT_SIMPLE FORMAT_SIMPLE #define _FORMAT_WITH_SPEC FORMAT_WITH_SPEC -#define _FOR_ITER 330 +#define _FOR_ITER 331 #define _FOR_ITER_GEN FOR_ITER_GEN -#define _FOR_ITER_TIER_TWO 331 +#define _FOR_ITER_TIER_TWO 332 #define _GET_AITER GET_AITER #define _GET_ANEXT GET_ANEXT #define _GET_AWAITABLE GET_AWAITABLE #define _GET_ITER GET_ITER #define _GET_LEN GET_LEN #define _GET_YIELD_FROM_ITER GET_YIELD_FROM_ITER -#define _GUARD_BOTH_FLOAT 332 -#define _GUARD_BOTH_INT 333 -#define _GUARD_BOTH_UNICODE 334 -#define _GUARD_BUILTINS_VERSION 335 -#define _GUARD_DORV_VALUES 336 -#define _GUARD_DORV_VALUES_INST_ATTR_FROM_DICT 337 -#define _GUARD_GLOBALS_VERSION 338 -#define _GUARD_IS_FALSE_POP 339 -#define _GUARD_IS_NONE_POP 340 -#define _GUARD_IS_NOT_NONE_POP 341 -#define _GUARD_IS_TRUE_POP 342 -#define _GUARD_KEYS_VERSION 343 -#define _GUARD_NOT_EXHAUSTED_LIST 344 -#define _GUARD_NOT_EXHAUSTED_RANGE 345 -#define _GUARD_NOT_EXHAUSTED_TUPLE 346 -#define _GUARD_TYPE_VERSION 347 -#define _INIT_CALL_BOUND_METHOD_EXACT_ARGS 348 -#define _INIT_CALL_PY_EXACT_ARGS 349 -#define _INIT_CALL_PY_EXACT_ARGS_0 350 -#define _INIT_CALL_PY_EXACT_ARGS_1 351 -#define _INIT_CALL_PY_EXACT_ARGS_2 352 -#define _INIT_CALL_PY_EXACT_ARGS_3 353 -#define _INIT_CALL_PY_EXACT_ARGS_4 354 +#define _GUARD_BOTH_FLOAT 333 +#define _GUARD_BOTH_INT 334 +#define _GUARD_BOTH_UNICODE 335 +#define _GUARD_BUILTINS_VERSION 336 +#define _GUARD_DORV_VALUES 337 +#define _GUARD_DORV_VALUES_INST_ATTR_FROM_DICT 338 +#define _GUARD_GLOBALS_VERSION 339 +#define _GUARD_IS_FALSE_POP 340 +#define _GUARD_IS_NONE_POP 341 +#define _GUARD_IS_NOT_NONE_POP 342 +#define _GUARD_IS_TRUE_POP 343 +#define _GUARD_KEYS_VERSION 344 +#define _GUARD_NOT_EXHAUSTED_LIST 345 +#define _GUARD_NOT_EXHAUSTED_RANGE 346 +#define _GUARD_NOT_EXHAUSTED_TUPLE 347 +#define _GUARD_TYPE_VERSION 348 +#define _INIT_CALL_BOUND_METHOD_EXACT_ARGS 349 +#define _INIT_CALL_PY_EXACT_ARGS 350 +#define _INIT_CALL_PY_EXACT_ARGS_0 351 +#define _INIT_CALL_PY_EXACT_ARGS_1 352 +#define _INIT_CALL_PY_EXACT_ARGS_2 353 +#define _INIT_CALL_PY_EXACT_ARGS_3 354 +#define _INIT_CALL_PY_EXACT_ARGS_4 355 #define _INSTRUMENTED_CALL INSTRUMENTED_CALL #define _INSTRUMENTED_CALL_FUNCTION_EX INSTRUMENTED_CALL_FUNCTION_EX #define _INSTRUMENTED_CALL_KW INSTRUMENTED_CALL_KW @@ -139,65 +144,65 @@ extern "C" { #define _INSTRUMENTED_RETURN_CONST INSTRUMENTED_RETURN_CONST #define _INSTRUMENTED_RETURN_VALUE INSTRUMENTED_RETURN_VALUE #define _INSTRUMENTED_YIELD_VALUE INSTRUMENTED_YIELD_VALUE -#define _INTERNAL_INCREMENT_OPT_COUNTER 355 -#define _IS_NONE 356 +#define _INTERNAL_INCREMENT_OPT_COUNTER 356 +#define _IS_NONE 357 #define _IS_OP IS_OP -#define _ITER_CHECK_LIST 357 -#define _ITER_CHECK_RANGE 358 -#define _ITER_CHECK_TUPLE 359 -#define _ITER_JUMP_LIST 360 -#define _ITER_JUMP_RANGE 361 -#define _ITER_JUMP_TUPLE 362 -#define _ITER_NEXT_LIST 363 -#define _ITER_NEXT_RANGE 364 -#define _ITER_NEXT_TUPLE 365 -#define _JUMP_TO_TOP 366 +#define _ITER_CHECK_LIST 358 +#define _ITER_CHECK_RANGE 359 +#define _ITER_CHECK_TUPLE 360 +#define _ITER_JUMP_LIST 361 +#define _ITER_JUMP_RANGE 362 +#define _ITER_JUMP_TUPLE 363 +#define _ITER_NEXT_LIST 364 +#define _ITER_NEXT_RANGE 365 +#define _ITER_NEXT_TUPLE 366 +#define _JUMP_TO_TOP 367 #define _LIST_APPEND LIST_APPEND #define _LIST_EXTEND LIST_EXTEND #define _LOAD_ASSERTION_ERROR LOAD_ASSERTION_ERROR -#define _LOAD_ATTR 367 -#define _LOAD_ATTR_CLASS 368 -#define _LOAD_ATTR_CLASS_0 369 -#define _LOAD_ATTR_CLASS_1 370 +#define _LOAD_ATTR 368 +#define _LOAD_ATTR_CLASS 369 +#define _LOAD_ATTR_CLASS_0 370 +#define _LOAD_ATTR_CLASS_1 371 #define _LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN -#define _LOAD_ATTR_INSTANCE_VALUE 371 -#define _LOAD_ATTR_INSTANCE_VALUE_0 372 -#define _LOAD_ATTR_INSTANCE_VALUE_1 373 -#define _LOAD_ATTR_METHOD_LAZY_DICT 374 -#define _LOAD_ATTR_METHOD_NO_DICT 375 -#define _LOAD_ATTR_METHOD_WITH_VALUES 376 -#define _LOAD_ATTR_MODULE 377 -#define _LOAD_ATTR_NONDESCRIPTOR_NO_DICT 378 -#define _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 379 +#define _LOAD_ATTR_INSTANCE_VALUE 372 +#define _LOAD_ATTR_INSTANCE_VALUE_0 373 +#define _LOAD_ATTR_INSTANCE_VALUE_1 374 +#define _LOAD_ATTR_METHOD_LAZY_DICT 375 +#define _LOAD_ATTR_METHOD_NO_DICT 376 +#define _LOAD_ATTR_METHOD_WITH_VALUES 377 +#define _LOAD_ATTR_MODULE 378 +#define _LOAD_ATTR_NONDESCRIPTOR_NO_DICT 379 +#define _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 380 #define _LOAD_ATTR_PROPERTY LOAD_ATTR_PROPERTY -#define _LOAD_ATTR_SLOT 380 -#define _LOAD_ATTR_SLOT_0 381 -#define _LOAD_ATTR_SLOT_1 382 -#define _LOAD_ATTR_WITH_HINT 383 +#define _LOAD_ATTR_SLOT 381 +#define _LOAD_ATTR_SLOT_0 382 +#define _LOAD_ATTR_SLOT_1 383 +#define _LOAD_ATTR_WITH_HINT 384 #define _LOAD_BUILD_CLASS LOAD_BUILD_CLASS #define _LOAD_CONST LOAD_CONST -#define _LOAD_CONST_INLINE 384 -#define _LOAD_CONST_INLINE_BORROW 385 -#define _LOAD_CONST_INLINE_BORROW_WITH_NULL 386 -#define _LOAD_CONST_INLINE_WITH_NULL 387 +#define _LOAD_CONST_INLINE 385 +#define _LOAD_CONST_INLINE_BORROW 386 +#define _LOAD_CONST_INLINE_BORROW_WITH_NULL 387 +#define _LOAD_CONST_INLINE_WITH_NULL 388 #define _LOAD_DEREF LOAD_DEREF -#define _LOAD_FAST 388 -#define _LOAD_FAST_0 389 -#define _LOAD_FAST_1 390 -#define _LOAD_FAST_2 391 -#define _LOAD_FAST_3 392 -#define _LOAD_FAST_4 393 -#define _LOAD_FAST_5 394 -#define _LOAD_FAST_6 395 -#define _LOAD_FAST_7 396 +#define _LOAD_FAST 389 +#define _LOAD_FAST_0 390 +#define _LOAD_FAST_1 391 +#define _LOAD_FAST_2 392 +#define _LOAD_FAST_3 393 +#define _LOAD_FAST_4 394 +#define _LOAD_FAST_5 395 +#define _LOAD_FAST_6 396 +#define _LOAD_FAST_7 397 #define _LOAD_FAST_AND_CLEAR LOAD_FAST_AND_CLEAR #define _LOAD_FAST_CHECK LOAD_FAST_CHECK #define _LOAD_FAST_LOAD_FAST LOAD_FAST_LOAD_FAST #define _LOAD_FROM_DICT_OR_DEREF LOAD_FROM_DICT_OR_DEREF #define _LOAD_FROM_DICT_OR_GLOBALS LOAD_FROM_DICT_OR_GLOBALS -#define _LOAD_GLOBAL 397 -#define _LOAD_GLOBAL_BUILTINS 398 -#define _LOAD_GLOBAL_MODULE 399 +#define _LOAD_GLOBAL 398 +#define _LOAD_GLOBAL_BUILTINS 399 +#define _LOAD_GLOBAL_MODULE 400 #define _LOAD_LOCALS LOAD_LOCALS #define _LOAD_NAME LOAD_NAME #define _LOAD_SUPER_ATTR_ATTR LOAD_SUPER_ATTR_ATTR @@ -211,48 +216,48 @@ extern "C" { #define _MATCH_SEQUENCE MATCH_SEQUENCE #define _NOP NOP #define _POP_EXCEPT POP_EXCEPT -#define _POP_FRAME 400 -#define _POP_JUMP_IF_FALSE 401 -#define _POP_JUMP_IF_TRUE 402 +#define _POP_FRAME 401 +#define _POP_JUMP_IF_FALSE 402 +#define _POP_JUMP_IF_TRUE 403 #define _POP_TOP POP_TOP -#define _POP_TOP_LOAD_CONST_INLINE_BORROW 403 +#define _POP_TOP_LOAD_CONST_INLINE_BORROW 404 #define _PUSH_EXC_INFO PUSH_EXC_INFO -#define _PUSH_FRAME 404 +#define _PUSH_FRAME 405 #define _PUSH_NULL PUSH_NULL -#define _REPLACE_WITH_TRUE 405 +#define _REPLACE_WITH_TRUE 406 #define _RESUME_CHECK RESUME_CHECK -#define _SAVE_RETURN_OFFSET 406 -#define _SEND 407 +#define _SAVE_RETURN_OFFSET 407 +#define _SEND 408 #define _SEND_GEN SEND_GEN #define _SETUP_ANNOTATIONS SETUP_ANNOTATIONS #define _SET_ADD SET_ADD #define _SET_FUNCTION_ATTRIBUTE SET_FUNCTION_ATTRIBUTE #define _SET_UPDATE SET_UPDATE -#define _START_EXECUTOR 408 -#define _STORE_ATTR 409 -#define _STORE_ATTR_INSTANCE_VALUE 410 -#define _STORE_ATTR_SLOT 411 +#define _START_EXECUTOR 409 +#define _STORE_ATTR 410 +#define _STORE_ATTR_INSTANCE_VALUE 411 +#define _STORE_ATTR_SLOT 412 #define _STORE_ATTR_WITH_HINT STORE_ATTR_WITH_HINT #define _STORE_DEREF STORE_DEREF -#define _STORE_FAST 412 -#define _STORE_FAST_0 413 -#define _STORE_FAST_1 414 -#define _STORE_FAST_2 415 -#define _STORE_FAST_3 416 -#define _STORE_FAST_4 417 -#define _STORE_FAST_5 418 -#define _STORE_FAST_6 419 -#define _STORE_FAST_7 420 +#define _STORE_FAST 413 +#define _STORE_FAST_0 414 +#define _STORE_FAST_1 415 +#define _STORE_FAST_2 416 +#define _STORE_FAST_3 417 +#define _STORE_FAST_4 418 +#define _STORE_FAST_5 419 +#define _STORE_FAST_6 420 +#define _STORE_FAST_7 421 #define _STORE_FAST_LOAD_FAST STORE_FAST_LOAD_FAST #define _STORE_FAST_STORE_FAST STORE_FAST_STORE_FAST #define _STORE_GLOBAL STORE_GLOBAL #define _STORE_NAME STORE_NAME #define _STORE_SLICE STORE_SLICE -#define _STORE_SUBSCR 421 +#define _STORE_SUBSCR 422 #define _STORE_SUBSCR_DICT STORE_SUBSCR_DICT #define _STORE_SUBSCR_LIST_INT STORE_SUBSCR_LIST_INT #define _SWAP SWAP -#define _TO_BOOL 422 +#define _TO_BOOL 423 #define _TO_BOOL_BOOL TO_BOOL_BOOL #define _TO_BOOL_INT TO_BOOL_INT #define _TO_BOOL_LIST TO_BOOL_LIST @@ -262,12 +267,12 @@ extern "C" { #define _UNARY_NEGATIVE UNARY_NEGATIVE #define _UNARY_NOT UNARY_NOT #define _UNPACK_EX UNPACK_EX -#define _UNPACK_SEQUENCE 423 +#define _UNPACK_SEQUENCE 424 #define _UNPACK_SEQUENCE_LIST UNPACK_SEQUENCE_LIST #define _UNPACK_SEQUENCE_TUPLE UNPACK_SEQUENCE_TUPLE #define _UNPACK_SEQUENCE_TWO_TUPLE UNPACK_SEQUENCE_TWO_TUPLE #define _WITH_EXCEPT_START WITH_EXCEPT_START -#define MAX_UOP_ID 423 +#define MAX_UOP_ID 424 #ifdef __cplusplus } diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index c388fbfdcab54b..23aead27503a7f 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -154,6 +154,11 @@ 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, @@ -308,6 +313,11 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = { [_COMPARE_OP_INT] = "_COMPARE_OP_INT", [_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 fe969342ee79e7..b76ad8b2bf0291 100644 --- a/Include/opcode_ids.h +++ b/Include/opcode_ids.h @@ -163,42 +163,47 @@ extern "C" { #define COMPARE_OP_FLOAT 180 #define COMPARE_OP_INT 181 #define COMPARE_OP_STR 182 -#define FOR_ITER_GEN 183 -#define FOR_ITER_LIST 184 -#define FOR_ITER_RANGE 185 -#define FOR_ITER_TUPLE 186 -#define LOAD_ATTR_CLASS 187 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 188 -#define LOAD_ATTR_INSTANCE_VALUE 189 -#define LOAD_ATTR_METHOD_LAZY_DICT 190 -#define LOAD_ATTR_METHOD_NO_DICT 191 -#define LOAD_ATTR_METHOD_WITH_VALUES 192 -#define LOAD_ATTR_MODULE 193 -#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 194 -#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 195 -#define LOAD_ATTR_PROPERTY 196 -#define LOAD_ATTR_SLOT 197 -#define LOAD_ATTR_WITH_HINT 198 -#define LOAD_GLOBAL_BUILTIN 199 -#define LOAD_GLOBAL_MODULE 200 -#define LOAD_SUPER_ATTR_ATTR 201 -#define LOAD_SUPER_ATTR_METHOD 202 -#define RESUME_CHECK 203 -#define SEND_GEN 204 -#define STORE_ATTR_INSTANCE_VALUE 205 -#define STORE_ATTR_SLOT 206 -#define STORE_ATTR_WITH_HINT 207 -#define STORE_SUBSCR_DICT 208 -#define STORE_SUBSCR_LIST_INT 209 -#define TO_BOOL_ALWAYS_TRUE 210 -#define TO_BOOL_BOOL 211 -#define TO_BOOL_INT 212 -#define TO_BOOL_LIST 213 -#define TO_BOOL_NONE 214 -#define TO_BOOL_STR 215 -#define UNPACK_SEQUENCE_LIST 216 -#define UNPACK_SEQUENCE_TUPLE 217 -#define UNPACK_SEQUENCE_TWO_TUPLE 218 +#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 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 fdb099bd0c2ecf..90dfaea46ca0d6 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -75,6 +75,13 @@ "COMPARE_OP_INT", "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", "FOR_ITER_TUPLE", @@ -138,42 +145,47 @@ 'COMPARE_OP_FLOAT': 180, 'COMPARE_OP_INT': 181, 'COMPARE_OP_STR': 182, - 'FOR_ITER_GEN': 183, - 'FOR_ITER_LIST': 184, - 'FOR_ITER_RANGE': 185, - 'FOR_ITER_TUPLE': 186, - 'LOAD_ATTR_CLASS': 187, - 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 188, - 'LOAD_ATTR_INSTANCE_VALUE': 189, - 'LOAD_ATTR_METHOD_LAZY_DICT': 190, - 'LOAD_ATTR_METHOD_NO_DICT': 191, - 'LOAD_ATTR_METHOD_WITH_VALUES': 192, - 'LOAD_ATTR_MODULE': 193, - 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 194, - 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 195, - 'LOAD_ATTR_PROPERTY': 196, - 'LOAD_ATTR_SLOT': 197, - 'LOAD_ATTR_WITH_HINT': 198, - 'LOAD_GLOBAL_BUILTIN': 199, - 'LOAD_GLOBAL_MODULE': 200, - 'LOAD_SUPER_ATTR_ATTR': 201, - 'LOAD_SUPER_ATTR_METHOD': 202, - 'RESUME_CHECK': 203, - 'SEND_GEN': 204, - 'STORE_ATTR_INSTANCE_VALUE': 205, - 'STORE_ATTR_SLOT': 206, - 'STORE_ATTR_WITH_HINT': 207, - 'STORE_SUBSCR_DICT': 208, - 'STORE_SUBSCR_LIST_INT': 209, - 'TO_BOOL_ALWAYS_TRUE': 210, - 'TO_BOOL_BOOL': 211, - 'TO_BOOL_INT': 212, - 'TO_BOOL_LIST': 213, - 'TO_BOOL_NONE': 214, - 'TO_BOOL_STR': 215, - 'UNPACK_SEQUENCE_LIST': 216, - 'UNPACK_SEQUENCE_TUPLE': 217, - 'UNPACK_SEQUENCE_TWO_TUPLE': 218, + '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, } opmap = { diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 2a9aef03179f6f..46ddceed07b0d4 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -465,6 +465,7 @@ def _write_atomic(path, data, mode=0o666): # Python 3.13a1 3566 (Emit JUMP_NO_INTERRUPT instead of JUMP for non-loop no-lineno cases) # Python 3.13a1 3567 (Reimplement line number propagation by the compiler) # Python 3.13a1 3568 (Change semantics of END_FOR) +# Python 3.13a5 3569 (Specialize CONTAINS_OP) # Python 3.14 will start with 3600 @@ -481,7 +482,7 @@ def _write_atomic(path, data, mode=0o666): # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # in PC/launcher.c must also be updated. -MAGIC_NUMBER = (3568).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3569).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c diff --git a/Lib/opcode.py b/Lib/opcode.py index 88f4df7c0e8c38..5735686fa7fb44 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -56,6 +56,9 @@ "COMPARE_OP": { "counter": 1, }, + "CONTAINS_OP": { + "counter": 1, + }, "BINARY_SUBSCR": { "counter": 1, }, diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-03-05-22-00-58.gh-issue-116381.0Nq9iO.rst b/Misc/NEWS.d/next/Core and Builtins/2024-03-05-22-00-58.gh-issue-116381.0Nq9iO.rst new file mode 100644 index 00000000000000..8b4e63d1d9c9e3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-05-22-00-58.gh-issue-116381.0Nq9iO.rst @@ -0,0 +1 @@ +Add specialization for ``CONTAINS_OP``. diff --git a/Objects/listobject.c b/Objects/listobject.c index e013383a712bc8..db8eedf18c556e 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -524,8 +524,8 @@ list_length(PyObject *a) return PyList_GET_SIZE(a); } -static int -list_contains(PyObject *aa, PyObject *el) +int +_PyList_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 */ - list_contains, /* sq_contains */ + _PyList_Contains, /* sq_contains */ list_inplace_concat, /* sq_inplace_concat */ list_inplace_repeat, /* sq_inplace_repeat */ }; diff --git a/Objects/setobject.c b/Objects/setobject.c index 6a4c8c45f0836d..b4d803c19e1e94 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1987,8 +1987,8 @@ set_add(PySetObject *so, PyObject *key) Py_RETURN_NONE; } -static int -set_contains(PySetObject *so, PyObject *key) +int +_PySet_Contains(PySetObject *so, PyObject *key) { PyObject *tmpkey; int rv; @@ -2023,7 +2023,7 @@ set___contains__(PySetObject *so, PyObject *key) { long result; - result = set_contains(so, key); + result = _PySet_Contains(so, key); if (result < 0) return NULL; return PyBool_FromLong(result); @@ -2198,7 +2198,7 @@ static PySequenceMethods set_as_sequence = { 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ - (objobjproc)set_contains, /* sq_contains */ + (objobjproc)_PySet_Contains, /* sq_contains */ }; /* set object ********************************************************/ diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index d9dc00da368a84..5bbc0279c21d8d 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -349,8 +349,8 @@ tuplelength(PyTupleObject *a) return Py_SIZE(a); } -static int -tuplecontains(PyTupleObject *a, PyObject *el) +int +_PyTuple_Contains(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)tuplecontains, /* sq_contains */ + (objobjproc)_PyTuple_Contains, /* sq_contains */ }; static PyObject* diff --git a/Python/bytecodes.c b/Python/bytecodes.c index ad4ea4ea312a65..bf0583d9c69014 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2237,13 +2237,75 @@ dummy_func( b = res ? Py_True : Py_False; } - inst(CONTAINS_OP, (left, right -- b)) { + 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)) { int res = PySequence_Contains(right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; } + specializing op(_SPECIALIZE_CONTAINS_OP, (counter/1, left, right -- left, right)) { + #if ENABLE_SPECIALIZATION + if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { + next_instr = this_instr; + _Py_Specialize_ContainsOp(right, next_instr); + DISPATCH_SAME_OPARG(); + } + STAT_INC(CONTAINS_OP, deferred); + DECREMENT_ADAPTIVE_COUNTER(this_instr[1].cache); + #endif /* ENABLE_SPECIALIZATION */ + } + + 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)); + 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)); + 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 a057466b778195..4420c400e3a392 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2189,6 +2189,96 @@ 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; + PyObject *b; + oparg = CURRENT_OPARG(); + right = stack_pointer[-1]; + left = stack_pointer[-2]; + if (!PySet_CheckExact(right)) goto deoptimize; + int res = _PySet_Contains((PySetObject *)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_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; + PyObject *b; + oparg = CURRENT_OPARG(); + right = stack_pointer[-1]; + left = stack_pointer[-2]; + if (!PyDict_CheckExact(right)) goto deoptimize; + int res = PyDict_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_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 4947c919b4ca20..6e8ca82b6db9dd 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2127,14 +2127,144 @@ TARGET(CONTAINS_OP) { frame->instr_ptr = next_instr; - next_instr += 1; + next_instr += 2; INSTRUCTION_STATS(CONTAINS_OP); + PREDICTED(CONTAINS_OP); + _Py_CODEUNIT *this_instr = next_instr - 2; + (void)this_instr; + PyObject *right; + PyObject *left; + PyObject *b; + // _SPECIALIZE_CONTAINS_OP + right = stack_pointer[-1]; + left = stack_pointer[-2]; + { + uint16_t counter = read_u16(&this_instr[1].cache); + (void)counter; + #if ENABLE_SPECIALIZATION + if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { + next_instr = this_instr; + _Py_Specialize_ContainsOp(right, next_instr); + DISPATCH_SAME_OPARG(); + } + STAT_INC(CONTAINS_OP, deferred); + DECREMENT_ADAPTIVE_COUNTER(this_instr[1].cache); + #endif /* ENABLE_SPECIALIZATION */ + } + // _CONTAINS_OP + { + int res = PySequence_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_DICT) { + frame->instr_ptr = next_instr; + next_instr += 2; + INSTRUCTION_STATS(CONTAINS_OP_DICT); + 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(!PyDict_CheckExact(right), CONTAINS_OP); + int res = PyDict_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_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; + INSTRUCTION_STATS(CONTAINS_OP_SET); + 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(!PySet_CheckExact(right), CONTAINS_OP); + int res = _PySet_Contains((PySetObject *)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_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]; - int res = PySequence_Contains(right, left); + 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; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index e664e638bdb749..6b3846d46296ad 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -182,6 +182,11 @@ static void *opcode_targets[256] = { &&TARGET_COMPARE_OP_FLOAT, &&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, @@ -230,11 +235,6 @@ static void *opcode_targets[256] = { &&_unknown_opcode, &&_unknown_opcode, &&_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 a114b039e2db8d..a19010cc403681 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1206,6 +1206,51 @@ 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); + if (b == NULL) goto out_of_space; + stack_pointer[-2] = b; + stack_pointer += -1; + 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); + if (b == NULL) goto out_of_space; + stack_pointer[-2] = b; + stack_pointer += -1; + 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 f83d8a9ceb0282..5d339b8a476cf5 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -103,6 +103,7 @@ _Py_GetSpecializationStats(void) { return NULL; } int err = 0; + err += add_stat_dict(stats, CONTAINS_OP, "contains_op"); err += add_stat_dict(stats, LOAD_SUPER_ATTR, "load_super_attr"); err += add_stat_dict(stats, LOAD_ATTR, "load_attr"); err += add_stat_dict(stats, LOAD_GLOBAL, "load_global"); @@ -2561,6 +2562,43 @@ _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr) cache->counter = adaptive_counter_cooldown(); } +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)) { + instr->op.code = CONTAINS_OP_SET; + goto success; + } + + + STAT_INC(CONTAINS_OP, failure); + instr->op.code = CONTAINS_OP; + cache->counter = adaptive_counter_backoff(cache->counter); + return; +success: + STAT_INC(CONTAINS_OP, success); + cache->counter = adaptive_counter_cooldown(); +} + /* Code init cleanup. * CALL_ALLOC_AND_ENTER_INIT will set up * the frame to execute the EXIT_INIT_CHECK