Skip to content

Commit 7bc5cf6

Browse files
committed
gh-131798: JIT: replace _CHECK_METHOD_VERSION with _CHECK_FUNCTION_VERSION_INLINE
Signed-off-by: Manjusaka <me@manjusaka.me>
1 parent f49a07b commit 7bc5cf6

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,23 @@ def testfunc(n):
13701370
# Removed guard
13711371
self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops)
13721372

1373+
def test_method_guards_removed_or_reduced(self):
1374+
1375+
def testfunc(n):
1376+
for i in range(n):
1377+
test_bound_method(i)
1378+
1379+
1380+
testfunc(TIER2_THRESHOLD)
1381+
1382+
ex = get_first_executor(testfunc)
1383+
self.assertIsNotNone(ex)
1384+
uops = get_opnames(ex)
1385+
self.assertIn("_PUSH_FRAME", uops)
1386+
# Strength reduced version
1387+
self.assertIn("_CHECK_FUNCTION_VERSION_INLINE", uops)
1388+
self.assertNotIn("_CHECK_METHOD_VERSION", uops)
1389+
13731390
def test_jit_error_pops(self):
13741391
"""
13751392
Tests that the correct number of pops are inserted into the
@@ -2219,6 +2236,14 @@ def f(n):
22192236
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
22202237
self.assertNotIn("_LOAD_ATTR_METHOD_LAZY_DICT", uops)
22212238

2239+
class TestObject:
2240+
def test(self, *args, **kwargs):
2241+
return args[0]
2242+
2243+
temp_object = TestObject()
2244+
2245+
test_bound_method = TestObject.test.__get__(temp_object)
2246+
22222247

22232248
def global_identity(x):
22242249
return x

Python/optimizer_bytecodes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,16 @@ dummy_func(void) {
708708
sym_set_type(callable, &PyFunction_Type);
709709
}
710710

711+
op(_CHECK_METHOD_VERSION, (func_version/2, callable, null, unused[oparg] -- callable, null, unused[oparg])) {
712+
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyMethod_Type)) {
713+
assert(PyMethod_Check(sym_get_const(ctx, callable)));
714+
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
715+
REPLACE_OP(this_instr, _CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
716+
this_instr->operand1 = (uintptr_t)method->im_func;
717+
}
718+
sym_set_type(callable, &PyMethod_Type);
719+
}
720+
711721
op(_CHECK_FUNCTION_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
712722
assert(sym_matches_type(callable, &PyFunction_Type));
713723
if (sym_is_const(ctx, callable)) {

Python/optimizer_cases.c.h

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)