https://github.com/python/cpython/commit/08e3389e8c433c16895127e8b745093f071b21b9
commit: 08e3389e8c433c16895127e8b745093f071b21b9
branch: main
author: Tomas R. <[email protected]>
committer: brandtbucher <[email protected]>
date: 2025-04-24T15:55:03-07:00
summary:
GH-131798: Split up and optimize CALL_TUPLE_1 in the JIT (GH-132851)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2025-04-23-20-54-17.gh-issue-131798.XYlp09.rst
M Include/internal/pycore_opcode_metadata.h
M Include/internal/pycore_uop_ids.h
M Include/internal/pycore_uop_metadata.h
M Lib/test/test_capi/test_opt.py
M Python/bytecodes.c
M Python/executor_cases.c.h
M Python/generated_cases.c.h
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h
diff --git a/Include/internal/pycore_opcode_metadata.h
b/Include/internal/pycore_opcode_metadata.h
index e276d109c6eb0d..c43d60175d5449 100644
--- a/Include/internal/pycore_opcode_metadata.h
+++ b/Include/internal/pycore_opcode_metadata.h
@@ -1353,7 +1353,7 @@ _PyOpcode_macro_expansion[256] = {
[CALL_PY_EXACT_ARGS] = { .nuops = 7, .uops = { { _CHECK_PEP_523,
OPARG_SIMPLE, 1 }, { _CHECK_FUNCTION_VERSION, 2, 1 }, {
_CHECK_FUNCTION_EXACT_ARGS, OPARG_SIMPLE, 3 }, { _CHECK_STACK_SPACE,
OPARG_SIMPLE, 3 }, { _INIT_CALL_PY_EXACT_ARGS, OPARG_SIMPLE, 3 }, {
_SAVE_RETURN_OFFSET, OPARG_SAVE_RETURN_OFFSET, 3 }, { _PUSH_FRAME,
OPARG_SIMPLE, 3 } } },
[CALL_PY_GENERAL] = { .nuops = 5, .uops = { { _CHECK_PEP_523,
OPARG_SIMPLE, 1 }, { _CHECK_FUNCTION_VERSION, 2, 1 }, { _PY_FRAME_GENERAL,
OPARG_SIMPLE, 3 }, { _SAVE_RETURN_OFFSET, OPARG_SAVE_RETURN_OFFSET, 3 }, {
_PUSH_FRAME, OPARG_SIMPLE, 3 } } },
[CALL_STR_1] = { .nuops = 4, .uops = { { _GUARD_NOS_NULL, OPARG_SIMPLE, 3
}, { _GUARD_CALLABLE_STR_1, OPARG_SIMPLE, 3 }, { _CALL_STR_1, OPARG_SIMPLE, 3
}, { _CHECK_PERIODIC, OPARG_SIMPLE, 3 } } },
- [CALL_TUPLE_1] = { .nuops = 2, .uops = { { _CALL_TUPLE_1, OPARG_SIMPLE, 3
}, { _CHECK_PERIODIC, OPARG_SIMPLE, 3 } } },
+ [CALL_TUPLE_1] = { .nuops = 4, .uops = { { _GUARD_NOS_NULL, OPARG_SIMPLE,
3 }, { _GUARD_CALLABLE_TUPLE_1, OPARG_SIMPLE, 3 }, { _CALL_TUPLE_1,
OPARG_SIMPLE, 3 }, { _CHECK_PERIODIC, OPARG_SIMPLE, 3 } } },
[CALL_TYPE_1] = { .nuops = 3, .uops = { { _GUARD_NOS_NULL, OPARG_SIMPLE, 3
}, { _GUARD_CALLABLE_TYPE_1, OPARG_SIMPLE, 3 }, { _CALL_TYPE_1, OPARG_SIMPLE, 3
} } },
[CHECK_EG_MATCH] = { .nuops = 1, .uops = { { _CHECK_EG_MATCH,
OPARG_SIMPLE, 0 } } },
[CHECK_EXC_MATCH] = { .nuops = 1, .uops = { { _CHECK_EXC_MATCH,
OPARG_SIMPLE, 0 } } },
diff --git a/Include/internal/pycore_uop_ids.h
b/Include/internal/pycore_uop_ids.h
index 98b8373ee2c649..d6d81f88c8e00c 100644
--- a/Include/internal/pycore_uop_ids.h
+++ b/Include/internal/pycore_uop_ids.h
@@ -117,44 +117,45 @@ extern "C" {
#define _GET_YIELD_FROM_ITER GET_YIELD_FROM_ITER
#define _GUARD_BINARY_OP_EXTEND 371
#define _GUARD_CALLABLE_STR_1 372
-#define _GUARD_CALLABLE_TYPE_1 373
-#define _GUARD_DORV_NO_DICT 374
-#define _GUARD_DORV_VALUES_INST_ATTR_FROM_DICT 375
-#define _GUARD_GLOBALS_VERSION 376
-#define _GUARD_IS_FALSE_POP 377
-#define _GUARD_IS_NONE_POP 378
-#define _GUARD_IS_NOT_NONE_POP 379
-#define _GUARD_IS_TRUE_POP 380
-#define _GUARD_KEYS_VERSION 381
-#define _GUARD_NOS_DICT 382
-#define _GUARD_NOS_FLOAT 383
-#define _GUARD_NOS_INT 384
-#define _GUARD_NOS_LIST 385
-#define _GUARD_NOS_NULL 386
-#define _GUARD_NOS_TUPLE 387
-#define _GUARD_NOS_UNICODE 388
-#define _GUARD_NOT_EXHAUSTED_LIST 389
-#define _GUARD_NOT_EXHAUSTED_RANGE 390
-#define _GUARD_NOT_EXHAUSTED_TUPLE 391
-#define _GUARD_TOS_ANY_SET 392
-#define _GUARD_TOS_DICT 393
-#define _GUARD_TOS_FLOAT 394
-#define _GUARD_TOS_INT 395
-#define _GUARD_TOS_LIST 396
-#define _GUARD_TOS_TUPLE 397
-#define _GUARD_TOS_UNICODE 398
-#define _GUARD_TYPE_VERSION 399
-#define _GUARD_TYPE_VERSION_AND_LOCK 400
+#define _GUARD_CALLABLE_TUPLE_1 373
+#define _GUARD_CALLABLE_TYPE_1 374
+#define _GUARD_DORV_NO_DICT 375
+#define _GUARD_DORV_VALUES_INST_ATTR_FROM_DICT 376
+#define _GUARD_GLOBALS_VERSION 377
+#define _GUARD_IS_FALSE_POP 378
+#define _GUARD_IS_NONE_POP 379
+#define _GUARD_IS_NOT_NONE_POP 380
+#define _GUARD_IS_TRUE_POP 381
+#define _GUARD_KEYS_VERSION 382
+#define _GUARD_NOS_DICT 383
+#define _GUARD_NOS_FLOAT 384
+#define _GUARD_NOS_INT 385
+#define _GUARD_NOS_LIST 386
+#define _GUARD_NOS_NULL 387
+#define _GUARD_NOS_TUPLE 388
+#define _GUARD_NOS_UNICODE 389
+#define _GUARD_NOT_EXHAUSTED_LIST 390
+#define _GUARD_NOT_EXHAUSTED_RANGE 391
+#define _GUARD_NOT_EXHAUSTED_TUPLE 392
+#define _GUARD_TOS_ANY_SET 393
+#define _GUARD_TOS_DICT 394
+#define _GUARD_TOS_FLOAT 395
+#define _GUARD_TOS_INT 396
+#define _GUARD_TOS_LIST 397
+#define _GUARD_TOS_TUPLE 398
+#define _GUARD_TOS_UNICODE 399
+#define _GUARD_TYPE_VERSION 400
+#define _GUARD_TYPE_VERSION_AND_LOCK 401
#define _IMPORT_FROM IMPORT_FROM
#define _IMPORT_NAME IMPORT_NAME
-#define _INIT_CALL_BOUND_METHOD_EXACT_ARGS 401
-#define _INIT_CALL_PY_EXACT_ARGS 402
-#define _INIT_CALL_PY_EXACT_ARGS_0 403
-#define _INIT_CALL_PY_EXACT_ARGS_1 404
-#define _INIT_CALL_PY_EXACT_ARGS_2 405
-#define _INIT_CALL_PY_EXACT_ARGS_3 406
-#define _INIT_CALL_PY_EXACT_ARGS_4 407
-#define _INSERT_NULL 408
+#define _INIT_CALL_BOUND_METHOD_EXACT_ARGS 402
+#define _INIT_CALL_PY_EXACT_ARGS 403
+#define _INIT_CALL_PY_EXACT_ARGS_0 404
+#define _INIT_CALL_PY_EXACT_ARGS_1 405
+#define _INIT_CALL_PY_EXACT_ARGS_2 406
+#define _INIT_CALL_PY_EXACT_ARGS_3 407
+#define _INIT_CALL_PY_EXACT_ARGS_4 408
+#define _INSERT_NULL 409
#define _INSTRUMENTED_FOR_ITER INSTRUMENTED_FOR_ITER
#define _INSTRUMENTED_INSTRUCTION INSTRUMENTED_INSTRUCTION
#define _INSTRUMENTED_JUMP_FORWARD INSTRUMENTED_JUMP_FORWARD
@@ -164,163 +165,163 @@ extern "C" {
#define _INSTRUMENTED_POP_JUMP_IF_NONE INSTRUMENTED_POP_JUMP_IF_NONE
#define _INSTRUMENTED_POP_JUMP_IF_NOT_NONE INSTRUMENTED_POP_JUMP_IF_NOT_NONE
#define _INSTRUMENTED_POP_JUMP_IF_TRUE INSTRUMENTED_POP_JUMP_IF_TRUE
-#define _IS_NONE 409
+#define _IS_NONE 410
#define _IS_OP IS_OP
-#define _ITER_CHECK_LIST 410
-#define _ITER_CHECK_RANGE 411
-#define _ITER_CHECK_TUPLE 412
-#define _ITER_JUMP_LIST 413
-#define _ITER_JUMP_RANGE 414
-#define _ITER_JUMP_TUPLE 415
-#define _ITER_NEXT_LIST 416
-#define _ITER_NEXT_LIST_TIER_TWO 417
-#define _ITER_NEXT_RANGE 418
-#define _ITER_NEXT_TUPLE 419
-#define _JUMP_TO_TOP 420
+#define _ITER_CHECK_LIST 411
+#define _ITER_CHECK_RANGE 412
+#define _ITER_CHECK_TUPLE 413
+#define _ITER_JUMP_LIST 414
+#define _ITER_JUMP_RANGE 415
+#define _ITER_JUMP_TUPLE 416
+#define _ITER_NEXT_LIST 417
+#define _ITER_NEXT_LIST_TIER_TWO 418
+#define _ITER_NEXT_RANGE 419
+#define _ITER_NEXT_TUPLE 420
+#define _JUMP_TO_TOP 421
#define _LIST_APPEND LIST_APPEND
#define _LIST_EXTEND LIST_EXTEND
-#define _LOAD_ATTR 421
-#define _LOAD_ATTR_CLASS 422
+#define _LOAD_ATTR 422
+#define _LOAD_ATTR_CLASS 423
#define _LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN
-#define _LOAD_ATTR_INSTANCE_VALUE 423
-#define _LOAD_ATTR_METHOD_LAZY_DICT 424
-#define _LOAD_ATTR_METHOD_NO_DICT 425
-#define _LOAD_ATTR_METHOD_WITH_VALUES 426
-#define _LOAD_ATTR_MODULE 427
-#define _LOAD_ATTR_NONDESCRIPTOR_NO_DICT 428
-#define _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 429
-#define _LOAD_ATTR_PROPERTY_FRAME 430
-#define _LOAD_ATTR_SLOT 431
-#define _LOAD_ATTR_WITH_HINT 432
+#define _LOAD_ATTR_INSTANCE_VALUE 424
+#define _LOAD_ATTR_METHOD_LAZY_DICT 425
+#define _LOAD_ATTR_METHOD_NO_DICT 426
+#define _LOAD_ATTR_METHOD_WITH_VALUES 427
+#define _LOAD_ATTR_MODULE 428
+#define _LOAD_ATTR_NONDESCRIPTOR_NO_DICT 429
+#define _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 430
+#define _LOAD_ATTR_PROPERTY_FRAME 431
+#define _LOAD_ATTR_SLOT 432
+#define _LOAD_ATTR_WITH_HINT 433
#define _LOAD_BUILD_CLASS LOAD_BUILD_CLASS
-#define _LOAD_BYTECODE 433
+#define _LOAD_BYTECODE 434
#define _LOAD_COMMON_CONSTANT LOAD_COMMON_CONSTANT
#define _LOAD_CONST LOAD_CONST
#define _LOAD_CONST_IMMORTAL LOAD_CONST_IMMORTAL
-#define _LOAD_CONST_INLINE 434
-#define _LOAD_CONST_INLINE_BORROW 435
+#define _LOAD_CONST_INLINE 435
+#define _LOAD_CONST_INLINE_BORROW 436
#define _LOAD_CONST_MORTAL LOAD_CONST_MORTAL
#define _LOAD_DEREF LOAD_DEREF
-#define _LOAD_FAST 436
-#define _LOAD_FAST_0 437
-#define _LOAD_FAST_1 438
-#define _LOAD_FAST_2 439
-#define _LOAD_FAST_3 440
-#define _LOAD_FAST_4 441
-#define _LOAD_FAST_5 442
-#define _LOAD_FAST_6 443
-#define _LOAD_FAST_7 444
+#define _LOAD_FAST 437
+#define _LOAD_FAST_0 438
+#define _LOAD_FAST_1 439
+#define _LOAD_FAST_2 440
+#define _LOAD_FAST_3 441
+#define _LOAD_FAST_4 442
+#define _LOAD_FAST_5 443
+#define _LOAD_FAST_6 444
+#define _LOAD_FAST_7 445
#define _LOAD_FAST_AND_CLEAR LOAD_FAST_AND_CLEAR
-#define _LOAD_FAST_BORROW 445
-#define _LOAD_FAST_BORROW_0 446
-#define _LOAD_FAST_BORROW_1 447
-#define _LOAD_FAST_BORROW_2 448
-#define _LOAD_FAST_BORROW_3 449
-#define _LOAD_FAST_BORROW_4 450
-#define _LOAD_FAST_BORROW_5 451
-#define _LOAD_FAST_BORROW_6 452
-#define _LOAD_FAST_BORROW_7 453
+#define _LOAD_FAST_BORROW 446
+#define _LOAD_FAST_BORROW_0 447
+#define _LOAD_FAST_BORROW_1 448
+#define _LOAD_FAST_BORROW_2 449
+#define _LOAD_FAST_BORROW_3 450
+#define _LOAD_FAST_BORROW_4 451
+#define _LOAD_FAST_BORROW_5 452
+#define _LOAD_FAST_BORROW_6 453
+#define _LOAD_FAST_BORROW_7 454
#define _LOAD_FAST_BORROW_LOAD_FAST_BORROW LOAD_FAST_BORROW_LOAD_FAST_BORROW
#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 454
-#define _LOAD_GLOBAL_BUILTINS 455
-#define _LOAD_GLOBAL_MODULE 456
+#define _LOAD_GLOBAL 455
+#define _LOAD_GLOBAL_BUILTINS 456
+#define _LOAD_GLOBAL_MODULE 457
#define _LOAD_LOCALS LOAD_LOCALS
#define _LOAD_NAME LOAD_NAME
-#define _LOAD_SMALL_INT 457
-#define _LOAD_SMALL_INT_0 458
-#define _LOAD_SMALL_INT_1 459
-#define _LOAD_SMALL_INT_2 460
-#define _LOAD_SMALL_INT_3 461
-#define _LOAD_SPECIAL 462
+#define _LOAD_SMALL_INT 458
+#define _LOAD_SMALL_INT_0 459
+#define _LOAD_SMALL_INT_1 460
+#define _LOAD_SMALL_INT_2 461
+#define _LOAD_SMALL_INT_3 462
+#define _LOAD_SPECIAL 463
#define _LOAD_SUPER_ATTR_ATTR LOAD_SUPER_ATTR_ATTR
#define _LOAD_SUPER_ATTR_METHOD LOAD_SUPER_ATTR_METHOD
-#define _MAKE_CALLARGS_A_TUPLE 463
+#define _MAKE_CALLARGS_A_TUPLE 464
#define _MAKE_CELL MAKE_CELL
#define _MAKE_FUNCTION MAKE_FUNCTION
-#define _MAKE_WARM 464
+#define _MAKE_WARM 465
#define _MAP_ADD MAP_ADD
#define _MATCH_CLASS MATCH_CLASS
#define _MATCH_KEYS MATCH_KEYS
#define _MATCH_MAPPING MATCH_MAPPING
#define _MATCH_SEQUENCE MATCH_SEQUENCE
-#define _MAYBE_EXPAND_METHOD 465
-#define _MAYBE_EXPAND_METHOD_KW 466
-#define _MONITOR_CALL 467
-#define _MONITOR_CALL_KW 468
-#define _MONITOR_JUMP_BACKWARD 469
-#define _MONITOR_RESUME 470
+#define _MAYBE_EXPAND_METHOD 466
+#define _MAYBE_EXPAND_METHOD_KW 467
+#define _MONITOR_CALL 468
+#define _MONITOR_CALL_KW 469
+#define _MONITOR_JUMP_BACKWARD 470
+#define _MONITOR_RESUME 471
#define _NOP NOP
#define _POP_EXCEPT POP_EXCEPT
-#define _POP_JUMP_IF_FALSE 471
-#define _POP_JUMP_IF_TRUE 472
+#define _POP_JUMP_IF_FALSE 472
+#define _POP_JUMP_IF_TRUE 473
#define _POP_TOP POP_TOP
-#define _POP_TOP_LOAD_CONST_INLINE 473
-#define _POP_TOP_LOAD_CONST_INLINE_BORROW 474
-#define _POP_TWO_LOAD_CONST_INLINE_BORROW 475
+#define _POP_TOP_LOAD_CONST_INLINE 474
+#define _POP_TOP_LOAD_CONST_INLINE_BORROW 475
+#define _POP_TWO_LOAD_CONST_INLINE_BORROW 476
#define _PUSH_EXC_INFO PUSH_EXC_INFO
-#define _PUSH_FRAME 476
+#define _PUSH_FRAME 477
#define _PUSH_NULL PUSH_NULL
-#define _PUSH_NULL_CONDITIONAL 477
-#define _PY_FRAME_GENERAL 478
-#define _PY_FRAME_KW 479
-#define _QUICKEN_RESUME 480
-#define _REPLACE_WITH_TRUE 481
+#define _PUSH_NULL_CONDITIONAL 478
+#define _PY_FRAME_GENERAL 479
+#define _PY_FRAME_KW 480
+#define _QUICKEN_RESUME 481
+#define _REPLACE_WITH_TRUE 482
#define _RESUME_CHECK RESUME_CHECK
#define _RETURN_GENERATOR RETURN_GENERATOR
#define _RETURN_VALUE RETURN_VALUE
-#define _SAVE_RETURN_OFFSET 482
-#define _SEND 483
-#define _SEND_GEN_FRAME 484
+#define _SAVE_RETURN_OFFSET 483
+#define _SEND 484
+#define _SEND_GEN_FRAME 485
#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 485
-#define _STORE_ATTR 486
-#define _STORE_ATTR_INSTANCE_VALUE 487
-#define _STORE_ATTR_SLOT 488
-#define _STORE_ATTR_WITH_HINT 489
+#define _START_EXECUTOR 486
+#define _STORE_ATTR 487
+#define _STORE_ATTR_INSTANCE_VALUE 488
+#define _STORE_ATTR_SLOT 489
+#define _STORE_ATTR_WITH_HINT 490
#define _STORE_DEREF STORE_DEREF
-#define _STORE_FAST 490
-#define _STORE_FAST_0 491
-#define _STORE_FAST_1 492
-#define _STORE_FAST_2 493
-#define _STORE_FAST_3 494
-#define _STORE_FAST_4 495
-#define _STORE_FAST_5 496
-#define _STORE_FAST_6 497
-#define _STORE_FAST_7 498
+#define _STORE_FAST 491
+#define _STORE_FAST_0 492
+#define _STORE_FAST_1 493
+#define _STORE_FAST_2 494
+#define _STORE_FAST_3 495
+#define _STORE_FAST_4 496
+#define _STORE_FAST_5 497
+#define _STORE_FAST_6 498
+#define _STORE_FAST_7 499
#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 499
-#define _STORE_SUBSCR 500
-#define _STORE_SUBSCR_DICT 501
-#define _STORE_SUBSCR_LIST_INT 502
+#define _STORE_SLICE 500
+#define _STORE_SUBSCR 501
+#define _STORE_SUBSCR_DICT 502
+#define _STORE_SUBSCR_LIST_INT 503
#define _SWAP SWAP
-#define _TIER2_RESUME_CHECK 503
-#define _TO_BOOL 504
+#define _TIER2_RESUME_CHECK 504
+#define _TO_BOOL 505
#define _TO_BOOL_BOOL TO_BOOL_BOOL
#define _TO_BOOL_INT TO_BOOL_INT
-#define _TO_BOOL_LIST 505
+#define _TO_BOOL_LIST 506
#define _TO_BOOL_NONE TO_BOOL_NONE
-#define _TO_BOOL_STR 506
+#define _TO_BOOL_STR 507
#define _UNARY_INVERT UNARY_INVERT
#define _UNARY_NEGATIVE UNARY_NEGATIVE
#define _UNARY_NOT UNARY_NOT
#define _UNPACK_EX UNPACK_EX
-#define _UNPACK_SEQUENCE 507
-#define _UNPACK_SEQUENCE_LIST 508
-#define _UNPACK_SEQUENCE_TUPLE 509
-#define _UNPACK_SEQUENCE_TWO_TUPLE 510
+#define _UNPACK_SEQUENCE 508
+#define _UNPACK_SEQUENCE_LIST 509
+#define _UNPACK_SEQUENCE_TUPLE 510
+#define _UNPACK_SEQUENCE_TWO_TUPLE 511
#define _WITH_EXCEPT_START WITH_EXCEPT_START
#define _YIELD_VALUE YIELD_VALUE
-#define MAX_UOP_ID 510
+#define MAX_UOP_ID 511
#ifdef __cplusplus
}
diff --git a/Include/internal/pycore_uop_metadata.h
b/Include/internal/pycore_uop_metadata.h
index c0a88278fbf4f3..0ea8e56b6cd0bc 100644
--- a/Include/internal/pycore_uop_metadata.h
+++ b/Include/internal/pycore_uop_metadata.h
@@ -246,7 +246,8 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = {
[_CALL_TYPE_1] = HAS_ARG_FLAG | HAS_ESCAPES_FLAG,
[_GUARD_CALLABLE_STR_1] = HAS_DEOPT_FLAG,
[_CALL_STR_1] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
- [_CALL_TUPLE_1] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG |
HAS_ESCAPES_FLAG,
+ [_GUARD_CALLABLE_TUPLE_1] = HAS_DEOPT_FLAG,
+ [_CALL_TUPLE_1] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
[_CHECK_AND_ALLOCATE_OBJECT] = HAS_ARG_FLAG | HAS_DEOPT_FLAG |
HAS_ERROR_FLAG | HAS_ERROR_NO_POP_FLAG | HAS_ESCAPES_FLAG,
[_CREATE_INIT_FRAME] = HAS_ARG_FLAG | HAS_ERROR_FLAG |
HAS_ERROR_NO_POP_FLAG | HAS_ESCAPES_FLAG,
[_EXIT_INIT_CHECK] = HAS_ERROR_FLAG | HAS_ERROR_NO_POP_FLAG |
HAS_ESCAPES_FLAG,
@@ -415,6 +416,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = {
[_GET_YIELD_FROM_ITER] = "_GET_YIELD_FROM_ITER",
[_GUARD_BINARY_OP_EXTEND] = "_GUARD_BINARY_OP_EXTEND",
[_GUARD_CALLABLE_STR_1] = "_GUARD_CALLABLE_STR_1",
+ [_GUARD_CALLABLE_TUPLE_1] = "_GUARD_CALLABLE_TUPLE_1",
[_GUARD_CALLABLE_TYPE_1] = "_GUARD_CALLABLE_TYPE_1",
[_GUARD_DORV_NO_DICT] = "_GUARD_DORV_NO_DICT",
[_GUARD_DORV_VALUES_INST_ATTR_FROM_DICT] =
"_GUARD_DORV_VALUES_INST_ATTR_FROM_DICT",
@@ -1052,6 +1054,8 @@ int _PyUop_num_popped(int opcode, int oparg)
return 0;
case _CALL_STR_1:
return 3;
+ case _GUARD_CALLABLE_TUPLE_1:
+ return 0;
case _CALL_TUPLE_1:
return 3;
case _CHECK_AND_ALLOCATE_OBJECT:
diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 67923c5bfa712e..0fb2d78a87ecf1 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -1854,6 +1854,63 @@ def testfunc(n):
self.assertNotIn("_TO_BOOL_STR", uops)
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
+ def test_call_tuple_1(self):
+ def testfunc(n):
+ x = 0
+ for _ in range(n):
+ y = tuple([1, 2]) # _CALL_TUPLE_1
+ if y == (1, 2):
+ x += 1
+ return x
+
+ res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+ self.assertIn("_CALL_TUPLE_1", uops)
+ self.assertNotIn("_GUARD_NOS_NULL", uops)
+ self.assertNotIn("_GUARD_CALLABLE_TUPLE_1", uops)
+
+ def test_call_tuple_1_result_is_tuple(self):
+ def testfunc(n):
+ x = 0
+ for _ in range(n):
+ y = tuple([1, 2]) # _CALL_TUPLE_1
+ if y[0] == 1: # _BINARY_OP_SUBSCR_TUPLE_INT
+ x += 1
+ return x
+
+ res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+ self.assertIn("_CALL_TUPLE_1", uops)
+ self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
+ self.assertNotIn("_GUARD_NOS_TUPLE", uops)
+
+ def test_call_tuple_1_result_propagates_for_tuple_input(self):
+ # Test a special case where the argument of tuple(arg)
+ # is known to be a tuple. The information about the
+ # argument being a tuple should be propagated to the
+ # result of tuple(arg).
+ def testfunc(n):
+ x = 0
+ for _ in range(n):
+ y = tuple((1, 2)) # tuple argument
+ a, _ = y # _UNPACK_SEQUENCE_TWO_TUPLE
+ if a == 1: # _COMPARE_OP_INT + _GUARD_IS_TRUE_POP are
removed
+ x += 1
+ return x
+
+ res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+ self.assertIn("_CALL_TUPLE_1", uops)
+ self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
+ self.assertNotIn("_COMPARE_OP_INT", uops)
+ self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
+
def global_identity(x):
return x
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-23-20-54-17.gh-issue-131798.XYlp09.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-23-20-54-17.gh-issue-131798.XYlp09.rst
new file mode 100644
index 00000000000000..d7db951433ea31
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-23-20-54-17.gh-issue-131798.XYlp09.rst
@@ -0,0 +1,2 @@
+Split ``CALL_TUPLE_1`` into several uops allowing the JIT to remove some of
+them. Patch by Tomas Roun
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 2ad1b3217f9612..f5c6e734a79960 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -4023,17 +4023,21 @@ dummy_func(
_CALL_STR_1 +
_CHECK_PERIODIC;
- op(_CALL_TUPLE_1, (callable, null, arg -- res)) {
+ op(_GUARD_CALLABLE_TUPLE_1, (callable, unused, unused -- callable,
unused, unused)) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
+ DEOPT_IF(callable_o != (PyObject *)&PyTuple_Type);
+ }
+
+ op(_CALL_TUPLE_1, (callable, null, arg -- res)) {
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
- DEOPT_IF(!PyStackRef_IsNull(null));
- DEOPT_IF(callable_o != (PyObject *)&PyTuple_Type);
STAT_INC(CALL, hit);
PyObject *res_o = PySequence_Tuple(arg_o);
DEAD(null);
DEAD(callable);
+ (void)callable; // Silence compiler warnings about unused variables
+ (void)null;
PyStackRef_CLOSE(arg);
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
@@ -4042,6 +4046,8 @@ dummy_func(
macro(CALL_TUPLE_1) =
unused/1 +
unused/2 +
+ _GUARD_NOS_NULL +
+ _GUARD_CALLABLE_TUPLE_1 +
_CALL_TUPLE_1 +
_CHECK_PERIODIC;
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index e5bba8ee959537..978662b241a01c 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -5215,6 +5215,17 @@
break;
}
+ case _GUARD_CALLABLE_TUPLE_1: {
+ _PyStackRef callable;
+ callable = stack_pointer[-3];
+ PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
+ if (callable_o != (PyObject *)&PyTuple_Type) {
+ UOP_STAT_INC(uopcode, miss);
+ JUMP_TO_JUMP_TARGET();
+ }
+ break;
+ }
+
case _CALL_TUPLE_1: {
_PyStackRef arg;
_PyStackRef null;
@@ -5224,21 +5235,14 @@
arg = stack_pointer[-1];
null = stack_pointer[-2];
callable = stack_pointer[-3];
- PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
- if (!PyStackRef_IsNull(null)) {
- UOP_STAT_INC(uopcode, miss);
- JUMP_TO_JUMP_TARGET();
- }
- if (callable_o != (PyObject *)&PyTuple_Type) {
- UOP_STAT_INC(uopcode, miss);
- JUMP_TO_JUMP_TARGET();
- }
STAT_INC(CALL, hit);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *res_o = PySequence_Tuple(arg_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
+ (void)callable;
+ (void)null;
stack_pointer += -3;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index fdd0750cf2a87d..48a446a0fe5486 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -4228,34 +4228,42 @@
next_instr += 4;
INSTRUCTION_STATS(CALL_TUPLE_1);
static_assert(INLINE_CACHE_ENTRIES_CALL == 3, "incorrect cache
size");
- _PyStackRef callable;
_PyStackRef null;
+ _PyStackRef callable;
_PyStackRef arg;
_PyStackRef res;
/* Skip 1 cache entry */
/* Skip 2 cache entries */
- // _CALL_TUPLE_1
+ // _GUARD_NOS_NULL
{
- arg = stack_pointer[-1];
null = stack_pointer[-2];
- callable = stack_pointer[-3];
- PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
- PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
- assert(oparg == 1);
if (!PyStackRef_IsNull(null)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
+ }
+ // _GUARD_CALLABLE_TUPLE_1
+ {
+ callable = stack_pointer[-3];
+ PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
if (callable_o != (PyObject *)&PyTuple_Type) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
+ }
+ // _CALL_TUPLE_1
+ {
+ arg = stack_pointer[-1];
+ PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
+ assert(oparg == 1);
STAT_INC(CALL, hit);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *res_o = PySequence_Tuple(arg_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
+ (void)callable;
+ (void)null;
stack_pointer += -3;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index 4f96140f8f706e..ff2830d3003c61 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -967,6 +967,16 @@ dummy_func(void) {
}
}
+ op(_CALL_TUPLE_1, (callable, null, arg -- res)) {
+ if (sym_matches_type(arg, &PyTuple_Type)) {
+ // e.g. tuple((1, 2)) or tuple(foo) where foo is known to be a
tuple
+ res = arg;
+ }
+ else {
+ res = sym_new_type(ctx, &PyTuple_Type);
+ }
+ }
+
op(_GUARD_TOS_LIST, (tos -- tos)) {
if (sym_matches_type(tos, &PyList_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
@@ -1031,6 +1041,13 @@ dummy_func(void) {
sym_set_const(callable, (PyObject *)&PyType_Type);
}
+ op(_GUARD_CALLABLE_TUPLE_1, (callable, unused, unused -- callable, unused,
unused)) {
+ if (sym_get_const(ctx, callable) == (PyObject *)&PyTuple_Type) {
+ REPLACE_OP(this_instr, _NOP, 0, 0);
+ }
+ sym_set_const(callable, (PyObject *)&PyTuple_Type);
+ }
+
op(_GUARD_CALLABLE_STR_1, (callable, unused, unused -- callable, unused,
unused)) {
if (sym_get_const(ctx, callable) == (PyObject *)&PyUnicode_Type) {
REPLACE_OP(this_instr, _NOP, 0, 0);
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 915dd642226892..303e402b759530 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -1918,9 +1918,26 @@
break;
}
+ case _GUARD_CALLABLE_TUPLE_1: {
+ JitOptSymbol *callable;
+ callable = stack_pointer[-3];
+ if (sym_get_const(ctx, callable) == (PyObject *)&PyTuple_Type) {
+ REPLACE_OP(this_instr, _NOP, 0, 0);
+ }
+ sym_set_const(callable, (PyObject *)&PyTuple_Type);
+ break;
+ }
+
case _CALL_TUPLE_1: {
+ JitOptSymbol *arg;
JitOptSymbol *res;
- res = sym_new_not_null(ctx);
+ arg = stack_pointer[-1];
+ if (sym_matches_type(arg, &PyTuple_Type)) {
+ res = arg;
+ }
+ else {
+ res = sym_new_type(ctx, &PyTuple_Type);
+ }
stack_pointer[-3] = res;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]