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 2 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
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)
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
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
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 = {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);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyUnicode_Type));
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
}
}

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);

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

‘error’ undeclared (first use in this function)

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

‘error’ undeclared (first use in this function)

Check warning on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'ERROR_IF' undefined; assuming extern returning int (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'error': undeclared identifier (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Address sanitizer

implicit declaration of function ‘ERROR_IF’ [-Werror=implicit-function-declaration]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘error’ undeclared (first use in this function)

Check warning on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'ERROR_IF' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'error': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'ERROR_IF' undefined; assuming extern returning int (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'error': undeclared identifier (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'ERROR_IF' undefined; assuming extern returning int (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 375 in Python/tier2_redundancy_eliminator_cases.c.h

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'error': undeclared identifier (compiling source file ..\Python\optimizer_analysis.c) [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
OUT_OF_SPACE_IF_NULL(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
Loading