Skip to content

Commit 667a86e

Browse files
gh-131798: JIT: replace _CHECK_METHOD_VERSION with _CHECK_FUNCTION_VERSION_INLINE (GH-135022)
Signed-off-by: Manjusaka <[email protected]> Co-authored-by: Brandt Bucher <[email protected]>
1 parent 60181f4 commit 667a86e

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,21 @@ def testfunc(n):
13811381
# Removed guard
13821382
self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops)
13831383

1384+
def test_method_guards_removed_or_reduced(self):
1385+
def testfunc(n):
1386+
result = 0
1387+
for i in range(n):
1388+
result += test_bound_method(i)
1389+
return result
1390+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1391+
self.assertEqual(res, sum(range(TIER2_THRESHOLD)))
1392+
self.assertIsNotNone(ex)
1393+
uops = get_opnames(ex)
1394+
self.assertIn("_PUSH_FRAME", uops)
1395+
# Strength reduced version
1396+
self.assertIn("_CHECK_FUNCTION_VERSION_INLINE", uops)
1397+
self.assertNotIn("_CHECK_METHOD_VERSION", uops)
1398+
13841399
def test_jit_error_pops(self):
13851400
"""
13861401
Tests that the correct number of pops are inserted into the
@@ -2294,5 +2309,12 @@ def testfunc(n):
22942309
def global_identity(x):
22952310
return x
22962311

2312+
class TestObject:
2313+
def test(self, *args, **kwargs):
2314+
return args[0]
2315+
2316+
test_object = TestObject()
2317+
test_bound_method = TestObject.test.__get__(test_object)
2318+
22972319
if __name__ == "__main__":
22982320
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize ``_CHECK_METHOD_VERSION`` into ``_CHECK_FUNCTION_VERSION_INLINE`` in JIT-compiled code.

Python/optimizer_bytecodes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,16 @@ dummy_func(void) {
672672
sym_set_type(callable, &PyFunction_Type);
673673
}
674674

675+
op(_CHECK_METHOD_VERSION, (func_version/2, callable, null, unused[oparg] -- callable, null, unused[oparg])) {
676+
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyMethod_Type)) {
677+
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
678+
assert(PyMethod_Check(method));
679+
REPLACE_OP(this_instr, _CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
680+
this_instr->operand1 = (uintptr_t)method->im_func;
681+
}
682+
sym_set_type(callable, &PyMethod_Type);
683+
}
684+
675685
op(_CHECK_FUNCTION_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
676686
assert(sym_matches_type(callable, &PyFunction_Type));
677687
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)