8000 Revert "gh-133395: add option for extension modules to specialize BIN… · python/cpython@296cd12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 296cd12

Browse files
authored
Revert "gh-133395: add option for extension modules to specialize BINARY_OP/SUBSCR, apply to arrays (#133396)" (#133498)
1 parent 3c73cf5 commit 296cd12

14 files changed

+44
-171
lines changed

Include/cpython/object.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,6 @@ typedef struct {
143143
* backwards-compatibility */
144144
typedef Py_ssize_t printfunc;
145145

146-
/* Specialize a binary op by setting the descriptor pointer */
147-
struct _PyBinopSpecializationDescr;
148-
typedef int (*binop_specialize_func)(PyObject *v, PyObject *w, int oparg,
149-
struct _PyBinopSpecializationDescr **descr);
150-
151146
// If this structure is modified, Doc/includes/typestruct.h should be updated
152147
// as well.
153148
struct _typeobject {
@@ -238,13 +233,6 @@ struct _typeobject {
238233
/* bitset of which type-watchers care about this type */
239234
unsigned char tp_watched;
240235

241-
/* callback that may specialize BINARY_OP
242-
* this is an experimental API based on the ideas in the paper
243-
* Cross Module Quickening - The Curious Case of C Extensions
244-
* by Felix Berlakovich and Stefan Brunthaler.
245-
*/
246-
binop_specialize_func tp_binop_specialize;
247-
248236
/* Number of tp_version_tag values used.
249237
* Set to _Py_ATTR_CACHE_UNUSED if the attribute cache is
250238
* disabled for this type (e.g. due to custom MRO entries).

Include/internal/pycore_code.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,13 @@ adaptive_counter_backoff(_Py_BackoffCounter counter) {
480480
/* Specialization Extensions */
481481

482482
/* callbacks for an external specialization */
483-
484-
struct _PyBinopSpecializationDescr;
485-
486483
typedef int (*binaryopguardfunc)(PyObject *lhs, PyObject *rhs);
487-
typedef PyObject* (*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
488-
typedef void (*binaryopfreefunc)(struct _PyBinopSpecializationDescr *descr);
484+
typedef PyObject *(*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
489485

490-
typedef struct _PyBinopSpecializationDescr {
486+
typedef struct {
491487
int oparg;
492488
binaryopguardfunc guard;
493489
binaryopactionfunc action;
494-
binaryopfreefunc free;
495490
} _PyBinaryOpSpecializationDescr;
496491

497492
/* Comparison bit masks. */

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/typeslots.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,4 @@
9393
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000
9494
/* New in 3.14 */
9595
#define Py_tp_token 83
96-
#define Py_tp_binop_specialize 84
9796
#endif

Lib/test/test_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ def delx(self): del self.__x
17761776
check((1,2,3), vsize('') + self.P + 3*self.P)
17771777
# type
17781778
# static type: PyTypeObject
1779-
fmt = 'P2nPI13Pl4Pn9Pn12PI3Pc'
1779+
fmt = 'P2nPI13Pl4Pn9Pn12PIPc'
17801780
s = vsize(fmt)
17811781
check(int, s)
17821782
typeid = 'n' if support.Py_GIL_DISABLED else ''

Misc/NEWS.d/next/Core_and_Builtins/2025-05-04-19-32-47.gh-issue-133395.VhWWEP.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Modules/arraymodule.c

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
1515
#include "pycore_moduleobject.h" // _PyModule_GetState()
1616

17-
#include "opcode.h" // binary op opargs (NB_*)
18-
1917
#include <stddef.h> // offsetof()
2018
#include <stdbool.h>
2119

@@ -850,10 +848,6 @@ array_richcompare(PyObject *v, PyObject *w, int op)
850848
return res;
851849
}
852850

853-
static int
854-
array_binop_specialize(PyObject *v, PyObject *w, int oparg,
855-
_PyBinaryOpSpecializationDescr **descr);
856-
857851
static Py_ssize_t
858852
array_length(PyObject *op)
859853
{
@@ -2969,8 +2963,6 @@ static PyType_Slot array_slots[] = {
29692963
{Py_tp_alloc, PyType_GenericAlloc},
29702964
{Py_tp_new, array_new},
29712965
{Py_tp_traverse, array_tp_traverse},
2972-
{Py_tp_token, Py_TP_USE_SPEC},
2973-
{Py_tp_binop_specialize, array_binop_specialize},
29742966

29752967
/* as sequence */
29762968
{Py_sq_length, array_length},
@@ -3003,70 +2995,6 @@ static PyType_Spec array_spec = {
30032995
.slots = array_slots,
30042996
};
30052997

3006-
static inline int
3007-
array_subscr_guard(PyObject *lhs, PyObject *rhs)
3008-
{
3009-
PyObject *exc = PyErr_GetRaisedException();
3010-
int ret = PyType_GetBaseByToken(Py_TYPE(lhs), &array_spec, NULL);
3011-
if (ret < 0) {
3012-
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3013-
PyErr_Clear();
3014-
ret = 0;
3015-
}
3016-
}
3017-
_PyErr_ChainExceptions1(exc);
3018-
return ret;
3019-
}
3020-
3021-
static PyObject *
3022-
array_subscr_action(PyObject *lhs, PyObject *rhs)
3023-
{
3024-
return array_subscr(lhs, rhs);
3025-
}
3026-
3027-
static void
3028-
array_subscr_free(_PyBinaryOpSpecializationDescr* descr)
3029-
{
3030-
if (descr != NULL) {
3031-
PyMem_Free(descr);
3032-
}
3033-
}
3034-
3035-
static int
3036-
array_binop_specialize(PyObject *v, PyObject *w, int oparg,
3037-
_PyBinaryOpSpecializationDescr **descr)
3038-
{
3039-
array_state *state = find_array_state_by_type(Py_TYPE(v));
3040-
3041-
if (!array_Check(v, state)) {
3042-
return 0;
3043-
}
3044-
3045-
*descr = NULL;
3046-
switch(oparg) {
3047-
case NB_SUBSCR:
3048-
if (array_subscr_guard(v, w)) {
3049-
*descr = (_PyBinaryOpSpecializationDescr*)PyMem_Malloc(
3050-
sizeof(_PyBinaryOpSpecializationDescr));
3051-
if (*descr == NULL) {
3052-
PyErr_NoMemory();
3053-
return -1;
3054-
}
3055-
**descr = (_PyBinaryOpSpecializationDescr) {
3056-
.oparg = oparg,
3057-
.guard = array_subscr_guard,
3058-
.action = array_subscr_action,
3059-
.free = array_subscr_free,
3060-
};
3061-
return 1;
3062-
}
3063-
break;
3064-
}
3065-
3066-
return 0;
3067-
}
3068-
3069-
30702998
/*********************** Array Iterator **************************/
30712999

30723000
/*[clinic input]

Objects/typeslots.inc

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -801,19 +801,9 @@ dummy_func(
801801
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
802802
_PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr*)descr;
803803
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
804-
assert(d);
805-
assert(d->guard);
804+
assert(d && d->guard);
806805
int res = d->guard(left_o, right_o);
807-
ERROR_IF(res < 0);
808-
if (res == 0) {
809-
if (d->free) {
810-
d->free(d);
811-
}
812-
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(this_instr+1);
813-
write_ptr(cache->external_cache, NULL);
814-
this_instr->op.code = BINARY_OP;
815-
DEOPT_IF(true);
816-
}
806+
DEOPT_IF(!res);
817807
}
818808

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

827817
PyObject *res_o = d->action(left_o, right_o);
828818
DECREF_INPUTS();
829-
ERROR_IF(res_o == NULL);
830819
res = PyStackRef_FromPyObjectSteal(res_o);
831820
}
832821

Python/executor_cases.c.h

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 5 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_cases.c.h

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/specialize.c

Lines changed: 4 additions & 31 deletions
< D409 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,7 @@ LONG_FLOAT_ACTION(compactlong_float_multiply, *)
25342534
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
25352535
#undef LONG_FLOAT_ACTION
25362536

2537-
static const _PyBinaryOpSpecializationDescr binaryop_extend_builtins[] = {
2537+
static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
25382538
/* long-long arithmetic */
25392539
{NB_OR, compactlongs_guard, compactlongs_or},
25402540
{NB_AND, compactlongs_guard, compactlongs_and},
@@ -2560,41 +2560,14 @@ static int
25602560
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
25612561
_PyBinaryOpSpecializationDescr **descr)
25622562
{
2563-
/* We are currently using this only for NB_SUBSCR, which is not
2564-
* commutative. Will need to revisit this function when we use
2565-
* this for operators which are.
2566-
*/
2567-
2568-
typedef _PyBinaryOpSpecializationDescr descr_type;
2569-
size_t size = Py_ARRAY_LENGTH(binaryop_extend_builtins);
2570-
for (size_t i = 0; i < size; i++) {
2571-
descr_type *d = (descr_type *)&binaryop_extend_builtins[i];
2572-
assert(d != NULL);
2573-
assert(d->guard != NULL);
2563+
size_t n = sizeof(binaryop_extend_descrs)/sizeof(_PyBinaryOpSpecializationDescr);
2564+
for (size_t i = 0; i < n; i++) {
2565+
_PyBinaryOpSpecializationDescr *d = &binaryop_extend_descrs[i];
25742566
if (d->oparg == oparg && d->guard(lhs, rhs)) {
25752567
*descr = d;
25762568
return 1;
25772569
}
25782570
}
2579-
2580-
PyTypeObject *lhs_type = Py_TYPE(lhs);
2581-
if (lhs_type->tp_binop_specialize != NULL) {
2582-
int ret = lhs_type->tp_binop_specialize(lhs, rhs, oparg, descr);
2583-
if (ret < 0) {
2584-
return -1;
2585-
}
2586-
if (ret == 1) {
2587-
if (*descr == NULL) {
2588-
PyErr_Format(
2589-
PyExc_ValueError,
2590-
"tp_binop_specialize of '%T' returned 1 with *descr == NULL",
2591-
lhs);
2592-
return -1;
2593-
}
2594-
(*descr)->oparg = oparg;
2595-
}
2596-
return ret;
2597-
}
25982571
return 0;
25992572
}
26002573

0 commit comments

Comments
 (0)
0