GH-131798: Split CALL_LIST_APPEND into several uops (GH-134240)

This commit is contained in:
Diego Russo 2025-05-19 15:48:55 -04:00 committed by GitHub
parent 92f85ff3a0
commit 42d03f3933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 370 additions and 259 deletions

View file

@ -4041,6 +4041,11 @@ dummy_func(
DEOPT_IF(!PyStackRef_IsNull(null));
}
op(_GUARD_NOS_NOT_NULL, (nos, unused -- nos, unused)) {
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
EXIT_IF(o == NULL);
}
op(_GUARD_THIRD_NULL, (null, unused, unused -- null, unused, unused)) {
DEOPT_IF(!PyStackRef_IsNull(null));
}
@ -4394,16 +4399,26 @@ dummy_func(
_GUARD_CALLABLE_ISINSTANCE +
_CALL_ISINSTANCE;
// This is secretly a super-instruction
inst(CALL_LIST_APPEND, (unused/1, unused/2, callable, self, arg -- )) {
assert(oparg == 1);
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyObject *self_o = PyStackRef_AsPyObjectBorrow(self);
macro(CALL_LIST_APPEND) =
unused/1 +
unused/2 +
_GUARD_CALLABLE_LIST_APPEND +
_GUARD_NOS_NOT_NULL +
_GUARD_NOS_LIST +
_CALL_LIST_APPEND;
op(_GUARD_CALLABLE_LIST_APPEND, (callable, unused, unused -- callable, unused, unused)){
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyInterpreterState *interp = tstate->interp;
DEOPT_IF(callable_o != interp->callable_cache.list_append);
DEOPT_IF(self_o == NULL);
DEOPT_IF(!PyList_Check(self_o));
}
// This is secretly a super-instruction
op(_CALL_LIST_APPEND, (callable, self, arg -- )) {
assert(oparg == 1);
PyObject *self_o = PyStackRef_AsPyObjectBorrow(self);
DEOPT_IF(!PyList_CheckExact(self_o));
DEOPT_IF(!LOCK_OBJECT(self_o));
STAT_INC(CALL, hit);
int err = _PyList_AppendTakeRef((PyListObject *)self_o, PyStackRef_AsPyObjectSteal(arg));

View file

@ -5276,6 +5276,17 @@
break;
}
case _GUARD_NOS_NOT_NULL: {
_PyStackRef nos;
nos = stack_pointer[-2];
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
if (o == NULL) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
break;
}
case _GUARD_THIRD_NULL: {
_PyStackRef null;
null = stack_pointer[-3];
@ -5920,6 +5931,18 @@
break;
}
case _GUARD_CALLABLE_LIST_APPEND: {
_PyStackRef callable;
callable = stack_pointer[-3];
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyInterpreterState *interp = tstate->interp;
if (callable_o != interp->callable_cache.list_append) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
break;
}
case _CALL_LIST_APPEND: {
_PyStackRef arg;
_PyStackRef self;
@ -5929,18 +5952,8 @@
self = stack_pointer[-2];
callable = stack_pointer[-3];
assert(oparg == 1);
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyObject *self_o = PyStackRef_AsPyObjectBorrow(self);
PyInterpreterState *interp = tstate->interp;
if (callable_o != interp->callable_cache.list_append) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
if (self_o == NULL) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
if (!PyList_Check(self_o)) {
if (!PyList_CheckExact(self_o)) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}

View file

@ -3475,58 +3475,79 @@
INSTRUCTION_STATS(CALL_LIST_APPEND);
static_assert(INLINE_CACHE_ENTRIES_CALL == 3, "incorrect cache size");
_PyStackRef callable;
_PyStackRef nos;
_PyStackRef self;
_PyStackRef arg;
/* Skip 1 cache entry */
/* Skip 2 cache entries */
arg = stack_pointer[-1];
self = stack_pointer[-2];
callable = stack_pointer[-3];
assert(oparg == 1);
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyObject *self_o = PyStackRef_AsPyObjectBorrow(self);
PyInterpreterState *interp = tstate->interp;
if (callable_o != interp->callable_cache.list_append) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
// _GUARD_CALLABLE_LIST_APPEND
{
callable = stack_pointer[-3];
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
PyInterpreterState *interp = tstate->interp;
if (callable_o != interp->callable_cache.list_append) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
}
if (self_o == NULL) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
// _GUARD_NOS_NOT_NULL
{
nos = stack_pointer[-2];
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
if (o == NULL) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
}
if (!PyList_Check(self_o)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
// _GUARD_NOS_LIST
{
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
if (!PyList_CheckExact(o)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
}
if (!LOCK_OBJECT(self_o)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
STAT_INC(CALL, hit);
int err = _PyList_AppendTakeRef((PyListObject *)self_o, PyStackRef_AsPyObjectSteal(arg));
UNLOCK_OBJECT(self_o);
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(self);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(callable);
stack_pointer = _PyFrame_GetStackPointer(frame);
if (err) {
JUMP_TO_LABEL(error);
}
#if TIER_ONE
// _CALL_LIST_APPEND
{
arg = stack_pointer[-1];
self = nos;
assert(oparg == 1);
PyObject *self_o = PyStackRef_AsPyObjectBorrow(self);
if (!PyList_CheckExact(self_o)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
if (!LOCK_OBJECT(self_o)) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
}
STAT_INC(CALL, hit);
int err = _PyList_AppendTakeRef((PyListObject *)self_o, PyStackRef_AsPyObjectSteal(arg));
UNLOCK_OBJECT(self_o);
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(self);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(callable);
stack_pointer = _PyFrame_GetStackPointer(frame);
if (err) {
JUMP_TO_LABEL(error);
}
#if TIER_ONE
assert(next_instr->op.code == POP_TOP);
SKIP_OVER(1);
#endif
assert(next_instr->op.code == POP_TOP);
SKIP_OVER(1);
#endif
}
DISPATCH();
}

View file

@ -1087,6 +1087,13 @@ dummy_func(void) {
sym_set_null(null);
}
op(_GUARD_NOS_NOT_NULL, (nos, unused -- nos, unused)) {
if (sym_is_not_null(nos)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_non_null(nos);
}
op(_GUARD_THIRD_NULL, (null, unused, unused -- null, unused, unused)) {
if (sym_is_null(null)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
@ -1135,6 +1142,14 @@ dummy_func(void) {
sym_set_const(callable, isinstance);
}
op(_GUARD_CALLABLE_LIST_APPEND, (callable, unused, unused -- callable, unused, unused)) {
PyObject *list_append = _PyInterpreterState_GET()->callable_cache.list_append;
if (sym_get_const(ctx, callable) == list_append) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_const(callable, list_append);
}
// END BYTECODES //
}

View file

@ -1935,6 +1935,16 @@
break;
}
case _GUARD_NOS_NOT_NULL: {
JitOptSymbol *nos;
nos = stack_pointer[-2];
if (sym_is_not_null(nos)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_non_null(nos);
break;
}
case _GUARD_THIRD_NULL: {
JitOptSymbol *null;
null = stack_pointer[-3];
@ -2146,6 +2156,17 @@
break;
}
case _GUARD_CALLABLE_LIST_APPEND: {
JitOptSymbol *callable;
callable = stack_pointer[-3];
PyObject *list_append = _PyInterpreterState_GET()->callable_cache.list_append;
if (sym_get_const(ctx, callable) == list_append) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_const(callable, list_append);
break;
}
case _CALL_LIST_APPEND: {
stack_pointer += -3;
assert(WITHIN_STACK_BOUNDS());