Skip to content

Commit 1571165

Browse files
also for COMPARE_OP_INT
1 parent 899c0e0 commit 1571165

File tree

9 files changed

+69
-65
lines changed

9 files changed

+69
-65
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: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,7 @@ def testfunc(n):
16461646
self.assertNotIn("_COMPARE_OP", uops)
16471647
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
16481648

1649+
@unittest.skip("TODO (gh-142764): Re-enable after we get back automatic constant propagation.")
16491650
def test_compare_op_int_pop_two_load_const_inline_borrow(self):
16501651
def testfunc(n):
16511652
x = 0
@@ -2065,8 +2066,9 @@ def testfunc(n):
20652066
uops = get_opnames(ex)
20662067
self.assertIn("_CALL_TUPLE_1", uops)
20672068
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
2068-
self.assertNotIn("_COMPARE_OP_INT", uops)
2069-
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
2069+
# TODO (gh-142764): Re-enable after we get back automatic constant propagation.
2070+
# self.assertNotIn("_COMPARE_OP_INT", uops)
2071+
# self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
20702072

20712073
def test_call_len(self):
20722074
def testfunc(n):
@@ -2131,8 +2133,9 @@ class C:
21312133
# length allows us to optimize more code, such as conditionals
21322134
# in this case
21332135
self.assertIn("_CALL_LEN", uops)
2134-
self.assertNotIn("_COMPARE_OP_INT", uops)
2135-
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
2136+
# TODO (gh-142764): Re-enable after we get back automatic constant propagation.
2137+
# self.assertNotIn("_COMPARE_OP_INT", uops)
2138+
# self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
21362139

21372140
def test_call_builtin_o(self):
21382141
def testfunc(n):
@@ -2207,8 +2210,9 @@ def testfunc(n):
22072210
self.assertIsNotNone(ex)
22082211
uops = get_opnames(ex)
22092212
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
2210-
self.assertNotIn("_COMPARE_OP_INT", uops)
2211-
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
2213+
# TODO (gh-142764): Re-enable after we get back automatic constant propagation.
2214+
# self.assertNotIn("_COMPARE_OP_INT", uops)
2215+
# self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
22122216

22132217
def test_call_isinstance_guards_removed(self):
22142218
def testfunc(n):
@@ -2514,6 +2518,21 @@ def testfunc(n):
25142518
self.assertIn("_POP_TOP_NOP", uops)
25152519
self.assertNotIn("_POP_TOP", uops)
25162520

2521+
def test_int_cmp_op_refcount_elimination(self):
2522+
def testfunc(n):
2523+
c = 1
2524+
res = 0
2525+
for _ in range(n):
2526+
res = c == c
2527+
return res
2528+
2529+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2530+
self.assertIsNotNone(ex)
2531+
uops = get_opnames(ex)
2532+
self.assertIn("_COMPARE_OP_INT", uops)
2533+
self.assertIn("_POP_TOP_NOP", uops)
2534+
self.assertNotIn("_POP_TOP", uops)
2535+
25172536
def test_remove_guard_for_slice_list(self):
25182537
def f(n):
25192538
for i in range(n):

Python/bytecodes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,7 +2701,7 @@ dummy_func(
27012701
_GUARD_TOS_FLOAT + _GUARD_NOS_FLOAT + unused/1 + _COMPARE_OP_FLOAT;
27022702

27032703
macro(COMPARE_OP_INT) =
2704-
_GUARD_TOS_INT + _GUARD_NOS_INT + unused/1 + _COMPARE_OP_INT;
2704+
_GUARD_TOS_INT + _GUARD_NOS_INT + unused/1 + _COMPARE_OP_INT + _POP_TOP_INT + _POP_TOP_INT;
27052705

27062706
macro(COMPARE_OP_STR) =
27072707
_GUARD_TOS_UNICODE + _GUARD_NOS_UNICODE + unused/1 + _COMPARE_OP_STR;
@@ -2724,7 +2724,7 @@ dummy_func(
27242724
}
27252725

27262726
// Similar to COMPARE_OP_FLOAT
2727-
op(_COMPARE_OP_INT, (left, right -- res)) {
2727+
op(_COMPARE_OP_INT, (left, right -- res, l, r)) {
27282728
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
27292729
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
27302730

@@ -2737,9 +2737,9 @@ dummy_func(
27372737
Py_ssize_t iright = _PyLong_CompactValue((PyLongObject *)right_o);
27382738
// 2 if <, 4 if >, 8 if ==; this matches the low 4 bits of the oparg
27392739
int sign_ish = COMPARISON_BIT(ileft, iright);
2740-
PyStackRef_CLOSE_SPECIALIZED(left, _PyLong_ExactDealloc);
2740+
l = left;
2741+
r = right;
27412742
DEAD(left);
2742-
PyStackRef_CLOSE_SPECIALIZED(right, _PyLong_ExactDealloc);
27432743
DEAD(right);
27442744
res = (sign_ish & oparg) ? PyStackRef_True : PyStackRef_False;
27452745
// It's always a bool, so we don't care about oparg & 16.

Python/executor_cases.c.h

Lines changed: 8 additions & 6 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: 16 additions & 2 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,10 @@ dummy_func(void) {
446446
}
447447
}
448448

449-
op(_COMPARE_OP_INT, (left, right -- res)) {
450-
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
449+
op(_COMPARE_OP_INT, (left, right -- res, l, r)) {
451450
res = sym_new_type(ctx, &PyBool_Type);
451+
l = left;
452+
r = right;
452453
}
453454

454455
op(_COMPARE_OP_FLOAT, (left, right -- res)) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)