8000 GH-98831: Implement basic cache effects (#99313) · python/cpython@e37744f · GitHub
[go: up one dir, main page]

Skip to content

Commit e37744f

Browse files
authored
GH-98831: Implement basic cache effects (#99313)
1 parent 4636df9 commit e37744f

File tree

4 files changed

+202
-128
lines changed

4 files changed

+202
-128
lines changed

Python/bytecodes.c

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,9 @@ do { \
7676
#define NAME_ERROR_MSG \
7777
"name '%.200s' is not defined"
7878

79-
typedef struct {
80-
PyObject *kwnames;
81-
} CallShape;
82-
8379
// Dummy variables for stack effects.
8480
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
85-
static PyObject *container, *start, *stop, *v;
81+
static PyObject *container, *start, *stop, *v, *lhs, *rhs;
8682

8783
static PyObject *
8884
dummy_func(
@@ -101,6 +97,8 @@ dummy_func(
10197
binaryfunc binary_ops[]
10298
)
10399
{
100+
_PyInterpreterFrame entry_frame;
101+
104102
switch (opcode) {
105103

106104
// BEGIN BYTECODES //
@@ -193,7 +191,21 @@ dummy_func(
193191
ERROR_IF(res == NULL, error);
194192
}
195193

196-
inst(BINARY_OP_MULTIPLY_INT, (left, right -- prod)) {
194+
family(binary_op, INLINE_CACHE_ENTRIES_BINARY_OP) = {
195+
BINARY_OP,
196+
BINARY_OP_ADD_FLOAT,
197+
BINARY_OP_ADD_INT,
198+
BINARY_OP_ADD_UNICODE,
199+
BINARY_OP_GENERIC,
200+
// BINARY_OP_INPLACE_ADD_UNICODE, // This is an odd duck.
201+
BINARY_OP_MULTIPLY_FLOAT,
202+
BINARY_OP_MULTIPLY_INT,
203+
BINARY_OP_SUBTRACT_FLOAT,
204+
BINARY_OP_SUBTRACT_INT,
205+
};
206+
207+
208+
inst(BINARY_OP_MULTIPLY_INT, (left, right, unused/1 -- prod)) {
197209
assert(cframe.use_tracing == 0);
198210
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
199211
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
@@ -202,10 +214,9 @@ dummy_func(
202214
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
203215
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
204216
ERROR_IF(prod == NULL, error);
205-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
206217
}
207218

208-
inst(BINARY_OP_MULTIPLY_FLOAT, (left, right -- prod)) {
219+
inst(BINARY_OP_MULTIPLY_FLOAT, (left, right, unused/1 -- prod)) {
209220
assert(cframe.use_tracing == 0);
210221
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
211222
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
@@ -216,10 +227,9 @@ dummy_func(
216227
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
217228
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
218229
ERROR_IF(prod == NULL, error);
219-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
220230
}
221231

222-
inst(BINARY_OP_SUBTRACT_INT, (left, right -- sub)) {
232+
inst(BINARY_OP_SUBTRACT_INT, (left, right, unused/1 -- sub)) {
223233
assert(cframe.use_tracing == 0);
224234
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
225235
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
@@ -228,10 +238,9 @@ dummy_func(
228238
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
229239
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
230240
ERROR_IF(sub == NULL, error);
231-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
232241
}
233242

234-
inst(BINARY_OP_SUBTRACT_FLOAT, (left, right -- sub)) {
243+
inst(BINARY_OP_SUBTRACT_FLOAT, (left, right, unused/1 -- sub)) {
235244
assert(cframe.use_tracing == 0);
236245
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
237246
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
@@ -241,10 +250,9 @@ dummy_func(
241250
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
242251
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
243252
ERROR_IF(sub == NULL, error);
244-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
245253
}
246254

247-
inst(BINARY_OP_ADD_UNICODE, (left, right -- res)) {
255+
inst(BINARY_OP_ADD_UNICODE, (left, 1E0A right, unused/1 -- res)) {
248256
assert(cframe.use_tracing == 0);
249257
DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
250258
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
@@ -253,7 +261,6 @@ dummy_func(
253261
_Py_DECREF_SPECIALIZED(left, _PyUnicode_ExactDealloc);
254262
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
255263
ERROR_IF(res == NULL, error);
256-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
257264
}
258265

259266
// This is a subtle one. It's a super-instruction for
@@ -292,7 +299,7 @@ dummy_func(
292299
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
293300
}
294301

295-
inst(BINARY_OP_ADD_FLOAT, (left, right -- sum)) {
302+
inst(BINARY_OP_ADD_FLOAT, (left, right, unused/1 -- sum)) {
296303
assert(cframe.use_tracing == 0);
297304
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
298305
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
@@ -303,10 +310,9 @@ dummy_func(
303310
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
304311
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
305312
ERROR_IF(sum == NULL, error);
306-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
307313
}
308314

309-
inst(BINARY_OP_ADD_INT, (left, right -- sum)) {
315+
inst(BINARY_OP_ADD_INT, (left, right, unused/1 -- sum)) {
310316
assert(cframe.use_tracing == 0);
311317
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
312318
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
@@ -315,7 +321,6 @@ dummy_func(
315321
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
316322
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
317323
ERROR_IF(sum == NULL, error);
318-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
319324
}
320325

321326
inst(BINARY_SUBSCR, (container, sub -- res)) {
@@ -3691,30 +3696,21 @@ dummy_func(
36913696
PUSH(Py_NewRef(peek));
36923697
}
36933698

3694-
// stack effect: (__0 -- )
3695-
inst(BINARY_OP_GENERIC) {
3696-
PyObject *rhs = POP();
3697-
PyObject *lhs = TOP();
3699+
inst(BINARY_OP_GENERIC, (lhs, rhs, unused/1 -- res)) {
36983700
assert(0 <= oparg);
36993701
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
37003702
assert(binary_ops[oparg]);
3701-
PyObject *res = binary_ops[oparg](lhs, rhs);
3703+
res = binary_ops[oparg](lhs, rhs);
37023704
Py_DECREF(lhs);
37033705
Py_DECREF(rhs);
3704-
SET_TOP(res);
3705-
if (res == NULL) {
3706-
goto error;
3707-
}
3708-
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
3706+
ERROR_IF(res == NULL, error);
37093707
}
37103708

3711-
// stack effect: (__0 -- )
3712-
inst(BINARY_OP) {
3709+
// This always dispatches, so the result is unused.
3710+
inst(BINARY_OP, (lhs, rhs, unused/1 -- unused)) {
37133711
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)next_instr;
37143712
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
37153713
assert(cframe.use_tracing == 0);
3716-
PyObject *lhs = SECOND();
3717-
PyObject *rhs = TOP();
37183714
next_instr--;
37193715
_Py_Specialize_BinaryOp(lhs, rhs, next_instr, oparg, &GETLOCAL(0));
37203716
DISPATCH_SAME_OPARG();
@@ -3761,13 +3757,8 @@ dummy_func(
37613757
;
37623758
}
37633759

3764-
// Families go below this point //
3760+
// Future families go below this point //
37653761

3766-
family(binary_op) = {
3767-
BINARY_OP, BINARY_OP_ADD_FLOAT,
3768-
BINARY_OP_ADD_INT, BINARY_OP_ADD_UNICODE, BINARY_OP_GENERIC, BINARY_OP_INPLACE_ADD_UNICODE,
3769-
BINARY_OP_MULTIPLY_FLOAT, BINARY_OP_MULTIPLY_INT, BINARY_OP_SUBTRACT_FLOAT,
3770-
BINARY_OP_SUBTRACT_INT };
37713762
family(binary_subscr) = {
37723763
BINARY_SUBSCR, BINARY_SUBSCR_DICT,
37733764
BINARY_SUBSCR_GETITEM, BINARY_SUBSCR_LIST_INT, BINARY_SUBSCR_TUPLE_INT };

Python/generated_cases.c.h

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

0 commit comments

Comments
 (0)
0