https://github.com/python/cpython/commit/98977ca4334b7f7738ff2ed9238616143a1dc69e
commit: 98977ca4334b7f7738ff2ed9238616143a1dc69e
branch: main
author: Mingzhu Yan <[email protected]>
committer: Fidget-Spinner <[email protected]>
date: 2026-03-20T05:55:22+08:00
summary:

gh-131798: JIT: optimize _LOAD_COMMON_CONSTANT (GH-146104)

files:
M Lib/test/test_capi/test_opt.py
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h

diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 05951771c3b92e..df127b55be4068 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -2832,6 +2832,18 @@ def testfunc(n):
         self.assertIn("_GUARD_TYPE_VERSION", uops)
         self.assertNotIn("_CHECK_ATTR_CLASS", uops)
 
+    def test_load_common_constant(self):
+        def testfunc(n):
+            for _ in range(n):
+                x = list(i for i in ())
+            return x
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, list(()))
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+        self.assertIn("_BUILD_LIST", uops)
+        self.assertNotIn("_LOAD_COMMON_CONSTANT", uops)
+
     def test_load_small_int(self):
         def testfunc(n):
             x = 0
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index 9cb701d6a86f59..fff05f06f647c2 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -631,6 +631,13 @@ dummy_func(void) {
         value = PyJitRef_Borrow(sym_new_const(ctx, val));
     }
 
+    op(_LOAD_COMMON_CONSTANT, (-- value)) {
+        assert(oparg < NUM_COMMON_CONSTANTS);
+        PyObject *val = _PyInterpreterState_GET()->common_consts[oparg];
+        ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
+        value = PyJitRef_Borrow(sym_new_const(ctx, val));
+    }
+
     op(_LOAD_SMALL_INT, (-- value)) {
         PyObject *val = PyLong_FromLong(oparg);
         assert(val);
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 481e099a3a37b0..1d1c62f9949e96 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -1435,7 +1435,10 @@
 
         case _LOAD_COMMON_CONSTANT: {
             JitOptRef value;
-            value = sym_new_not_null(ctx);
+            assert(oparg < NUM_COMMON_CONSTANTS);
+            PyObject *val = _PyInterpreterState_GET()->common_consts[oparg];
+            ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
+            value = PyJitRef_Borrow(sym_new_const(ctx, val));
             CHECK_STACK_BOUNDS(1);
             stack_pointer[0] = value;
             stack_pointer += 1;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to