GeezerVison is showing github.com — gh-131798: JIT: Narrow the return type of `_GET_LEN` to int by Zheaoli · Pull Request #133345 · python/cpython
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cabe8f1
gh-131798: JIT: Narrow the return type of _GET_LEN to int
Zheaoli May 3, 2025
1f8a607
Add news
Zheaoli May 3, 2025
f2f71d9
Fix typo
Zheaoli May 3, 2025
1662b47
Fix review idea
Zheaoli May 8, 2025
52ce698
Fix review idea
Zheaoli May 8, 2025
b1640ed
Fix review idea
Zheaoli May 8, 2025
cf17d92
fix review idea
Zheaoli May 8, 2025
c401539
fix review idea
Zheaoli May 10, 2025
619a846
fix review idea
Zheaoli May 10, 2025
9adefc3
fix review idea
Zheaoli May 10, 2025
9c6efcb
fix review idea
Zheaoli May 10, 2025
69b929b
Merge branch 'main' into manjusaka/get-len
Zheaoli May 20, 2025
d1e3a2b
fix review idea
Zheaoli May 20, 2025
e0e3745
fix review idea
Zheaoli May 20, 2025
81c4996
fix review idea
Zheaoli May 20, 2025
93e1e86
fix review idea
Zheaoli May 20, 2025
6b5282f
fix review idea
Zheaoli May 20, 2025
2eefeb3
fix review idea
Zheaoli May 20, 2025
9c58f2c
fix review idea
Zheaoli May 20, 2025
f9822e9
fix review idea
Zheaoli May 20, 2025
6a06aff
fix review idea
Zheaoli May 20, 2025
6d92614
fix review idea
Zheaoli May 20, 2025
5cfbad4
format code
Zheaoli May 20, 2025
File filter
Filter by extension
.c
(1)
.h
(1)
.py
(1)
.rst
(1)
All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
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)
Comment thread
Zheaoli marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
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.
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);
Comment thread
Zheaoli marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
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);
Comment thread
Zheaoli marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.