Skip to content

Commit cabe8f1

Browse files
committed
gh-131798: JIT: Narrow the return type of _GET_LEN to int
Signed-off-by: Manjusaka <[email protected]>
1 parent 8679c8d commit cabe8f1

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/test/test_capi/test_opt.py

+13
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,19 @@ def testfunc(n):
19251925
self.assertNotIn("_GUARD_NOS_INT", uops)
19261926
self.assertNotIn("_GUARD_TOS_INT", uops)
19271927

1928+
def test_get_len(self):
1929+
def testfunc(n):
1930+
a = [1, 2, 3, 4]
1931+
for _ in range(n):
1932+
match a:
1933+
case [1,2]:
1934+
_ = len(a) - 1
1935+
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1936+
uops = get_opnames(ex)
1937+
self.assertNotIn("_GUARD_NOS_INT", uops)
1938+
self.assertNotIn("_GUARD_TOS_INT", uops)
1939+
self.assertIn("_GET_LEN", uops)
1940+
19281941
def test_binary_op_subscr_tuple_int(self):
19291942
def testfunc(n):
19301943
x = 0

Python/optimizer_bytecodes.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ dummy_func(void) {
480480
res = sym_new_const(ctx, tmp);
481481
Py_DECREF(tmp);
482482
}
483-
else {
483+
else {
484484
res = sym_new_type(ctx, &PyBool_Type);
485485
}
486486
}
@@ -1088,6 +1088,10 @@ dummy_func(void) {
10881088
res = sym_new_type(ctx, &PyLong_Type);
10891089
}
10901090

1091+
op(_GET_LEN, (-- len)) {
1092+
len = sym_new_type(ctx, &PyLong_Type);
1093+
}
1094+
10911095
op(_GUARD_CALLABLE_LEN, (callable, unused, unused -- callable, unused, unused)) {
10921096
PyObject *len = _PyInterpreterState_GET()->callable_cache.len;
10931097
if (sym_get_const(ctx, callable) == len) {

Python/optimizer_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)