Skip to content

gh-131798: JIT: Narrow the return type of _CALL_LEN to int #132940

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 4 commits into from
Apr 25, 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
12 changes: 12 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,18 @@ def testfunc(n):
self.assertNotIn("_COMPARE_OP_INT", uops)
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)

def test_call_len(self):
def testfunc(n):
a = [1, 2, 3, 4]
for _ in range(n):
_ = len(a) - 1

_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
Comment on lines +1916 to +1920
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's needed in this case, but for other tests there's normally a 'control' variable that checks that the result is still correct even with the optimization. Something like this:

Suggested change
a = [1, 2, 3, 4]
for _ in range(n):
_ = len(a) - 1
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
x = 0
a = [1, 2, 3, 4]
for _ in range(n):
_ = len(a) - 1
if _ == 3:
x += 1
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK the problem is if I add this, _GUARD_TOS_INT and _GUARD_NOS_INT are appearing in the uops list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can't do it. x is from outside the loop (trace), so the first x += 1 has to have a guard somewhere.

uops = get_opnames(ex)
self.assertNotIn("_GUARD_NOS_INT", uops)
self.assertNotIn("_GUARD_TOS_INT", uops)
self.assertIn("_CALL_LEN", uops)


def global_identity(x):
return x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to int. Patch by Diego Russo
4 changes: 4 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@ dummy_func(void) {
sym_set_const(callable, (PyObject *)&PyUnicode_Type);
}

op(_CALL_LEN, (callable[1], self_or_null[1], args[oparg] -- res)) {
res = sym_new_type(ctx, &PyLong_Type);
}

// END BYTECODES //

}
2 changes: 1 addition & 1 deletion Python/optimizer_cases.c.h

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

Loading