From e10861959d031120fdc2fb58cb7033c6c6bd1092 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Sun, 14 Dec 2025 16:45:52 -0800 Subject: [PATCH 1/4] Remove redundant refcount --- Include/internal/pycore_opcode_metadata.h | 2 +- Include/internal/pycore_uop_ids.h | 2 +- Include/internal/pycore_uop_metadata.h | 6 +++--- Lib/test/test_capi/test_opt.py | 18 ++++++++++++++++++ Python/bytecodes.c | 8 +++++--- Python/executor_cases.c.h | 14 ++++++++++---- Python/generated_cases.c.h | 15 +++++++++++++-- Python/optimizer_cases.c.h | 7 +++++-- 8 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 0a29fabe7676dc..be7c08b65b644f 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -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 = 3, .uops = { { _GUARD_TYPE_VERSION_AND_LOCK, 2, 1 }, { _GUARD_DORV_NO_DICT, OPARG_SIMPLE, 3 }, { _STORE_ATTR_INSTANCE_VALUE, 1, 3 } } }, - [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 } } }, diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 64e51bd2b8bb58..75ca8524db60c5 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -1049,7 +1049,7 @@ extern "C" { #define _START_EXECUTOR_r00 1242 #define _STORE_ATTR_r20 1243 #define _STORE_ATTR_INSTANCE_VALUE_r20 1244 -#define _STORE_ATTR_SLOT_r20 1245 +#define _STORE_ATTR_SLOT_r21 1245 #define _STORE_ATTR_WITH_HINT_r20 1246 #define _STORE_DEREF_r10 1247 #define _STORE_FAST_r10 1248 diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index 5fa375a8ce6b4a..b05dfa868c464d 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -1876,7 +1876,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 }, }, }, @@ -3589,7 +3589,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = { [_GUARD_DORV_NO_DICT_r33] = _GUARD_DORV_NO_DICT, [_STORE_ATTR_INSTANCE_VALUE_r20] = _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, @@ -4835,7 +4835,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = { [_STORE_ATTR_INSTANCE_VALUE] = "_STORE_ATTR_INSTANCE_VALUE", [_STORE_ATTR_INSTANCE_VALUE_r20] = "_STORE_ATTR_INSTANCE_VALUE_r20", [_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", diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 0f6ed3d85f0330..56629be89bf36d 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2517,6 +2517,24 @@ def testfunc(n): self.assertNotIn("_POP_TOP_INT", uops) self.assertIn("_POP_TOP_NOP", uops) + 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", uops) + def test_attr_promotion_failure(self): # We're not testing for any specific uops here, just # testing it doesn't crash. diff --git a/Python/bytecodes.c b/Python/bytecodes.c index daa3d218e387f9..9ad75aa165dc84 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2668,7 +2668,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)); @@ -2677,14 +2677,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, diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 2a1156091e3d37..211d62f548ccfe 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -8873,11 +8873,12 @@ break; } - case _STORE_ATTR_SLOT_r20: { + case _STORE_ATTR_SLOT_r21: { CHECK_CURRENT_CACHED_VALUES(2); assert(WITHIN_STACK_BOUNDS_WITH_CACHE()); _PyStackRef owner; _PyStackRef value; + _PyStackRef o; _PyStackRef _stack_item_0 = _tos_cache0; _PyStackRef _stack_item_1 = _tos_cache1; owner = _stack_item_1; @@ -8894,14 +8895,19 @@ 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_WITH_CACHE()); break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index ab9373e0af5afc..a6cae48e88f326 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -10549,6 +10549,7 @@ static_assert(INLINE_CACHE_ENTRIES_STORE_ATTR == 4, "incorrect cache size"); _PyStackRef owner; _PyStackRef value; + _PyStackRef o; /* Skip 1 cache entry */ // _GUARD_TYPE_VERSION { @@ -10577,13 +10578,23 @@ 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(); } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 1a3d4ad50bd824..e71493fb98fdfb 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1904,8 +1904,11 @@ } case _STORE_ATTR_SLOT: { - CHECK_STACK_BOUNDS(-2); - stack_pointer += -2; + JitOptRef o; + o = sym_new_not_null(ctx); + CHECK_STACK_BOUNDS(-1); + stack_pointer[-2] = o; + stack_pointer += -1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; } From 5728410f61bad4d7f3df943945e6e3de2f02ef94 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Sun, 14 Dec 2025 17:20:59 -0800 Subject: [PATCH 2/4] Add _STORE_ATTR_SLOT to optimizer bytecodes --- Lib/test/test_capi/test_opt.py | 2 +- Python/optimizer_bytecodes.c | 6 ++++++ Python/optimizer_cases.c.h | 9 ++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 56629be89bf36d..2344e6eea14971 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2533,7 +2533,7 @@ def testfunc(n): self.assertIsNotNone(ex) uops = get_opnames(ex) self.assertIn("_STORE_ATTR_SLOT", uops) - self.assertIn("_POP_TOP", uops) + self.assertIn("_POP_TOP_NOP", uops) def test_attr_promotion_failure(self): # We're not testing for any specific uops here, just diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 66aecf7ef54355..fa18727cb0c283 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -109,6 +109,12 @@ dummy_func(void) { ss = sub_st; } + op(_STORE_ATTR_SLOT, (index/1, value, owner -- o)) { + (void)index; + (void)value; + o = owner; + } + op(_PUSH_NULL, (-- res)) { res = sym_new_null(ctx); } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index e71493fb98fdfb..fef809745be931 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1904,8 +1904,15 @@ } case _STORE_ATTR_SLOT: { + JitOptRef owner; + JitOptRef value; JitOptRef o; - o = sym_new_not_null(ctx); + 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; From fea9f7d582fa3dba0c6b6597ee94ac517437af84 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 15 Dec 2025 12:39:01 -0800 Subject: [PATCH 3/4] lint --- Python/optimizer_bytecodes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 13a77e54af0e24..5e64dcd58a8e59 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -119,7 +119,7 @@ dummy_func(void) { (void)value; o = owner; } - + op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) { (void)value; st = dict_st; From 3e0b77607f6bff09eaf5fc725e34114d3789e72a Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 15 Dec 2025 14:04:36 -0800 Subject: [PATCH 4/4] Fix bad merge --- Lib/test/test_capi/test_opt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index b333161f1af746..c5d6b7d3dcc4c2 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2581,6 +2581,7 @@ def testfunc(n): uops = get_opnames(ex) self.assertIn("_STORE_SUBSCR_DICT", uops) self.assertNotIn("_POP_TOP", uops) + self.assertIn("_POP_TOP_NOP", uops) def test_attr_promotion_failure(self): # We're not testing for any specific uops here, just