Skip to content

Commit 2c046f1

Browse files
In progress
1 parent 8f59fbb commit 2c046f1

File tree

4 files changed

+229
-37
lines changed

4 files changed

+229
-37
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ def testfunc(n):
843843
self.assertLessEqual(len(guard_tos_unicode_count), 1)
844844
self.assertLessEqual(len(guard_nos_unicode_count), 1)
845845
self.assertIn("_COMPARE_OP_STR", uops)
846+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
846847

847848
def test_type_inconsistency(self):
848849
ns = {}
@@ -1612,7 +1613,7 @@ def f(n):
16121613
# But all of the appends we care about are still there:
16131614
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
16141615

1615-
def test_compare_pop_two_load_const_inline_borrow(self):
1616+
def test_compare_pop_two_load_const_inline_borrow_int(self):
16161617
def testfunc(n):
16171618
x = 0
16181619
for _ in range(n):
@@ -1629,6 +1630,23 @@ def testfunc(n):
16291630
self.assertNotIn("_COMPARE_OP_INT", uops)
16301631
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
16311632

1633+
def test_compare_pop_two_load_const_inline_borrow_float(self):
1634+
def testfunc(n):
1635+
x = 0
1636+
for _ in range(n):
1637+
a = 10.0
1638+
b = 10.0
1639+
if a == b:
1640+
x += 1
1641+
return x
1642+
1643+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1644+
self.assertEqual(res, TIER2_THRESHOLD)
1645+
self.assertIsNotNone(ex)
1646+
uops = get_opnames(ex)
1647+
self.assertNotIn("_COMPARE_OP_FLOAT", uops)
1648+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
1649+
16321650
def test_to_bool_bool_contains_op_set(self):
16331651
"""
16341652
Test that _TO_BOOL_BOOL is removed from code like:
@@ -1953,6 +1971,7 @@ def testfunc(n):
19531971
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
19541972
self.assertNotIn("_COMPARE_OP_INT", uops)
19551973
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
1974+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
19561975

19571976
def test_call_len(self):
19581977
def testfunc(n):
@@ -2071,6 +2090,7 @@ def testfunc(n):
20712090
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
20722091
self.assertNotIn("_COMPARE_OP_INT", uops)
20732092
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
2093+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
20742094

20752095
def test_call_isinstance_guards_removed(self):
20762096
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -430,31 +430,17 @@ dummy_func(void) {
430430
}
431431

432432
op(_COMPARE_OP_INT, (left, right -- res)) {
433-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
434-
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
435-
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
436-
PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
437-
sym_get_const(ctx, right),
438-
oparg >> 5);
439-
if (tmp == NULL) {
440-
goto error;
441-
}
442-
assert(PyBool_Check(tmp));
443-
assert(_Py_IsImmortal(tmp));
444-
REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)tmp);
445-
res = sym_new_const(ctx, tmp);
446-
Py_DECREF(tmp);
447-
}
448-
else {
449-
res = sym_new_type(ctx, &PyBool_Type);
450-
}
433+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
434+
res = sym_new_type(ctx, &PyBool_Type);
451435
}
452436

453437
op(_COMPARE_OP_FLOAT, (left, right -- res)) {
438+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
454439
res = sym_new_type(ctx, &PyBool_Type);
455440
}
456441

457442
op(_COMPARE_OP_STR, (left, right -- res)) {
443+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
458444
res = sym_new_type(ctx, &PyBool_Type);
459445
}
460446

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)