https://github.com/python/cpython/commit/33d94abafd0c6707561493a6dd16a85ff9776f05
commit: 33d94abafd0c6707561493a6dd16a85ff9776f05
branch: main
author: LloydZ <[email protected]>
committer: Fidget-Spinner <[email protected]>
date: 2025-12-18T18:25:36Z
summary:
gh-134584: Eliminate redundant refcounting from _BINARY_OP_SUBSCR_LIST_INT
(GH-142926)
files:
M Include/internal/pycore_opcode_metadata.h
M Include/internal/pycore_uop_ids.h
M Include/internal/pycore_uop_metadata.h
M Lib/test/test_capi/test_opt.py
M Python/bytecodes.c
M Python/executor_cases.c.h
M Python/generated_cases.c.h
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h
diff --git a/Include/internal/pycore_opcode_metadata.h
b/Include/internal/pycore_opcode_metadata.h
index 906ea0db0a5fe0..913359ee4bdc4f 100644
--- a/Include/internal/pycore_opcode_metadata.h
+++ b/Include/internal/pycore_opcode_metadata.h
@@ -1338,7 +1338,7 @@ _PyOpcode_macro_expansion[256] = {
[BINARY_OP_MULTIPLY_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT,
OPARG_SIMPLE, 0 }, { _GUARD_NOS_INT, OPARG_SIMPLE, 0 }, {
_BINARY_OP_MULTIPLY_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 },
{ _POP_TOP_INT, OPARG_SIMPLE, 5 } } },
[BINARY_OP_SUBSCR_DICT] = { .nuops = 2, .uops = { { _GUARD_NOS_DICT,
OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_DICT, OPARG_SIMPLE, 5 } } },
[BINARY_OP_SUBSCR_GETITEM] = { .nuops = 4, .uops = { { _CHECK_PEP_523,
OPARG_SIMPLE, 5 }, { _BINARY_OP_SUBSCR_CHECK_FUNC, OPARG_SIMPLE, 5 }, {
_BINARY_OP_SUBSCR_INIT_CALL, OPARG_SIMPLE, 5 }, { _PUSH_FRAME, OPARG_SIMPLE, 5
} } },
- [BINARY_OP_SUBSCR_LIST_INT] = { .nuops = 3, .uops = { { _GUARD_TOS_INT,
OPARG_SIMPLE, 0 }, { _GUARD_NOS_LIST, OPARG_SIMPLE, 0 }, {
_BINARY_OP_SUBSCR_LIST_INT, OPARG_SIMPLE, 5 } } },
+ [BINARY_OP_SUBSCR_LIST_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT,
OPARG_SIMPLE, 0 }, { _GUARD_NOS_LIST, OPARG_SIMPLE, 0 }, {
_BINARY_OP_SUBSCR_LIST_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5
}, { _POP_TOP, OPARG_SIMPLE, 5 } } },
[BINARY_OP_SUBSCR_LIST_SLICE] = { .nuops = 3, .uops = { {
_GUARD_TOS_SLICE, OPARG_SIMPLE, 0 }, { _GUARD_NOS_LIST, OPARG_SIMPLE, 0 }, {
_BINARY_OP_SUBSCR_LIST_SLICE, OPARG_SIMPLE, 5 } } },
[BINARY_OP_SUBSCR_STR_INT] = { .nuops = 3, .uops = { { _GUARD_TOS_INT,
OPARG_SIMPLE, 0 }, { _GUARD_NOS_UNICODE, OPARG_SIMPLE, 0 }, {
_BINARY_OP_SUBSCR_STR_INT, OPARG_SIMPLE, 5 } } },
[BINARY_OP_SUBSCR_TUPLE_INT] = { .nuops = 3, .uops = { { _GUARD_TOS_INT,
OPARG_SIMPLE, 0 }, { _GUARD_NOS_TUPLE, OPARG_SIMPLE, 0 }, {
_BINARY_OP_SUBSCR_TUPLE_INT, OPARG_SIMPLE, 5 } } },
diff --git a/Include/internal/pycore_uop_ids.h
b/Include/internal/pycore_uop_ids.h
index f73aad9de1c0bf..3b7b17b5d3e53f 100644
--- a/Include/internal/pycore_uop_ids.h
+++ b/Include/internal/pycore_uop_ids.h
@@ -382,7 +382,7 @@ extern "C" {
#define _BINARY_OP_SUBSCR_INIT_CALL_r11 575
#define _BINARY_OP_SUBSCR_INIT_CALL_r21 576
#define _BINARY_OP_SUBSCR_INIT_CALL_r31 577
-#define _BINARY_OP_SUBSCR_LIST_INT_r21 578
+#define _BINARY_OP_SUBSCR_LIST_INT_r23 578
#define _BINARY_OP_SUBSCR_LIST_SLICE_r21 579
#define _BINARY_OP_SUBSCR_STR_INT_r21 580
#define _BINARY_OP_SUBSCR_TUPLE_INT_r21 581
diff --git a/Include/internal/pycore_uop_metadata.h
b/Include/internal/pycore_uop_metadata.h
index 0cc38919565bc1..99ade22c0f437a 100644
--- a/Include/internal/pycore_uop_metadata.h
+++ b/Include/internal/pycore_uop_metadata.h
@@ -1108,7 +1108,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = {
.entries = {
{ -1, -1, -1 },
{ -1, -1, -1 },
- { 1, 2, _BINARY_OP_SUBSCR_LIST_INT_r21 },
+ { 3, 2, _BINARY_OP_SUBSCR_LIST_INT_r23 },
{ -1, -1, -1 },
},
},
@@ -3422,7 +3422,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = {
[_BINARY_OP_EXTEND_r21] = _BINARY_OP_EXTEND,
[_BINARY_SLICE_r31] = _BINARY_SLICE,
[_STORE_SLICE_r30] = _STORE_SLICE,
- [_BINARY_OP_SUBSCR_LIST_INT_r21] = _BINARY_OP_SUBSCR_LIST_INT,
+ [_BINARY_OP_SUBSCR_LIST_INT_r23] = _BINARY_OP_SUBSCR_LIST_INT,
[_BINARY_OP_SUBSCR_LIST_SLICE_r21] = _BINARY_OP_SUBSCR_LIST_SLICE,
[_BINARY_OP_SUBSCR_STR_INT_r21] = _BINARY_OP_SUBSCR_STR_INT,
[_GUARD_NOS_TUPLE_r02] = _GUARD_NOS_TUPLE,
@@ -3930,7 +3930,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] =
{
[_BINARY_OP_SUBSCR_INIT_CALL_r21] = "_BINARY_OP_SUBSCR_INIT_CALL_r21",
[_BINARY_OP_SUBSCR_INIT_CALL_r31] = "_BINARY_OP_SUBSCR_INIT_CALL_r31",
[_BINARY_OP_SUBSCR_LIST_INT] = "_BINARY_OP_SUBSCR_LIST_INT",
- [_BINARY_OP_SUBSCR_LIST_INT_r21] = "_BINARY_OP_SUBSCR_LIST_INT_r21",
+ [_BINARY_OP_SUBSCR_LIST_INT_r23] = "_BINARY_OP_SUBSCR_LIST_INT_r23",
[_BINARY_OP_SUBSCR_LIST_SLICE] = "_BINARY_OP_SUBSCR_LIST_SLICE",
[_BINARY_OP_SUBSCR_LIST_SLICE_r21] = "_BINARY_OP_SUBSCR_LIST_SLICE_r21",
[_BINARY_OP_SUBSCR_STR_INT] = "_BINARY_OP_SUBSCR_STR_INT",
diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 6f1b115b31257f..a35dea8557ddbb 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -2988,6 +2988,24 @@ class Obj:
for _ in range(TIER2_THRESHOLD+1):
obj.attr = EvilAttr(obj.__dict__)
+ def test_binary_subscr_list_int(self):
+ def testfunc(n):
+ l = [1]
+ x = 0
+ for _ in range(n):
+ y = l[0]
+ x += y
+ return x
+
+ res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+
+ self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops)
+ self.assertNotIn("_POP_TOP", uops)
+ self.assertNotIn("_POP_TOP_INT", uops)
+ self.assertIn("_POP_TOP_NOP", uops)
def global_identity(x):
return x
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 3e3b3d2f446d33..1c6d0bb9cdf256 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -893,9 +893,9 @@ dummy_func(
macro(STORE_SLICE) = _SPECIALIZE_STORE_SLICE + _STORE_SLICE;
macro(BINARY_OP_SUBSCR_LIST_INT) =
- _GUARD_TOS_INT + _GUARD_NOS_LIST + unused/5 +
_BINARY_OP_SUBSCR_LIST_INT;
+ _GUARD_TOS_INT + _GUARD_NOS_LIST + unused/5 +
_BINARY_OP_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;
- op(_BINARY_OP_SUBSCR_LIST_INT, (list_st, sub_st -- res)) {
+ op(_BINARY_OP_SUBSCR_LIST_INT, (list_st, sub_st -- res, ls, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *list = PyStackRef_AsPyObjectBorrow(list_st);
@@ -918,7 +918,9 @@ dummy_func(
res = PyStackRef_FromPyObjectNew(res_o);
#endif
STAT_INC(BINARY_OP, hit);
- DECREF_INPUTS();
+ ls = list_st;
+ ss = sub_st;
+ INPUTS_DEAD();
}
macro(BINARY_OP_SUBSCR_LIST_SLICE) =
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index b7ee752cd44e16..36ae739e58d6c5 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -4544,12 +4544,14 @@
break;
}
- case _BINARY_OP_SUBSCR_LIST_INT_r21: {
+ case _BINARY_OP_SUBSCR_LIST_INT_r23: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef sub_st;
_PyStackRef list_st;
_PyStackRef res;
+ _PyStackRef ls;
+ _PyStackRef ss;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
sub_st = _stack_item_1;
@@ -4600,23 +4602,13 @@
stack_pointer += 2;
#endif
STAT_INC(BINARY_OP, hit);
- _PyFrame_SetStackPointer(frame, stack_pointer);
- _PyStackRef tmp = list_st;
- list_st = res;
- stack_pointer[-2] = list_st;
- PyStackRef_CLOSE(tmp);
- tmp = sub_st;
- sub_st = PyStackRef_NULL;
- stack_pointer[-1] = sub_st;
- PyStackRef_CLOSE(tmp);
- stack_pointer = _PyFrame_GetStackPointer(frame);
- stack_pointer += -1;
- ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+ ls = list_st;
+ ss = sub_st;
+ _tos_cache2 = ss;
+ _tos_cache1 = ls;
_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;
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 28dc7b25f0d2cc..0c0256bcc3b753 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -764,6 +764,8 @@
_PyStackRef list_st;
_PyStackRef sub_st;
_PyStackRef res;
+ _PyStackRef ls;
+ _PyStackRef ss;
// _GUARD_TOS_INT
{
value = stack_pointer[-1];
@@ -822,18 +824,24 @@
res = PyStackRef_FromPyObjectNew(res_o);
#endif
STAT_INC(BINARY_OP, hit);
- _PyFrame_SetStackPointer(frame, stack_pointer);
- _PyStackRef tmp = list_st;
- list_st = res;
- stack_pointer[-2] = list_st;
- PyStackRef_CLOSE(tmp);
- tmp = sub_st;
- sub_st = PyStackRef_NULL;
- stack_pointer[-1] = sub_st;
- PyStackRef_CLOSE(tmp);
- stack_pointer = _PyFrame_GetStackPointer(frame);
+ ls = list_st;
+ ss = sub_st;
+ }
+ // _POP_TOP_INT
+ {
+ value = ss;
+ assert(PyLong_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
+ PyStackRef_CLOSE_SPECIALIZED(value, _PyLong_ExactDealloc);
+ }
+ // _POP_TOP
+ {
+ value = ls;
+ stack_pointer[-2] = res;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+ _PyFrame_SetStackPointer(frame, stack_pointer);
+ PyStackRef_XCLOSE(value);
+ stack_pointer = _PyFrame_GetStackPointer(frame);
}
DISPATCH();
}
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index e3921054f7cb43..9578b56e707393 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -1417,6 +1417,12 @@ dummy_func(void) {
}
}
+ op(_BINARY_OP_SUBSCR_LIST_INT, (list_st, sub_st -- res, ls, ss)) {
+ res = sym_new_unknown(ctx);
+ ls = list_st;
+ ss = sub_st;
+ }
+
// END BYTECODES //
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 32d60c76a4f3ce..f047cd82d4c5e7 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -763,11 +763,21 @@
}
case _BINARY_OP_SUBSCR_LIST_INT: {
+ JitOptRef sub_st;
+ JitOptRef list_st;
JitOptRef res;
- res = sym_new_not_null(ctx);
- CHECK_STACK_BOUNDS(-1);
+ JitOptRef ls;
+ JitOptRef ss;
+ sub_st = stack_pointer[-1];
+ list_st = stack_pointer[-2];
+ res = sym_new_unknown(ctx);
+ ls = list_st;
+ ss = sub_st;
+ CHECK_STACK_BOUNDS(1);
stack_pointer[-2] = res;
- stack_pointer += -1;
+ stack_pointer[-1] = ls;
+ stack_pointer[0] = ss;
+ stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]