gh-92031: Deoptimize Static Code at Finalization (GH-92039)

This commit is contained in:
Dennis Sweeney 2022-05-03 10:59:12 -04:00 committed by GitHub
parent 04dc4b06a3
commit b156578bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 225 additions and 10 deletions

View File

@ -16,6 +16,8 @@ extern const uint8_t _PyOpcode_Caches[256];
extern const uint8_t _PyOpcode_Deopt[256];
extern const uint8_t _PyOpcode_Original[256];
#ifdef NEED_OPCODE_TABLES
static const uint32_t _PyOpcode_RelativeJump[8] = {
0U,
@ -235,6 +237,190 @@ const uint8_t _PyOpcode_Deopt[256] = {
[WITH_EXCEPT_START] = WITH_EXCEPT_START,
[YIELD_VALUE] = YIELD_VALUE,
};
const uint8_t _PyOpcode_Original[256] = {
[ASYNC_GEN_WRAP] = ASYNC_GEN_WRAP,
[BEFORE_ASYNC_WITH] = BEFORE_ASYNC_WITH,
[BEFORE_WITH] = BEFORE_WITH,
[BINARY_OP] = BINARY_OP,
[BINARY_OP_ADAPTIVE] = BINARY_OP,
[BINARY_OP_ADD_FLOAT] = BINARY_OP,
[BINARY_OP_ADD_INT] = BINARY_OP,
[BINARY_OP_ADD_UNICODE] = BINARY_OP,
[BINARY_OP_INPLACE_ADD_UNICODE] = BINARY_OP,
[BINARY_OP_MULTIPLY_FLOAT] = BINARY_OP,
[BINARY_OP_MULTIPLY_INT] = BINARY_OP,
[BINARY_OP_SUBTRACT_FLOAT] = BINARY_OP,
[BINARY_OP_SUBTRACT_INT] = BINARY_OP,
[BINARY_SUBSCR] = BINARY_SUBSCR,
[BINARY_SUBSCR_ADAPTIVE] = BINARY_SUBSCR,
[BINARY_SUBSCR_DICT] = BINARY_SUBSCR,
[BINARY_SUBSCR_GETITEM] = BINARY_SUBSCR,
[BINARY_SUBSCR_LIST_INT] = BINARY_SUBSCR,
[BINARY_SUBSCR_TUPLE_INT] = BINARY_SUBSCR,
[BUILD_CONST_KEY_MAP] = BUILD_CONST_KEY_MAP,
[BUILD_LIST] = BUILD_LIST,
[BUILD_MAP] = BUILD_MAP,
[BUILD_SET] = BUILD_SET,
[BUILD_SLICE] = BUILD_SLICE,
[BUILD_STRING] = BUILD_STRING,
[BUILD_TUPLE] = BUILD_TUPLE,
[CACHE] = CACHE,
[CALL] = CALL,
[CALL_ADAPTIVE] = CALL,
[CALL_FUNCTION_EX] = CALL_FUNCTION_EX,
[CALL_PY_EXACT_ARGS] = CALL,
[CALL_PY_WITH_DEFAULTS] = CALL,
[CHECK_EG_MATCH] = CHECK_EG_MATCH,
[CHECK_EXC_MATCH] = CHECK_EXC_MATCH,
[COMPARE_OP] = COMPARE_OP,
[COMPARE_OP_ADAPTIVE] = COMPARE_OP,
[COMPARE_OP_FLOAT_JUMP] = COMPARE_OP,
[COMPARE_OP_INT_JUMP] = COMPARE_OP,
[COMPARE_OP_STR_JUMP] = COMPARE_OP,
[CONTAINS_OP] = CONTAINS_OP,
[COPY] = COPY,
[COPY_FREE_VARS] = COPY_FREE_VARS,
[DELETE_ATTR] = DELETE_ATTR,
[DELETE_DEREF] = DELETE_DEREF,
[DELETE_FAST] = DELETE_FAST,
[DELETE_GLOBAL] = DELETE_GLOBAL,
[DELETE_NAME] = DELETE_NAME,
[DELETE_SUBSCR] = DELETE_SUBSCR,
[DICT_MERGE] = DICT_MERGE,
[DICT_UPDATE] = DICT_UPDATE,
[END_ASYNC_FOR] = END_ASYNC_FOR,
[EXTENDED_ARG] = EXTENDED_ARG_QUICK,
[EXTENDED_ARG_QUICK] = EXTENDED_ARG_QUICK,
[FORMAT_VALUE] = FORMAT_VALUE,
[FOR_ITER] = FOR_ITER,
[GET_AITER] = GET_AITER,
[GET_ANEXT] = GET_ANEXT,
[GET_AWAITABLE] = GET_AWAITABLE,
[GET_ITER] = GET_ITER,
[GET_LEN] = GET_LEN,
[GET_YIELD_FROM_ITER] = GET_YIELD_FROM_ITER,
[IMPORT_FROM] = IMPORT_FROM,
[IMPORT_NAME] = IMPORT_NAME,
[IMPORT_STAR] = IMPORT_STAR,
[IS_OP] = IS_OP,
[JUMP_BACKWARD] = JUMP_BACKWARD,
[JUMP_BACKWARD_NO_INTERRUPT] = JUMP_BACKWARD_NO_INTERRUPT,
[JUMP_BACKWARD_QUICK] = JUMP_BACKWARD,
[JUMP_FORWARD] = JUMP_FORWARD,
[JUMP_IF_FALSE_OR_POP] = JUMP_IF_FALSE_OR_POP,
[JUMP_IF_TRUE_OR_POP] = JUMP_IF_TRUE_OR_POP,
[KW_NAMES] = KW_NAMES,
[LIST_APPEND] = LIST_APPEND,
[LIST_EXTEND] = LIST_EXTEND,
[LIST_TO_TUPLE] = LIST_TO_TUPLE,
[LOAD_ASSERTION_ERROR] = LOAD_ASSERTION_ERROR,
[LOAD_ATTR] = LOAD_ATTR,
[LOAD_ATTR_ADAPTIVE] = LOAD_ATTR,
[LOAD_ATTR_INSTANCE_VALUE] = LOAD_ATTR,
[LOAD_ATTR_MODULE] = LOAD_ATTR,
[LOAD_ATTR_SLOT] = LOAD_ATTR,
[LOAD_ATTR_WITH_HINT] = LOAD_ATTR,
[LOAD_BUILD_CLASS] = LOAD_BUILD_CLASS,
[LOAD_CLASSDEREF] = LOAD_CLASSDEREF,
[LOAD_CLOSURE] = LOAD_CLOSURE,
[LOAD_CONST] = LOAD_CONST,
[LOAD_CONST__LOAD_FAST] = LOAD_CONST,
[LOAD_DEREF] = LOAD_DEREF,
[LOAD_FAST] = LOAD_FAST,
[LOAD_FAST__LOAD_CONST] = LOAD_FAST,
[LOAD_FAST__LOAD_FAST] = LOAD_FAST,
[LOAD_GLOBAL] = LOAD_GLOBAL,
[LOAD_GLOBAL_ADAPTIVE] = LOAD_GLOBAL,
[LOAD_GLOBAL_BUILTIN] = LOAD_GLOBAL,
[LOAD_GLOBAL_MODULE] = LOAD_GLOBAL,
[LOAD_METHOD] = LOAD_METHOD,
[LOAD_METHOD_ADAPTIVE] = LOAD_METHOD,
[LOAD_METHOD_CLASS] = LOAD_METHOD,
[LOAD_METHOD_MODULE] = LOAD_METHOD,
[LOAD_METHOD_NO_DICT] = LOAD_METHOD,
[LOAD_METHOD_WITH_DICT] = LOAD_METHOD,
[LOAD_METHOD_WITH_VALUES] = LOAD_METHOD,
[LOAD_NAME] = LOAD_NAME,
[MAKE_CELL] = MAKE_CELL,
[MAKE_FUNCTION] = MAKE_FUNCTION,
[MAP_ADD] = MAP_ADD,
[MATCH_CLASS] = MATCH_CLASS,
[MATCH_KEYS] = MATCH_KEYS,
[MATCH_MAPPING] = MATCH_MAPPING,
[MATCH_SEQUENCE] = MATCH_SEQUENCE,
[NOP] = NOP,
[POP_EXCEPT] = POP_EXCEPT,
[POP_JUMP_BACKWARD_IF_FALSE] = POP_JUMP_BACKWARD_IF_FALSE,
[POP_JUMP_BACKWARD_IF_NONE] = POP_JUMP_BACKWARD_IF_NONE,
[POP_JUMP_BACKWARD_IF_NOT_NONE] = POP_JUMP_BACKWARD_IF_NOT_NONE,
[POP_JUMP_BACKWARD_IF_TRUE] = POP_JUMP_BACKWARD_IF_TRUE,
[POP_JUMP_FORWARD_IF_FALSE] = POP_JUMP_FORWARD_IF_FALSE,
[POP_JUMP_FORWARD_IF_NONE] = POP_JUMP_FORWARD_IF_NONE,
[POP_JUMP_FORWARD_IF_NOT_NONE] = POP_JUMP_FORWARD_IF_NOT_NONE,
[POP_JUMP_FORWARD_IF_TRUE] = POP_JUMP_FORWARD_IF_TRUE,
[POP_TOP] = POP_TOP,
[PRECALL] = PRECALL,
[PRECALL_ADAPTIVE] = PRECALL,
[PRECALL_BOUND_METHOD] = PRECALL,
[PRECALL_BUILTIN_CLASS] = PRECALL,
[PRECALL_BUILTIN_FAST_WITH_KEYWORDS] = PRECALL,
[PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = PRECALL,
[PRECALL_NO_KW_BUILTIN_FAST] = PRECALL,
[PRECALL_NO_KW_BUILTIN_O] = PRECALL,
[PRECALL_NO_KW_ISINSTANCE] = PRECALL,
[PRECALL_NO_KW_LEN] = PRECALL,
[PRECALL_NO_KW_LIST_APPEND] = PRECALL,
[PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST] = PRECALL,
[PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS] = PRECALL,
[PRECALL_NO_KW_METHOD_DESCRIPTOR_O] = PRECALL,
[PRECALL_NO_KW_STR_1] = PRECALL,
[PRECALL_NO_KW_TUPLE_1] = PRECALL,
[PRECALL_NO_KW_TYPE_1] = PRECALL,
[PRECALL_PYFUNC] = PRECALL,
[PREP_RERAISE_STAR] = PREP_RERAISE_STAR,
[PRINT_EXPR] = PRINT_EXPR,
[PUSH_EXC_INFO] = PUSH_EXC_INFO,
[PUSH_NULL] = PUSH_NULL,
[RAISE_VARARGS] = RAISE_VARARGS,
[RERAISE] = RERAISE,
[RESUME] = RESUME,
[RESUME_QUICK] = RESUME,
[RETURN_GENERATOR] = RETURN_GENERATOR,
[RETURN_VALUE] = RETURN_VALUE,
[SEND] = SEND,
[SETUP_ANNOTATIONS] = SETUP_ANNOTATIONS,
[SET_ADD] = SET_ADD,
[SET_UPDATE] = SET_UPDATE,
[STORE_ATTR] = STORE_ATTR,
[STORE_ATTR_ADAPTIVE] = STORE_ATTR,
[STORE_ATTR_INSTANCE_VALUE] = STORE_ATTR,
[STORE_ATTR_SLOT] = STORE_ATTR,
[STORE_ATTR_WITH_HINT] = STORE_ATTR,
[STORE_DEREF] = STORE_DEREF,
[STORE_FAST] = STORE_FAST,
[STORE_FAST__LOAD_FAST] = STORE_FAST,
[STORE_FAST__STORE_FAST] = STORE_FAST,
[STORE_GLOBAL] = STORE_GLOBAL,
[STORE_NAME] = STORE_NAME,
[STORE_SUBSCR] = STORE_SUBSCR,
[STORE_SUBSCR_ADAPTIVE] = STORE_SUBSCR,
[STORE_SUBSCR_DICT] = STORE_SUBSCR,
[STORE_SUBSCR_LIST_INT] = STORE_SUBSCR,
[SWAP] = SWAP,
[UNARY_INVERT] = UNARY_INVERT,
[UNARY_NEGATIVE] = UNARY_NEGATIVE,
[UNARY_NOT] = UNARY_NOT,
[UNARY_POSITIVE] = UNARY_POSITIVE,
[UNPACK_EX] = UNPACK_EX,
[UNPACK_SEQUENCE] = UNPACK_SEQUENCE,
[UNPACK_SEQUENCE_ADAPTIVE] = UNPACK_SEQUENCE,
[UNPACK_SEQUENCE_LIST] = UNPACK_SEQUENCE,
[UNPACK_SEQUENCE_TUPLE] = UNPACK_SEQUENCE,
[UNPACK_SEQUENCE_TWO_TUPLE] = UNPACK_SEQUENCE,
[WITH_EXCEPT_START] = WITH_EXCEPT_START,
[YIELD_VALUE] = YIELD_VALUE,
};
#endif // NEED_OPCODE_TABLES
#ifdef Py_DEBUG

View File

@ -343,6 +343,20 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)
@support.skip_if_pgo_task
def test_quickened_static_code_gets_unquickened_at_Py_FINALIZE(self):
# https://github.com/python/cpython/issues/92031
code = """if 1:
from importlib._bootstrap import _handle_fromlist
import dis
for name in dis.opmap:
# quicken this frozen code object.
_handle_fromlist(dis, [name], lambda *args: None)
"""
run = self.run_embedded_interpreter
for i in range(50):
out, err = run("test_repeated_init_exec", code, timeout=60)
def test_ucnhash_capi_reset(self):
# bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown.
code = "print('\\N{digit nine}')"

View File

@ -0,0 +1 @@
Deoptimize statically-allocated code objects during ``Py_FINALIZE()`` so that future ``_PyCode_Quicken`` calls always start with unquickened code.

View File

@ -1350,23 +1350,29 @@ _PyCode_GetFreevars(PyCodeObject *co)
return get_localsplus_names(co, CO_FAST_FREE, co->co_nfreevars);
}
PyObject *
_PyCode_GetCode(PyCodeObject *co)
static void
deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len)
{
PyObject *code = PyBytes_FromStringAndSize(NULL, _PyCode_NBYTES(co));
if (code == NULL) {
return NULL;
}
_Py_CODEUNIT *instructions = (_Py_CODEUNIT *)PyBytes_AS_STRING(code);
for (int i = 0; i < Py_SIZE(co); i++) {
_Py_CODEUNIT instruction = _PyCode_CODE(co)[i];
int opcode = _PyOpcode_Deopt[_Py_OPCODE(instruction)];
for (int i = 0; i < len; i++) {
_Py_CODEUNIT instruction = instructions[i];
int opcode = _PyOpcode_Original[_Py_OPCODE(instruction)];
int caches = _PyOpcode_Caches[opcode];
instructions[i] = _Py_MAKECODEUNIT(opcode, _Py_OPARG(instruction));
while (caches--) {
instructions[++i] = _Py_MAKECODEUNIT(CACHE, 0);
}
}
}
PyObject *
_PyCode_GetCode(PyCodeObject *co)
{
PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
_PyCode_NBYTES(co));
if (code == NULL) {
return NULL;
}
deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co));
return code;
}
@ -2076,6 +2082,7 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
if (co->co_warmup == 0) {
_Py_QuickenedCount--;
}
deopt_code(_PyCode_CODE(co), Py_SIZE(co));
co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
PyMem_Free(co->co_extra);
co->co_extra = NULL;

View File

@ -117,6 +117,7 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna
iobj.write("\nextern const uint8_t _PyOpcode_Caches[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Deopt[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Original[256];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
write_int_array_from_ops("_PyOpcode_RelativeJump", opcode['hasjrel'], iobj)
write_int_array_from_ops("_PyOpcode_Jump", opcode['hasjrel'] + opcode['hasjabs'], iobj)
@ -137,6 +138,12 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna
for opt, deopt in sorted(deoptcodes.items()):
iobj.write(f" [{opt}] = {deopt},\n")
iobj.write("};\n")
iobj.write("\nconst uint8_t _PyOpcode_Original[256] = {\n")
for opt, deopt in sorted(deoptcodes.items()):
if opt.startswith("EXTENDED_ARG"):
deopt = "EXTENDED_ARG_QUICK"
iobj.write(f" [{opt}] = {deopt},\n")
iobj.write("};\n")
iobj.write("#endif // NEED_OPCODE_TABLES\n")
fobj.write("\n")