Skip to content
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

bpo-44900: Add five superinstructions. #27741

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 5 additions & 0 deletions Include/opcode.h

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

7 changes: 6 additions & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,13 @@ def jabs_op(name, op):
"STORE_ATTR_SPLIT_KEYS",
"STORE_ATTR_SLOT",
"STORE_ATTR_WITH_HINT",
# Super instructions
"LOAD_FAST__LOAD_FAST",
"STORE_FAST__LOAD_FAST",
"LOAD_FAST__LOAD_CONST",
"LOAD_CONST__LOAD_FAST",
"STORE_FAST__STORE_FAST",
]

_specialization_stats = [
"specialization_success",
"specialization_failure",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Add five superinstructions for PEP 659 quickening:

* LOAD_FAST LOAD_FAST
* STORE_FAST LOAD_FAST
* LOAD_FAST LOAD_CONST
* LOAD_CONST LOAD_FAST
* STORE_FAST STORE_FAST
95 changes: 86 additions & 9 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,16 +1266,21 @@ eval_frame_handle_pending(PyThreadState *tstate)
#define PRE_DISPATCH_GOTO() do { LLTRACE_INSTR(); RECORD_DXPROFILE(); } while (0)
#endif

#define NOTRACE_DISPATCH() \
{ \
frame->f_lasti = INSTR_OFFSET(); \
NEXTOPARG(); \
PRE_DISPATCH_GOTO(); \
DISPATCH_GOTO(); \
}

/* Do interpreter dispatch accounting for tracing and instrumentation */
#define DISPATCH() \
{ \
if (cframe.use_tracing OR_DTRACE_LINE) { \
goto tracing_dispatch; \
} \
frame->f_lasti = INSTR_OFFSET(); \
NEXTOPARG(); \
PRE_DISPATCH_GOTO(); \
DISPATCH_GOTO(); \
NOTRACE_DISPATCH(); \
}

#define CHECK_EVAL_BREAKER() \
Expand Down Expand Up @@ -1682,11 +1687,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
TARGET(LOAD_FAST): {
PyObject *value = GETLOCAL(oparg);
if (value == NULL) {
format_exc_check_arg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(co->co_localsplusnames,
oparg));
goto error;
goto unbound_local_error;
}
Py_INCREF(value);
PUSH(value);
Expand All @@ -1708,6 +1709,73 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
DISPATCH();
}

TARGET(LOAD_FAST__LOAD_FAST): {
PyObject *value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
NEXTOPARG();
Py_INCREF(value);
PUSH(value);
value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
Py_INCREF(value);
PUSH(value);
NOTRACE_DISPATCH();
}

TARGET(LOAD_FAST__LOAD_CONST): {
PyObject *value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
NEXTOPARG();
Py_INCREF(value);
PUSH(value);
value = GETITEM(consts, oparg);
Py_INCREF(value);
PUSH(value);
NOTRACE_DISPATCH();
}

TARGET(STORE_FAST__LOAD_FAST): {
PyObject *value = POP();
SETLOCAL(oparg, value);
NEXTOPARG();
value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
Py_INCREF(value);
PUSH(value);
NOTRACE_DISPATCH();
}

TARGET(STORE_FAST__STORE_FAST): {
PyObject *value = POP();
SETLOCAL(oparg, value);
NEXTOPARG();
value = POP();
SETLOCAL(oparg, value);
NOTRACE_DISPATCH();
}

TARGET(LOAD_CONST__LOAD_FAST): {
PyObject *value = GETITEM(consts, oparg);
NEXTOPARG();
Py_INCREF(value);
PUSH(value);
value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
Py_INCREF(value);
PUSH(value);
NOTRACE_DISPATCH();
}

TARGET(POP_TOP): {
PyObject *value = POP();
Py_DECREF(value);
Expand Down Expand Up @@ -4592,6 +4660,15 @@ MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)
goto error;
}

unbound_local_error:
{
format_exc_check_arg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(co->co_localsplusnames, oparg)
);
goto error;
}

error:
/* Double-check exception status. */
#ifdef NDEBUG
Expand Down
10 changes: 5 additions & 5 deletions Python/opcode_targets.h

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

29 changes: 24 additions & 5 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ optimize(SpecializedCacheOrInstruction *quickened, int len)
_Py_CODEUNIT *instructions = first_instruction(quickened);
int cache_offset = 0;
int previous_opcode = -1;
int previous_oparg = 0;
for(int i = 0; i < len; i++) {
int opcode = _Py_OPCODE(instructions[i]);
int oparg = _Py_OPARG(instructions[i]);
Expand Down Expand Up @@ -338,14 +339,32 @@ optimize(SpecializedCacheOrInstruction *quickened, int len)
case JUMP_ABSOLUTE:
instructions[i] = _Py_MAKECODEUNIT(JUMP_ABSOLUTE_QUICK, oparg);
break;
/* Insert superinstructions here
E.g.
case LOAD_FAST:
if (previous_opcode == LOAD_FAST)
instructions[i-1] = _Py_MAKECODEUNIT(LOAD_FAST__LOAD_FAST, oparg);
*/
switch(previous_opcode) {
case LOAD_FAST:
instructions[i-1] = _Py_MAKECODEUNIT(LOAD_FAST__LOAD_FAST, previous_oparg);
break;
case STORE_FAST:
instructions[i-1] = _Py_MAKECODEUNIT(STORE_FAST__LOAD_FAST, previous_oparg);
break;
case LOAD_CONST:
instructions[i-1] = _Py_MAKECODEUNIT(LOAD_CONST__LOAD_FAST, previous_oparg);
break;
}
break;
case STORE_FAST:
if (previous_opcode == STORE_FAST) {
instructions[i-1] = _Py_MAKECODEUNIT(STORE_FAST__STORE_FAST, previous_oparg);
}
break;
case LOAD_CONST:
if (previous_opcode == LOAD_FAST) {
instructions[i-1] = _Py_MAKECODEUNIT(LOAD_FAST__LOAD_CONST, previous_oparg);
}
break;
}
previous_opcode = opcode;
previous_oparg = oparg;
}
}
assert(cache_offset+1 == get_cache_count(quickened));
Expand Down