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

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