Skip to content
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

gh-115480: Type propagate _BINARY_OP_ADD_UNICODE #115710

Merged
merged 4 commits into from
Mar 1, 2024
Merged
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
39 changes: 33 additions & 6 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,14 @@ def test_float_add_constant_propagation(self):
def testfunc(n):
a = 1.0
for _ in range(n):
a = a + 0.1
a = a + 0.25
a = a + 0.25
a = a + 0.25
a = a + 0.25
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, 4.2)
self.assertAlmostEqual(res, 33.0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -812,11 +815,14 @@ def test_float_subtract_constant_propagation(self):
def testfunc(n):
a = 1.0
for _ in range(n):
a = a - 0.1
a = a - 0.25
a = a - 0.25
a = a - 0.25
a = a - 0.25
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, -2.2)
self.assertAlmostEqual(res, -31.0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -829,11 +835,14 @@ def test_float_multiply_constant_propagation(self):
def testfunc(n):
a = 1.0
for _ in range(n):
a = a * 2.0
a = a * 1.0
a = a * 1.0
a = a * 1.0
a = a * 1.0
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, 2 ** 32)
self.assertAlmostEqual(res, 1.0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -842,6 +851,24 @@ def testfunc(n):
# We'll also need to verify that propagation actually occurs.
self.assertIn("_BINARY_OP_MULTIPLY_FLOAT", uops)

def test_add_unicode_propagation(self):
def testfunc(n):
a = ""
for _ in range(n):
a + a
a + a
a + a
a + a
Comment on lines +858 to +861
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sooner or later we'll have a peephole pass that will delete the generated code for this completely. Is there a reason you're not doing something like a = a + a?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That transforms it to BINARY_OP_INPLACE_ADD_UNICODE sadly. Which is an unsupported opcode, so the trace ends there.

return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertEqual(res, "")
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_both_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_UNICODE"]
self.assertLessEqual(len(guard_both_unicode_count), 1)
self.assertIn("_BINARY_OP_ADD_UNICODE", uops)

def test_compare_op_type_propagation_float(self):
def testfunc(n):
a = 1.0
Expand Down
16 changes: 16 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ dummy_func(void) {
}
}

op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
if (sym_is_const(left) && sym_is_const(right) &&
sym_matches_type(left, &PyUnicode_Type) && sym_matches_type(right, &PyUnicode_Type)) {
PyObject *temp = PyUnicode_Concat(sym_get_const(left), sym_get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
OUT_OF_SPACE_IF_NULL(res);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyUnicode_Type));
}
}

op(_TO_BOOL, (value -- res)) {
(void)value;
res = sym_new_type(ctx, &PyBool_Type);
Expand Down
19 changes: 17 additions & 2 deletions Python/optimizer_cases.c.h

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

Loading