Skip to content

bpo-26280: Port BINARY_SUBSCR to PEP 659 adaptive interpreter #27043

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 23 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ed1b0f4
bpo-26280: Port BINARY_SUBSCR to PEP 659 adaptive interpreter
iritkatriel Jul 5, 2021
99219b0
POP() -->STACK_SHRINK(1)
iritkatriel Jul 6, 2021
f051c19
revert change to MAGIC_NUMBER
iritkatriel Jul 7, 2021
6401da8
add STAT_INCs
iritkatriel Jul 7, 2021
95f85d2
use unchecked GET_ITEM
iritkatriel Jul 7, 2021
f6337be
add SPECIALIZATION_FAIL stats
iritkatriel Jul 7, 2021
dc45cac
print BINARY_SUBSCR stats
iritkatriel Jul 7, 2021
09f8c50
fix SPECIALIZATION_FAIL
iritkatriel Jul 7, 2021
1efce2e
optimise bytemode implementations
iritkatriel Jul 8, 2021
d32bfc9
more precise opcode and label names. Added (currently redundant) goto…
iritkatriel Jul 13, 2021
ccaea79
remove redundant null check
iritkatriel Jul 13, 2021
b40bfb7
check container type more efficiently
iritkatriel Jul 13, 2021
61fa932
use oparg as the adaptive cache counter for BINARY_SUBSCR
iritkatriel Jul 13, 2021
289f7be
avoid PyLong_AsSsize_t for index bounds check
iritkatriel Jul 14, 2021
2f4d359
update Python/importlib_external.h
iritkatriel Jul 14, 2021
1a2c302
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 14, 2021
5b2e3f9
revert change to adptive cache functions api
iritkatriel Jul 14, 2021
4f909d6
add missing include
iritkatriel Jul 14, 2021
a863a3d
added UPDATE_PREV_INSTR_OPARG macro and use it to control the oparg c…
iritkatriel Jul 14, 2021
c30a188
use the full weird sign check
iritkatriel Jul 14, 2021
d5b0f32
revert unintended change to magic number
iritkatriel Jul 14, 2021
a9c9851
update Python/importlib_external.h
iritkatriel Jul 14, 2021
19cdf02
_Size --> _GET_SIZE
iritkatriel Jul 15, 2021
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
5 changes: 3 additions & 2 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,18 @@ too_many_cache_misses(_PyAdaptiveEntry *entry) {
return entry->counter == saturating_zero();
}

#define BACKOFF 64
#define ADAPTIVE_CACHE_BACKOFF 64
Copy link
Member

Choose a reason for hiding this comment

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

👍


static inline void
cache_backoff(_PyAdaptiveEntry *entry) {
entry->counter = BACKOFF;
entry->counter = ADAPTIVE_CACHE_BACKOFF;
}

/* Specialization functions */

int _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);

#define SPECIALIZATION_STATS 0
#define SPECIALIZATION_STATS_DETAILED 0
Expand Down
22 changes: 13 additions & 9 deletions Include/opcode.h

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

4 changes: 4 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def jabs_op(name, op):
del def_op, name_op, jrel_op, jabs_op

