8000 gh-100239: specialize bitwise logical binary ops on ints by iritkatriel · Pull Request #128927 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100239: specialize bitwise logical binary ops on ints #128927

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 13 commits into from
Jan 29, 2025
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
11 changes: 11 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,17 @@ def binary_op_add_extend():
self.assert_specialized(binary_op_add_extend, "BINARY_OP_EXTEND")
self.assert_no_opcode(binary_op_add_extend, "BINARY_OP")

def binary_op_bitwise_extend():
for _ in range(100):
a, b = 1, 2
x = a | b
y = a &a 8000 mp; b
z = a ^ b

binary_op_bitwise_extend()
self.assert_specialized(binary_op_bitwise_extend, "BINARY_OP_EXTEND")
self.assert_no_opcode(binary_op_bitwise_extend, "BINARY_OP")


@cpython_only
@requires_specialization_ft
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Specialize ``BINARY_OP`` for bitwise logical ops on non-negative ints.
36 changes: 36 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,35 @@

/** Binary Op Specialization Extensions */

/* long-long */

static inline int
is_nonnegative_compactlong(PyObject *v)
{
return PyLong_CheckExact(v) &&
(!_PyLong_IsNegative((PyLongObject *)v)) &&
_PyLong_IsCompact((PyLongObject *)v);
}

static int
nonnegative_compactlongs_guard(PyObject *lhs, PyObject *rhs)
{
return (is_nonnegative_compactlong(lhs) && is_nonnegative_compactlong(rhs));
}

#define NONNEGATIVE_LONGS_ACTION(NAME, OP) \
Copy link
Contributor

Choose a reason for hiding this comment

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

Why restrict to nonnegative longs here? If we do restrict, then we can replace the calls _PyLong_CompactValue with direct access to op->long_value.ob_digit[0]

Copy link
Member Author

Choose a reason for hiding this comment

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

Because negative ints need more work at runtime and I don't think they're common with bitwise logical ops.

Copy link
Contributor

Choose a reason for hiding this comment

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

The extra work is already done by _PyLong_CompactValue or am missing something? The output of ls OP rhs might not be a compact int, but there are no guards for the output type.

Copy link
Member Author

Choose a reason for hiding this comment

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

The extra work is already done by _PyLong_CompactValue or am missing something?

No, I think you're right. Good point.

The output of ls OP rhs might not be a compact int, but there are no guards for the output type.

For bitwise logical operators we should expect the results to have the same size as the inputs.

static PyObject * \
(NAME)(PyObject *lhs, PyObject *rhs) \
{ \
Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
return PyLong_FromLong(lhs_val OP rhs_val); \
}
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_or, |)

Check warning on line 2441 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2441 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2441 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2441 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_and, &)

Check warning on line 2442 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2442 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2442 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2442 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_xor, ^)

Check warning on line 2443 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2443 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2443 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2443 in Python/specialize.c

View workflow job for this annotation

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

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
#undef NONNEGATIVE_LONGS_ACTION

/* float-long */

static int
Expand Down Expand Up @@ -2466,6 +2495,12 @@
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
#undef LONG_FLOAT_ACTION

static _PyBinaryOpSpecializationDescr nonnegative_compactlongs_specs[NB_OPARG_LAST+1] = {
[NB_OR] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_or},
[NB_AND] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_and},
[NB_XOR] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_xor},
};

static _PyBinaryOpSpecializationDescr float_compactlong_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {float_compactlong_guard, float_compactlong_add},
[NB_SUBTRACT] = {float_compactlong_guard, float_compactlong_subtract},
Expand Down Expand Up @@ -2494,6 +2529,7 @@

LOOKUP_SPEC(compactlong_float_specs, oparg);
LOOKUP_SPEC(float_compactlong_specs, oparg);
LOOKUP_SPEC(nonnegative_compactlongs_specs, oparg);
#undef LOOKUP_SPEC
return 0;
}
Expand Down
Loading
0