gh-146058: Fix _GUARD_CODE_VERSION_* (GH-146060)

This commit is contained in:
Ken Jin 2026-03-18 01:19:44 +08:00 committed by GitHub
parent ff287a72de
commit 966fc81531
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 1557 additions and 1154 deletions

View file

@ -528,6 +528,26 @@ guard_ip_uop[MAX_UOP_ID + 1] = {
[_YIELD_VALUE] = _GUARD_IP_YIELD_VALUE,
};
static const uint16_t
guard_code_version_uop[MAX_UOP_ID + 1] = {
[_PUSH_FRAME] = _GUARD_CODE_VERSION__PUSH_FRAME,
[_RETURN_GENERATOR] = _GUARD_CODE_VERSION_RETURN_GENERATOR,
[_RETURN_VALUE] = _GUARD_CODE_VERSION_RETURN_VALUE,
[_YIELD_VALUE] = _GUARD_CODE_VERSION_YIELD_VALUE,
};
static const uint16_t
dynamic_exit_uop[MAX_UOP_ID + 1] = {
[_GUARD_IP__PUSH_FRAME] = 1,
[_GUARD_IP_RETURN_GENERATOR] = 1,
[_GUARD_IP_RETURN_VALUE] = 1,
[_GUARD_IP_YIELD_VALUE] = 1,
[_GUARD_CODE_VERSION__PUSH_FRAME] = 1,
[_GUARD_CODE_VERSION_RETURN_GENERATOR] = 1,
[_GUARD_CODE_VERSION_RETURN_VALUE] = 1,
[_GUARD_CODE_VERSION_YIELD_VALUE] = 1,
};
#define CONFIDENCE_RANGE 1000
#define CONFIDENCE_CUTOFF 333
@ -939,9 +959,10 @@ _PyJit_translate_single_bytecode_to_trace(
} // End switch (opcode)
if (needs_guard_ip) {
uint16_t guard_ip = guard_ip_uop[uop_buffer_last(trace)->opcode];
int last_opcode = uop_buffer_last(trace)->opcode;
uint16_t guard_ip = guard_ip_uop[last_opcode];
if (guard_ip == 0) {
DPRINTF(1, "Unknown uop needing guard ip %s\n", _PyOpcode_uop_name[uop_buffer_last(trace)->opcode]);
DPRINTF(1, "Unknown uop needing guard ip %s\n", _PyOpcode_uop_name[last_opcode]);
Py_UNREACHABLE();
}
PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
@ -952,7 +973,7 @@ _PyJit_translate_single_bytecode_to_trace(
/* Record stack depth, in operand1 */
int stack_depth = (int)(frame->stackpointer - _PyFrame_Stackbase(frame));
uop_buffer_last(trace)->operand1 = stack_depth;
ADD_TO_TRACE(_GUARD_CODE_VERSION, 0, ((PyCodeObject *)code)->co_version, 0);
ADD_TO_TRACE(guard_code_version_uop[last_opcode], 0, ((PyCodeObject *)code)->co_version, 0);
}
}
// Loop back to the start
@ -1219,13 +1240,7 @@ prepare_for_execution(_PyUOpInstruction *buffer, int length)
base_exit_op = _HANDLE_PENDING_AND_DEOPT;
}
int32_t jump_target = target;
if (
base_opcode == _GUARD_IP__PUSH_FRAME ||
base_opcode == _GUARD_IP_RETURN_VALUE ||
base_opcode == _GUARD_IP_YIELD_VALUE ||
base_opcode == _GUARD_IP_RETURN_GENERATOR ||
base_opcode == _GUARD_CODE_VERSION
) {
if (dynamic_exit_uop[base_opcode]) {
base_exit_op = _DYNAMIC_EXIT;
}
int exit_depth = get_cached_entries_for_side_exit(inst);