Revert "gh-133395: add option for extension modules to specialize BINARY_OP/SUBSCR, apply to arrays (#133396)" by iritkatriel · Pull Request #133498 · python/cpython · GitHub
Skip to content
Merged
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
12 changes: 0 additions & 12 deletions Include/cpython/object.h
9 changes: 2 additions & 7 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,13 @@ adaptive_counter_backoff(_Py_BackoffCounter counter) {
/* Specialization Extensions */

/* callbacks for an external specialization */

struct _PyBinopSpecializationDescr;

typedef int (*binaryopguardfunc)(PyObject *lhs, PyObject *rhs);
typedef PyObject* (*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
typedef void (*binaryopfreefunc)(struct _PyBinopSpecializationDescr *descr);
typedef PyObject *(*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);

typedef struct _PyBinopSpecializationDescr {
typedef struct {
int oparg;
binaryopguardfunc guard;
binaryopactionfunc action;
binaryopfreefunc free;
} _PyBinaryOpSpecializationDescr;

/* Comparison bit masks. */
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_opcode_metadata.h

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

6 changes: 5 additions & 1 deletion Include/internal/pycore_uop_metadata.h

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

1 change: 0 additions & 1 deletion Include/typeslots.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,4 @@
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000
/* New in 3.14 */
#define Py_tp_token 83
#define Py_tp_binop_specialize 84
#endif
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ def delx(self): del self.__x
check((1,2,3), vsize('') + self.P + 3*self.P)
# type
# static type: PyTypeObject
fmt = 'P2nPI13Pl4Pn9Pn12PI3Pc'
fmt = 'P2nPI13Pl4Pn9Pn12PIPc'
s = vsize(fmt)
check(int, s)
typeid = 'n' if support.Py_GIL_DISABLED else ''
Expand Down

This file was deleted.

72 changes: 0 additions & 72 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_moduleobject.h" // _PyModule_GetState()

#include "opcode.h" // binary op opargs (NB_*)

#include <stddef.h> // offsetof()
#include <stdbool.h>

Expand Down Expand Up @@ -850,10 +848,6 @@ array_richcompare(PyObject *v, PyObject *w, int op)
return res;
}

static int
array_binop_specialize(PyObject *v, PyObject *w, int oparg,
_PyBinaryOpSpecializationDescr **descr);

static Py_ssize_t
array_length(PyObject *op)
{
Expand Down Expand Up @@ -2969,8 +2963,6 @@ static PyType_Slot array_slots[] = {
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, array_new},
{Py_tp_traverse, array_tp_traverse},
{Py_tp_token, Py_TP_USE_SPEC},
{Py_tp_binop_specialize, array_binop_specialize},

/* as sequence */
{Py_sq_length, array_length},
Expand Down Expand Up @@ -3003,70 +2995,6 @@ static PyType_Spec array_spec = {
.slots = array_slots,
};

static inline int
array_subscr_guard(PyObject *lhs, PyObject *rhs)
{
PyObject *exc = PyErr_GetRaisedException();
int ret = PyType_GetBaseByToken(Py_TYPE(lhs), &array_spec, NULL);
if (ret < 0) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Clear();
ret = 0;
}
}
_PyErr_ChainExceptions1(exc);
return ret;
}

static PyObject *
array_subscr_action(PyObject *lhs, PyObject *rhs)
{
return array_subscr(lhs, rhs);
}

static void
array_subscr_free(_PyBinaryOpSpecializationDescr* descr)
{
if (descr != NULL) {
PyMem_Free(descr);
}
}

static int
array_binop_specialize(PyObject *v, PyObject *w, int oparg,
_PyBinaryOpSpecializationDescr **descr)
{
array_state *state = find_array_state_by_type(Py_TYPE(v));

if (!array_Check(v, state)) {
return 0;
}

*descr = NULL;
switch(oparg) {
case NB_SUBSCR:
if (array_subscr_guard(v, w)) {
*descr = (_PyBinaryOpSpecializationDescr*)PyMem_Malloc(
sizeof(_PyBinaryOpSpecializationDescr));
if (*descr == NULL) {
PyErr_NoMemory();
return -1;
}
**descr = (_PyBinaryOpSpecializationDescr) {
.oparg = oparg,
.guard = array_subscr_guard,
.action = array_subscr_action,
.free = array_subscr_free,
};
return 1;
}
break;
}

return 0;
}


/*********************** Array Iterator **************************/

/*[clinic input]
Expand Down
1 change: 0 additions & 1 deletion Objects/typeslots.inc

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

15 changes: 2 additions & 13 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,9 @@ dummy_func(
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
_PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr*)descr;
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
assert(d);
assert(d->guard);
assert(d && d->guard);
int res = d->guard(left_o, right_o);
ERROR_IF(res < 0);
if (res == 0) {
if (d->free) {
d->free(d);
}
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(this_instr+1);
write_ptr(cache->external_cache, NULL);
this_instr->op.code = BINARY_OP;
DEOPT_IF(true);
}
DEOPT_IF(!res);
}

pure op(_BINARY_OP_EXTEND, (descr/4, left, right -- res)) {
Expand All @@ -826,7 +816,6 @@ dummy_func(

PyObject *res_o = d->action(left_o, right_o);
DECREF_INPUTS();
ERROR_IF(res_o == NULL);
res = PyStackRef_FromPyObjectSteal(res_o);
}

Expand Down
24 changes: 20 additions & 4 deletions Python/executor_cases.c.h

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

29 changes: 5 additions & 24 deletions Python/generated_cases.c.h

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

4 changes: 3 additions & 1 deletion Python/optimizer_cases.c.h

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

35 changes: 4 additions & 31 deletions Python/specialize.c