mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
GH-118095: Add tier 2 support for YIELD_VALUE (GH-118380)
This commit is contained in:
parent
0f797402bc
commit
5b05d452cd
9 changed files with 92 additions and 16 deletions
1
Include/internal/pycore_opcode_metadata.h
generated
1
Include/internal/pycore_opcode_metadata.h
generated
|
@ -1347,6 +1347,7 @@ _PyOpcode_macro_expansion[256] = {
|
||||||
[UNPACK_SEQUENCE_TUPLE] = { .nuops = 1, .uops = { { _UNPACK_SEQUENCE_TUPLE, 0, 0 } } },
|
[UNPACK_SEQUENCE_TUPLE] = { .nuops = 1, .uops = { { _UNPACK_SEQUENCE_TUPLE, 0, 0 } } },
|
||||||
[UNPACK_SEQUENCE_TWO_TUPLE] = { .nuops = 1, .uops = { { _UNPACK_SEQUENCE_TWO_TUPLE, 0, 0 } } },
|
[UNPACK_SEQUENCE_TWO_TUPLE] = { .nuops = 1, .uops = { { _UNPACK_SEQUENCE_TWO_TUPLE, 0, 0 } } },
|
||||||
[WITH_EXCEPT_START] = { .nuops = 1, .uops = { { _WITH_EXCEPT_START, 0, 0 } } },
|
[WITH_EXCEPT_START] = { .nuops = 1, .uops = { { _WITH_EXCEPT_START, 0, 0 } } },
|
||||||
|
[YIELD_VALUE] = { .nuops = 1, .uops = { { _YIELD_VALUE, 0, 0 } } },
|
||||||
};
|
};
|
||||||
#endif // NEED_OPCODE_METADATA
|
#endif // NEED_OPCODE_METADATA
|
||||||
|
|
||||||
|
|
1
Include/internal/pycore_uop_ids.h
generated
1
Include/internal/pycore_uop_ids.h
generated
|
@ -282,6 +282,7 @@ extern "C" {
|
||||||
#define _UNPACK_SEQUENCE_TUPLE UNPACK_SEQUENCE_TUPLE
|
#define _UNPACK_SEQUENCE_TUPLE UNPACK_SEQUENCE_TUPLE
|
||||||
#define _UNPACK_SEQUENCE_TWO_TUPLE UNPACK_SEQUENCE_TWO_TUPLE
|
#define _UNPACK_SEQUENCE_TWO_TUPLE UNPACK_SEQUENCE_TWO_TUPLE
|
||||||
#define _WITH_EXCEPT_START WITH_EXCEPT_START
|
#define _WITH_EXCEPT_START WITH_EXCEPT_START
|
||||||
|
#define _YIELD_VALUE YIELD_VALUE
|
||||||
#define MAX_UOP_ID 447
|
#define MAX_UOP_ID 447
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
4
Include/internal/pycore_uop_metadata.h
generated
4
Include/internal/pycore_uop_metadata.h
generated
|
@ -91,6 +91,7 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = {
|
||||||
[_GET_AITER] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
[_GET_AITER] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
||||||
[_GET_ANEXT] = HAS_ERROR_FLAG | HAS_ERROR_NO_POP_FLAG | HAS_ESCAPES_FLAG,
|
[_GET_ANEXT] = HAS_ERROR_FLAG | HAS_ERROR_NO_POP_FLAG | HAS_ESCAPES_FLAG,
|
||||||
[_GET_AWAITABLE] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
[_GET_AWAITABLE] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
||||||
|
[_YIELD_VALUE] = HAS_ARG_FLAG | HAS_ESCAPES_FLAG,
|
||||||
[_POP_EXCEPT] = HAS_ESCAPES_FLAG,
|
[_POP_EXCEPT] = HAS_ESCAPES_FLAG,
|
||||||
[_LOAD_ASSERTION_ERROR] = 0,
|
[_LOAD_ASSERTION_ERROR] = 0,
|
||||||
[_LOAD_BUILD_CLASS] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
[_LOAD_BUILD_CLASS] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
|
||||||
|
@ -500,6 +501,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = {
|
||||||
[_UNPACK_SEQUENCE_TUPLE] = "_UNPACK_SEQUENCE_TUPLE",
|
[_UNPACK_SEQUENCE_TUPLE] = "_UNPACK_SEQUENCE_TUPLE",
|
||||||
[_UNPACK_SEQUENCE_TWO_TUPLE] = "_UNPACK_SEQUENCE_TWO_TUPLE",
|
[_UNPACK_SEQUENCE_TWO_TUPLE] = "_UNPACK_SEQUENCE_TWO_TUPLE",
|
||||||
[_WITH_EXCEPT_START] = "_WITH_EXCEPT_START",
|
[_WITH_EXCEPT_START] = "_WITH_EXCEPT_START",
|
||||||
|
[_YIELD_VALUE] = "_YIELD_VALUE",
|
||||||
};
|
};
|
||||||
int _PyUop_num_popped(int opcode, int oparg)
|
int _PyUop_num_popped(int opcode, int oparg)
|
||||||
{
|
{
|
||||||
|
@ -648,6 +650,8 @@ int _PyUop_num_popped(int opcode, int oparg)
|
||||||
return 1;
|
return 1;
|
||||||
case _GET_AWAITABLE:
|
case _GET_AWAITABLE:
|
||||||
return 1;
|
return 1;
|
||||||
|
case _YIELD_VALUE:
|
||||||
|
return 1;
|
||||||
case _POP_EXCEPT:
|
case _POP_EXCEPT:
|
||||||
return 1;
|
return 1;
|
||||||
case _LOAD_ASSERTION_ERROR:
|
case _LOAD_ASSERTION_ERROR:
|
||||||
|
|
|
@ -1089,32 +1089,38 @@ dummy_func(
|
||||||
goto resume_frame;
|
goto resume_frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
tier1 inst(YIELD_VALUE, (retval -- unused)) {
|
inst(YIELD_VALUE, (retval -- value)) {
|
||||||
// NOTE: It's important that YIELD_VALUE never raises an exception!
|
// NOTE: It's important that YIELD_VALUE never raises an exception!
|
||||||
// The compiler treats any exception raised here as a failed close()
|
// The compiler treats any exception raised here as a failed close()
|
||||||
// or throw() call.
|
// or throw() call.
|
||||||
|
#if TIER_ONE
|
||||||
assert(frame != &entry_frame);
|
assert(frame != &entry_frame);
|
||||||
frame->instr_ptr = next_instr;
|
#endif
|
||||||
|
frame->instr_ptr++;
|
||||||
PyGenObject *gen = _PyFrame_GetGenerator(frame);
|
PyGenObject *gen = _PyFrame_GetGenerator(frame);
|
||||||
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);
|
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);
|
||||||
assert(oparg == 0 || oparg == 1);
|
assert(oparg == 0 || oparg == 1);
|
||||||
gen->gi_frame_state = FRAME_SUSPENDED + oparg;
|
gen->gi_frame_state = FRAME_SUSPENDED + oparg;
|
||||||
_PyFrame_SetStackPointer(frame, stack_pointer - 1);
|
SYNC_SP();
|
||||||
|
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||||
tstate->exc_info = gen->gi_exc_state.previous_item;
|
tstate->exc_info = gen->gi_exc_state.previous_item;
|
||||||
gen->gi_exc_state.previous_item = NULL;
|
gen->gi_exc_state.previous_item = NULL;
|
||||||
_Py_LeaveRecursiveCallPy(tstate);
|
_Py_LeaveRecursiveCallPy(tstate);
|
||||||
_PyInterpreterFrame *gen_frame = frame;
|
_PyInterpreterFrame *gen_frame = frame;
|
||||||
frame = tstate->current_frame = frame->previous;
|
frame = tstate->current_frame = frame->previous;
|
||||||
gen_frame->previous = NULL;
|
gen_frame->previous = NULL;
|
||||||
_PyFrame_StackPush(frame, retval);
|
|
||||||
/* We don't know which of these is relevant here, so keep them equal */
|
/* We don't know which of these is relevant here, so keep them equal */
|
||||||
assert(INLINE_CACHE_ENTRIES_SEND == INLINE_CACHE_ENTRIES_FOR_ITER);
|
assert(INLINE_CACHE_ENTRIES_SEND == INLINE_CACHE_ENTRIES_FOR_ITER);
|
||||||
|
#if TIER_ONE
|
||||||
assert(_PyOpcode_Deopt[frame->instr_ptr->op.code] == SEND ||
|
assert(_PyOpcode_Deopt[frame->instr_ptr->op.code] == SEND ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == FOR_ITER ||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == FOR_ITER ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == INTERPRETER_EXIT ||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == INTERPRETER_EXIT ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == ENTER_EXECUTOR);
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == ENTER_EXECUTOR);
|
||||||
|
#endif
|
||||||
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
|
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
|
||||||
goto resume_frame;
|
LOAD_SP();
|
||||||
|
value = retval;
|
||||||
|
LLTRACE_RESUME_FRAME();
|
||||||
}
|
}
|
||||||
|
|
||||||
inst(POP_EXCEPT, (exc_value -- )) {
|
inst(POP_EXCEPT, (exc_value -- )) {
|
||||||
|
|
41
Python/executor_cases.c.h
generated
41
Python/executor_cases.c.h
generated
|
@ -1110,6 +1110,47 @@
|
||||||
|
|
||||||
/* _INSTRUMENTED_YIELD_VALUE is not a viable micro-op for tier 2 because it is instrumented */
|
/* _INSTRUMENTED_YIELD_VALUE is not a viable micro-op for tier 2 because it is instrumented */
|
||||||
|
|
||||||
|
case _YIELD_VALUE: {
|
||||||
|
PyObject *retval;
|
||||||
|
PyObject *value;
|
||||||
|
oparg = CURRENT_OPARG();
|
||||||
|
retval = stack_pointer[-1];
|
||||||
|
// NOTE: It's important that YIELD_VALUE never raises an exception!
|
||||||
|
// The compiler treats any exception raised here as a failed close()
|
||||||
|
// or throw() call.
|
||||||
|
#if TIER_ONE
|
||||||
|
assert(frame != &entry_frame);
|
||||||
|
#endif
|
||||||
|
frame->instr_ptr++;
|
||||||
|
PyGenObject *gen = _PyFrame_GetGenerator(frame);
|
||||||
|
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);
|
||||||
|
assert(oparg == 0 || oparg == 1);
|
||||||
|
gen->gi_frame_state = FRAME_SUSPENDED + oparg;
|
||||||
|
stack_pointer += -1;
|
||||||
|
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||||
|
tstate->exc_info = gen->gi_exc_state.previous_item;
|
||||||
|
gen->gi_exc_state.previous_item = NULL;
|
||||||
|
_Py_LeaveRecursiveCallPy(tstate);
|
||||||
|
_PyInterpreterFrame *gen_frame = frame;
|
||||||
|
frame = tstate->current_frame = frame->previous;
|
||||||
|
gen_frame->previous = NULL;
|
||||||
|
/* We don't know which of these is relevant here, so keep them equal */
|
||||||
|
assert(INLINE_CACHE_ENTRIES_SEND == INLINE_CACHE_ENTRIES_FOR_ITER);
|
||||||
|
#if TIER_ONE
|
||||||
|
assert(_PyOpcode_Deopt[frame->instr_ptr->op.code] == SEND ||
|
||||||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == FOR_ITER ||
|
||||||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == INTERPRETER_EXIT ||
|
||||||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == ENTER_EXECUTOR);
|
||||||
|
#endif
|
||||||
|
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
|
||||||
|
LOAD_SP();
|
||||||
|
value = retval;
|
||||||
|
LLTRACE_RESUME_FRAME();
|
||||||
|
stack_pointer[0] = value;
|
||||||
|
stack_pointer += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case _POP_EXCEPT: {
|
case _POP_EXCEPT: {
|
||||||
PyObject *exc_value;
|
PyObject *exc_value;
|
||||||
exc_value = stack_pointer[-1];
|
exc_value = stack_pointer[-1];
|
||||||
|
|
18
Python/generated_cases.c.h
generated
18
Python/generated_cases.c.h
generated
|
@ -6012,31 +6012,41 @@
|
||||||
next_instr += 1;
|
next_instr += 1;
|
||||||
INSTRUCTION_STATS(YIELD_VALUE);
|
INSTRUCTION_STATS(YIELD_VALUE);
|
||||||
PyObject *retval;
|
PyObject *retval;
|
||||||
|
PyObject *value;
|
||||||
retval = stack_pointer[-1];
|
retval = stack_pointer[-1];
|
||||||
// NOTE: It's important that YIELD_VALUE never raises an exception!
|
// NOTE: It's important that YIELD_VALUE never raises an exception!
|
||||||
// The compiler treats any exception raised here as a failed close()
|
// The compiler treats any exception raised here as a failed close()
|
||||||
// or throw() call.
|
// or throw() call.
|
||||||
|
#if TIER_ONE
|
||||||
assert(frame != &entry_frame);
|
assert(frame != &entry_frame);
|
||||||
frame->instr_ptr = next_instr;
|
#endif
|
||||||
|
frame->instr_ptr++;
|
||||||
PyGenObject *gen = _PyFrame_GetGenerator(frame);
|
PyGenObject *gen = _PyFrame_GetGenerator(frame);
|
||||||
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);
|
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);
|
||||||
assert(oparg == 0 || oparg == 1);
|
assert(oparg == 0 || oparg == 1);
|
||||||
gen->gi_frame_state = FRAME_SUSPENDED + oparg;
|
gen->gi_frame_state = FRAME_SUSPENDED + oparg;
|
||||||
_PyFrame_SetStackPointer(frame, stack_pointer - 1);
|
stack_pointer += -1;
|
||||||
|
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||||
tstate->exc_info = gen->gi_exc_state.previous_item;
|
tstate->exc_info = gen->gi_exc_state.previous_item;
|
||||||
gen->gi_exc_state.previous_item = NULL;
|
gen->gi_exc_state.previous_item = NULL;
|
||||||
_Py_LeaveRecursiveCallPy(tstate);
|
_Py_LeaveRecursiveCallPy(tstate);
|
||||||
_PyInterpreterFrame *gen_frame = frame;
|
_PyInterpreterFrame *gen_frame = frame;
|
||||||
frame = tstate->current_frame = frame->previous;
|
frame = tstate->current_frame = frame->previous;
|
||||||
gen_frame->previous = NULL;
|
gen_frame->previous = NULL;
|
||||||
_PyFrame_StackPush(frame, retval);
|
|
||||||
/* We don't know which of these is relevant here, so keep them equal */
|
/* We don't know which of these is relevant here, so keep them equal */
|
||||||
assert(INLINE_CACHE_ENTRIES_SEND == INLINE_CACHE_ENTRIES_FOR_ITER);
|
assert(INLINE_CACHE_ENTRIES_SEND == INLINE_CACHE_ENTRIES_FOR_ITER);
|
||||||
|
#if TIER_ONE
|
||||||
assert(_PyOpcode_Deopt[frame->instr_ptr->op.code] == SEND ||
|
assert(_PyOpcode_Deopt[frame->instr_ptr->op.code] == SEND ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == FOR_ITER ||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == FOR_ITER ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == INTERPRETER_EXIT ||
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == INTERPRETER_EXIT ||
|
||||||
_PyOpcode_Deopt[frame->instr_ptr->op.code] == ENTER_EXECUTOR);
|
_PyOpcode_Deopt[frame->instr_ptr->op.code] == ENTER_EXECUTOR);
|
||||||
|
#endif
|
||||||
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
|
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
|
||||||
goto resume_frame;
|
LOAD_SP();
|
||||||
|
value = retval;
|
||||||
|
LLTRACE_RESUME_FRAME();
|
||||||
|
stack_pointer[0] = value;
|
||||||
|
stack_pointer += 1;
|
||||||
|
DISPATCH();
|
||||||
}
|
}
|
||||||
#undef TIER_ONE
|
#undef TIER_ONE
|
||||||
|
|
|
@ -704,7 +704,7 @@ top: // Jump here after _PUSH_FRAME or likely branches
|
||||||
int nuops = expansion->nuops;
|
int nuops = expansion->nuops;
|
||||||
RESERVE(nuops + 1); /* One extra for exit */
|
RESERVE(nuops + 1); /* One extra for exit */
|
||||||
int16_t last_op = expansion->uops[nuops-1].uop;
|
int16_t last_op = expansion->uops[nuops-1].uop;
|
||||||
if (last_op == _POP_FRAME || last_op == _RETURN_GENERATOR) {
|
if (last_op == _POP_FRAME || last_op == _RETURN_GENERATOR || last_op == _YIELD_VALUE) {
|
||||||
// Check for trace stack underflow now:
|
// Check for trace stack underflow now:
|
||||||
// We can't bail e.g. in the middle of
|
// We can't bail e.g. in the middle of
|
||||||
// LOAD_CONST + _POP_FRAME.
|
// LOAD_CONST + _POP_FRAME.
|
||||||
|
@ -763,7 +763,7 @@ top: // Jump here after _PUSH_FRAME or likely branches
|
||||||
Py_FatalError("garbled expansion");
|
Py_FatalError("garbled expansion");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uop == _POP_FRAME || uop == _RETURN_GENERATOR) {
|
if (uop == _POP_FRAME || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE) {
|
||||||
TRACE_STACK_POP();
|
TRACE_STACK_POP();
|
||||||
/* Set the operand to the function or code object returned to,
|
/* Set the operand to the function or code object returned to,
|
||||||
* to assist optimization passes. (See _PUSH_FRAME below.)
|
* to assist optimization passes. (See _PUSH_FRAME below.)
|
||||||
|
|
|
@ -673,6 +673,15 @@ dummy_func(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
op(_YIELD_VALUE, (unused -- res)) {
|
||||||
|
OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx));
|
||||||
|
}
|
||||||
|
|
||||||
|
op(_FOR_ITER_GEN_FRAME, ( -- )) {
|
||||||
|
/* We are about to hit the end of the trace */
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
op(_CHECK_STACK_SPACE, ( --)) {
|
op(_CHECK_STACK_SPACE, ( --)) {
|
||||||
assert(corresponding_check_stack == NULL);
|
assert(corresponding_check_stack == NULL);
|
||||||
corresponding_check_stack = this_instr;
|
corresponding_check_stack = this_instr;
|
||||||
|
|
14
Python/optimizer_cases.c.h
generated
14
Python/optimizer_cases.c.h
generated
|
@ -684,6 +684,13 @@
|
||||||
|
|
||||||
/* _INSTRUMENTED_YIELD_VALUE is not a viable micro-op for tier 2 */
|
/* _INSTRUMENTED_YIELD_VALUE is not a viable micro-op for tier 2 */
|
||||||
|
|
||||||
|
case _YIELD_VALUE: {
|
||||||
|
_Py_UopsSymbol *res;
|
||||||
|
OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx));
|
||||||
|
stack_pointer[-1] = res;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case _POP_EXCEPT: {
|
case _POP_EXCEPT: {
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
|
@ -1440,11 +1447,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
case _FOR_ITER_GEN_FRAME: {
|
case _FOR_ITER_GEN_FRAME: {
|
||||||
_PyInterpreterFrame *gen_frame;
|
/* We are about to hit the end of the trace */
|
||||||
gen_frame = sym_new_not_null(ctx);
|
goto done;
|
||||||
if (gen_frame == NULL) goto out_of_space;
|
|
||||||
stack_pointer[0] = (_Py_UopsSymbol *)gen_frame;
|
|
||||||
stack_pointer += 1;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue