gh-100239: specialize long tail of binary operations (#128722)

This commit is contained in:
Irit Katriel 2025-01-16 15:22:13 +00:00 committed by GitHub
parent e81fe940c9
commit 3893a92d95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 796 additions and 492 deletions

View file

@ -11,10 +11,10 @@
TARGET(BINARY_OP) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP);
PREDICTED(BINARY_OP);
_Py_CODEUNIT* const this_instr = next_instr - 2;
_Py_CODEUNIT* const this_instr = next_instr - 6;
(void)this_instr;
_PyStackRef lhs;
_PyStackRef rhs;
@ -39,6 +39,7 @@
assert(NB_ADD <= oparg);
assert(oparg <= NB_INPLACE_XOR);
}
/* Skip 4 cache entries */
// _BINARY_OP
{
PyObject *lhs_o = PyStackRef_AsPyObjectBorrow(lhs);
@ -60,9 +61,9 @@
TARGET(BINARY_OP_ADD_FLOAT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_ADD_FLOAT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -75,7 +76,7 @@
DEOPT_IF(!PyFloat_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_ADD_FLOAT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -98,9 +99,9 @@
TARGET(BINARY_OP_ADD_INT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_ADD_INT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -113,7 +114,7 @@
DEOPT_IF(!PyLong_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_ADD_INT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -135,9 +136,9 @@
TARGET(BINARY_OP_ADD_UNICODE) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_ADD_UNICODE);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -150,7 +151,7 @@
DEOPT_IF(!PyUnicode_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyUnicode_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_ADD_UNICODE
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -170,11 +171,57 @@
DISPATCH();
}
TARGET(BINARY_OP_EXTEND) {
_Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_EXTEND);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
/* Skip 1 cache entry */
// _GUARD_BINARY_OP_EXTEND
{
right = stack_pointer[-1];
left = stack_pointer[-2];
PyObject *descr = read_obj(&this_instr[2].cache);
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
_PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr*)descr;
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
assert(d && d->guard);
_PyFrame_SetStackPointer(frame, stack_pointer);
int res = d->guard(left_o, right_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
DEOPT_IF(!res, BINARY_OP);
}
/* Skip -4 cache entry */
// _BINARY_OP_EXTEND
{
PyObject *descr = read_obj(&this_instr[2].cache);
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
_PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr*)descr;
STAT_INC(BINARY_OP, hit);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *res_o = d->action(left_o, right_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
PyStackRef_CLOSE(left);
PyStackRef_CLOSE(right);
res = PyStackRef_FromPyObjectSteal(res_o);
}
stack_pointer[-2] = res;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
TARGET(BINARY_OP_INPLACE_ADD_UNICODE) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_INPLACE_ADD_UNICODE);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
// _GUARD_BOTH_UNICODE
@ -186,7 +233,7 @@
DEOPT_IF(!PyUnicode_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyUnicode_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_INPLACE_ADD_UNICODE
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -235,9 +282,9 @@
TARGET(BINARY_OP_MULTIPLY_FLOAT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_MULTIPLY_FLOAT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -250,7 +297,7 @@
DEOPT_IF(!PyFloat_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_MULTIPLY_FLOAT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -273,9 +320,9 @@
TARGET(BINARY_OP_MULTIPLY_INT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_MULTIPLY_INT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -288,7 +335,7 @@
DEOPT_IF(!PyLong_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_MULTIPLY_INT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -310,9 +357,9 @@
TARGET(BINARY_OP_SUBTRACT_FLOAT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_SUBTRACT_FLOAT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -325,7 +372,7 @@
DEOPT_IF(!PyFloat_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_SUBTRACT_FLOAT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
@ -348,9 +395,9 @@
TARGET(BINARY_OP_SUBTRACT_INT) {
frame->instr_ptr = next_instr;
next_instr += 2;
next_instr += 6;
INSTRUCTION_STATS(BINARY_OP_SUBTRACT_INT);
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 1, "incorrect cache size");
static_assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5, "incorrect cache size");
_PyStackRef left;
_PyStackRef right;
_PyStackRef res;
@ -363,7 +410,7 @@
DEOPT_IF(!PyLong_CheckExact(left_o), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right_o), BINARY_OP);
}
/* Skip 1 cache entry */
/* Skip 5 cache entries */
// _BINARY_OP_SUBTRACT_INT
{
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);