Skip to content

Commit 6e08161

Browse files
gh-148210: fix incorrect _BINARY_OP_SUBSCR_DICT JIT optimization (GH-148213)
1 parent bb03c8b commit 6e08161

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,21 @@ def testfunc(n):
23402340
self.assertIn("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH", uops)
23412341
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)
23422342

2343+
2344+
def test_binary_op_subscr_constant_frozendict_known_hash(self):
2345+
def testfunc(n):
2346+
x = 0
2347+
for _ in range(n):
2348+
x += FROZEN_DICT_CONST['x']
2349+
return x
2350+
2351+
res, ex = self._run_with_optimizer(testfunc, 2 * TIER2_THRESHOLD)
2352+
self.assertEqual(res, 2 * TIER2_THRESHOLD)
2353+
self.assertIsNotNone(ex)
2354+
uops = get_opnames(ex)
2355+
self.assertNotIn("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH", uops)
2356+
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)
2357+
23432358
def test_store_subscr_dict_known_hash(self):
23442359
# str, int, bytes, float, complex, tuple and any python object which has generic hash
23452360
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,18 @@ dummy_func(void) {
515515
}
516516

517517
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
518-
PyObject *sub = sym_get_const(ctx, sub_st);
519-
if (sub != NULL) {
520-
optimize_dict_known_hash(ctx, dependencies, this_instr,
521-
sub, _BINARY_OP_SUBSCR_DICT_KNOWN_HASH);
522-
}
523518
res = sym_new_not_null(ctx);
524519
ds = dict_st;
525520
ss = sub_st;
521+
PyObject *sub = sym_get_const(ctx, sub_st);
526522
if (sym_is_not_container(sub_st) &&
527523
sym_matches_type(dict_st, &PyFrozenDict_Type)) {
528524
REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res);
529525
}
526+
else if (sub != NULL) {
527+
optimize_dict_known_hash(ctx, dependencies, this_instr,
528+
sub, _BINARY_OP_SUBSCR_DICT_KNOWN_HASH);
529+
}
530530
}
531531

532532
op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res, ls, ss)) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)