GH-138378: Move globals-to-consts pass into main optimizer pass (GH-138379)

This commit is contained in:
Mark Shannon 2025-09-18 10:09:59 +01:00 committed by GitHub
parent d22b25081b
commit 3b83257366
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 486 additions and 490 deletions

View file

@ -5345,12 +5345,6 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(ptr);
}
tier2 op(_CHECK_FUNCTION, (func_version/2 -- )) {
assert(PyStackRef_FunctionCheck(frame->f_funcobj));
PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
DEOPT_IF(func->func_version != func_version);
}
tier2 op(_START_EXECUTOR, (executor/4 --)) {
#ifndef _Py_JIT
assert(current_executor == (_PyExecutorObject*)executor);

View file

@ -7386,17 +7386,6 @@
break;
}
case _CHECK_FUNCTION: {
uint32_t func_version = (uint32_t)CURRENT_OPERAND0();
assert(PyStackRef_FunctionCheck(frame->f_funcobj));
PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
if (func->func_version != func_version) {
UOP_STAT_INC(uopcode, miss);
JUMP_TO_JUMP_TARGET();
}
break;
}
case _START_EXECUTOR: {
PyObject *executor = (PyObject *)CURRENT_OPERAND0();
#ifndef _Py_JIT

View file

@ -893,7 +893,7 @@ translate_bytecode_to_trace(
_Py_BloomFilter_Add(dependencies, new_code);
/* Set the operand to the callee's function or code object,
* to assist optimization passes.
* We prefer setting it to the function (for remove_globals())
* We prefer setting it to the function
* but if that's not available but the code is available,
* use the code, setting the low bit so the optimizer knows.
*/

View file

@ -128,184 +128,16 @@ convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool pop)
return res;
}
static int
incorrect_keys(_PyUOpInstruction *inst, PyObject *obj)
static bool
incorrect_keys(PyObject *obj, uint32_t version)
{
if (!PyDict_CheckExact(obj)) {
return 1;
return true;
}
PyDictObject *dict = (PyDictObject *)obj;
if (dict->ma_keys->dk_version != inst->operand0) {
return 1;
}
return 0;
return dict->ma_keys->dk_version != version;
}
/* Returns 1 if successfully optimized
* 0 if the trace is not suitable for optimization (yet)
* -1 if there was an error. */
static int
remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
int buffer_size, _PyBloomFilter *dependencies)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *builtins = frame->f_builtins;
if (builtins != interp->builtins) {
OPT_STAT_INC(remove_globals_builtins_changed);
return 1;
}
PyObject *globals = frame->f_globals;
PyFunctionObject *function = _PyFrame_GetFunction(frame);
assert(PyFunction_Check(function));
assert(function->func_builtins == builtins);
assert(function->func_globals == globals);
uint32_t function_version = _PyFunction_GetVersionForCurrentState(function);
/* In order to treat globals as constants, we need to
* know that the globals dict is the one we expected, and
* that it hasn't changed
* In order to treat builtins as constants, we need to
* know that the builtins dict is the one we expected, and
* that it hasn't changed and that the global dictionary's
* keys have not changed */
/* These values represent stacks of booleans (one bool per bit).
* Pushing a frame shifts left, popping a frame shifts right. */
uint32_t function_checked = 0;
uint32_t builtins_watched = 0;
uint32_t globals_watched = 0;
uint32_t prechecked_function_version = 0;
if (interp->dict_state.watchers[GLOBALS_WATCHER_ID] == NULL) {
interp->dict_state.watchers[GLOBALS_WATCHER_ID] = globals_watcher_callback;
}
if (interp->type_watchers[TYPE_WATCHER_ID] == NULL) {
interp->type_watchers[TYPE_WATCHER_ID] = type_watcher_callback;
}
for (int pc = 0; pc < buffer_size; pc++) {
_PyUOpInstruction *inst = &buffer[pc];
int opcode = inst->opcode;
switch(opcode) {
case _GUARD_GLOBALS_VERSION:
if (incorrect_keys(inst, globals)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
return 0;
}
if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
continue;
}
if ((globals_watched & 1) == 0) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
globals_watched |= 1;
}
if (function_checked & 1) {
buffer[pc].opcode = NOP;
}
else {
buffer[pc].opcode = _CHECK_FUNCTION;
buffer[pc].operand0 = function_version;
function_checked |= 1;
}
break;
case _LOAD_GLOBAL_BUILTINS:
if (incorrect_keys(inst, builtins)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
return 0;
}
if (interp->rare_events.builtin_dict >= _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) {
continue;
}
if ((builtins_watched & 1) == 0) {
PyDict_Watch(BUILTINS_WATCHER_ID, builtins);
builtins_watched |= 1;
}
if (function_checked & globals_watched & 1) {
convert_global_to_const(inst, builtins, false);
}
break;
case _LOAD_GLOBAL_MODULE:
if (incorrect_keys(inst, globals)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
return 0;
}
if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
continue;
}
if ((globals_watched & 1) == 0) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
globals_watched |= 1;
}
if ((function_checked & 1) == 0 && buffer[pc-1].opcode == _NOP) {
buffer[pc-1].opcode = _CHECK_FUNCTION;
buffer[pc-1].operand0 = function_version;
function_checked |= 1;
}
if (function_checked & 1) {
convert_global_to_const(inst, globals, false);
}
break;
case _PUSH_FRAME:
{
builtins_watched <<= 1;
globals_watched <<= 1;
function_checked <<= 1;
uint64_t operand = buffer[pc].operand0;
if (operand == 0 || (operand & 1)) {
// It's either a code object or NULL, so bail
return 1;
}
PyFunctionObject *func = (PyFunctionObject *)operand;
if (func == NULL) {
return 1;
}
assert(PyFunction_Check(func));
function_version = func->func_version;
if (prechecked_function_version == function_version) {
function_checked |= 1;
}
prechecked_function_version = 0;
globals = func->func_globals;
builtins = func->func_builtins;
if (builtins != interp->builtins) {
OPT_STAT_INC(remove_globals_builtins_changed);
return 1;
}
break;
}
case _RETURN_VALUE:
{
builtins_watched >>= 1;
globals_watched >>= 1;
function_checked >>= 1;
uint64_t operand = buffer[pc].operand0;
if (operand == 0 || (operand & 1)) {
// It's either a code object or NULL, so bail
return 1;
}
PyFunctionObject *func = (PyFunctionObject *)operand;
if (func == NULL) {
return 1;
}
assert(PyFunction_Check(func));
function_version = func->func_version;
globals = func->func_globals;
builtins = func->func_builtins;
break;
}
case _CHECK_FUNCTION_EXACT_ARGS:
prechecked_function_version = (uint32_t)buffer[pc].operand0;
break;
default:
if (is_terminator(inst)) {
return 1;
}
break;
}
}
return 0;
}
#define STACK_LEVEL() ((int)(stack_pointer - ctx->frame->stack))
#define STACK_SIZE() ((int)(ctx->frame->stack_len))
@ -317,9 +149,9 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define GETLOCAL(idx) ((ctx->frame->locals[idx]))
#define REPLACE_OP(INST, OP, ARG, OPERAND) \
INST->opcode = OP; \
INST->oparg = ARG; \
INST->operand0 = OPERAND;
(INST)->opcode = OP; \
(INST)->oparg = ARG; \
(INST)->operand0 = OPERAND;
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
#define sym_is_not_null _Py_uop_sym_is_not_null
@ -407,30 +239,6 @@ lookup_attr(JitOptContext *ctx, _PyUOpInstruction *this_instr,
return sym_new_not_null(ctx);
}
/* _PUSH_FRAME/_RETURN_VALUE's operand can be 0, a PyFunctionObject *, or a
* PyCodeObject *. Retrieve the code object if possible.
*/
static PyCodeObject *
get_code(_PyUOpInstruction *op)
{
assert(op->opcode == _PUSH_FRAME || op->opcode == _RETURN_VALUE || op->opcode == _RETURN_GENERATOR);
PyCodeObject *co = NULL;
uint64_t operand = op->operand0;
if (operand == 0) {
return NULL;
}
if (operand & 1) {
co = (PyCodeObject *)(operand & ~1);
}
else {
PyFunctionObject *func = (PyFunctionObject *)operand;
assert(PyFunction_Check(func));
co = (PyCodeObject *)func->func_code;
}
assert(PyCode_Check(co));
return co;
}
static PyCodeObject *
get_code_with_logging(_PyUOpInstruction *op)
{
@ -455,6 +263,19 @@ get_code_with_logging(_PyUOpInstruction *op)
return co;
}
static
PyCodeObject *
get_current_code_object(JitOptContext *ctx)
{
return (PyCodeObject *)ctx->frame->func->func_code;
}
static PyObject *
get_co_name(JitOptContext *ctx, int index)
{
return PyTuple_GET_ITEM(get_current_code_object(ctx)->co_names, index);
}
// TODO (gh-134584) generate most of this table automatically
const uint16_t op_without_decref_inputs[MAX_UOP_ID + 1] = {
[_BINARY_OP_MULTIPLY_FLOAT] = _BINARY_OP_MULTIPLY_FLOAT__NO_DECREF_INPUTS,
@ -465,7 +286,7 @@ const uint16_t op_without_decref_inputs[MAX_UOP_ID + 1] = {
/* >0 (length) for success, 0 for not ready, clears all possible errors. */
static int
optimize_uops(
PyCodeObject *co,
PyFunctionObject *func,
_PyUOpInstruction *trace,
int trace_len,
int curr_stacklen,
@ -482,11 +303,19 @@ optimize_uops(
_PyUOpInstruction *first_valid_check_stack = NULL;
_PyUOpInstruction *corresponding_check_stack = NULL;
// Make sure that watchers are set up
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->dict_state.watchers[GLOBALS_WATCHER_ID] == NULL) {
interp->dict_state.watchers[GLOBALS_WATCHER_ID] = globals_watcher_callback;
interp->type_watchers[TYPE_WATCHER_ID] = type_watcher_callback;
}
_Py_uop_abstractcontext_init(ctx);
_Py_UOpsAbstractFrame *frame = _Py_uop_frame_new(ctx, co, curr_stacklen, NULL, 0);
_Py_UOpsAbstractFrame *frame = _Py_uop_frame_new(ctx, (PyCodeObject *)func->func_code, curr_stacklen, NULL, 0);
if (frame == NULL) {
return 0;
}
frame->func = func;
ctx->curr_frame_depth++;
ctx->frame = frame;
@ -698,13 +527,8 @@ _Py_uop_analyze_and_optimize(
{
OPT_STAT_INC(optimizer_attempts);
int err = remove_globals(frame, buffer, length, dependencies);
if (err <= 0) {
return err;
}
length = optimize_uops(
_PyFrame_GetCode(frame), buffer,
_PyFrame_GetFunction(frame), buffer,
length, curr_stacklen, dependencies);
if (length == 0) {

View file

@ -470,6 +470,7 @@ dummy_func(void) {
}
op(_LOAD_CONST, (-- value)) {
PyCodeObject *co = get_current_code_object(ctx);
PyObject *val = PyTuple_GET_ITEM(co->co_consts, oparg);
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
value = PyJitRef_Borrow(sym_new_const(ctx, val));
@ -606,7 +607,7 @@ dummy_func(void) {
op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr)) {
(void)descr;
PyTypeObject *type = (PyTypeObject *)sym_get_const(ctx, owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -615,7 +616,7 @@ dummy_func(void) {
op(_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES, (descr/4, owner -- attr)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -624,7 +625,7 @@ dummy_func(void) {
op(_LOAD_ATTR_NONDESCRIPTOR_NO_DICT, (descr/4, owner -- attr)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -633,7 +634,7 @@ dummy_func(void) {
op(_LOAD_ATTR_METHOD_WITH_VALUES, (descr/4, owner -- attr, self)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -643,7 +644,7 @@ dummy_func(void) {
op(_LOAD_ATTR_METHOD_NO_DICT, (descr/4, owner -- attr, self)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -653,7 +654,7 @@ dummy_func(void) {
op(_LOAD_ATTR_METHOD_LAZY_DICT, (descr/4, owner -- attr, self)) {
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -711,15 +712,13 @@ dummy_func(void) {
op(_INIT_CALL_PY_EXACT_ARGS, (callable, self_or_null, args[oparg] -- new_frame)) {
int argcount = oparg;
PyCodeObject *co = NULL;
assert((this_instr + 2)->opcode == _PUSH_FRAME);
co = get_code_with_logging((this_instr + 2));
PyCodeObject *co = get_code_with_logging((this_instr + 2));
if (co == NULL) {
ctx->done = true;
break;
}
assert(!PyJitRef_IsNull(self_or_null));
assert(args != NULL);
if (sym_is_not_null(self_or_null)) {
@ -742,9 +741,8 @@ dummy_func(void) {
}
op(_PY_FRAME_GENERAL, (callable, self_or_null, args[oparg] -- new_frame)) {
PyCodeObject *co = NULL;
assert((this_instr + 2)->opcode == _PUSH_FRAME);
co = get_code_with_logging((this_instr + 2));
PyCodeObject *co = get_code_with_logging((this_instr + 2));
if (co == NULL) {
ctx->done = true;
break;
@ -775,6 +773,7 @@ dummy_func(void) {
JitOptRef temp = PyJitRef_StripReferenceInfo(retval);
DEAD(retval);
SAVE_STACK();
PyCodeObject *co = get_current_code_object(ctx);
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
@ -787,17 +786,13 @@ dummy_func(void) {
assert(framesize <= curr_space);
curr_space -= framesize;
co = get_code(this_instr);
if (co == NULL) {
// might be impossible, but bailing is still safe
ctx->done = true;
}
RELOAD_STACK();
res = temp;
}
op(_RETURN_GENERATOR, ( -- res)) {
SYNC_SP();
PyCodeObject *co = get_current_code_object(ctx);
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
@ -810,12 +805,6 @@ dummy_func(void) {
assert(framesize > 0);
assert(framesize <= curr_space);
curr_space -= framesize;
co = get_code(this_instr);
if (co == NULL) {
// might be impossible, but bailing is still safe
ctx->done = true;
}
}
op(_YIELD_VALUE, (unused -- value)) {
@ -863,13 +852,16 @@ dummy_func(void) {
ctx->frame = (_Py_UOpsAbstractFrame *)PyJitRef_Unwrap(new_frame);
ctx->curr_frame_depth++;
stack_pointer = ctx->frame->stack_pointer;
co = get_code(this_instr);
if (co == NULL) {
// should be about to _EXIT_TRACE anyway
uint64_t operand = this_instr->operand0;
if (operand == 0 || (operand & 1)) {
// It's either a code object or NULL
ctx->done = true;
break;
}
PyFunctionObject *func = (PyFunctionObject *)operand;
PyCodeObject *co = (PyCodeObject *)func->func_code;
assert(PyFunction_Check(func));
ctx->frame->func = func;
/* Stack space handling */
int framesize = co->co_framesize;
assert(framesize > 0);
@ -1250,6 +1242,96 @@ dummy_func(void) {
}
}
op(_GUARD_GLOBALS_VERSION, (version/1 --)) {
if (ctx->frame->func != NULL) {
PyObject *globals = ctx->frame->func->func_globals;
if (incorrect_keys(globals, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
/* Do nothing */
}
else {
if (!ctx->frame->globals_watched) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
ctx->frame->globals_watched = true;
}
if (ctx->frame->globals_checked_version == version) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
}
}
ctx->frame->globals_checked_version = version;
}
op(_LOAD_GLOBAL_BUILTINS, (version/1, index/1 -- res)) {
(void)version;
(void)index;
PyObject *cnst = NULL;
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *builtins = interp->builtins;
if (incorrect_keys(builtins, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (interp->rare_events.builtin_dict >= _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) {
/* Do nothing */
}
else {
if (!ctx->builtins_watched) {
PyDict_Watch(BUILTINS_WATCHER_ID, builtins);
ctx->builtins_watched = true;
}
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
cnst = convert_global_to_const(this_instr, builtins, false);
}
}
if (cnst == NULL) {
res = sym_new_not_null(ctx);
}
else {
res = sym_new_const(ctx, cnst);
}
}
op(_LOAD_GLOBAL_MODULE, (version/1, unused/1, index/1 -- res)) {
(void)index;
PyObject *cnst = NULL;
if (ctx->frame->func != NULL) {
PyObject *globals = ctx->frame->func->func_globals;
if (incorrect_keys(globals, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
/* Do nothing */
}
else {
if (!ctx->frame->globals_watched) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
ctx->frame->globals_watched = true;
}
if (ctx->frame->globals_checked_version != version && this_instr[-1].opcode == _NOP) {
REPLACE_OP(this_instr-1, _GUARD_GLOBALS_VERSION, 0, version);
ctx->frame->globals_checked_version = version;
}
if (ctx->frame->globals_checked_version == version) {
cnst = convert_global_to_const(this_instr, globals, false);
}
}
}
if (cnst == NULL) {
res = sym_new_not_null(ctx);
}
else {
res = sym_new_const(ctx, cnst);
}
}
// END BYTECODES //
}

View file

@ -70,6 +70,7 @@
case _LOAD_CONST: {
JitOptRef value;
PyCodeObject *co = get_current_code_object(ctx);
PyObject *val = PyTuple_GET_ITEM(co->co_consts, oparg);
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);
value = PyJitRef_Borrow(sym_new_const(ctx, val));
@ -1115,6 +1116,7 @@
JitOptRef temp = PyJitRef_StripReferenceInfo(retval);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
PyCodeObject *co = get_current_code_object(ctx);
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
@ -1124,10 +1126,6 @@
assert(framesize > 0);
assert(framesize <= curr_space);
curr_space -= framesize;
co = get_code(this_instr);
if (co == NULL) {
ctx->done = true;
}
res = temp;
stack_pointer[0] = res;
stack_pointer += 1;
@ -1343,12 +1341,65 @@
}
case _GUARD_GLOBALS_VERSION: {
uint16_t version = (uint16_t)this_instr->operand0;
if (ctx->frame->func != NULL) {
PyObject *globals = ctx->frame->func->func_globals;
if (incorrect_keys(globals, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
}
else {
if (!ctx->frame->globals_watched) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
ctx->frame->globals_watched = true;
}
if (ctx->frame->globals_checked_version == version) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
}
}
ctx->frame->globals_checked_version = version;
break;
}
case _LOAD_GLOBAL_MODULE: {
JitOptRef res;
res = sym_new_not_null(ctx);
uint16_t version = (uint16_t)this_instr->operand0;
uint16_t index = (uint16_t)this_instr->operand0;
(void)index;
PyObject *cnst = NULL;
if (ctx->frame->func != NULL) {
PyObject *globals = ctx->frame->func->func_globals;
if (incorrect_keys(globals, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (get_mutations(globals) >= _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
}
else {
if (!ctx->frame->globals_watched) {
PyDict_Watch(GLOBALS_WATCHER_ID, globals);
_Py_BloomFilter_Add(dependencies, globals);
ctx->frame->globals_watched = true;
}
if (ctx->frame->globals_checked_version != version && this_instr[-1].opcode == _NOP) {
REPLACE_OP(this_instr-1, _GUARD_GLOBALS_VERSION, 0, version);
ctx->frame->globals_checked_version = version;
}
if (ctx->frame->globals_checked_version == version) {
cnst = convert_global_to_const(this_instr, globals, false);
}
}
}
if (cnst == NULL) {
res = sym_new_not_null(ctx);
}
else {
res = sym_new_const(ctx, cnst);
}
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
@ -1357,7 +1408,34 @@
case _LOAD_GLOBAL_BUILTINS: {
JitOptRef res;
res = sym_new_not_null(ctx);
uint16_t version = (uint16_t)this_instr->operand0;
uint16_t index = (uint16_t)this_instr->operand0;
(void)version;
(void)index;
PyObject *cnst = NULL;
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *builtins = interp->builtins;
if (incorrect_keys(builtins, version)) {
OPT_STAT_INC(remove_globals_incorrect_keys);
ctx->done = true;
}
else if (interp->rare_events.builtin_dict >= _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) {
}
else {
if (!ctx->builtins_watched) {
PyDict_Watch(BUILTINS_WATCHER_ID, builtins);
ctx->builtins_watched = true;
}
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
cnst = convert_global_to_const(this_instr, builtins, false);
}
}
if (cnst == NULL) {
res = sym_new_not_null(ctx);
}
else {
res = sym_new_const(ctx, cnst);
}
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
@ -1651,7 +1729,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = (PyTypeObject *)sym_get_const(ctx, owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -2263,7 +2341,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -2283,7 +2361,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -2302,7 +2380,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -2317,7 +2395,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_POP_TOP_LOAD_CONST_INLINE_BORROW,
_POP_TOP_LOAD_CONST_INLINE);
@ -2337,7 +2415,7 @@
PyObject *descr = (PyObject *)this_instr->operand0;
(void)descr;
PyTypeObject *type = sym_get_type(owner);
PyObject *name = PyTuple_GET_ITEM(co->co_names, oparg >> 1);
PyObject *name = get_co_name(ctx, oparg >> 1);
attr = lookup_attr(ctx, this_instr, type, name,
_LOAD_CONST_UNDER_INLINE_BORROW,
_LOAD_CONST_UNDER_INLINE);
@ -2370,9 +2448,8 @@
case _PY_FRAME_GENERAL: {
JitOptRef new_frame;
PyCodeObject *co = NULL;
assert((this_instr + 2)->opcode == _PUSH_FRAME);
co = get_code_with_logging((this_instr + 2));
PyCodeObject *co = get_code_with_logging((this_instr + 2));
if (co == NULL) {
ctx->done = true;
break;
@ -2496,9 +2573,8 @@
args = &stack_pointer[-oparg];
self_or_null = stack_pointer[-1 - oparg];
int argcount = oparg;
PyCodeObject *co = NULL;
assert((this_instr + 2)->opcode == _PUSH_FRAME);
co = get_code_with_logging((this_instr + 2));
PyCodeObject *co = get_code_with_logging((this_instr + 2));
if (co == NULL) {
ctx->done = true;
break;
@ -2529,11 +2605,15 @@
ctx->frame = (_Py_UOpsAbstractFrame *)PyJitRef_Unwrap(new_frame);
ctx->curr_frame_depth++;
stack_pointer = ctx->frame->stack_pointer;
co = get_code(this_instr);
if (co == NULL) {
uint64_t operand = this_instr->operand0;
if (operand == 0 || (operand & 1)) {
ctx->done = true;
break;
}
PyFunctionObject *func = (PyFunctionObject *)operand;
PyCodeObject *co = (PyCodeObject *)func->func_code;
assert(PyFunction_Check(func));
ctx->frame->func = func;
int framesize = co->co_framesize;
assert(framesize > 0);
curr_space += framesize;
@ -2925,6 +3005,7 @@
case _RETURN_GENERATOR: {
JitOptRef res;
PyCodeObject *co = get_current_code_object(ctx);
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
@ -2938,10 +3019,6 @@
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
co = get_code(this_instr);
if (co == NULL) {
ctx->done = true;
}
break;
}
@ -3309,10 +3386,6 @@
break;
}
case _CHECK_FUNCTION: {
break;
}
case _START_EXECUTOR: {
break;
}

View file

@ -827,6 +827,9 @@ _Py_uop_frame_new(
frame->locals = ctx->n_consumed;
frame->stack = frame->locals + co->co_nlocalsplus;
frame->stack_pointer = frame->stack + curr_stackentries;
frame->globals_checked_version = 0;
frame->globals_watched = false;
frame->func = NULL;
ctx->n_consumed = ctx->n_consumed + (co->co_nlocalsplus + co->co_stacksize);
if (ctx->n_consumed >= ctx->limit) {
ctx->done = true;
@ -895,6 +898,7 @@ _Py_uop_abstractcontext_init(JitOptContext *ctx)
ctx->done = false;
ctx->out_of_space = false;
ctx->contradiction = false;
ctx->builtins_watched = false;
}
int