]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131798: JIT: optimize _LOAD_COMMON_CONSTANT (GH-146104)
authorMingzhu Yan <69898423+trdthg@users.noreply.github.com>
Thu, 19 Mar 2026 21:55:22 +0000 (05:55 +0800)
committerGitHub <noreply@github.com>
Thu, 19 Mar 2026 21:55:22 +0000 (05:55 +0800)
Lib/test/test_capi/test_opt.py
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 05951771c3b92e7bc95d51b36653e8a7481c8803..df127b55be40680bc780f42070e70653586b6695 100644 (file)
@@ -2832,6 +2832,18 @@ class TestUopsOptimization(unittest.TestCase):
         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
index 9cb701d6a86f59e71395bd7c82da9dd3ef8b3db8..fff05f06f647c2dab101da6a394b2ef5934b7b51 100644 (file)
@@ -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);
index 481e099a3a37b0ee110f07438d5465049c6f52a3..1d1c62f9949e96d4fa512325822d7d35dfb96548 100644 (file)
 
         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;