Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-131798: JIT: Narrow the return type of _GET_LEN to int #133345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 44 Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,50 @@ def testfunc(n):
self.assertNotIn("_GUARD_NOS_INT", uops)
self.assertNotIn("_GUARD_TOS_INT", uops)

def test_get_len_with_const_tuple(self):
def testfunc(n):
x = 0.0
for _ in range(n):
match (1, 2, 3, 4):
case [_, _, _, _]:
x += 1.0
return x
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(int(res), TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertNotIn("_GUARD_NOS_INT", uops)
Zheaoli marked this conversation as resolved.
Show resolved Hide resolved
self.assertNotIn("_GET_LEN", uops)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)

def test_get_len_with_non_const_tuple(self):
def testfunc(n):
x = 0.0
for _ in range(n):
match object(), object():
case [_, _]:
x += 1.0
return x
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(int(res), TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertNotIn("_GUARD_NOS_INT", uops)
self.assertNotIn("_GET_LEN", uops)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)

def test_get_len_with_non_tuple(self):
def testfunc(n):
x = 0.0
for _ in range(n):
match [1, 2, 3, 4]:
case [_, _, _, _]:
x += 1.0
return x
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(int(res), TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertNotIn("_GUARD_NOS_INT", uops)
self.assertIn("_GET_LEN", uops)

def test_binary_op_subscr_tuple_int(self):
def testfunc(n):
x = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow the JIT to remove int guards after ``_GET_LEN`` by setting the return
type to int.
19 changes: 19 additions & 0 deletions 19 Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,25 @@ dummy_func(void) {
res = sym_new_type(ctx, &PyLong_Type);
}

op(_GET_LEN, (obj -- obj, len)) {
int tuple_length = sym_tuple_length(obj);
if (tuple_length == -1) {
len = sym_new_type(ctx, &PyLong_Type);
Zheaoli marked this conversation as resolved.
Show resolved Hide resolved
}
else {
assert(tuple_length >= 0);
PyObject *temp = PyLong_FromLong(tuple_length);
if (temp == NULL) {
goto error;
}
if (_Py_IsImmortal(temp)) {
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
}
len = sym_new_const(ctx, temp);
Zheaoli marked this conversation as resolved.
Show resolved Hide resolved
Py_DECREF(temp);
}
}

op(_GUARD_CALLABLE_LEN, (callable, unused, unused -- callable, unused, unused)) {
PyObject *len = _PyInterpreterState_GET()->callable_cache.len;
if (sym_get_const(ctx, callable) == len) {
Expand Down
23 changes: 22 additions & 1 deletion 23 Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.