GH-113710: Tier 2 optimizer: check the function instead of checking globals. (GH-116410)

This commit is contained in:
Mark Shannon 2024-03-06 13:12:23 +00:00 committed by GitHub
parent 33c0aa3bb9
commit 27858e2a17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 137 additions and 149 deletions

View File

@ -59,22 +59,21 @@ extern "C" {
#define _CHECK_ATTR_METHOD_LAZY_DICT 313
#define _CHECK_ATTR_MODULE 314
#define _CHECK_ATTR_WITH_HINT 315
#define _CHECK_BUILTINS 316
#define _CHECK_CALL_BOUND_METHOD_EXACT_ARGS 317
#define _CHECK_CALL_BOUND_METHOD_EXACT_ARGS 316
#define _CHECK_EG_MATCH CHECK_EG_MATCH
#define _CHECK_EXC_MATCH CHECK_EXC_MATCH
#define _CHECK_FUNCTION 317
#define _CHECK_FUNCTION_EXACT_ARGS 318
#define _CHECK_GLOBALS 319
#define _CHECK_MANAGED_OBJECT_HAS_VALUES 320
#define _CHECK_PEP_523 321
#define _CHECK_STACK_SPACE 322
#define _CHECK_VALIDITY 323
#define _CHECK_VALIDITY_AND_SET_IP 324
#define _COLD_EXIT 325
#define _COMPARE_OP 326
#define _COMPARE_OP_FLOAT 327
#define _COMPARE_OP_INT 328
#define _COMPARE_OP_STR 329
#define _CHECK_MANAGED_OBJECT_HAS_VALUES 319
#define _CHECK_PEP_523 320
#define _CHECK_STACK_SPACE 321
#define _CHECK_VALIDITY 322
#define _CHECK_VALIDITY_AND_SET_IP 323
#define _COLD_EXIT 324
#define _COMPARE_OP 325
#define _COMPARE_OP_FLOAT 326
#define _COMPARE_OP_INT 327
#define _COMPARE_OP_STR 328
#define _CONTAINS_OP CONTAINS_OP
#define _CONVERT_VALUE CONVERT_VALUE
#define _COPY COPY
@ -89,41 +88,41 @@ extern "C" {
#define _DICT_UPDATE DICT_UPDATE
#define _END_SEND END_SEND
#define _EXIT_INIT_CHECK EXIT_INIT_CHECK
#define _FATAL_ERROR 330
#define _FATAL_ERROR 329
#define _FORMAT_SIMPLE FORMAT_SIMPLE
#define _FORMAT_WITH_SPEC FORMAT_WITH_SPEC
#define _FOR_ITER 331
#define _FOR_ITER 330
#define _FOR_ITER_GEN FOR_ITER_GEN
#define _FOR_ITER_TIER_TWO 332
#define _FOR_ITER_TIER_TWO 331
#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 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 _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 _INSTRUMENTED_CALL INSTRUMENTED_CALL
#define _INSTRUMENTED_CALL_FUNCTION_EX INSTRUMENTED_CALL_FUNCTION_EX
#define _INSTRUMENTED_CALL_KW INSTRUMENTED_CALL_KW
@ -140,65 +139,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 356
#define _IS_NONE 357
#define _INTERNAL_INCREMENT_OPT_COUNTER 355
#define _IS_NONE 356
#define _IS_OP IS_OP
#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 _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 _LIST_APPEND LIST_APPEND
#define _LIST_EXTEND LIST_EXTEND
#define _LOAD_ASSERTION_ERROR LOAD_ASSERTION_ERROR
#define _LOAD_ATTR 368
#define _LOAD_ATTR_CLASS 369
#define _LOAD_ATTR_CLASS_0 370
#define _LOAD_ATTR_CLASS_1 371
#define _LOAD_ATTR 367
#define _LOAD_ATTR_CLASS 368
#define _LOAD_ATTR_CLASS_0 369
#define _LOAD_ATTR_CLASS_1 370
#define _LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN
#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_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_PROPERTY LOAD_ATTR_PROPERTY
#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_ATTR_SLOT 380
#define _LOAD_ATTR_SLOT_0 381
#define _LOAD_ATTR_SLOT_1 382
#define _LOAD_ATTR_WITH_HINT 383
#define _LOAD_BUILD_CLASS LOAD_BUILD_CLASS
#define _LOAD_CONST LOAD_CONST
#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_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_DEREF LOAD_DEREF
#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 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_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 398
#define _LOAD_GLOBAL_BUILTINS 399
#define _LOAD_GLOBAL_MODULE 400
#define _LOAD_GLOBAL 397
#define _LOAD_GLOBAL_BUILTINS 398
#define _LOAD_GLOBAL_MODULE 399
#define _LOAD_LOCALS LOAD_LOCALS
#define _LOAD_NAME LOAD_NAME
#define _LOAD_SUPER_ATTR_ATTR LOAD_SUPER_ATTR_ATTR
@ -212,48 +211,48 @@ extern "C" {
#define _MATCH_SEQUENCE MATCH_SEQUENCE
#define _NOP NOP
#define _POP_EXCEPT POP_EXCEPT
#define _POP_FRAME 401
#define _POP_JUMP_IF_FALSE 402
#define _POP_JUMP_IF_TRUE 403
#define _POP_FRAME 400
#define _POP_JUMP_IF_FALSE 401
#define _POP_JUMP_IF_TRUE 402
#define _POP_TOP POP_TOP
#define _POP_TOP_LOAD_CONST_INLINE_BORROW 404
#define _POP_TOP_LOAD_CONST_INLINE_BORROW 403
#define _PUSH_EXC_INFO PUSH_EXC_INFO
#define _PUSH_FRAME 405
#define _PUSH_FRAME 404
#define _PUSH_NULL PUSH_NULL
#define _REPLACE_WITH_TRUE 406
#define _REPLACE_WITH_TRUE 405
#define _RESUME_CHECK RESUME_CHECK
#define _SAVE_RETURN_OFFSET 407
#define _SEND 408
#define _SAVE_RETURN_OFFSET 406
#define _SEND 407
#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 409
#define _STORE_ATTR 410
#define _STORE_ATTR_INSTANCE_VALUE 411
#define _STORE_ATTR_SLOT 412
#define _START_EXECUTOR 408
#define _STORE_ATTR 409
#define _STORE_ATTR_INSTANCE_VALUE 410
#define _STORE_ATTR_SLOT 411
#define _STORE_ATTR_WITH_HINT STORE_ATTR_WITH_HINT
#define _STORE_DEREF STORE_DEREF
#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 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_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 422
#define _STORE_SUBSCR 421
#define _STORE_SUBSCR_DICT STORE_SUBSCR_DICT
#define _STORE_SUBSCR_LIST_INT STORE_SUBSCR_LIST_INT
#define _SWAP SWAP
#define _TO_BOOL 423
#define _TO_BOOL 422
#define _TO_BOOL_BOOL TO_BOOL_BOOL
#define _TO_BOOL_INT TO_BOOL_INT
#define _TO_BOOL_LIST TO_BOOL_LIST
@ -263,12 +262,12 @@ extern "C" {
#define _UNARY_NEGATIVE UNARY_NEGATIVE
#define _UNARY_NOT UNARY_NOT
#define _UNPACK_EX UNPACK_EX
#define _UNPACK_SEQUENCE 424
#define _UNPACK_SEQUENCE 423
#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 424
#define MAX_UOP_ID 423
#ifdef __cplusplus
}

View File

@ -235,8 +235,7 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = {
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = HAS_PURE_FLAG,
[_LOAD_CONST_INLINE_WITH_NULL] = HAS_PURE_FLAG,
[_LOAD_CONST_INLINE_BORROW_WITH_NULL] = HAS_PURE_FLAG,
[_CHECK_GLOBALS] = HAS_DEOPT_FLAG,
[_CHECK_BUILTINS] = HAS_DEOPT_FLAG,
[_CHECK_FUNCTION] = HAS_DEOPT_FLAG,
[_INTERNAL_INCREMENT_OPT_COUNTER] = 0,
[_COLD_EXIT] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
[_START_EXECUTOR] = 0,
@ -293,12 +292,11 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = {
[_CHECK_ATTR_METHOD_LAZY_DICT] = "_CHECK_ATTR_METHOD_LAZY_DICT",
[_CHECK_ATTR_MODULE] = "_CHECK_ATTR_MODULE",
[_CHECK_ATTR_WITH_HINT] = "_CHECK_ATTR_WITH_HINT",
[_CHECK_BUILTINS] = "_CHECK_BUILTINS",
[_CHECK_CALL_BOUND_METHOD_EXACT_ARGS] = "_CHECK_CALL_BOUND_METHOD_EXACT_ARGS",
[_CHECK_EG_MATCH] = "_CHECK_EG_MATCH",
[_CHECK_EXC_MATCH] = "_CHECK_EXC_MATCH",
[_CHECK_FUNCTION] = "_CHECK_FUNCTION",
[_CHECK_FUNCTION_EXACT_ARGS] = "_CHECK_FUNCTION_EXACT_ARGS",
[_CHECK_GLOBALS] = "_CHECK_GLOBALS",
[_CHECK_MANAGED_OBJECT_HAS_VALUES] = "_CHECK_MANAGED_OBJECT_HAS_VALUES",
[_CHECK_PEP_523] = "_CHECK_PEP_523",
[_CHECK_STACK_SPACE] = "_CHECK_STACK_SPACE",

View File

@ -4058,12 +4058,8 @@ dummy_func(
null = NULL;
}
tier2 op(_CHECK_GLOBALS, (dict/4 -- )) {
DEOPT_IF(GLOBALS() != dict);
}
tier2 op(_CHECK_BUILTINS, (dict/4 -- )) {
DEOPT_IF(BUILTINS() != dict);
tier2 op(_CHECK_FUNCTION, (func/4 -- )) {
DEOPT_IF(frame->f_funcobj != func);
}
/* Internal -- for testing executors */

View File

@ -3789,15 +3789,9 @@
break;
}
case _CHECK_GLOBALS: {
PyObject *dict = (PyObject *)CURRENT_OPERAND();
if (GLOBALS() != dict) goto deoptimize;
break;
}
case _CHECK_BUILTINS: {
PyObject *dict = (PyObject *)CURRENT_OPERAND();
if (BUILTINS() != dict) goto deoptimize;
case _CHECK_FUNCTION: {
PyObject *func = (PyObject *)CURRENT_OPERAND();
if (frame->f_funcobj != func) goto deoptimize;
break;
}

View File

@ -154,10 +154,10 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
/* These values represent stacks of booleans (one bool per bit).
* Pushing a frame shifts left, popping a frame shifts right. */
uint32_t builtins_checked = 0;
uint32_t function_checked = 0;
uint32_t builtins_watched = 0;
uint32_t globals_checked = 0;
uint32_t globals_watched = 0;
uint32_t prechecked_function_version = 0;
if (interp->dict_state.watchers[GLOBALS_WATCHER_ID] == NULL) {
interp->dict_state.watchers[GLOBALS_WATCHER_ID] = globals_watcher_callback;
}
@ -176,13 +176,13 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
PyDict_Watch(BUILTINS_WATCHER_ID, builtins);
builtins_watched |= 1;
}
if (builtins_checked & 1) {
if (function_checked & 1) {
buffer[pc].opcode = NOP;
}
else {
buffer[pc].opcode = _CHECK_BUILTINS;
buffer[pc].opcode = _CHECK_FUNCTION;
buffer[pc].operand = (uintptr_t)builtins;
builtins_checked |= 1;
function_checked |= 1;
}
break;
case _GUARD_GLOBALS_VERSION:
@ -198,36 +198,39 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
_Py_BloomFilter_Add(dependencies, globals);
globals_watched |= 1;
}
if (globals_checked & 1) {
if (function_checked & 1) {
buffer[pc].opcode = NOP;
}
else {
buffer[pc].opcode = _CHECK_GLOBALS;
buffer[pc].opcode = _CHECK_FUNCTION;
buffer[pc].operand = (uintptr_t)globals;
globals_checked |= 1;
function_checked |= 1;
}
break;
case _LOAD_GLOBAL_BUILTINS:
if (globals_checked & builtins_checked & globals_watched & builtins_watched & 1) {
if (function_checked & globals_watched & builtins_watched & 1) {
convert_global_to_const(inst, builtins);
}
break;
case _LOAD_GLOBAL_MODULE:
if (globals_checked & globals_watched & 1) {
if (function_checked & globals_watched & 1) {
convert_global_to_const(inst, globals);
}
break;
case _PUSH_FRAME:
{
globals_checked <<= 1;
globals_watched <<= 1;
builtins_checked <<= 1;
builtins_watched <<= 1;
globals_watched <<= 1;
function_checked <<= 1;
PyFunctionObject *func = (PyFunctionObject *)buffer[pc].operand;
if (func == NULL) {
return 1;
}
assert(PyFunction_Check(func));
if (prechecked_function_version == func->func_version) {
function_checked |= 1;
}
prechecked_function_version = 0;
globals = func->func_globals;
builtins = func->func_builtins;
if (builtins != interp->builtins) {
@ -237,16 +240,18 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
}
case _POP_FRAME:
{
globals_checked >>= 1;
globals_watched >>= 1;
builtins_checked >>= 1;
builtins_watched >>= 1;
globals_watched >>= 1;
function_checked >>= 1;
PyFunctionObject *func = (PyFunctionObject *)buffer[pc].operand;
assert(PyFunction_Check(func));
globals = func->func_globals;
builtins = func->func_builtins;
break;
}
case _CHECK_FUNCTION_EXACT_ARGS:
prechecked_function_version = (uint32_t)buffer[pc].operand;
break;
default:
if (op_is_end(opcode)) {
return 1;

View File

@ -1951,11 +1951,7 @@
break;
}
case _CHECK_GLOBALS: {
break;
}
case _CHECK_BUILTINS: {
case _CHECK_FUNCTION: {
break;
}