Skip to content

Commit e998eb9

Browse files
authored
gh-148604: change ADD_OP(_POP_TOP, ...) to optimize_pop_top in optimizer_bytecodes.c (GH-148619)
1 parent 55c9d60 commit e998eb9

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,9 @@ def testfunc(n):
26422642
uops = get_opnames(ex)
26432643
# When the result of type(...) is known, _CALL_TYPE_1 is decomposed.
26442644
self.assertNotIn("_CALL_TYPE_1", uops)
2645+
# _CALL_TYPE_1 produces 2 _POP_TOP_NOP (callable and null)
2646+
# type(42) is int produces 4 _POP_TOP_NOP
2647+
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 6)
26452648

26462649
def test_call_type_1_result_is_const(self):
26472650
def testfunc(n):
@@ -3453,6 +3456,10 @@ def f(n):
34533456
self.assertEqual(res, TIER2_THRESHOLD)
34543457
uops = get_opnames(ex)
34553458
self.assertNotIn("_LOAD_SPECIAL", uops)
3459+
# __enter__/__exit__ produce 2 _POP_TOP_NOP
3460+
# x += 1 produces 2 _POP_TOP_NOP
3461+
# __exit__()'s None return produces 1 _POP_TOP_NOP
3462+
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 5)
34563463

34573464
def test_store_fast_refcount_elimination(self):
34583465
def foo(x):

Python/optimizer_bytecodes.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,13 +1480,13 @@ dummy_func(void) {
14801480
next = sym_new_type(ctx, &PyLong_Type);
14811481
}
14821482

1483-
op(_CALL_TYPE_1, (unused, unused, arg -- res, a)) {
1483+
op(_CALL_TYPE_1, (callable, null, arg -- res, a)) {
14841484
PyObject* type = (PyObject *)sym_get_type(arg);
14851485
if (type) {
14861486
res = sym_new_const(ctx, type);
14871487
ADD_OP(_SWAP, 3, 0);
1488-
ADD_OP(_POP_TOP, 0, 0);
1489-
ADD_OP(_POP_TOP, 0, 0);
1488+
optimize_pop_top(ctx, this_instr, callable);
1489+
optimize_pop_top(ctx, this_instr, null);
14901490
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
14911491
ADD_OP(_SWAP, 2, 0);
14921492
}
@@ -1509,7 +1509,7 @@ dummy_func(void) {
15091509
a = arg;
15101510
}
15111511

1512-
op(_CALL_ISINSTANCE, (unused, unused, instance, cls -- res)) {
1512+
op(_CALL_ISINSTANCE, (callable, null, instance, cls -- res)) {
15131513
// the result is always a bool, but sometimes we can
15141514
// narrow it down to True or False
15151515
res = sym_new_type(ctx, &PyBool_Type);
@@ -1525,10 +1525,10 @@ dummy_func(void) {
15251525
out = Py_True;
15261526
}
15271527
sym_set_const(res, out);
1528-
ADD_OP(_POP_TOP, 0, 0);
1529-
ADD_OP(_POP_TOP, 0, 0);
1530-
ADD_OP(_POP_TOP_NOP, 0, 0);
1531-
ADD_OP(_POP_TOP, 0, 0);
1528+
optimize_pop_top(ctx, this_instr, cls);
1529+
optimize_pop_top(ctx, this_instr, instance);
1530+
optimize_pop_top(ctx, this_instr, null);
1531+
optimize_pop_top(ctx, this_instr, callable);
15321532
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out);
15331533
}
15341534
}
@@ -1902,7 +1902,7 @@ dummy_func(void) {
19021902
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
19031903
0, (uintptr_t)descr);
19041904
ADD_OP(_SWAP, 3, 0);
1905-
ADD_OP(_POP_TOP, 0, 0);
1905+
optimize_pop_top(ctx, this_instr, method_and_self[0]);
19061906
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
19071907
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
19081908
_Py_BloomFilter_Add(dependencies, type);

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)