From d7a5aca982def155a9255893cefcc1493c127c9c Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Thu, 10 Feb 2022 09:50:02 -0800 Subject: [PATCH] bpo-45923: Add `RESUME_QUICK` (GH-31244) --- Include/opcode.h | 19 ++++++++++--------- Lib/opcode.py | 1 + .../2022-02-09-20-21-43.bpo-45923.tJ4gDX.rst | 1 + Python/ceval.c | 16 +++++++++------- Python/opcode_targets.h | 12 ++++++------ Python/specialize.c | 3 +++ 6 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-09-20-21-43.bpo-45923.tJ4gDX.rst diff --git a/Include/opcode.h b/Include/opcode.h index bce7010ab18..58fc6280893 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -164,15 +164,16 @@ extern "C" { #define LOAD_METHOD_CLASS 76 #define LOAD_METHOD_MODULE 77 #define LOAD_METHOD_NO_DICT 78 -#define STORE_ATTR_ADAPTIVE 79 -#define STORE_ATTR_INSTANCE_VALUE 80 -#define STORE_ATTR_SLOT 81 -#define STORE_ATTR_WITH_HINT 131 -#define LOAD_FAST__LOAD_FAST 140 -#define STORE_FAST__LOAD_FAST 141 -#define LOAD_FAST__LOAD_CONST 143 -#define LOAD_CONST__LOAD_FAST 150 -#define STORE_FAST__STORE_FAST 153 +#define RESUME_QUICK 79 +#define STORE_ATTR_ADAPTIVE 80 +#define STORE_ATTR_INSTANCE_VALUE 81 +#define STORE_ATTR_SLOT 131 +#define STORE_ATTR_WITH_HINT 140 +#define LOAD_FAST__LOAD_FAST 141 +#define STORE_FAST__LOAD_FAST 143 +#define LOAD_FAST__LOAD_CONST 150 +#define LOAD_CONST__LOAD_FAST 153 +#define STORE_FAST__STORE_FAST 154 #define DO_TRACING 255 #ifdef NEED_OPCODE_JUMP_TABLES static uint32_t _PyOpcode_RelativeJump[8] = { diff --git a/Lib/opcode.py b/Lib/opcode.py index c672aa59f8e..a1f0c6e4326 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -278,6 +278,7 @@ _specialized_instructions = [ "LOAD_METHOD_CLASS", "LOAD_METHOD_MODULE", "LOAD_METHOD_NO_DICT", + "RESUME_QUICK", "STORE_ATTR_ADAPTIVE", "STORE_ATTR_INSTANCE_VALUE", "STORE_ATTR_SLOT", diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-09-20-21-43.bpo-45923.tJ4gDX.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-09-20-21-43.bpo-45923.tJ4gDX.rst new file mode 100644 index 00000000000..5ab5d59e50f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-09-20-21-43.bpo-45923.tJ4gDX.rst @@ -0,0 +1 @@ +Add a quickened form of :opcode:`RESUME` that skips quickening checks. diff --git a/Python/ceval.c b/Python/ceval.c index ffce6b735c1..5eb91502c30 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1734,9 +1734,6 @@ handle_eval_breaker: } TARGET(RESUME) { - assert(tstate->cframe == &cframe); - assert(frame == cframe.current_frame); - int err = _Py_IncrementCountAndMaybeQuicken(frame->f_code); if (err) { if (err < 0) { @@ -1747,6 +1744,13 @@ handle_eval_breaker: first_instr = frame->f_code->co_firstinstr; next_instr = first_instr + nexti; } + JUMP_TO_INSTRUCTION(RESUME_QUICK); + } + + TARGET(RESUME_QUICK) { + PREDICTED(RESUME_QUICK); + assert(tstate->cframe == &cframe); + assert(frame == cframe.current_frame); frame->f_state = FRAME_EXECUTING; if (_Py_atomic_load_relaxed(eval_breaker) && oparg < 2) { goto handle_eval_breaker; @@ -4004,7 +4008,6 @@ handle_eval_breaker: TARGET(JUMP_ABSOLUTE) { PREDICTED(JUMP_ABSOLUTE); - assert(oparg < INSTR_OFFSET()); int err = _Py_IncrementCountAndMaybeQuicken(frame->f_code); if (err) { if (err < 0) { @@ -4015,9 +4018,7 @@ handle_eval_breaker: first_instr = frame->f_code->co_firstinstr; next_instr = first_instr + nexti; } - JUMPTO(oparg); - CHECK_EVAL_BREAKER(); - DISPATCH(); + JUMP_TO_INSTRUCTION(JUMP_ABSOLUTE_QUICK); } TARGET(JUMP_NO_INTERRUPT) { @@ -4032,6 +4033,7 @@ handle_eval_breaker: } TARGET(JUMP_ABSOLUTE_QUICK) { + PREDICTED(JUMP_ABSOLUTE_QUICK); assert(oparg < INSTR_OFFSET()); JUMPTO(oparg); CHECK_EVAL_BREAKER(); diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 1a809ed409d..f47da2bbb1e 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -78,9 +78,9 @@ static void *opcode_targets[256] = { &&TARGET_LOAD_METHOD_CLASS, &&TARGET_LOAD_METHOD_MODULE, &&TARGET_LOAD_METHOD_NO_DICT, + &&TARGET_RESUME_QUICK, &&TARGET_STORE_ATTR_ADAPTIVE, &&TARGET_STORE_ATTR_INSTANCE_VALUE, - &&TARGET_STORE_ATTR_SLOT, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, &&TARGET_IMPORT_STAR, @@ -130,7 +130,7 @@ static void *opcode_targets[256] = { &&TARGET_POP_JUMP_IF_NOT_NONE, &&TARGET_POP_JUMP_IF_NONE, &&TARGET_RAISE_VARARGS, - &&TARGET_STORE_ATTR_WITH_HINT, + &&TARGET_STORE_ATTR_SLOT, &&TARGET_MAKE_FUNCTION, &&TARGET_BUILD_SLICE, &&TARGET_JUMP_NO_INTERRUPT, @@ -139,21 +139,21 @@ static void *opcode_targets[256] = { &&TARGET_LOAD_DEREF, &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, + &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_LOAD_FAST__LOAD_FAST, - &&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_CALL_FUNCTION_EX, - &&TARGET_LOAD_FAST__LOAD_CONST, + &&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, &&TARGET_MAP_ADD, &&TARGET_LOAD_CLASSDEREF, &&TARGET_COPY_FREE_VARS, - &&TARGET_LOAD_CONST__LOAD_FAST, + &&TARGET_LOAD_FAST__LOAD_CONST, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, + &&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_STORE_FAST__STORE_FAST, - &&_unknown_opcode, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, diff --git a/Python/specialize.c b/Python/specialize.c index b051f45157a..e610a2d85fe 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -407,6 +407,9 @@ optimize(SpecializedCacheOrInstruction *quickened, int len) case JUMP_ABSOLUTE: instructions[i] = _Py_MAKECODEUNIT(JUMP_ABSOLUTE_QUICK, oparg); break; + case RESUME: + instructions[i] = _Py_MAKECODEUNIT(RESUME_QUICK, oparg); + break; case LOAD_FAST: switch(previous_opcode) { case LOAD_FAST: