Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115999: Add free-threaded specialization for SEND. #127426

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
@@ -1272,6 +1272,48 @@ def g():
self.assert_specialized(g, "CONTAINS_OP_SET")
self.assert_no_opcode(g, "CONTAINS_OP")

@cpython_only
@requires_specialization_ft
def test_send_with(self):
def run_async(coro):
while True:
try:
coro.send(None)
except StopIteration:
break

class CM:
async def __aenter__(self):
return self

async def __aexit__(self, *exc):
pass

async def send_with():
for i in range(100):
async with CM():
x = 1

run_async(send_with())
# Note there are still unspecialized "SEND" opcodes in the
# cleanup paths of the 'with' statement.
self.assert_specialized(send_with, "SEND_GEN")

@cpython_only
@requires_specialization_ft
def test_send_yield_from(self):
def g():
yield None

def send_yield_from():
yield from g()

for i in range(100):
list(send_yield_from())

self.assert_specialized(send_yield_from, "SEND_GEN")
self.assert_no_opcode(send_yield_from, "SEND")

@cpython_only
@requires_specialization_ft
def test_to_bool(self):
4 changes: 2 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
@@ -1111,15 +1111,15 @@ dummy_func(
};

specializing op(_SPECIALIZE_SEND, (counter/1, receiver, unused -- receiver, unused)) {
#if ENABLE_SPECIALIZATION
#if ENABLE_SPECIALIZATION_FT
if (ADAPTIVE_COUNTER_TRIGGERS(counter)) {
next_instr = this_instr;
_Py_Specialize_Send(receiver, next_instr);
DISPATCH_SAME_OPARG();
}
OPCODE_DEFERRED_INC(SEND);
ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter);
#endif /* ENABLE_SPECIALIZATION */
#endif /* ENABLE_SPECIALIZATION_FT */
}

op(_SEND, (receiver, v -- receiver, retval)) {
4 changes: 2 additions & 2 deletions Python/generated_cases.c.h

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

15 changes: 4 additions & 11 deletions Python/specialize.c
Original file line number Diff line number Diff line change
@@ -2636,28 +2636,21 @@ _Py_Specialize_Send(_PyStackRef receiver_st, _Py_CODEUNIT *instr)
{
PyObject *receiver = PyStackRef_AsPyObjectBorrow(receiver_st);

assert(ENABLE_SPECIALIZATION);
assert(ENABLE_SPECIALIZATION_FT);
assert(_PyOpcode_Caches[SEND] == INLINE_CACHE_ENTRIES_SEND);
_PySendCache *cache = (_PySendCache *)(instr + 1);
PyTypeObject *tp = Py_TYPE(receiver);
if (tp == &PyGen_Type || tp == &PyCoro_Type) {
if (_PyInterpreterState_GET()->eval_frame) {
SPECIALIZATION_FAIL(SEND, SPEC_FAIL_OTHER);
goto failure;
}
instr->op.code = SEND_GEN;
goto success;
specialize(instr, SEND_GEN);
return;
}
SPECIALIZATION_FAIL(SEND,
_PySpecialization_ClassifyIterator(receiver));
failure:
STAT_INC(SEND, failure);
instr->op.code = SEND;
cache->counter = adaptive_counter_backoff(cache->counter);
return;
success:
STAT_INC(SEND, success);
cache->counter = adaptive_counter_cooldown();
unspecialize(instr);
}

#ifdef Py_STATS