Skip to content

Revert "gh-133395: add option for extension modules to specialize BINARY_OP/SUBSCR, apply to arrays (#133396)" #133498

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 1 commit into from
May 6, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ typedef struct {
* backwards-compatibility */
typedef Py_ssize_t printfunc;

/* Specialize a binary op by setting the descriptor pointer */
struct _PyBinopSpecializationDescr;
typedef int (*binop_specialize_func)(PyObject *v, PyObject *w, int oparg,
struct _PyBinopSpecializationDescr **descr);

// If this structure is modified, Doc/includes/typestruct.h should be updated
// as well.
struct _typeobject {
Expand Down Expand Up @@ -238,13 +233,6 @@ struct _typeobject {
/* bitset of which type-watchers care about this type */
unsigned char tp_watched;

/* callback that may specialize BINARY_OP
* this is an experimental API based on the ideas in the paper
* Cross Module Quickening - The Curious Case of C Extensions
* by Felix Berlakovich and Stefan Brunthaler.
*/
binop_specialize_func tp_binop_specialize;

/* Number of tp_version_tag values used.
* Set to _Py_ATTR_CACHE_UNUSED if the attribute cache is
* disabled for this type (e.g. due to custom MRO entries).
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,7 @@ LONG_FLOAT_ACTION(compactlong_float_multiply, *)
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
#undef LONG_FLOAT_ACTION

static const _PyBinaryOpSpecializationDescr binaryop_extend_builtins[] = {
static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
/* long-long arithmetic */
{NB_OR, compactlongs_guard, compactlongs_or},
{NB_AND, compactlongs_guard, compactlongs_and},
Expand All @@ -2560,41 +2560,14 @@ static int
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
_PyBinaryOpSpecializationDescr **descr)
{
/* We are currently using this only for NB_SUBSCR, which is not
* commutative. Will need to revisit this function when we use
* this for operators which are.
*/

typedef _PyBinaryOpSpecializationDescr descr_type;
size_t size = Py_ARRAY_LENGTH(binaryop_extend_builtins);
for (size_t i = 0; i < size; i++) {
descr_type *d = (descr_type *)&binaryop_extend_builtins[i];
assert(d != NULL);
assert(d->guard != NULL);
size_t n = sizeof(binaryop_extend_descrs)/sizeof(_PyBinaryOpSpecializationDescr);
for (size_t i = 0; i < n; i++) {
_PyBinaryOpSpecializationDescr *d = &binaryop_extend_descrs[i];
if (d->oparg == oparg && d->guard(lhs, rhs)) {
*descr = d;
return 1;
}
}

PyTypeObject *lhs_type = Py_TYPE(lhs);
if (lhs_type->tp_binop_specialize != NULL) {
int ret = lhs_type->tp_binop_specialize(lhs, rhs, oparg, descr);
if (ret < 0) {
return -1;
}
if (ret == 1) {
if (*descr == NULL) {
PyErr_Format(
PyExc_ValueError,
"tp_binop_specialize of '%T' returned 1 with *descr == NULL",
lhs);
return -1;
}
(*descr)->oparg = oparg;
}
return ret;
}
return 0;
}

Expand Down
Loading