Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2880,6 +2880,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_remove_guard_for_slice_list(self):
def f(n):
for i in range(n):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminate redundant refcounting in the JIT for ``BINARY_OP``.
27 changes: 19 additions & 8 deletions Modules/_testinternalcapi/test_cases.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
_PyStackRef lhs;
_PyStackRef rhs;
_PyStackRef res;
_PyStackRef l;
_PyStackRef r;
_PyStackRef value;
// _SPECIALIZE_BINARY_OP
{
rhs = stack_pointer[-1];
Expand Down Expand Up @@ -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();
}
Expand Down
9 changes: 6 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5113,7 +5113,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);

Expand All @@ -5123,10 +5123,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)) {
Expand Down
26 changes: 9 additions & 17 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,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);
Expand Down
26 changes: 20 additions & 6 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading