Skip to content

GH-98831: Implement basic cache effects #99313

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 22 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9f15c4b
Support simple cache effects
gvanrossum Nov 10, 2022
6189043
More BINARY_OP instructions
gvanrossum Nov 10, 2022
f5e1aed
Merge remote-tracking branch 'origin/main' into cache-effects
gvanrossum Nov 10, 2022
a8d608d
Tweak dummy definitions in bytecodes.c after merge
gvanrossum Nov 10, 2022
4ee85e7
gh-99300: Use Py_NewRef() in Objects/ directory (#99332)
vstinner Nov 10, 2022
873da31
gh-99300: Use Py_NewRef() in Objects/dictobject.c (#99333)
vstinner Nov 10, 2022
e0ab5b8
gh-90110: Update the C-analyzer Tool (gh-99307)
ericsnowcurrently Nov 10, 2022
882fdec
gh-99277: remove older version of `get_write_buffer_limits` (#99280)
csuriano23 Nov 10, 2022
8226fb9
gh-99204: Calculate base_executable by alternate names in POSIX venvs…
vfazio Nov 10, 2022
1aa0124
GH-99298: Don't perform jumps before error handling (GH-99299)
brandtbucher Nov 10, 2022
d094e42
GH-98831: Remove all remaining DISPATCH() calls from bytecodes.c (#99…
gvanrossum Nov 10, 2022
d3d907a
Remaining BINARY_OP family members
gvanrossum Nov 10, 2022
0339a67
Uniformly skip 'unused' effects
gvanrossum Nov 10, 2022
f3e7dd6
Remove superfluous asserts; fix one 'is not'
gvanrossum Nov 10, 2022
e3ff6ac
Make BINARY_OP result unused
gvanrossum Nov 11, 2022
3db443a
Fix parser for family()
gvanrossum Nov 11, 2022
c58a85a
Check family consistency
gvanrossum Nov 11, 2022
756a41b
Add first family (binary_op)
gvanrossum Nov 11, 2022
48400ac
Add assert() to double-check cache struct size
gvanrossum Nov 11, 2022
433243a
Merge commit '00ee6d506e' into cache-effects
gvanrossum Nov 11, 2022
3d51484
Merge commit '694cdb24a6' into cache-effects
gvanrossum Nov 11, 2022
4d42a0a
Merge remote-tracking branch 'origin/main' into cache-effects
gvanrossum Nov 11, 2022
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
69 changes: 30 additions & 39 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ do { \
#define NAME_ERROR_MSG \
"name '%.200s' is not defined"

typedef struct {
PyObject *kwnames;
} CallShape;

// Dummy variables for stack effects.
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
static PyObject *container, *start, *stop, *v;
static PyObject *container, *start, *stop, *v, *lhs, *rhs;
Comment on lines 80 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(


static PyObject *
dummy_func(
Expand All @@ -101,6 +97,8 @@ dummy_func(
binaryfunc binary_ops[]
)
{
_PyInterpreterFrame entry_frame;

switch (opcode) {

// BEGIN BYTECODES //
Expand Down Expand Up @@ -193,7 +191,21 @@ dummy_func(
ERROR_IF(res == NULL, error);
}

inst(BINARY_OP_MULTIPLY_INT, (left, right -- prod)) {
family(binary_op, INLINE_CACHE_ENTRIES_BINARY_OP) = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't think we really need the INLINE_CACHE_ENTRIES_WHATEVER stuff (or the asserts it produces) in this file anymore (they were originally added to simplify the JUMPBY(...) moves, but those are going to be generated now).

I feel like it sort of just complicates parsing and code generation for no real benefit... plus, we actually already have asserts to this effect in specialize.c where we ended up re-using these constants in some places.

Suggested change
family(binary_op, INLINE_CACHE_ENTRIES_BINARY_OP) = {
family(binary_op) = {

BINARY_OP,
BINARY_OP_ADD_FLOAT,
BINARY_OP_ADD_INT,
BINARY_OP_ADD_UNICODE,
BINARY_OP_GENERIC,
// BINARY_OP_INPLACE_ADD_UNICODE, // This is an odd duck.
BINARY_OP_MULTIPLY_FLOAT,
BINARY_OP_MULTIPLY_INT,
BINARY_OP_SUBTRACT_FLOAT,
BINARY_OP_SUBTRACT_INT,
};


inst(BINARY_OP_MULTIPLY_INT, (left, right, unused/1 -- prod)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my preference, but I sort of prefer a name like _ to a name like unused for our syntax here. I guess it just feels more "special", and doesn't distract:

Suggested change
inst(BINARY_OP_MULTIPLY_INT, (left, right, unused/1 -- prod)) {
inst(BINARY_OP_MULTIPLY_INT, (left, right, _/1 -- prod)) {

assert(cframe.use_tracing == 0);
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
Expand All @@ -202,10 +214,9 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
ERROR_IF(prod == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_OP_MULTIPLY_FLOAT, (left, right -- prod)) {
inst(BINARY_OP_MULTIPLY_FLOAT, (left, right, unused/1 -- prod)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
Expand All @@ -216,10 +227,9 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(prod == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_OP_SUBTRACT_INT, (left, right -- sub)) {
inst(BINARY_OP_SUBTRACT_INT, (left, right, unused/1 -- sub)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
Expand All @@ -228,10 +238,9 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
ERROR_IF(sub == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_OP_SUBTRACT_FLOAT, (left, right -- sub)) {
inst(BINARY_OP_SUBTRACT_FLOAT, (left, right, unused/1 -- sub)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
Expand All @@ -241,10 +250,9 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(sub == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_OP_ADD_UNICODE, (left, right -- res)) {
inst(BINARY_OP_ADD_UNICODE, (left, right, unused/1 -- res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
Expand All @@ -253,7 +261,6 @@ dummy_func(
_Py_DECREF_SPECIALIZED(left, _PyUnicode_ExactDealloc);
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
ERROR_IF(res == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

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

inst(BINARY_OP_ADD_FLOAT, (left, right -- sum)) {
inst(BINARY_OP_ADD_FLOAT, (left, right, unused/1 -- sum)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
Expand All @@ -303,10 +310,9 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(sum == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_OP_ADD_INT, (left, right -- sum)) {
inst(BINARY_OP_ADD_INT, (left, right, unused/1 -- sum)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
Expand All @@ -315,7 +321,6 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
ERROR_IF(sum == NULL, error);
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
}

inst(BINARY_SUBSCR, (container, sub -- res)) {
Expand Down Expand Up @@ -3691,30 +3696,21 @@ dummy_func(
PUSH(Py_NewRef(peek));
}

// stack effect: (__0 -- )
inst(BINARY_OP_GENERIC) {
PyObject *rhs = POP();
PyObject *lhs = TOP();
inst(BINARY_OP_GENERIC, (lhs, rhs, unused/1 -- res)) {
assert(0 <= oparg);
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
assert(binary_ops[oparg]);
PyObject *res = binary_ops[oparg](lhs, rhs);
res = binary_ops[oparg](lhs, rhs);
Py_DECREF(lhs);
Py_DECREF(rhs);
SET_TOP(res);
if (res == NULL) {
goto error;
}
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
ERROR_IF(res == NULL, error);
}

// stack effect: (__0 -- )
inst(BINARY_OP) {
// This always dispatches, so the result is unused.
inst(BINARY_OP, (lhs, rhs, unused/1 -- unused)) {
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
assert(cframe.use_tracing == 0);
PyObject *lhs = SECOND();
PyObject *rhs = TOP();
next_instr--;
_Py_Specialize_BinaryOp(lhs, rhs, next_instr, oparg, &GETLOCAL(0));
DISPATCH_SAME_OPARG();
Expand Down Expand Up @@ -3761,13 +3757,8 @@ dummy_func(
;
}

// Families go below this point //
// Future families go below this point //

family(binary_op) = {
BINARY_OP, BINARY_OP_ADD_FLOAT,
BINARY_OP_ADD_INT, BINARY_OP_ADD_UNICODE, BINARY_OP_GENERIC, BINARY_OP_INPLACE_ADD_UNICODE,
BINARY_OP_MULTIPLY_FLOAT, BINARY_OP_MULTIPLY_INT, BINARY_OP_SUBTRACT_FLOAT,
BINARY_OP_SUBTRACT_INT };
family(binary_subscr) = {
BINARY_SUBSCR, BINARY_SUBSCR_DICT,
BINARY_SUBSCR_GETITEM, BINARY_SUBSCR_LIST_INT, BINARY_SUBSCR_TUPLE_INT };
Expand Down
35 changes: 18 additions & 17 deletions Python/generated_cases.c.h

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

Loading