mirror of
https://github.com/python/cpython.git
synced 2026-02-06 09:50:43 +00:00
gh-144007: Eliminate redundant refcounting in the JIT for BINARY_OP (GH-144011)
This commit is contained in:
parent
29f1e778fa
commit
4e10fa993a
11 changed files with 105 additions and 48 deletions
|
|
@ -5119,7 +5119,7 @@ dummy_func(
|
|||
assert(oparg <= NB_OPARG_LAST);
|
||||
}
|
||||
|
||||
op(_BINARY_OP, (lhs, rhs -- res)) {
|
||||
op(_BINARY_OP, (lhs, rhs -- res, l, r)) {
|
||||
PyObject *lhs_o = PyStackRef_AsPyObjectBorrow(lhs);
|
||||
PyObject *rhs_o = PyStackRef_AsPyObjectBorrow(rhs);
|
||||
|
||||
|
|
@ -5129,10 +5129,13 @@ dummy_func(
|
|||
ERROR_NO_POP();
|
||||
}
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
DECREF_INPUTS();
|
||||
l = lhs;
|
||||
r = rhs;
|
||||
DEAD(lhs);
|
||||
DEAD(rhs);
|
||||
}
|
||||
|
||||
macro(BINARY_OP) = _SPECIALIZE_BINARY_OP + unused/4 + _BINARY_OP;
|
||||
macro(BINARY_OP) = _SPECIALIZE_BINARY_OP + unused/4 + _BINARY_OP + POP_TOP + POP_TOP;
|
||||
|
||||
pure replicate(2:4) inst(SWAP, (bottom, unused[oparg-2], top --
|
||||
bottom, unused[oparg-2], top)) {
|
||||
|
|
|
|||
26
Python/executor_cases.c.h
generated
26
Python/executor_cases.c.h
generated
|
|
@ -16594,12 +16594,14 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_r21: {
|
||||
case _BINARY_OP_r23: {
|
||||
CHECK_CURRENT_CACHED_VALUES(2);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef rhs;
|
||||
_PyStackRef lhs;
|
||||
_PyStackRef res;
|
||||
_PyStackRef l;
|
||||
_PyStackRef r;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
_PyStackRef _stack_item_1 = _tos_cache1;
|
||||
oparg = CURRENT_OPARG();
|
||||
|
|
@ -16620,23 +16622,13 @@
|
|||
JUMP_TO_ERROR();
|
||||
}
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
_PyStackRef tmp = lhs;
|
||||
lhs = res;
|
||||
stack_pointer[-2] = lhs;
|
||||
PyStackRef_CLOSE(tmp);
|
||||
tmp = rhs;
|
||||
rhs = PyStackRef_NULL;
|
||||
stack_pointer[-1] = rhs;
|
||||
PyStackRef_CLOSE(tmp);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
stack_pointer += -1;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
l = lhs;
|
||||
r = rhs;
|
||||
_tos_cache2 = r;
|
||||
_tos_cache1 = l;
|
||||
_tos_cache0 = res;
|
||||
_tos_cache1 = PyStackRef_ZERO_BITS;
|
||||
_tos_cache2 = PyStackRef_ZERO_BITS;
|
||||
SET_CURRENT_CACHED_VALUES(1);
|
||||
stack_pointer += -1;
|
||||
SET_CURRENT_CACHED_VALUES(3);
|
||||
stack_pointer += -2;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
break;
|
||||
|
|
|
|||
27
Python/generated_cases.c.h
generated
27
Python/generated_cases.c.h
generated
|
|
@ -32,6 +32,9 @@
|
|||
_PyStackRef lhs;
|
||||
_PyStackRef rhs;
|
||||
_PyStackRef res;
|
||||
_PyStackRef l;
|
||||
_PyStackRef r;
|
||||
_PyStackRef value;
|
||||
// _SPECIALIZE_BINARY_OP
|
||||
{
|
||||
rhs = stack_pointer[-1];
|
||||
|
|
@ -65,18 +68,26 @@
|
|||
JUMP_TO_LABEL(error);
|
||||
}
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
l = lhs;
|
||||
r = rhs;
|
||||
}
|
||||
// _POP_TOP
|
||||
{
|
||||
value = r;
|
||||
stack_pointer[-2] = res;
|
||||
stack_pointer[-1] = l;
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
_PyStackRef tmp = lhs;
|
||||
lhs = res;
|
||||
stack_pointer[-2] = lhs;
|
||||
PyStackRef_CLOSE(tmp);
|
||||
tmp = rhs;
|
||||
rhs = PyStackRef_NULL;
|
||||
stack_pointer[-1] = rhs;
|
||||
PyStackRef_CLOSE(tmp);
|
||||
PyStackRef_XCLOSE(value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
// _POP_TOP
|
||||
{
|
||||
value = l;
|
||||
stack_pointer += -1;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_XCLOSE(value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
DISPATCH();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -211,7 +211,9 @@ dummy_func(void) {
|
|||
sym_set_type(left, &PyFloat_Type);
|
||||
}
|
||||
|
||||
op(_BINARY_OP, (lhs, rhs -- res)) {
|
||||
op(_BINARY_OP, (lhs, rhs -- res, l, r)) {
|
||||
l = lhs;
|
||||
r = rhs;
|
||||
REPLACE_OPCODE_IF_EVALUATES_PURE(lhs, rhs, res);
|
||||
bool lhs_int = sym_matches_type(lhs, &PyLong_Type);
|
||||
bool rhs_int = sym_matches_type(rhs, &PyLong_Type);
|
||||
|
|
|
|||
26
Python/optimizer_cases.c.h
generated
26
Python/optimizer_cases.c.h
generated
|
|
@ -3616,8 +3616,12 @@
|
|||
JitOptRef rhs;
|
||||
JitOptRef lhs;
|
||||
JitOptRef res;
|
||||
JitOptRef l;
|
||||
JitOptRef r;
|
||||
rhs = stack_pointer[-1];
|
||||
lhs = stack_pointer[-2];
|
||||
l = lhs;
|
||||
r = rhs;
|
||||
if (
|
||||
sym_is_safe_const(ctx, lhs) &&
|
||||
sym_is_safe_const(ctx, rhs)
|
||||
|
|
@ -3627,6 +3631,8 @@
|
|||
_PyStackRef lhs = sym_get_const_as_stackref(ctx, lhs_sym);
|
||||
_PyStackRef rhs = sym_get_const_as_stackref(ctx, rhs_sym);
|
||||
_PyStackRef res_stackref;
|
||||
_PyStackRef l_stackref;
|
||||
_PyStackRef r_stackref;
|
||||
/* Start of uop copied from bytecodes for constant evaluation */
|
||||
PyObject *lhs_o = PyStackRef_AsPyObjectBorrow(lhs);
|
||||
PyObject *rhs_o = PyStackRef_AsPyObjectBorrow(rhs);
|
||||
|
|
@ -3636,18 +3642,24 @@
|
|||
JUMP_TO_LABEL(error);
|
||||
}
|
||||
res_stackref = PyStackRef_FromPyObjectSteal(res_o);
|
||||
l_stackref = lhs;
|
||||
r_stackref = rhs;
|
||||
/* End of uop copied from bytecodes for constant evaluation */
|
||||
(void)l_stackref;
|
||||
(void)r_stackref;
|
||||
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
|
||||
if (sym_is_const(ctx, res)) {
|
||||
PyObject *result = sym_get_const(ctx, res);
|
||||
if (_Py_IsImmortal(result)) {
|
||||
// Replace with _POP_TWO_LOAD_CONST_INLINE_BORROW since we have two inputs and an immortal result
|
||||
ADD_OP(_POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)result);
|
||||
// Replace with _INSERT_2_LOAD_CONST_INLINE_BORROW since we have two inputs and an immortal result
|
||||
ADD_OP(_INSERT_2_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)result);
|
||||
}
|
||||
}
|
||||
CHECK_STACK_BOUNDS(-1);
|
||||
CHECK_STACK_BOUNDS(1);
|
||||
stack_pointer[-2] = res;
|
||||
stack_pointer += -1;
|
||||
stack_pointer[-1] = l;
|
||||
stack_pointer[0] = r;
|
||||
stack_pointer += 1;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
break;
|
||||
}
|
||||
|
|
@ -3684,9 +3696,11 @@
|
|||
else {
|
||||
res = sym_new_type(ctx, &PyFloat_Type);
|
||||
}
|
||||
CHECK_STACK_BOUNDS(-1);
|
||||
CHECK_STACK_BOUNDS(1);
|
||||
stack_pointer[-2] = res;
|
||||
stack_pointer += -1;
|
||||
stack_pointer[-1] = l;
|
||||
stack_pointer[0] = r;
|
||||
stack_pointer += 1;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue