Skip to content

Commit 19fc90f

Browse files
committed
gh-131798: JIT: optimize _LOAD_COMMON_CONSTANT
1 parent e0f7c10 commit 19fc90f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,25 @@ def testfunc(n):
28322832
self.assertIn("_GUARD_TYPE_VERSION", uops)
28332833
self.assertNotIn("_CHECK_ATTR_CLASS", uops)
28342834

2835+
def test_load_common_constant(self):
2836+
def testfunc(n):
2837+
x = 0
2838+
for _ in range(n):
2839+
y = list(i for i in (1, 2, 3))
2840+
if len(y) == 3:
2841+
x += 1
2842+
return x
2843+
res = testfunc(TIER2_THRESHOLD)
2844+
self.assertEqual(res, TIER2_THRESHOLD)
2845+
executors = get_all_executors(testfunc)
2846+
uops = [
2847+
opname
2848+
for executor in executors
2849+
for opname in get_opnames(executor)
2850+
]
2851+
self.assertIn("_BUILD_LIST", uops)
2852+
self.assertNotIn("_LOAD_COMMON_CONSTANT", uops)
2853+
28352854
def test_load_small_int(self):
28362855
def testfunc(n):
28372856
x = 0

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ dummy_func(void) {
631631
value = PyJitRef_Borrow(sym_new_const(ctx, val));
632632
}
633633

634+
op(_LOAD_COMMON_CONSTANT, (-- value)) {
635+
assert(oparg < NUM_COMMON_CONSTANTS);
636+
PyObject *val = _PyInterpreterState_GET()->common_consts[oparg];
637+
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
638+
value = PyJitRef_Borrow(sym_new_const(ctx, val));
639+
}
640+
634641
op(_LOAD_SMALL_INT, (-- value)) {
635642
PyObject *val = PyLong_FromLong(oparg);
636643
assert(val);

Python/optimizer_cases.c.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)