]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-131798: Turn _LOAD_SMALL_INT into _LOAD_CONST_INLINE_BORROW in the JIT (GH-134406)
authorNadeshiko Manju <me@manjusaka.me>
Thu, 22 May 2025 13:54:57 +0000 (21:54 +0800)
committerGitHub <noreply@github.com>
Thu, 22 May 2025 13:54:57 +0000 (09:54 -0400)
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-05-21-13-57-26.gh-issue-131798.QwS5Bb.rst [new file with mode: 0644]
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 98b434313e4d2d5051d8638b22c4ee0f0cabf413..2b777acb1ec4fd1f5d197fb421803fe0cc64bc38 100644 (file)
@@ -2156,6 +2156,18 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertIn("_GUARD_TYPE_VERSION", uops)
         self.assertNotIn("_CHECK_ATTR_CLASS", uops)
 
+    def test_load_small_int(self):
+        def testfunc(n):
+            x = 0
+            for i in range(n):
+                x += 1
+            return x
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, TIER2_THRESHOLD)
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+        self.assertNotIn("_LOAD_SMALL_INT", uops)
+        self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
 
 def global_identity(x):
     return x
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-05-21-13-57-26.gh-issue-131798.QwS5Bb.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-21-13-57-26.gh-issue-131798.QwS5Bb.rst
new file mode 100644 (file)
index 0000000..f873bbf
--- /dev/null
@@ -0,0 +1 @@
+JIT: replace ``_LOAD_SMALL_INT`` with ``_LOAD_CONST_INLINE_BORROW``
index f12cd7b968cffc45c47b36f167700db1aaac010a..b9ebd8678e0f1e7cc1fa99b3f656574113afa175 100644 (file)
@@ -528,7 +528,10 @@ dummy_func(void) {
     }
 
     op(_LOAD_SMALL_INT, (-- value)) {
-        PyObject *val = PyLong_FromLong(this_instr->oparg);
+        PyObject *val = PyLong_FromLong(oparg);
+        assert(val);
+        assert(_Py_IsImmortal(val));
+        REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
         value = sym_new_const(ctx, val);
     }
 
index 602f5e2cfaf77e46bc6ab2e0bbc2c21f691b673b..0ba45e1f58fe08f37f6958cce46ff5c64746c2f1 100644 (file)
 
         case _LOAD_SMALL_INT: {
             JitOptSymbol *value;
-            PyObject *val = PyLong_FromLong(this_instr->oparg);
+            PyObject *val = PyLong_FromLong(oparg);
+            assert(val);
+            assert(_Py_IsImmortal(val));
+            REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
             value = sym_new_const(ctx, val);
             stack_pointer[0] = value;
             stack_pointer += 1;