Skip to content

Commit 72a851a

Browse files
committed
gh-134584: Eliminate redundant refcounting from _LOAD_ATTR_SLOT
Signed-off-by: Manjusaka <me@manjusaka.me>
1 parent c521597 commit 72a851a

File tree

10 files changed

+53
-25
lines changed

10 files changed

+53
-25
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,25 @@ class C:
25732573
self.assertNotIn("_POP_TOP", uops)
25742574
self.assertIn("_POP_TOP_NOP", uops)
25752575

2576+
def test_load_addr_slot(self):
2577+
def testfunc(n):
2578+
class C:
2579+
__slots__ = ('x',)
2580+
c = C()
2581+
c.x = 42
2582+
x = 0
2583+
for _ in range(n):
2584+
x += c.x
2585+
return x
2586+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2587+
self.assertEqual(res, 42 * TIER2_THRESHOLD)
2588+
self.assertIsNotNone(ex)
2589+
uops = get_opnames(ex)
2590+
2591+
self.assertIn("_LOAD_ATTR_SLOT", uops)
2592+
self.assertNotIn("_POP_TOP", uops)
2593+
self.assertIn("_POP_TOP_NOP", uops)
2594+
25762595
def test_int_add_op_refcount_elimination(self):
25772596
def testfunc(n):
25782597
c = 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Eliminate redundant refcounting from ``_LOAD_ATTR_SLOT``.

Python/bytecodes.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ dummy_func(
24852485
unused/5 +
24862486
_PUSH_NULL_CONDITIONAL;
24872487

2488-
op(_LOAD_ATTR_SLOT, (index/1, owner -- attr)) {
2488+
op(_LOAD_ATTR_SLOT, (index/1, owner -- attr, o)) {
24892489
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
24902490

24912491
PyObject **addr = (PyObject **)((char *)owner_o + index);
@@ -2498,13 +2498,16 @@ dummy_func(
24982498
attr = PyStackRef_FromPyObjectNew(attr_o);
24992499
#endif
25002500
STAT_INC(LOAD_ATTR, hit);
2501+
o = owner;
2502+
DEAD(owner);
25012503
DECREF_INPUTS();
25022504
}
25032505

25042506
macro(LOAD_ATTR_SLOT) =
25052507
unused/1 +
25062508
_GUARD_TYPE_VERSION +
25072509
_LOAD_ATTR_SLOT + // NOTE: This action may also deopt
2510+
POP_TOP +
25082511
unused/5 +
25092512
_PUSH_NULL_CONDITIONAL;
25102513

Python/executor_cases.c.h

Lines changed: 5 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ dummy_func(void) {
661661
o = owner;
662662
}
663663

664-
op(_LOAD_ATTR_SLOT, (index/1, owner -- attr)) {
664+
op(_LOAD_ATTR_SLOT, (index/1, owner -- attr, o)) {
665665
attr = sym_new_not_null(ctx);
666666
(void)index;
667+
o = owner;
667668
}
668669

669670
op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr)) {

Python/optimizer_cases.c.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)