_specialized_instructions = [
"BINARY_SUBSCR_ADAPTIVE",
"BINARY_SUBSCR_LIST_INT",
"BINARY_SUBSCR_TUPLE_INT",
"BINARY_SUBSCR_DICT",
"JUMP_ABSOLUTE_QUICK",
"LOAD_ATTR_ADAPTIVE",
"LOAD_ATTR_SPLIT_KEYS",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Implement adaptive specialization for BINARY_SUBSCR

Three specialized forms of BINARY_SUBSCR are added:

* BINARY_SUBSCR_LIST_INT

* BINARY_SUBSCR_TUPLE_INT

* BINARY_SUBSCR_DICT
116 changes: 116 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
#include "pycore_code.h"
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_moduleobject.h"
#include "pycore_pyerrors.h" // _PyErr_Fetch()
Expand Down Expand Up @@ -1398,6 +1399,8 @@ eval_frame_handle_pending(PyThreadState *tstate)

#define DEOPT_IF(cond, instname) if (cond) { goto instname ## _miss; }

#define UPDATE_PREV_INSTR_OPARG(instr, oparg) ((uint8_t*)(instr))[-1] = (oparg)

#define GLOBALS() specials[FRAME_SPECIALS_GLOBALS_OFFSET]
#define BUILTINS() specials[FRAME_SPECIALS_BUILTINS_OFFSET]
#define LOCALS() specials[FRAME_SPECIALS_LOCALS_OFFSET]
Expand Down Expand Up @@ -1913,6 +1916,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(BINARY_SUBSCR): {
PREDICTED(BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, unquickened);
PyObject *sub = POP();
PyObject *container = TOP();
PyObject *res = PyObject_GetItem(container, sub);
Expand All @@ -1924,6 +1929,91 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
DISPATCH();
}

case TARGET(BINARY_SUBSCR_ADAPTIVE): {
if (oparg == 0) {
PyObject *sub = TOP();
PyObject *container = SECOND();
next_instr--;
if (_Py_Specialize_BinarySubscr(container, sub, next_instr) < 0) {
goto error;
}
DISPATCH();
}
else {
STAT_INC(BINARY_SUBSCR, deferred);
// oparg is the adaptive cache counter
UPDATE_PREV_INSTR_OPARG(next_instr, oparg - 1);
assert(_Py_OPCODE(next_instr[-1]) == BINARY_SUBSCR_ADAPTIVE);
assert(_Py_OPARG(next_instr[-1]) == oparg - 1);
JUMP_TO_INSTRUCTION(BINARY_SUBSCR);
}
}

case TARGET(BINARY_SUBSCR_LIST_INT): {
PyObject *sub = TOP();
PyObject *list = SECOND();
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyList_CheckExact(list), BINARY_SUBSCR);

// Deopt unless 0 <= sub < PyList_Size(list)
Py_ssize_t signed_magnitude = Py_SIZE(sub);
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
assert(((PyLongObject *)_PyLong_GetZero())->ob_digit[0] == 0);
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyList_GET_SIZE(list), BINARY_SUBSCR);

STAT_INC(BINARY_SUBSCR, hit);
PyObject *res = PyList_GET_ITEM(list, index);
assert(res != NULL);
Py_INCREF(res);
STACK_SHRINK(1);
Py_DECREF(sub);
SET_TOP(res);
Py_DECREF(list);
DISPATCH();
}

case TARGET(BINARY_SUBSCR_TUPLE_INT): {
PyObject *sub = TOP();
PyObject *tuple = SECOND();
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);

// Deopt unless 0 <= sub < PyTuple_Size(list)
Py_ssize_t signed_magnitude = Py_SIZE(sub);
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
assert(((PyLongObject *)_PyLong_GetZero())->ob_digit[0] == 0);
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyTuple_GET_SIZE(tuple), BINARY_SUBSCR);

STAT_INC(BINARY_SUBSCR, hit);
PyObject *res = PyTuple_GET_ITEM(tuple, index);
assert(res != NULL);
Py_INCREF(res);
STACK_SHRINK(1);
Py_DECREF(sub);
SET_TOP(res);
Py_DECREF(tuple);
DISPATCH();
}

case TARGET(BINARY_SUBSCR_DICT): {
PyObject *dict = SECOND();
DEOPT_IF(!PyDict_CheckExact(SECOND()), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
PyObject *sub = TOP();
PyObject *res = PyDict_GetItemWithError(dict, sub);
if (res == NULL) {
goto binary_subscr_dict_error;
}
Py_INCREF(res);
STACK_SHRINK(1);
Py_DECREF(sub);
SET_TOP(res);
Py_DECREF(dict);
DISPATCH();
}

case TARGET(BINARY_LSHIFT): {
PyObject *right = POP();
PyObject *left = TOP();
Expand Down Expand Up @@ -4327,8 +4417,34 @@ opname ## _miss: \
JUMP_TO_INSTRUCTION(opname); \
}

#define MISS_WITH_OPARG_COUNTER(opname) \
opname ## _miss: \
{ \
STAT_INC(opname, miss); \
uint8_t oparg = saturating_decrement(_Py_OPARG(next_instr[-1])); \
UPDATE_PREV_INSTR_OPARG(next_instr, oparg); \
assert(_Py_OPARG(next_instr[-1]) == oparg); \
if (oparg == saturating_zero()) /* too many cache misses */ { \
oparg = ADAPTIVE_CACHE_BACKOFF; \
next_instr[-1] = _Py_MAKECODEUNIT(opname ## _ADAPTIVE, oparg); \
STAT_INC(opname, deopt); \
} \
JUMP_TO_INSTRUCTION(opname); \
}

MISS_WITH_CACHE(LOAD_ATTR)
MISS_WITH_CACHE(LOAD_GLOBAL)
MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)

binary_subscr_dict_error:
{
PyObject *sub = POP();
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub);
}
Py_DECREF(sub);
goto error;
}

