Skip to content
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
Type propagate _BINARY_OP_ADD_UNICODE
  • Loading branch information
Fidget-Spinner committed Feb 20, 2024
commit b63b142b324d2f09bf4221ec7dec7f2d46d94a6c
33 changes: 30 additions & 3 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,13 @@ def testfunc(n):
a = 1.0
for _ in range(n):
a = a + 0.1
a = a + 0.1
a = a + 0.1
a = a + 0.1
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, 4.2)
self.assertAlmostEqual(res, 13.8)
Comment thread
Fidget-Spinner marked this conversation as resolved.
Outdated
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -792,10 +795,13 @@ def testfunc(n):
a = 1.0
for _ in range(n):
a = a - 0.1
a = a - 0.1
a = a - 0.1
a = a - 0.1
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, -2.2)
self.assertAlmostEqual(res, -11.8)
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -809,10 +815,13 @@ def testfunc(n):
a = 1.0
for _ in range(n):
a = a * 2.0
a = a * 2.0
a = a * 2.0
a = a * 2.0
return a

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertAlmostEqual(res, 2 ** 32)
self.assertAlmostEqual(res, 2 ** (32 * 4))
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_FLOAT"]
Expand All @@ -821,6 +830,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
Copy Markdown
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
Copy Markdown
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 = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_UNICODE"]
self.assertLessEqual(len(guard_both_float_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
11 changes: 11 additions & 0 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ dummy_func(void) {
}
}

op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
PyObject *temp = PyUnicode_Concat(get_const(left), get_const(right));
ERROR_IF(temp == NULL, error);
res = sym_new_const(ctx, temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyUnicode_Type));
Comment thread
Fidget-Spinner marked this conversation as resolved.
Outdated
}
}

op(_LOAD_CONST, (-- value)) {
// There should be no LOAD_CONST. It should be all
// replaced by peephole_opt.
Expand Down
14 changes: 12 additions & 2 deletions Python/tier2_redundancy_eliminator_cases.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,19 @@
}

case _BINARY_OP_ADD_UNICODE: {
_Py_UOpsSymType *right;
_Py_UOpsSymType *left;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
right = stack_pointer[-1];
left = stack_pointer[-2];
if (is_const(left) && is_const(right)) {
PyObject *temp = PyUnicode_Concat(get_const(left), get_const(right));
ERROR_IF(temp == NULL, error);
res = sym_new_const(ctx, temp);
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyUnicode_Type));
}
stack_pointer[-2] = res;
stack_pointer += -1;
break;
Expand Down