GH-131798: Split up and optimize CALL_TUPLE_1 in the JIT (GH-132851)

This commit is contained in:
Tomas R. 2025-04-25 00:55:03 +02:00 committed by GitHub
parent 15ff60aff0
commit 08e3389e8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 277 additions and 161 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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());