mirror of
https://github.com/python/cpython.git
synced 2025-08-26 11:45:20 +00:00
gh-100239: specialize long tail of binary operations (#128722)
This commit is contained in:
parent
e81fe940c9
commit
3893a92d95
21 changed files with 796 additions and 492 deletions
|
@ -522,6 +522,7 @@ dummy_func(
|
|||
BINARY_OP_SUBTRACT_FLOAT,
|
||||
BINARY_OP_ADD_UNICODE,
|
||||
// BINARY_OP_INPLACE_ADD_UNICODE, // See comments at that opcode.
|
||||
BINARY_OP_EXTEND,
|
||||
};
|
||||
|
||||
op(_GUARD_BOTH_INT, (left, right -- left, right)) {
|
||||
|
@ -587,11 +588,11 @@ dummy_func(
|
|||
}
|
||||
|
||||
macro(BINARY_OP_MULTIPLY_INT) =
|
||||
_GUARD_BOTH_INT + unused/1 + _BINARY_OP_MULTIPLY_INT;
|
||||
_GUARD_BOTH_INT + unused/5 + _BINARY_OP_MULTIPLY_INT;
|
||||
macro(BINARY_OP_ADD_INT) =
|
||||
_GUARD_BOTH_INT + unused/1 + _BINARY_OP_ADD_INT;
|
||||
_GUARD_BOTH_INT + unused/5 + _BINARY_OP_ADD_INT;
|
||||
macro(BINARY_OP_SUBTRACT_INT) =
|
||||
_GUARD_BOTH_INT + unused/1 + _BINARY_OP_SUBTRACT_INT;
|
||||
_GUARD_BOTH_INT + unused/5 + _BINARY_OP_SUBTRACT_INT;
|
||||
|
||||
op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) {
|
||||
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
|
||||
|
@ -659,11 +660,11 @@ dummy_func(
|
|||
}
|
||||
|
||||
macro(BINARY_OP_MULTIPLY_FLOAT) =
|
||||
_GUARD_BOTH_FLOAT + unused/1 + _BINARY_OP_MULTIPLY_FLOAT;
|
||||
_GUARD_BOTH_FLOAT + unused/5 + _BINARY_OP_MULTIPLY_FLOAT;
|
||||
macro(BINARY_OP_ADD_FLOAT) =
|
||||
_GUARD_BOTH_FLOAT + unused/1 + _BINARY_OP_ADD_FLOAT;
|
||||
_GUARD_BOTH_FLOAT + unused/5 + _BINARY_OP_ADD_FLOAT;
|
||||
macro(BINARY_OP_SUBTRACT_FLOAT) =
|
||||
_GUARD_BOTH_FLOAT + unused/1 + _BINARY_OP_SUBTRACT_FLOAT;
|
||||
_GUARD_BOTH_FLOAT + unused/5 + _BINARY_OP_SUBTRACT_FLOAT;
|
||||
|
||||
op(_GUARD_BOTH_UNICODE, (left, right -- left, right)) {
|
||||
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
|
||||
|
@ -689,7 +690,7 @@ dummy_func(
|
|||
}
|
||||
|
||||
macro(BINARY_OP_ADD_UNICODE) =
|
||||
_GUARD_BOTH_UNICODE + unused/1 + _BINARY_OP_ADD_UNICODE;
|
||||
_GUARD_BOTH_UNICODE + unused/5 + _BINARY_OP_ADD_UNICODE;
|
||||
|
||||
// This is a subtle one. It's a super-instruction for
|
||||
// BINARY_OP_ADD_UNICODE followed by STORE_FAST
|
||||
|
@ -741,8 +742,34 @@ dummy_func(
|
|||
#endif
|
||||
}
|
||||
|
||||
op(_GUARD_BINARY_OP_EXTEND, (descr/4, left, right -- left, right)) {
|
||||
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);
|
||||
int res = d->guard(left_o, right_o);
|
||||
EXIT_IF(!res);
|
||||
}
|
||||
|
||||
pure op(_BINARY_OP_EXTEND, (descr/4, left, right -- res)) {
|
||||
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);
|
||||
|
||||
PyObject *res_o = d->action(left_o, right_o);
|
||||
DECREF_INPUTS();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
}
|
||||
|
||||
macro(BINARY_OP_EXTEND) =
|
||||
unused/1 + _GUARD_BINARY_OP_EXTEND + rewind/-4 + _BINARY_OP_EXTEND;
|
||||
|
||||
macro(BINARY_OP_INPLACE_ADD_UNICODE) =
|
||||
_GUARD_BOTH_UNICODE + unused/1 + _BINARY_OP_INPLACE_ADD_UNICODE;
|
||||
_GUARD_BOTH_UNICODE + unused/5 + _BINARY_OP_INPLACE_ADD_UNICODE;
|
||||
|
||||
family(BINARY_SUBSCR, INLINE_CACHE_ENTRIES_BINARY_SUBSCR) = {
|
||||
BINARY_SUBSCR_DICT,
|
||||
|
@ -4742,7 +4769,7 @@ dummy_func(
|
|||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
}
|
||||
|
||||
macro(BINARY_OP) = _SPECIALIZE_BINARY_OP + _BINARY_OP;
|
||||
macro(BINARY_OP) = _SPECIALIZE_BINARY_OP + unused/4 + _BINARY_OP;
|
||||
|
||||
pure inst(SWAP, (bottom_in, unused[oparg-2], top_in --
|
||||
top_out, unused[oparg-2], bottom_out)) {
|
||||
|
|
45
Python/executor_cases.c.h
generated
45
Python/executor_cases.c.h
generated
|
@ -899,6 +899,51 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_EXTEND: {
|
||||
_PyStackRef right;
|
||||
_PyStackRef left;
|
||||
right = stack_pointer[-1];
|
||||
left = stack_pointer[-2];
|
||||
PyObject *descr = (PyObject *)CURRENT_OPERAND0();
|
||||
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);
|
||||
if (!res) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_EXTEND: {
|
||||
_PyStackRef right;
|
||||
_PyStackRef left;
|
||||
_PyStackRef res;
|
||||
right = stack_pointer[-1];
|
||||
left = stack_pointer[-2];
|
||||
PyObject *descr = (PyObject *)CURRENT_OPERAND0();
|
||||
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());
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_SUBSCR: {
|
||||
_PyStackRef sub;
|
||||
_PyStackRef container;
|
||||
|
|
99
Python/generated_cases.c.h
generated
99
Python/generated_cases.c.h
generated
|
@ -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);
|
||||
|
|
2
Python/opcode_targets.h
generated
2
Python/opcode_targets.h
generated
|
@ -152,6 +152,7 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_BINARY_OP_ADD_FLOAT,
|
||||
&&TARGET_BINARY_OP_ADD_INT,
|
||||
&&TARGET_BINARY_OP_ADD_UNICODE,
|
||||
&&TARGET_BINARY_OP_EXTEND,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_FLOAT,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_INT,
|
||||
&&TARGET_BINARY_OP_SUBTRACT_FLOAT,
|
||||
|
@ -233,7 +234,6 @@ static void *opcode_targets[256] = {
|
|||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&TARGET_INSTRUMENTED_END_FOR,
|
||||
&&TARGET_INSTRUMENTED_POP_ITER,
|
||||
&&TARGET_INSTRUMENTED_END_SEND,
|
||||
|
|
13
Python/optimizer_cases.c.h
generated
13
Python/optimizer_cases.c.h
generated
|
@ -562,6 +562,19 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_EXTEND: {
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_EXTEND: {
|
||||
_Py_UopsSymbol *res;
|
||||
res = sym_new_not_null(ctx);
|
||||
stack_pointer[-2] = res;
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_SUBSCR: {
|
||||
_Py_UopsSymbol *res;
|
||||
res = sym_new_not_null(ctx);
|
||||
|
|
|
@ -1174,7 +1174,7 @@ do_specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject*
|
|||
assert(tp_version != 0);
|
||||
write_u32(lm_cache->type_version, tp_version);
|
||||
/* borrowed */
|
||||
write_obj(lm_cache->descr, fget);
|
||||
write_ptr(lm_cache->descr, fget);
|
||||
specialize(instr, LOAD_ATTR_PROPERTY);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1254,7 +1254,7 @@ do_specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject*
|
|||
#endif
|
||||
write_u32(lm_cache->keys_version, version);
|
||||
/* borrowed */
|
||||
write_obj(lm_cache->descr, descr);
|
||||
write_ptr(lm_cache->descr, descr);
|
||||
write_u32(lm_cache->type_version, tp_version);
|
||||
specialize(instr, LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN);
|
||||
return 0;
|
||||
|
@ -1534,7 +1534,7 @@ specialize_class_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
|
|||
}
|
||||
#endif
|
||||
write_u32(cache->type_version, tp_version);
|
||||
write_obj(cache->descr, descr);
|
||||
write_ptr(cache->descr, descr);
|
||||
if (metaclass_check) {
|
||||
write_u32(cache->keys_version, meta_version);
|
||||
specialize(instr, LOAD_ATTR_CLASS_WITH_METACLASS_CHECK);
|
||||
|
@ -1642,7 +1642,7 @@ specialize_attr_loadclassattr(PyObject *owner, _Py_CODEUNIT *instr,
|
|||
* working since Python 2.6 and it's battle-tested.
|
||||
*/
|
||||
write_u32(cache->type_version, tp_version);
|
||||
write_obj(cache->descr, descr);
|
||||
write_ptr(cache->descr, descr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -2412,6 +2412,92 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs)
|
|||
}
|
||||
#endif
|
||||
|
||||
/** Binary Op Specialization Extensions */
|
||||
|
||||
/* float-long */
|
||||
|
||||
static int
|
||||
float_compactlong_guard(PyObject *lhs, PyObject *rhs)
|
||||
{
|
||||
return (
|
||||
PyFloat_CheckExact(lhs) &&
|
||||
PyLong_CheckExact(rhs) &&
|
||||
_PyLong_IsCompact((PyLongObject *)rhs)
|
||||
);
|
||||
}
|
||||
|
||||
#define FLOAT_LONG_ACTION(NAME, OP) \
|
||||
static PyObject * \
|
||||
(NAME)(PyObject *lhs, PyObject *rhs) \
|
||||
{ \
|
||||
double lhs_val = PyFloat_AsDouble(lhs); \
|
||||
Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
|
||||
return PyFloat_FromDouble(lhs_val OP rhs_val); \
|
||||
}
|
||||
FLOAT_LONG_ACTION(float_compactlong_add, +)
|
||||
FLOAT_LONG_ACTION(float_compactlong_subtract, -)
|
||||
FLOAT_LONG_ACTION(float_compactlong_multiply, *)
|
||||
FLOAT_LONG_ACTION(float_compactlong_true_div, /)
|
||||
#undef FLOAT_LONG_ACTION
|
||||
|
||||
/* long-float */
|
||||
|
||||
static int
|
||||
compactlong_float_guard(PyObject *lhs, PyObject *rhs)
|
||||
{
|
||||
return (
|
||||
PyFloat_CheckExact(rhs) &&
|
||||
PyLong_CheckExact(lhs) &&
|
||||
_PyLong_IsCompact((PyLongObject *)lhs)
|
||||
);
|
||||
}
|
||||
|
||||
#define LONG_FLOAT_ACTION(NAME, OP) \
|
||||
static PyObject * \
|
||||
(NAME)(PyObject *lhs, PyObject *rhs) \
|
||||
{ \
|
||||
double rhs_val = PyFloat_AsDouble(rhs); \
|
||||
Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
|
||||
return PyFloat_FromDouble(lhs_val OP rhs_val); \
|
||||
}
|
||||
LONG_FLOAT_ACTION(compactlong_float_add, +)
|
||||
LONG_FLOAT_ACTION(compactlong_float_subtract, -)
|
||||
LONG_FLOAT_ACTION(compactlong_float_multiply, *)
|
||||
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
|
||||
#undef LONG_FLOAT_ACTION
|
||||
|
||||
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},
|
||||
[NB_TRUE_DIVIDE] = {float_compactlong_guard, float_compactlong_true_div},
|
||||
[NB_MULTIPLY] = {float_compactlong_guard, float_compactlong_multiply},
|
||||
};
|
||||
|
||||
static _PyBinaryOpSpecializationDescr compactlong_float_specs[NB_OPARG_LAST+1] = {
|
||||
[NB_ADD] = {compactlong_float_guard, compactlong_float_add},
|
||||
[NB_SUBTRACT] = {compactlong_float_guard, compactlong_float_subtract},
|
||||
[NB_TRUE_DIVIDE] = {compactlong_float_guard, compactlong_float_true_div},
|
||||
[NB_MULTIPLY] = {compactlong_float_guard, compactlong_float_multiply},
|
||||
};
|
||||
|
||||
static int
|
||||
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
|
||||
_PyBinaryOpSpecializationDescr **descr)
|
||||
{
|
||||
#define LOOKUP_SPEC(TABLE, OPARG) \
|
||||
if ((TABLE)[(OPARG)].action) { \
|
||||
if ((TABLE)[(OPARG)].guard(lhs, rhs)) { \
|
||||
*descr = &((TABLE)[OPARG]); \
|
||||
return 1; \
|
||||
} \
|
||||
}
|
||||
|
||||
LOOKUP_SPEC(compactlong_float_specs, oparg);
|
||||
LOOKUP_SPEC(float_compactlong_specs, oparg);
|
||||
#undef LOOKUP_SPEC
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
_Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *instr,
|
||||
int oparg, _PyStackRef *locals)
|
||||
|
@ -2420,6 +2506,12 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
|
|||
PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st);
|
||||
assert(ENABLE_SPECIALIZATION_FT);
|
||||
assert(_PyOpcode_Caches[BINARY_OP] == INLINE_CACHE_ENTRIES_BINARY_OP);
|
||||
|
||||
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(instr + 1);
|
||||
if (instr->op.code == BINARY_OP_EXTEND) {
|
||||
write_ptr(cache->external_cache, NULL);
|
||||
}
|
||||
|
||||
switch (oparg) {
|
||||
case NB_ADD:
|
||||
case NB_INPLACE_ADD:
|
||||
|
@ -2474,8 +2566,17 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
|
|||
}
|
||||
break;
|
||||
}
|
||||
|
||||
_PyBinaryOpSpecializationDescr *descr;
|
||||
if (binary_op_extended_specialization(lhs, rhs, oparg, &descr)) {
|
||||
specialize(instr, BINARY_OP_EXTEND);
|
||||
write_ptr(cache->external_cache, (void*)descr);
|
||||
return;
|
||||
}
|
||||
|
||||
SPECIALIZATION_FAIL(BINARY_OP, binary_op_fail_kind(oparg, lhs, rhs));
|
||||
unspecialize(instr);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue