gh-134584: Eliminate redundant refcounting from _CALL_TYPE_1 (GH-135818)

This commit is contained in:
Tomas R. 2025-12-23 18:01:10 +01:00 committed by GitHub
parent c8b80f5e23
commit 25c294b6ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1047 additions and 819 deletions

View file

@ -3986,17 +3986,14 @@ dummy_func(
DEOPT_IF(callable_o != (PyObject *)&PyType_Type);
}
op(_CALL_TYPE_1, (callable, null, arg -- res)) {
op(_CALL_TYPE_1, (callable, null, arg -- res, a)) {
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
DEAD(null);
DEAD(callable);
(void)callable; // Silence compiler warnings about unused variables
(void)null;
STAT_INC(CALL, hit);
a = arg;
INPUTS_DEAD();
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
PyStackRef_CLOSE(arg);
}
macro(CALL_TYPE_1) =
@ -4004,7 +4001,8 @@ dummy_func(
unused/2 +
_GUARD_NOS_NULL +
_GUARD_CALLABLE_TYPE_1 +
_CALL_TYPE_1;
_CALL_TYPE_1 +
POP_TOP;
op(_GUARD_CALLABLE_STR_1, (callable, unused, unused -- callable, unused, unused)) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
@ -5258,6 +5256,12 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(ptr);
}
tier2 op(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
INPUTS_DEAD();
}
tier2 op(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a, c)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;

View file

@ -12810,38 +12810,94 @@
break;
}
case _CALL_TYPE_1_r31: {
case _CALL_TYPE_1_r02: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
oparg = CURRENT_OPARG();
arg = stack_pointer[-1];
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
STAT_INC(CALL, hit);
a = arg;
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -3;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _CALL_TYPE_1_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
oparg = CURRENT_OPARG();
arg = _stack_item_0;
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
STAT_INC(CALL, hit);
a = arg;
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _CALL_TYPE_1_r22: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
oparg = CURRENT_OPARG();
arg = _stack_item_1;
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
STAT_INC(CALL, hit);
a = arg;
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _CALL_TYPE_1_r32: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef null;
_PyStackRef callable;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
oparg = CURRENT_OPARG();
arg = _stack_item_2;
null = _stack_item_1;
callable = _stack_item_0;
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
(void)callable;
(void)null;
STAT_INC(CALL, hit);
a = arg;
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
stack_pointer[0] = res;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(arg);
stack_pointer = _PyFrame_GetStackPointer(frame);
_tos_cache1 = a;
_tos_cache0 = res;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
SET_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
@ -16599,6 +16655,86 @@
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r02: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
arg = stack_pointer[-1];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -3;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
arg = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r22: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
arg = _stack_item_1;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r32: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
arg = _stack_item_2;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r03: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());

View file

@ -4144,6 +4144,8 @@
_PyStackRef callable;
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef value;
/* Skip 1 cache entry */
/* Skip 2 cache entries */
// _GUARD_NOS_NULL
@ -4170,15 +4172,18 @@
arg = stack_pointer[-1];
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
assert(oparg == 1);
(void)callable;
(void)null;
STAT_INC(CALL, hit);
a = arg;
res = PyStackRef_FromPyObjectNew(Py_TYPE(arg_o));
}
// _POP_TOP
{
value = a;
stack_pointer[-3] = res;
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(arg);
PyStackRef_XCLOSE(value);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
DISPATCH();

View file

@ -446,7 +446,9 @@ const uint16_t op_without_push[MAX_UOP_ID + 1] = {
[_POP_TOP_LOAD_CONST_INLINE] = _POP_TOP,
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _POP_TOP,
[_POP_TWO_LOAD_CONST_INLINE_BORROW] = _POP_TWO,
[_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW] = _POP_CALL_ONE,
[_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW] = _POP_CALL_TWO,
[_SHUFFLE_2_LOAD_CONST_INLINE_BORROW] = _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
};
const bool op_skip[MAX_UOP_ID + 1] = {
@ -458,6 +460,10 @@ const bool op_skip[MAX_UOP_ID + 1] = {
const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
[_POP_TOP] = _NOP,
[_POP_TOP_NOP] = _NOP,
[_POP_TOP_INT] = _NOP,
[_POP_TOP_FLOAT] = _NOP,
[_POP_TOP_UNICODE] = _NOP,
[_POP_TOP_LOAD_CONST_INLINE] = _LOAD_CONST_INLINE,
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _LOAD_CONST_INLINE_BORROW,
[_POP_TWO] = _POP_TOP,

View file

@ -533,6 +533,11 @@ dummy_func(void) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}
op(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, arg -- res, a)) {
res = PyJitRef_Borrow(sym_new_const(ctx, ptr));
a = arg;
}
op(_POP_TOP, (value -- )) {
PyTypeObject *typ = sym_get_type(value);
if (PyJitRef_IsBorrowed(value) ||
@ -981,16 +986,17 @@ dummy_func(void) {
next = sym_new_type(ctx, &PyLong_Type);
}
op(_CALL_TYPE_1, (unused, unused, arg -- res)) {
op(_CALL_TYPE_1, (unused, unused, arg -- res, a)) {
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, 0,
REPLACE_OP(this_instr, _SHUFFLE_2_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
}
else {
res = sym_new_not_null(ctx);
}
a = arg;
}
op(_CALL_STR_1, (unused, unused, arg -- res, a)) {

View file

@ -2680,19 +2680,22 @@
case _CALL_TYPE_1: {
JitOptRef arg;
JitOptRef res;
JitOptRef a;
arg = stack_pointer[-1];
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, 0,
REPLACE_OP(this_instr, _SHUFFLE_2_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
}
else {
res = sym_new_not_null(ctx);
}
CHECK_STACK_BOUNDS(-2);
a = arg;
CHECK_STACK_BOUNDS(-1);
stack_pointer[-3] = res;
stack_pointer += -2;
stack_pointer[-2] = a;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
@ -3450,6 +3453,22 @@
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW: {
JitOptRef arg;
JitOptRef res;
JitOptRef a;
arg = stack_pointer[-1];
PyObject *ptr = (PyObject *)this_instr->operand0;
res = PyJitRef_Borrow(sym_new_const(ctx, ptr));
a = arg;
CHECK_STACK_BOUNDS(-1);
stack_pointer[-3] = res;
stack_pointer[-2] = a;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW: {
JitOptRef res;
JitOptRef a;