Skip to content

Experimental variant of BINARY_ADD specialization. #29059

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

Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef struct {
uint8_t original_oparg;
uint8_t counter;
uint16_t index;
uint16_t left_version;
uint16_t right_version;
} _PyAdaptiveEntry;


Expand All @@ -41,6 +43,10 @@ typedef struct {
uint16_t defaults_len;
} _PyCallCache;

typedef struct {
binaryfunc function;
} _PyFuncPtrCache;

/* Add specialized versions of entries to this union.
*
* Do not break the invariant: sizeof(SpecializedCacheEntry) == 8
Expand All @@ -57,6 +63,7 @@ typedef union {
_PyAttrCache attr;
_PyLoadGlobalCache load_global;
_PyObjectCache obj;
_PyFuncPtrCache func_ptr;
_PyCallCache call;
} SpecializedCacheEntry;

Expand Down Expand Up @@ -267,7 +274,7 @@ int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *nam
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryAdd(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryAdd(PyObject *left, PyObject *right, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
int _Py_Specialize_BinaryMultiply(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_CallFunction(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);

Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
int base,
int alternate);

PyObject *_PyAdd_Float_Long(PyFloatObject *a, PyLongObject *b);

PyObject *_PyAdd_Long_Float(PyLongObject *a, PyFloatObject *b);

#ifdef __cplusplus
}
#endif
Expand Down
81 changes: 41 additions & 40 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def jabs_op(name, op):

_specialized_instructions = [
"BINARY_ADD_ADAPTIVE",
"BINARY_ADD_CACHED",
"BINARY_ADD_INT",
"BINARY_ADD_FLOAT",
"BINARY_ADD_UNICODE",
Expand Down
20 changes: 20 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,26 @@ _PyLong_Add(PyLongObject *a, PyLongObject *b)
return (PyObject *)z;
}

PyObject *
_PyAdd_Long_Float(PyLongObject *a, PyFloatObject *b)
{
if (IS_MEDIUM_VALUE(a)) {
double l = (double)medium_value(a);
return PyFloat_FromDouble(l + b->ob_fval);
}
return PyFloat_Type.tp_as_number->nb_add((PyObject *)a, (PyObject *)b);
}

PyObject *
_PyAdd_Float_Long(PyFloatObject *a, PyLongObject *b)
{
if (IS_MEDIUM_VALUE(b)) {
double r = (double)medium_value(b);
return PyFloat_FromDouble(a->ob_fval + r);
}
return PyFloat_Type.tp_as_number->nb_add((PyObject *)a, (PyObject *)b);
}

static PyObject *
long_add(PyLongObject *a, PyLongObject *b)
{
Expand Down
32 changes: 28 additions & 4 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2072,18 +2072,19 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
}

TARGET(BINARY_ADD_ADAPTIVE) {
if (oparg == 0) {
SpecializedCacheEntry *cache = GET_CACHE();
if (cache->adaptive.counter == 0) {
PyObject *left = SECOND();
PyObject *right = TOP();
next_instr--;
if (_Py_Specialize_BinaryAdd(left, right, next_instr) < 0) {
if (_Py_Specialize_BinaryAdd(left, right, next_instr, cache) < 0) {
goto error;
}
DISPATCH();
}
else {
STAT_INC(BINARY_ADD, deferred);
UPDATE_PREV_INSTR_OPARG(next_instr, oparg - 1);
cache->adaptive.counter--;
STAT_DEC(BINARY_ADD, unquickened);
JUMP_TO_INSTRUCTION(BINARY_ADD);
}
Expand Down Expand Up @@ -2170,6 +2171,29 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
DISPATCH();
}

TARGET(BINARY_ADD_CACHED) {
SpecializedCacheEntry *caches = GET_CACHE();
_PyAdaptiveEntry *cache0 = &caches[0].adaptive;
PyObject *left = SECOND();
PyObject *right = TOP();
DEOPT_IF(Py_TYPE(left)->tp_version_tag != cache0->left_version, BINARY_ADD);
DEOPT_IF(Py_TYPE(right)->tp_version_tag != cache0->right_version, BINARY_ADD);
binaryfunc function = caches[-1].func_ptr.function;
PyObject *sum = function(left, right);
if (sum == Py_NotImplemented) {
Py_DECREF(sum);
sum = NULL;
Comment on lines +2184 to +2185
Copy link
Member

Choose a reason for hiding this comment

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

You may want to consider replacing this with

Suggested change
Py_DECREF(sum);
sum = NULL;
Py_SETREF(sum, NULL);

Copy link
Member

Choose a reason for hiding this comment

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

Or:

Suggested change
Py_DECREF(sum);
sum = NULL;
Py_CLEAR(sum);

}
SET_SECOND(sum);
Py_DECREF(right);
Py_DECREF(left);
STACK_SHRINK(1);
if (sum == NULL) {
goto error;
}
DISPATCH();
}

TARGET(BINARY_SUBTRACT) {
PyObject *right = POP();
PyObject *left = TOP();
Expand Down Expand Up @@ -5126,7 +5150,7 @@ MISS_WITH_CACHE(LOAD_GLOBAL)
MISS_WITH_CACHE(LOAD_METHOD)
MISS_WITH_CACHE(CALL_FUNCTION)
MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)
MISS_WITH_OPARG_COUNTER(BINARY_ADD)
MISS_WITH_CACHE(BINARY_ADD)
MISS_WITH_OPARG_COUNTER(BINARY_MULTIPLY)

binary_subscr_dict_error:
Expand Down
38 changes: 19 additions & 19 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading