]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-134584: Remove redundant refcount from `_STORE_ATTR_SLOT` (#142729)
authorSavannah Ostrowski <savannah@python.org>
Mon, 15 Dec 2025 23:18:44 +0000 (15:18 -0800)
committerGitHub <noreply@github.com>
Mon, 15 Dec 2025 23:18:44 +0000 (15:18 -0800)
Include/internal/pycore_opcode_metadata.h
Include/internal/pycore_uop_ids.h
Include/internal/pycore_uop_metadata.h
Lib/test/test_capi/test_opt.py
Python/bytecodes.c
Python/executor_cases.c.h
Python/generated_cases.c.h
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index cbbbbcedf2bed276d91911b27b16152d50d22cba..ce8a26c551bf175517ddbcea7d827cb2a20a91aa 100644 (file)
@@ -1483,7 +1483,7 @@ _PyOpcode_macro_expansion[256] = {
     [SET_UPDATE] = { .nuops = 1, .uops = { { _SET_UPDATE, OPARG_SIMPLE, 0 } } },
     [STORE_ATTR] = { .nuops = 1, .uops = { { _STORE_ATTR, OPARG_SIMPLE, 3 } } },
     [STORE_ATTR_INSTANCE_VALUE] = { .nuops = 4, .uops = { { _GUARD_TYPE_VERSION_AND_LOCK, 2, 1 }, { _GUARD_DORV_NO_DICT, OPARG_SIMPLE, 3 }, { _STORE_ATTR_INSTANCE_VALUE, 1, 3 }, { _POP_TOP, OPARG_SIMPLE, 4 } } },
-    [STORE_ATTR_SLOT] = { .nuops = 2, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _STORE_ATTR_SLOT, 1, 3 } } },
+    [STORE_ATTR_SLOT] = { .nuops = 3, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _STORE_ATTR_SLOT, 1, 3 }, { _POP_TOP, OPARG_SIMPLE, 4 } } },
     [STORE_ATTR_WITH_HINT] = { .nuops = 2, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _STORE_ATTR_WITH_HINT, 1, 3 } } },
     [STORE_DEREF] = { .nuops = 1, .uops = { { _STORE_DEREF, OPARG_SIMPLE, 0 } } },
     [STORE_FAST] = { .nuops = 1, .uops = { { _STORE_FAST, OPARG_SIMPLE, 0 } } },
