Skip to content

Commit

Permalink
port changes from Mark's PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner committed Dec 23, 2024
1 parent be02b9d commit ca98f9e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 37 deletions.
48 changes: 25 additions & 23 deletions Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2950,8 +2950,7 @@ dummy_func(
_PyErr_Clear(tstate);
}
/* iterator ended normally */
assert(next_instr[oparg].op.code == END_FOR ||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
assert(base_opcode(_PyFrame_GetCode(frame), INSTR_OFFSET() + oparg) == END_FOR);
PyStackRef_CLOSE(iter);
STACK_SHRINK(1);
/* Jump forward oparg, then skip following END_FOR and POP_TOP instruction */
Expand Down Expand Up @@ -3004,8 +3003,7 @@ dummy_func(
_PyErr_Clear(tstate);
}
/* iterator ended normally */
assert(next_instr[oparg].op.code == END_FOR ||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
assert(base_opcode(_PyFrame_GetCode(frame), INSTR_OFFSET() + oparg) == END_FOR);
STACK_SHRINK(1);
PyStackRef_CLOSE(iter_stackref);
/* Skip END_FOR and POP_TOP */
Expand Down Expand Up @@ -5146,6 +5144,14 @@ dummy_func(
// assert(tstate->tracing || eval_breaker == FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(_PyFrame_GetCode(frame)->_co_instrumentation_version));
}

tier2 op(_RETURN_OFFSET, (--)) {
frame->instr_ptr += frame->return_offset;
}

tier2 op(_YIELD_OFFSET, (--)) {
frame->instr_ptr += 1 + INLINE_CACHE_ENTRIES_SEND;
}

// END BYTECODES //

}
Expand Down
12 changes: 12 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,18 @@ static const _Py_CODEUNIT _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS[] = {

#ifdef Py_DEBUG
extern void _PyUOpPrint(const _PyUOpInstruction *uop);

static int
base_opcode(PyCodeObject *code, int offset)
{
int opcode = _Py_GetBaseOpcode(code, offset);
if (opcode == ENTER_EXECUTOR) {
int oparg = _PyCode_CODE(code)[offset].op.arg;
_PyExecutorObject *ex = code->co_executors->executors[oparg];
return ex->vm_data.opcode;
}
return opcode;
}
#endif


Expand Down
10 changes: 10 additions & 0 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ _PyOptimizer_Optimize(
_PyInterpreterFrame *frame, _Py_CODEUNIT *start,
_PyStackRef *stack_pointer, _PyExecutorObject **executor_ptr, int chain_depth)
{
if (start->op.code == INTERPRETER_EXIT || start->op.code == EXIT_INIT_CHECK) {
return 0;
}
// The first executor in a chain and the MAX_CHAIN_DEPTH'th executor *must*
// make progress in order to avoid infinite loops or excessively-long
// side-exit chains. We can only insert the executor into the bytecode if
Expand Down Expand Up @@ -735,7 +738,7 @@ translate_bytecode_to_trace(
int nuops = expansion->nuops;
RESERVE(nuops + 1); /* One extra for exit */
int16_t last_op = expansion->uops[nuops-1].uop;
if (last_op == _RETURN_VALUE || last_op == _RETURN_GENERATOR || last_op == _YIELD_VALUE) {
if (last_op == _RETURN_VALUE) {
// Check for trace stack underflow now:
// We can't bail e.g. in the middle of
// LOAD_CONST + _RETURN_VALUE.
Expand Down Expand Up @@ -797,6 +800,19 @@ translate_bytecode_to_trace(
}

if (uop == _RETURN_VALUE || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE) {
if (trace_stack_depth == 0) {
DPRINTF(2, "Trace stack underflow\n");
OPT_STAT_INC(trace_stack_underflow);
ADD_TO_TRACE(uop, oparg, 0, target);
if (uop == _RETURN_GENERATOR) {
ADD_TO_TRACE(_RETURN_OFFSET, 0, 0, 0);
}
else {
ADD_TO_TRACE(_YIELD_OFFSET, 0, 0, 0);
}
ADD_TO_TRACE(_DYNAMIC_EXIT, 0, 0, 0);
goto done;
}
TRACE_STACK_POP();
/* Set the operand to the function or code object returned to,
* to assist optimization passes. (See _PUSH_FRAME below.)
Expand Down
8 changes: 6 additions & 2 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,9 @@ dummy_func(void) {
op(_RETURN_VALUE, (retval -- res)) {
SAVE_STACK();
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
if (frame_pop(ctx)) {
goto done;
}
stack_pointer = ctx->frame->stack_pointer;

/* Stack space handling */
Expand All @@ -725,7 +727,9 @@ dummy_func(void) {
op(_RETURN_GENERATOR, ( -- res)) {
SYNC_SP();
ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
if (frame_pop(ctx)) {
goto done;
}
stack_pointer = ctx->frame->stack_pointer;
res = sym_new_unknown(ctx);

Expand Down
5 changes: 4 additions & 1 deletion Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ _Py_uop_frame_pop(_Py_UOpsContext *ctx)
_Py_UOpsAbstractFrame *frame = ctx->frame;
ctx->n_consumed = frame->locals;
ctx->curr_frame_depth--;
assert(ctx->curr_frame_depth >= 1);
if (ctx->curr_frame_depth == 0) {
ctx->frame = NULL;
return -1;
}
ctx->frame = &ctx->frames[ctx->curr_frame_depth - 1];

return 0;
Expand Down
4 changes: 2 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2619,8 +2619,8 @@ _Py_Specialize_ForIter(_PyStackRef iter, _Py_CODEUNIT *instr, int oparg)
}
else if (tp == &PyGen_Type && oparg <= SHRT_MAX) {
assert(instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == END_FOR ||
instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == INSTRUMENTED_END_FOR
);
instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == INSTRUMENTED_END_FOR ||
instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == ENTER_EXECUTOR);
if (_PyInterpreterState_GET()->eval_frame) {
SPECIALIZATION_FAIL(FOR_ITER, SPEC_FAIL_OTHER);
goto failure;
Expand Down

0 comments on commit ca98f9e

Please sign in to comment.