Skip to content

GH-132732: Use pure op machinery to optimize COMPARE_OP_INT/FLOAT/STR #137062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
In progress
  • Loading branch information
savannahostrowski committed Jul 21, 2025
commit 2c046f1e0acc6fbddc72f750c05a3f09809e6519
22 changes: 21 additions & 1 deletion Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ def testfunc(n):
self.assertLessEqual(len(guard_tos_unicode_count), 1)
self.assertLessEqual(len(guard_nos_unicode_count), 1)
self.assertIn("_COMPARE_OP_STR", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

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

def test_compare_pop_two_load_const_inline_borrow(self):
def test_compare_pop_two_load_const_inline_borrow_int(self):
def testfunc(n):
x = 0
for _ in range(n):
Expand All @@ -1629,6 +1630,23 @@ def testfunc(n):
self.assertNotIn("_COMPARE_OP_INT", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_compare_pop_two_load_const_inline_borrow_float(self):
def testfunc(n):
x = 0
for _ in range(n):
a = 10.0
b = 10.0
if a == b:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_COMPARE_OP_FLOAT", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_to_bool_bool_contains_op_set(self):
"""
Test that _TO_BOOL_BOOL is removed from code like:
Expand Down Expand Up @@ -1953,6 +1971,7 @@ def testfunc(n):
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
self.assertNotIn("_COMPARE_OP_INT", uops)
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_call_len(self):
def testfunc(n):
Expand Down Expand Up @@ -2071,6 +2090,7 @@ def testfunc(n):
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
self.assertNotIn("_COMPARE_OP_INT", uops)
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_call_isinstance_guards_removed(self):
def testfunc(n):
Expand Down
22 changes: 4 additions & 18 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,31 +430,17 @@ dummy_func(void) {
}

op(_COMPARE_OP_INT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
sym_get_const(ctx, right),
oparg >> 5);
if (tmp == NULL) {
goto error;
}
assert(PyBool_Check(tmp));
assert(_Py_IsImmortal(tmp));
REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)tmp);
res = sym_new_const(ctx, tmp);
Py_DECREF(tmp);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyBool_Type);
}

op(_COMPARE_OP_FLOAT, (left, right -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyBool_Type);
}

op(_COMPARE_OP_STR, (left, right -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyBool_Type);
}

Expand Down
208 changes: 190 additions & 18 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy