Skip to content

Commit

Permalink
Make deopts significantly more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner committed Jan 20, 2025
1 parent 297f5b9 commit e4f2147
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 470 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_predictions(self):
INSTRUCTION_STATS(OP3);
static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
_PyStackRef res;
DEOPT_IF(xxx, OP1);
DEOPT_IF(xxx, OP1, INLINE_CACHE_ENTRIES_OP1);
res = Py_None;
stack_pointer[-1] = res;
DISPATCH();
Expand Down
8 changes: 4 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include "ceval_macros.h"

/* Flow control macros */
#define GO_TO_INSTRUCTION(instname) ((void)0)
#define GO_TO_INSTRUCTION(instname, size) ((void)0)

#define inst(name, ...) case name:
#define op(name, ...) /* NAME is ignored */
Expand Down Expand Up @@ -2014,7 +2014,7 @@ dummy_func(
// cancel out the decrement that will happen in LOAD_SUPER_ATTR; we
// don't want to specialize instrumented instructions
PAUSE_ADAPTIVE_COUNTER(this_instr[1].counter);
GO_TO_INSTRUCTION(LOAD_SUPER_ATTR);
GO_TO_INSTRUCTION(LOAD_SUPER_ATTR, INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR);
}

family(LOAD_SUPER_ATTR, INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR) = {
Expand Down Expand Up @@ -4313,7 +4313,7 @@ dummy_func(
frame, this_instr, function, arg);
ERROR_IF(err, error);
PAUSE_ADAPTIVE_COUNTER(this_instr[1].counter);
GO_TO_INSTRUCTION(CALL_KW);
GO_TO_INSTRUCTION(CALL_KW, INLINE_CACHE_ENTRIES_CALL_KW);
}

op(_MAYBE_EXPAND_METHOD_KW, (callable[1], self_or_null[1], args[oparg], kwnames_in -- func[1], maybe_self[1], args[oparg], kwnames_out)) {
Expand Down Expand Up @@ -4539,7 +4539,7 @@ dummy_func(
_CHECK_PERIODIC;

inst(INSTRUMENTED_CALL_FUNCTION_EX, ( -- )) {
GO_TO_INSTRUCTION(CALL_FUNCTION_EX);
GO_TO_INSTRUCTION(CALL_FUNCTION_EX, 0);
}

op(_MAKE_CALLARGS_A_TUPLE, (func, unused, callargs, kwargs_in if (oparg & 1) -- func, unused, tuple, kwargs_out if (oparg & 1))) {
Expand Down
14 changes: 7 additions & 7 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,18 @@ GETITEM(PyObject *v, Py_ssize_t i) {
PyStackRef_XCLOSE(tmp); } while (0)
#ifdef Py_TAIL_CALL_INTERP
#ifdef LLTRACE
#define GO_TO_INSTRUCTION(op) do { \
#define GO_TO_INSTRUCTION(op, size) do { \
Py_MUSTTAIL \
return (INSTRUCTION_TABLE[op])(frame, stack_pointer, tstate, next_instr - 1 - _PyOpcode_Caches[_PyOpcode_Deopt[op]], opcode, oparg, lltrace); \
return (INSTRUCTION_TABLE[op])(frame, stack_pointer, tstate, next_instr - 1 - size, opcode, oparg, lltrace); \
} while (0)
#else
#define GO_TO_INSTRUCTION(op) do { \
#define GO_TO_INSTRUCTION(op, size) do { \
Py_MUSTTAIL \
return (INSTRUCTION_TABLE[op])(frame, stack_pointer, tstate, next_instr - 1 - _PyOpcode_Caches[_PyOpcode_Deopt[op]], opcode, oparg); \
return (INSTRUCTION_TABLE[op])(frame, stack_pointer, tstate, next_instr - 1 - size, opcode, oparg); \
} while (0)
#endif
#else
#define GO_TO_INSTRUCTION(op) goto PREDICT_ID(op)
#define GO_TO_INSTRUCTION(op, size) goto PREDICT_ID(op)
#endif

#ifdef Py_STATS
Expand All @@ -354,12 +354,12 @@ GETITEM(PyObject *v, Py_ssize_t i) {
#define UPDATE_MISS_STATS(INSTNAME) ((void)0)
#endif

#define DEOPT_IF(COND, INSTNAME) \
#define DEOPT_IF(COND, INSTNAME, SIZE) \
if ((COND)) { \
/* This is only a single jump on release builds! */ \
UPDATE_MISS_STATS((INSTNAME)); \
assert(_PyOpcode_Deopt[opcode] == (INSTNAME)); \
GO_TO_INSTRUCTION(INSTNAME); \
GO_TO_INSTRUCTION(INSTNAME, SIZE); \
}


Expand Down
Loading

0 comments on commit e4f2147

Please sign in to comment.