error:
/* Double-check exception status. */
Expand Down
22 changes: 11 additions & 11 deletions Python/opcode_targets.h

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

48 changes: 47 additions & 1 deletion Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ _Py_PrintSpecializationStats(void)
printf("Specialization stats:\n");
print_stats(&_specialization_stats[LOAD_ATTR], "load_attr");
print_stats(&_specialization_stats[LOAD_GLOBAL], "load_global");
print_stats(&_specialization_stats[BINARY_SUBSCR], "binary_subscr");
}

#if SPECIALIZATION_STATS_DETAILED
Expand Down Expand Up @@ -162,12 +163,14 @@ get_cache_count(SpecializedCacheOrInstruction *quickened) {
static uint8_t adaptive_opcodes[256] = {
[LOAD_ATTR] = LOAD_ATTR_ADAPTIVE,
[LOAD_GLOBAL] = LOAD_GLOBAL_ADAPTIVE,
[BINARY_SUBSCR] = BINARY_SUBSCR_ADAPTIVE,
};

/* The number of cache entries required for a "family" of instructions. */
static uint8_t cache_requirements[256] = {
[LOAD_ATTR] = 2, /* _PyAdaptiveEntry and _PyLoadAttrCache */
[LOAD_GLOBAL] = 2, /* _PyAdaptiveEntry and _PyLoadGlobalCache */
[BINARY_SUBSCR] = 0,
};

/* Return the oparg for the cache_offset and instruction index.
Expand Down Expand Up @@ -251,7 +254,6 @@ optimize(SpecializedCacheOrInstruction *quickened, int len)
previous_opcode = opcode;
continue;
}
instructions[i] = _Py_MAKECODEUNIT(adaptive_opcode, new_oparg);
previous_opcode = adaptive_opcode;
int entries_needed = cache_requirements[opcode];
if (entries_needed) {
Expand All @@ -261,7 +263,11 @@ optimize(SpecializedCacheOrInstruction *quickened, int len)
_GetSpecializedCacheEntry(instructions, cache0_offset);
cache->adaptive.original_oparg = oparg;
cache->adaptive.counter = 0;
} else {
// oparg is the adaptive cache counter
new_oparg = 0;
}
instructions[i] = _Py_MAKECODEUNIT(adaptive_opcode, new_oparg);
}
else {
/* Super instructions don't use the cache,
Expand Down Expand Up @@ -637,3 +643,43 @@ _Py_Specialize_LoadGlobal(
cache0->counter = saturating_start();
return 0;
}

int
_Py_Specialize_BinarySubscr(
PyObject *container, PyObject *sub, _Py_CODEUNIT *instr)
{
PyTypeObject *container_type = Py_TYPE(container);
if (container_type == &PyList_Type) {
if (PyLong_CheckExact(sub)) {
*instr = _Py_MAKECODEUNIT(BINARY_SUBSCR_LIST_INT, saturating_start());
goto success;
} else {
SPECIALIZATION_FAIL(BINARY_SUBSCR, Py_TYPE(container), sub, "list; non-integer subscr");
}
}
if (container_type == &PyTuple_Type) {
if (PyLong_CheckExact(sub)) {
*instr = _Py_MAKECODEUNIT(BINARY_SUBSCR_TUPLE_INT, saturating_start());
goto success;
} else {
SPECIALIZATION_FAIL(BINARY_SUBSCR, Py_TYPE(container), sub, "tuple; non-integer subscr");
}
}
if (container_type == &PyDict_Type) {
*instr = _Py_MAKECODEUNIT(BINARY_SUBSCR_DICT, saturating_start());
goto success;
}

SPECIALIZATION_FAIL(BINARY_SUBSCR, Py_TYPE(container), sub, "not list|tuple|dict");
goto fail;
fail:
STAT_INC(BINARY_SUBSCR, specialization_failure);
assert(!PyErr_Occurred());
*instr = _Py_MAKECODEUNIT(_Py_OPCODE(*instr), ADAPTIVE_CACHE_BACKOFF);
return 0;
success:
STAT_INC(BINARY_SUBSCR, specialization_success);
assert(!PyErr_Occurred());
return 0;
}