8000 gh-131798: JIT: Narrow the return type of `_GET_LEN` to int by Zheaoli · Pull Request #133345 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue 8000

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 1 commit
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
Prev Previous commit
Next Next commit
fix review idea
Signed-off-by: Manjusaka <me@manjusaka.me>
  • Loading branch information
Zheaoli committed May 20, 2025
commit d1e3a2be92950ae536ccfab45f7eef4984b4b325
30 changes: 23 additions & 7 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,33 +1942,49 @@ def testfunc(n):

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

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

def test_get_len_with_immortal_tuple(self):
def testfunc(n):
class TestObject(tuple):
pass
x = 0.0
for _ in range(n):
match TestObject.__mro__:
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.assertIn("_GET_LEN", uops)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)


def test_binary_op_subscr_tuple_int(self):
def testfunc(n):
Expand Down
4 changes: 4 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pycore_uops.h"
#include "pycore_uop_ids.h"
#include "internal/pycore_moduleobject.h"
#include "refcount.h"

#define op(name, ...) /* NAME is ignored */

Expand Down Expand Up @@ -1128,6 +1129,9 @@ dummy_func(void) {
goto error;
}
len = sym_new_const(ctx, temp);
if (_Py_IsImmortal(temp)) {
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
}
Py_DECREF(temp);
}
}
Expand Down
3 changes: 3 additions & 0 deletions Python/optimizer_cases.c.h

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

0