index 69bd9af099161d5dc9b3e21e46bf1ed422c78f94..df623f49b0d52cf8d47b705b1dc5cab177001c6e 100644 (file)
@@ -1035,7 +1035,7 @@ extern "C" {
 #define _START_EXECUTOR_r00 1228
 #define _STORE_ATTR_r20 1229
 #define _STORE_ATTR_INSTANCE_VALUE_r21 1230
-#define _STORE_ATTR_SLOT_r20 1231
+#define _STORE_ATTR_SLOT_r21 1231
 #define _STORE_ATTR_WITH_HINT_r20 1232
 #define _STORE_DEREF_r10 1233
 #define _STORE_FAST_r10 1234
index d06eba97ccf785f3c6bb748fb0bf2f84e9228df4..f600468c321642527f496bcde5b353b2f922cc46 100644 (file)
@@ -1846,7 +1846,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = {
         .entries = {
             { -1, -1, -1 },
             { -1, -1, -1 },
-            { 0, 2, _STORE_ATTR_SLOT_r20 },
+            { 1, 2, _STORE_ATTR_SLOT_r21 },
             { -1, -1, -1 },
         },
     },
@@ -3545,7 +3545,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = {
     [_GUARD_DORV_NO_DICT_r33] = _GUARD_DORV_NO_DICT,
     [_STORE_ATTR_INSTANCE_VALUE_r21] = _STORE_ATTR_INSTANCE_VALUE,
     [_STORE_ATTR_WITH_HINT_r20] = _STORE_ATTR_WITH_HINT,
-    [_STORE_ATTR_SLOT_r20] = _STORE_ATTR_SLOT,
+    [_STORE_ATTR_SLOT_r21] = _STORE_ATTR_SLOT,
     [_COMPARE_OP_r21] = _COMPARE_OP,
     [_COMPARE_OP_FLOAT_r01] = _COMPARE_OP_FLOAT,
     [_COMPARE_OP_FLOAT_r11] = _COMPARE_OP_FLOAT,
@@ -4780,7 +4780,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = {
     [_STORE_ATTR_INSTANCE_VALUE] = "_STORE_ATTR_INSTANCE_VALUE",
     [_STORE_ATTR_INSTANCE_VALUE_r21] = "_STORE_ATTR_INSTANCE_VALUE_r21",
     [_STORE_ATTR_SLOT] = "_STORE_ATTR_SLOT",
-    [_STORE_ATTR_SLOT_r20] = "_STORE_ATTR_SLOT_r20",
+    [_STORE_ATTR_SLOT_r21] = "_STORE_ATTR_SLOT_r21",
     [_STORE_ATTR_WITH_HINT] = "_STORE_ATTR_WITH_HINT",
     [_STORE_ATTR_WITH_HINT_r20] = "_STORE_ATTR_WITH_HINT_r20",
     [_STORE_DEREF] = "_STORE_DEREF",
index 5a52d7609d2cf1d3842eb1cbb6b88b4c36e54369..0fa74b9e8fae27e5df6b5c572ec62eacf90d85ba 100644 (file)
@@ -2612,7 +2612,25 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertNotIn("_POP_TOP_INT", uops)
         self.assertIn("_POP_TOP_NOP", uops)
 
-    def test_store_susbscr_dict(self):
+    def test_store_attr_slot(self):
+        class C:
+            __slots__ = ('x',)
+
+        def testfunc(n):
+            c = C()
+            for _ in range(n):
+                c.x = 42
+                y = c.x
+            return y
+
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, 42)
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+        self.assertIn("_STORE_ATTR_SLOT", uops)
+        self.assertIn("_POP_TOP_NOP", uops)
+
+    def test_store_subscr_dict(self):
         def testfunc(n):
             d = {}
             for _ in range(n):
index 8420d09e81903698758f02558b17eca8a3e967d8..0dbfe962684e208bcad773710fc704ea94e5a016 100644 (file)
@@ -2640,7 +2640,7 @@ dummy_func(
             _GUARD_TYPE_VERSION +
             _STORE_ATTR_WITH_HINT;
 
-        op(_STORE_ATTR_SLOT, (index/1, value, owner --)) {
+        op(_STORE_ATTR_SLOT, (index/1, value, owner -- o)) {
             PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
 
             DEOPT_IF(!LOCK_OBJECT(owner_o));
@@ -2649,14 +2649,16 @@ dummy_func(
             PyObject *old_value = *(PyObject **)addr;
             FT_ATOMIC_STORE_PTR_RELEASE(*(PyObject **)addr, PyStackRef_AsPyObjectSteal(value));
             UNLOCK_OBJECT(owner_o);
-            PyStackRef_CLOSE(owner);
+            INPUTS_DEAD();
+            o = owner;
             Py_XDECREF(old_value);
         }
 
         macro(STORE_ATTR_SLOT) =
             unused/1 +
             _GUARD_TYPE_VERSION +
-            _STORE_ATTR_SLOT;
+            _STORE_ATTR_SLOT +
+            POP_TOP;
 
         family(COMPARE_OP, INLINE_CACHE_ENTRIES_COMPARE_OP) = {
             COMPARE_OP_FLOAT,
index b4d4684f8785b132040daf3636c71cedf513c855..b2ce7d0d182215afca5a53093cbeee04b4295688 100644 (file)
             break;
         }
 
-        case _STORE_ATTR_SLOT_r20: {
+        case _STORE_ATTR_SLOT_r21: {
             CHECK_CURRENT_CACHED_VALUES(2);
             assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
             _PyStackRef owner;
             _PyStackRef value;
+            _PyStackRef o;
             _PyStackRef _stack_item_0 = _tos_cache0;
             _PyStackRef _stack_item_1 = _tos_cache1;
             owner = _stack_item_1;
             PyObject *old_value = *(PyObject **)addr;
             FT_ATOMIC_STORE_PTR_RELEASE(*(PyObject **)addr, PyStackRef_AsPyObjectSteal(value));
             UNLOCK_OBJECT(owner_o);
+            o = owner;
+            stack_pointer[0] = o;
+            stack_pointer += 1;
+            ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             _PyFrame_SetStackPointer(frame, stack_pointer);
-            PyStackRef_CLOSE(owner);
             Py_XDECREF(old_value);
             stack_pointer = _PyFrame_GetStackPointer(frame);
-            _tos_cache0 = PyStackRef_ZERO_BITS;
+            _tos_cache0 = o;
             _tos_cache1 = PyStackRef_ZERO_BITS;
             _tos_cache2 = PyStackRef_ZERO_BITS;
-            SET_CURRENT_CACHED_VALUES(0);
+            SET_CURRENT_CACHED_VALUES(1);
+            stack_pointer += -1;
+            ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
             break;
         }
index 3b8ad4565af987806eb90d3549f07e9c1ff55ec1..2811a2ec8ac82dd7a9dc0c6a3d6112578c6c015f 100644 (file)
             static_assert(INLINE_CACHE_ENTRIES_STORE_ATTR == 4, "incorrect cache size");
             _PyStackRef owner;
             _PyStackRef value;
+            _PyStackRef o;
             /* Skip 1 cache entry */
             // _GUARD_TYPE_VERSION
             {
                 PyObject *old_value = *(PyObject **)addr;
                 FT_ATOMIC_STORE_PTR_RELEASE(*(PyObject **)addr, PyStackRef_AsPyObjectSteal(value));
                 UNLOCK_OBJECT(owner_o);
-                stack_pointer += -2;
+                o = owner;
+                stack_pointer[-2] = o;
+                stack_pointer += -1;
                 ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
                 _PyFrame_SetStackPointer(frame, stack_pointer);
-                PyStackRef_CLOSE(owner);
                 Py_XDECREF(old_value);
                 stack_pointer = _PyFrame_GetStackPointer(frame);
             }
+            // _POP_TOP
+            {
+                value = o;
+                stack_pointer += -1;
+                ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+                _PyFrame_SetStackPointer(frame, stack_pointer);
+                PyStackRef_XCLOSE(value);
+                stack_pointer = _PyFrame_GetStackPointer(frame);
+            }
             DISPATCH();
         }
 
index b97b4c35d9aab3eaddc8444663c74c7f8ded1192..decf98865aea79bb53dd1df0eee656d115fdc6f5 100644 (file)
@@ -114,6 +114,12 @@ dummy_func(void) {
         ss = sub_st;
     }
 
+    op(_STORE_ATTR_SLOT, (index/1, value, owner -- o)) {
+        (void)index;
+        (void)value;
+        o = owner;
+    }
+
     op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) {
         (void)value;
         st = dict_st;
index 0d56464ea91c88d7be6234562c4c3087255ee0b9..cdab0fd2ef872ecebaa0a6a74fd1edac2211ecfd 100644 (file)
         }
 
         case _STORE_ATTR_SLOT: {
-            CHECK_STACK_BOUNDS(-2);
-            stack_pointer += -2;
+            JitOptRef owner;
+            JitOptRef value;
+            JitOptRef o;
+            owner = stack_pointer[-1];
+            value = stack_pointer[-2];
+            uint16_t index = (uint16_t)this_instr->operand0;
+            (void)index;
+            (void)value;
+            o = owner;
+            CHECK_STACK_BOUNDS(-1);
+            stack_pointer[-2] = o;
+            stack_pointer += -1;
             ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             break;
         }