Skip to content

GH-132732: Use pure op machinery to optimize various instructions with _POP_TOP and _POP_TWO #137577

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
85 changes: 85 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,74 @@ 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_unary_negative_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for i in range(n):
a = 1
result = -a
if result < 0:
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("_UNARY_NEGATIVE", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_unary_not_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for i in range(n):
a = 42
result = not a
if result:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_UNARY_NOT", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_unary_invert_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for i in range(n):
a = 0
result = ~a
if result < 0:
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("_UNARY_INVERT", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_compare_op_pop_two_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for _ in range(n):
a = 10
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", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_compare_op_int_pop_two_load_const_inline_borrow(self):
def testfunc(n):
x = 0
Expand Down Expand Up @@ -1665,6 +1733,23 @@ def testfunc(n):
self.assertNotIn("_COMPARE_OP_FLOAT", uops)
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_contains_op_pop_two_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for _ in range(n):
a = "foo"
s = "foo bar baz"
if a in s:
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("_CONTAINS_OP", 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize ``_COMPARE_OP``, ``_CONTAINS_OP``, ``_UNARY_NEGATIVE``, ``_UNARY_NOT``, and ``_UNARY_INVERT`` in JIT builds with constant-loading uops (``_POP_TWO_LOAD_CONST_INLINE_BORROW`` and ``_POP_TOP_LOAD_CONST_INLINE_BORROW``), and then remove both to reduce instruction count.
6 changes: 6 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ dummy_func(void) {
}

op(_UNARY_NEGATIVE, (value -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(value);
if (sym_is_compact_int(value)) {
res = sym_new_compact_int(ctx);
}
Expand All @@ -412,6 +413,9 @@ dummy_func(void) {
}

op(_UNARY_INVERT, (value -- res)) {
if (!sym_matches_type(value, &PyBool_Type)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(value);
}
if (sym_matches_type(value, &PyLong_Type)) {
res = sym_new_type(ctx, &PyLong_Type);
}
Expand All @@ -421,6 +425,7 @@ dummy_func(void) {
}

op(_COMPARE_OP, (left, right -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
if (oparg & 16) {
res = sym_new_type(ctx, &PyBool_Type);
}
Expand Down Expand Up @@ -449,6 +454,7 @@ dummy_func(void) {
}

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

Expand Down
Loading
Loading