gh-144007: Eliminate redundant refcounting in the JIT for BINARY_OP (GH-144011)

This commit is contained in:
AN Long 2026-01-24 18:35:32 +09:00 committed by GitHub
parent 29f1e778fa
commit 4e10fa993a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 105 additions and 48 deletions

View file

@ -1343,7 +1343,7 @@ extern const struct opcode_macro_expansion _PyOpcode_macro_expansion[256];
#ifdef NEED_OPCODE_METADATA
const struct opcode_macro_expansion
_PyOpcode_macro_expansion[256] = {
[BINARY_OP] = { .nuops = 1, .uops = { { _BINARY_OP, OPARG_SIMPLE, 4 } } },
[BINARY_OP] = { .nuops = 3, .uops = { { _BINARY_OP, OPARG_SIMPLE, 4 }, { _POP_TOP, OPARG_SIMPLE, 4 }, { _POP_TOP, OPARG_SIMPLE, 4 } } },
[BINARY_OP_ADD_FLOAT] = { .nuops = 5, .uops = { { _GUARD_TOS_FLOAT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_FLOAT, OPARG_SIMPLE, 0 }, { _BINARY_OP_ADD_FLOAT, OPARG_SIMPLE, 5 }, { _POP_TOP_FLOAT, OPARG_SIMPLE, 5 }, { _POP_TOP_FLOAT, OPARG_SIMPLE, 5 } } },
[BINARY_OP_ADD_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_INT, OPARG_SIMPLE, 0 }, { _BINARY_OP_ADD_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 } } },
[BINARY_OP_ADD_UNICODE] = { .nuops = 5, .uops = { { _GUARD_TOS_UNICODE, OPARG_SIMPLE, 0 }, { _GUARD_NOS_UNICODE, OPARG_SIMPLE, 0 }, { _BINARY_OP_ADD_UNICODE, OPARG_SIMPLE, 5 }, { _POP_TOP_UNICODE, OPARG_SIMPLE, 5 }, { _POP_TOP_UNICODE, OPARG_SIMPLE, 5 } } },

View file

@ -379,7 +379,7 @@ extern "C" {
#define _WITH_EXCEPT_START WITH_EXCEPT_START
#define _YIELD_VALUE YIELD_VALUE
#define MAX_UOP_ID 578
#define _BINARY_OP_r21 579
#define _BINARY_OP_r23 579
#define _BINARY_OP_ADD_FLOAT_r03 580
#define _BINARY_OP_ADD_FLOAT_r13 581
#define _BINARY_OP_ADD_FLOAT_r23 582

View file

@ -2931,7 +2931,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = {
.entries = {
{ -1, -1, -1 },
{ -1, -1, -1 },
{ 1, 2, _BINARY_OP_r21 },
{ 3, 2, _BINARY_OP_r23 },
{ -1, -1, -1 },
},
},
@ -4031,7 +4031,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = {
[_COPY_3_r23] = _COPY_3,
[_COPY_3_r33] = _COPY_3,
[_COPY_r01] = _COPY,
[_BINARY_OP_r21] = _BINARY_OP,
[_BINARY_OP_r23] = _BINARY_OP,
[_SWAP_2_r02] = _SWAP_2,
[_SWAP_2_r12] = _SWAP_2,
[_SWAP_2_r22] = _SWAP_2,
@ -4225,7 +4225,7 @@ const uint16_t _PyUop_SpillsAndReloads[4][4] = {
const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = {
[_BINARY_OP] = "_BINARY_OP",
[_BINARY_OP_r21] = "_BINARY_OP_r21",
[_BINARY_OP_r23] = "_BINARY_OP_r23",
[_BINARY_OP_ADD_FLOAT] = "_BINARY_OP_ADD_FLOAT",
[_BINARY_OP_ADD_FLOAT_r03] = "_BINARY_OP_ADD_FLOAT_r03",
[_BINARY_OP_ADD_FLOAT_r13] = "_BINARY_OP_ADD_FLOAT_r13",

View file

@ -2897,6 +2897,29 @@ def testfunc(n):
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
def test_binary_op_refcount_elimination(self):
class CustomAdder:
def __init__(self, val):
self.val = val
def __add__(self, other):
return CustomAdder(self.val + other.val)
def testfunc(n):
a = CustomAdder(1)
b = CustomAdder(2)
res = None
for _ in range(n):
res = a + b
return res.val if res else 0
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 3)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_BINARY_OP", uops)
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
def test_binary_op_extend_float_long_add_refcount_elimination(self):
def testfunc(n):
a = 1.5

View file

@ -0,0 +1 @@
Eliminate redundant refcounting in the JIT for ``BINARY_OP``.

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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