gh-144540: Add _MAKE_HEAP_SAFE uop to eliminate unnecessary refcount operations in RETURN_VALUE and YIELD_VALUE (GH-144414)

This commit is contained in:
Hai Zhu 2026-03-12 04:24:19 +08:00 committed by GitHub
parent 77d6d5d8fc
commit f062014d38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 1288 additions and 1072 deletions

View file

@ -1299,12 +1299,16 @@ dummy_func(
return result;
}
op(_MAKE_HEAP_SAFE, (value -- value)) {
value = PyStackRef_MakeHeapSafe(value);
}
// The stack effect here is a bit misleading.
// retval is popped from the stack, but res
// is pushed to a different frame, the callers' frame.
inst(RETURN_VALUE, (retval -- res)) {
op(_RETURN_VALUE, (retval -- res)) {
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
_PyStackRef temp = PyStackRef_MakeHeapSafe(retval);
_PyStackRef temp = retval;
DEAD(retval);
SAVE_STACK();
assert(STACK_LEVEL() == 0);
@ -1319,6 +1323,10 @@ dummy_func(
LLTRACE_RESUME_FRAME();
}
macro(RETURN_VALUE) =
_MAKE_HEAP_SAFE +
_RETURN_VALUE;
tier1 op(_RETURN_VALUE_EVENT, (val -- val)) {
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
@ -1328,7 +1336,8 @@ dummy_func(
macro(INSTRUMENTED_RETURN_VALUE) =
_RETURN_VALUE_EVENT +
RETURN_VALUE;
_MAKE_HEAP_SAFE +
_RETURN_VALUE;
inst(GET_AITER, (obj -- iter)) {
unaryfunc getter = NULL;
@ -1470,7 +1479,7 @@ dummy_func(
_SEND_GEN_FRAME +
_PUSH_FRAME;
inst(YIELD_VALUE, (retval -- value)) {
op(_YIELD_VALUE, (retval -- value)) {
// 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.
@ -1502,10 +1511,14 @@ dummy_func(
#endif
RELOAD_STACK();
LOAD_IP(1 + INLINE_CACHE_ENTRIES_SEND);
value = PyStackRef_MakeHeapSafe(temp);
value = temp;
LLTRACE_RESUME_FRAME();
}
macro(YIELD_VALUE) =
_MAKE_HEAP_SAFE +
_YIELD_VALUE;
tier1 op(_YIELD_VALUE_EVENT, (val -- val)) {
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_YIELD,
@ -1521,7 +1534,8 @@ dummy_func(
macro(INSTRUMENTED_YIELD_VALUE) =
_YIELD_VALUE_EVENT +
YIELD_VALUE;
_MAKE_HEAP_SAFE +
_YIELD_VALUE;
inst(POP_EXCEPT, (exc_value -- )) {
_PyErr_StackItem *exc_info = tstate->exc_info;