Skip to content

Commit 72dfba4

Browse files
committed
basic array plugin
1 parent 79e45a1 commit 72dfba4

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 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: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/arraymodule.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,25 +3204,38 @@ do { \
32043204
} while (0)
32053205

32063206
static inline int
3207-
array_guard(PyObject *lhs, PyObject *rhs)
3207+
array_subscr_guard(PyObject *lhs, PyObject *rhs)
32083208
{
3209-
fprintf(stderr, "array_guard\n");
3210-
return 0;
3209+
PyObject *exc = PyErr_GetRaisedException();
3210+
PyObject *module = PyType_GetModuleByDef(Py_TYPE(lhs), &arraymodule);
3211+
if (module == NULL) {
3212+
if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_TypeError)) {
3213+
/* lhs is not an array instance - ignore the TypeError (if any) */
3214+
PyErr_SetRaisedException(exc);
3215+
return 0;
3216+
}
3217+
else {
3218+
_PyErr_ChainExceptions1(exc);
3219+
return -1;
3220+
}
3221+
}
3222+
PyErr_SetRaisedException(exc);
3223+
return array_Check(lhs, get_array_state(module));
32113224
}
32123225

32133226
static PyObject *
3214-
array_action(PyObject *lhs, PyObject *rhs)
3227+
array_subscr_action(PyObject *lhs, PyObject *rhs)
32153228
{
3216-
return NULL;
3229+
return array_subscr(lhs, rhs);
32173230
}
32183231

32193232
static int
32203233
array_register_specializations(void)
32213234
{
32223235
_PyBinaryOpSpecializationDescr descr = {
3223-
.oparg = NB_MULTIPLY,
3224-
.guard = array_guard,
3225-
.action = array_action,
3236+
.oparg = NB_SUBSCR,
3237+
.guard = array_subscr_guard,
3238+
.action = array_subscr_action,
32263239
};
32273240
if (_Py_Specialize_AddBinaryOpExtention(&descr) < 0) {
32283241
return -1;

Python/bytecodes.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ dummy_func(
803803
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
804804
assert(d && d->guard);
805805
int res = d->guard(left_o, right_o);
806-
DEOPT_IF(!res);
806+
ERROR_IF(res < 0, error);
807+
DEOPT_IF(res == 0);
807808
}
808809

809810
pure op(_BINARY_OP_EXTEND, (descr/4, left, right -- res)) {
@@ -816,6 +817,7 @@ dummy_func(
816817

817818
PyObject *res_o = d->action(left_o, right_o);
818819
DECREF_INPUTS();
820+
ERROR_IF(res_o == NULL, error);
819821
res = PyStackRef_FromPyObjectSteal(res_o);
820822
}
821823

Python/executor_cases.c.h

Lines changed: 7 additions & 1 deletion
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: 7 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: 7 additions & 4 deletions
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 _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
2537+
static _PyBinaryOpSpecializationDescr binaryop_extend_builtins[] = {
25382538
/* long-long arithmetic */
25392539
{NB_OR, compactlongs_guard, compactlongs_or},
25402540
{NB_AND, compactlongs_guard, compactlongs_and},
@@ -2582,6 +2582,8 @@ binary_op_extended_specialization_from_list(
25822582
{
25832583
for (size_t i = 0; i < size; i++) {
25842584
_PyBinaryOpSpecializationDescr *d = &descrs[i];
2585+
assert(d != NULL);
2586+
assert(d->guard != NULL);
25852587
if (d->oparg == oparg && d->guard(lhs, rhs)) {
25862588
*descr = d;
25872589
return 1;
@@ -2594,9 +2596,10 @@ static int
25942596
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
25952597
_PyBinaryOpSpecializationDescr **descr)
25962598
{
2599+
typedef _PyBinaryOpSpecializationDescr descr_type;
25972600
if (binary_op_extended_specialization_from_list(
2598-
binaryop_extend_descrs,
2599-
sizeof(binaryop_extend_descrs)/sizeof(_PyBinaryOpSpecializationDescr),
2601+
binaryop_extend_builtins,
2602+
sizeof(binaryop_extend_builtins)/sizeof(descr_type),
26002603
lhs, rhs, oparg, descr))
26012604
{
26022605
return 1;
@@ -2605,7 +2608,7 @@ binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
26052608
PyThreadState *tstate = PyThreadState_Get();
26062609
_Py_c_array_t *extensions = &tstate->interp->binop_specializer_extentions;
26072610
if (binary_op_extended_specialization_from_list(
2608-
(_PyBinaryOpSpecializationDescr *)extensions->array,
2611+
(descr_type*)extensions->array,
26092612
tstate->interp->num_binop_specializer_extentions,
26102613
lhs, rhs, oparg, descr))
26112614
{

0 commit comments

Comments
 (0)