https://github.com/python/cpython/commit/8c5e5557c64a8d6310b2a958f576d5eb245cb1b6
commit: 8c5e5557c64a8d6310b2a958f576d5eb245cb1b6
branch: main
author: Nadeshiko Manju <m...@manjusaka.me>
committer: brandtbucher <brandtbuc...@gmail.com>
date: 2025-05-22T09:54:57-04:00
summary:

GH-131798: Turn _LOAD_SMALL_INT into _LOAD_CONST_INLINE_BORROW in the JIT 
(GH-134406)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-05-21-13-57-26.gh-issue-131798.QwS5Bb.rst
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 98b434313e4d2d..2b777acb1ec4fd 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -2156,6 +2156,18 @@ def testfunc(n):
         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
index 00000000000000..f873bbfb4dcb68
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-21-13-57-26.gh-issue-131798.QwS5Bb.rst
@@ -0,0 +1 @@
+JIT: replace ``_LOAD_SMALL_INT`` with ``_LOAD_CONST_INLINE_BORROW``
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index f12cd7b968cffc..b9ebd8678e0f1e 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -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);
     }
 
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 602f5e2cfaf77e..0ba45e1f58fe08 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -79,7 +79,10 @@
 
         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;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to