Skip to content

Commit 054a565

Browse files
authored
gh-134584: JIT: Remove redundant refcount for _BINARY_OP_SUBSCR_DICT (GH-143724)
1 parent 42f7c2d commit 054a565

File tree

9 files changed

+83
-44
lines changed

9 files changed

+83
-44
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,23 @@ def testfunc(n):
19751975
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
19761976
self.assertLessEqual(count_ops(ex, "_POP_TOP_INT"), 1)
19771977

1978+
def test_binary_op_subscr_dict(self):
1979+
def testfunc(n):
1980+
x = 0
1981+
d = {'a': 1, 'b': 2}
1982+
for _ in range(n):
1983+
v = d['a'] # _BINARY_OP_SUBSCR_DICT
1984+
if v == 1:
1985+
x += 1
1986+
return x
1987+
1988+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1989+
self.assertEqual(res, TIER2_THRESHOLD)
1990+
self.assertIsNotNone(ex)
1991+
uops = get_opnames(ex)
1992+
self.assertIn("_BINARY_OP_SUBSCR_DICT", uops)
1993+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
1994+
19781995
def test_call_type_1_guards_removed(self):
19791996
def testfunc(n):
19801997
x = 0

Python/bytecodes.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,9 @@ dummy_func(
10561056
}
10571057

10581058
macro(BINARY_OP_SUBSCR_DICT) =
1059-
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT;
1059+
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT + POP_TOP + POP_TOP;
10601060

1061-
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res)) {
1061+
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
10621062
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
10631063
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);
10641064

@@ -1069,9 +1069,13 @@ dummy_func(
10691069
if (rc == 0) {
10701070
_PyErr_SetKeyError(sub);
10711071
}
1072-
DECREF_INPUTS();
1073-
ERROR_IF(rc <= 0); // not found or error
1072+
if (rc <= 0) {
1073+
ERROR_NO_POP();
1074+
}
10741075
res = PyStackRef_FromPyObjectSteal(res_o);
1076+
ds = dict_st;
1077+
ss = sub_st;
1078+
INPUTS_DEAD();
10751079
}
10761080

10771081
op(_BINARY_OP_SUBSCR_CHECK_FUNC, (container, unused -- container, unused, getitem)) {

Python/executor_cases.c.h

Lines changed: 10 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 23 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ dummy_func(void) {
380380
ss = sub_st;
381381
}
382382

383+
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
384+
res = sym_new_not_null(ctx);
385+
ds = dict_st;
386+
ss = sub_st;
387+
}
388+
383389
op(_TO_BOOL, (value -- res)) {
384390
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
385391
if (!already_bool) {

Python/optimizer_cases.c.h

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)