GH-109369: Exit tier 2 if executor is invalid (GH-111657)

This commit is contained in:
Mark Shannon 2023-11-09 11:19:51 +00:00 committed by GitHub
parent 6046aec377
commit 25c4956488
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 348 additions and 230 deletions

View file

@ -944,3 +944,7 @@
PARTITIONNODE_OVERWRITE((_Py_PARTITIONNODE_t *)PARTITIONNODE_NULLROOT, PEEK(-(-1 - oparg)), true);
break;
}
case _CHECK_VALIDITY: {
break;
}

View file

@ -4021,6 +4021,11 @@ dummy_func(
memmove(&stack_pointer[-1 - oparg], &stack_pointer[-oparg], oparg * sizeof(stack_pointer[0]));
}
op(_CHECK_VALIDITY, (--)) {
TIER_TWO_ONLY
DEOPT_IF(!current_executor->base.vm_data.valid);
}
// END BYTECODES //

View file

@ -1057,7 +1057,7 @@ error_tier_two:
deoptimize:
// On DEOPT_IF we just repeat the last instruction.
// This presumes nothing was popped from the stack (nor pushed).
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 " @ %d]\n", opcode, operand, (int)(next_uop-current_executor->trace-1));
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
UOP_STAT_INC(opcode, miss);
frame->return_offset = 0; // Dispatch to frame->instr_ptr

View file

@ -3273,4 +3273,10 @@
break;
}
case _CHECK_VALIDITY: {
TIER_TWO_ONLY
DEOPT_IF(!current_executor->base.vm_data.valid, _CHECK_VALIDITY);
break;
}
#undef TIER_TWO

View file

@ -472,8 +472,8 @@ translate_bytecode_to_trace(
} \
reserved = (n); // Keep ADD_TO_TRACE / ADD_TO_STUB honest
// Reserve space for main+stub uops, plus 2 for _SET_IP and _EXIT_TRACE
#define RESERVE(main, stub) RESERVE_RAW((main) + (stub) + 2, uop_name(opcode))
// Reserve space for main+stub uops, plus 3 for _SET_IP, _CHECK_VALIDITY and _EXIT_TRACE
#define RESERVE(main, stub) RESERVE_RAW((main) + (stub) + 3, uop_name(opcode))
// Trace stack operations (used by _PUSH_FRAME, _POP_FRAME)
#define TRACE_STACK_PUSH() \
@ -503,8 +503,9 @@ translate_bytecode_to_trace(
top: // Jump here after _PUSH_FRAME or likely branches
for (;;) {
RESERVE_RAW(2, "epilogue"); // Always need space for _SET_IP and _EXIT_TRACE
RESERVE_RAW(3, "epilogue"); // Always need space for _SET_IP, _CHECK_VALIDITY and _EXIT_TRACE
ADD_TO_TRACE(_SET_IP, INSTR_IP(instr, code), 0);
ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0);
uint32_t opcode = instr->op.code;
uint32_t oparg = instr->op.arg;

View file

@ -12,13 +12,13 @@
#include <stddef.h>
#include "pycore_optimizer.h"
static void
remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
{
// Note that we don't enter stubs, those SET_IPs are needed.
int last_set_ip = -1;
bool need_ip = true;
bool maybe_invalid = false;
for (int pc = 0; pc < buffer_size; pc++) {
int opcode = buffer[pc].opcode;
if (opcode == _SET_IP) {
@ -28,6 +28,16 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
need_ip = false;
last_set_ip = pc;
}
else if (opcode == _CHECK_VALIDITY) {
if (maybe_invalid) {
/* Exiting the trace requires that IP is correct */
need_ip = true;
maybe_invalid = false;
}
else {
buffer[pc].opcode = NOP;
}
}
else if (opcode == _JUMP_TO_TOP || opcode == _EXIT_TRACE) {
break;
}
@ -36,6 +46,9 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
if (_PyOpcode_opcode_metadata[opcode].flags & (HAS_ERROR_FLAG | HAS_DEOPT_FLAG) || opcode == _PUSH_FRAME) {
need_ip = true;
}
if (_PyOpcode_opcode_metadata[opcode].flags & HAS_ESCAPES_FLAG) {
maybe_invalid = true;
}
}
}
}