Skip to content

Commit 8cf122d

Browse files
committed
gh-131798: Propagate iterator types from GET_ITER in the JIT
1 parent 7a91841 commit 8cf122d

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,34 @@ def testfunc(n):
458458
self.assertIsNotNone(ex)
459459
uops = get_opnames(ex)
460460
self.assertIn("_GET_ITER_TRAD", uops)
461+
self.assertIn("_ITER_NEXT_INLINE", uops)
462+
self.assertNotIn("_GUARD_TYPE_ITER", uops)
461463
self.assertNotIn("_GET_ITER", uops)
462464
self.assertNotIn("_GET_ITER_VIRTUAL", uops)
463465
self.assertNotIn("_GET_ITER_SELF", uops)
464466

467+
def test_get_iter_trad_set(self):
468+
s = set(range(10))
469+
def testfunc(n):
470+
total = 0
471+
while n:
472+
n -= 1
473+
for value in s:
474+
total += value
475+
break
476+
return total
477+
478+
total = testfunc(TIER2_THRESHOLD)
479+
self.assertEqual(total, next(iter(s)) * TIER2_THRESHOLD)
480+
ex = get_first_executor(testfunc)
481+
self.assertIsNotNone(ex)
482+
uops = get_opnames(ex)
483+
self.assertIn("_GET_ITER_TRAD", uops)
484+
self.assertIn("_ITER_NEXT_INLINE", uops)
485+
self.assertNotIn("_GUARD_TYPE_ITER", uops)
486+
self.assertNotIn("_GET_ITER", uops)
487+
self.assertNotIn("_GET_ITER_VIRTUAL", uops)
488+
self.assertNotIn("_GET_ITER_SELF", uops)
465489

466490
def test_for_iter_range(self):
467491
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,15 @@ dummy_func(void) {
14481448
index_or_null = sym_new_null(ctx);
14491449
}
14501450
else if (is_trad) {
1451-
iter = sym_new_not_null(ctx);
1451+
if (tp == &PyDict_Type || tp == &PyFrozenDict_Type) {
1452+
iter = sym_new_type(ctx, &PyDictIterKey_Type);
1453+
}
1454+
else if (tp == &PySet_Type || tp == &PyFrozenSet_Type) {
1455+
iter = sym_new_type(ctx, &PySetIter_Type);
1456+
}
1457+
else {
1458+
iter = sym_new_not_null(ctx);
1459+
}
14521460
index_or_null = sym_new_null(ctx);
14531461
}
14541462
else {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)