[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGenCoroutines / pr56301.cpp
blobcd851c0b815db9031cc17e89d07538daba263802
1 // An end-to-end test to make sure things get processed correctly.
2 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s -O3 | \
3 // RUN: FileCheck %s
5 #include "Inputs/coroutine.h"
7 struct SomeAwaitable {
8 // Resume the supplied handle once the awaitable becomes ready,
9 // returning a handle that should be resumed now for the sake of symmetric transfer.
10 // If the awaitable is already ready, return an empty handle without doing anything.
12 // Defined in another translation unit. Note that this may contain
13 // code that synchronizees with another thread.
14 std::coroutine_handle<> Register(std::coroutine_handle<>);
17 // Defined in another translation unit.
18 void DidntSuspend();
20 struct Awaiter {
21 SomeAwaitable&& awaitable;
22 bool suspended;
24 bool await_ready() { return false; }
26 std::coroutine_handle<> await_suspend(const std::coroutine_handle<> h) {
27 // Assume we will suspend unless proven otherwise below. We must do
28 // this *before* calling Register, since we may be destroyed by another
29 // thread asynchronously as soon as we have registered.
30 suspended = true;
32 // Attempt to hand off responsibility for resuming/destroying the coroutine.
33 const auto to_resume = awaitable.Register(h);
35 if (!to_resume) {
36 // The awaitable is already ready. In this case we know that Register didn't
37 // hand off responsibility for the coroutine. So record the fact that we didn't
38 // actually suspend, and tell the compiler to resume us inline.
39 suspended = false;
40 return h;
43 // Resume whatever Register wants us to resume.
44 return to_resume;
47 void await_resume() {
48 // If we didn't suspend, make note of that fact.
49 if (!suspended) {
50 DidntSuspend();
55 struct MyTask{
56 struct promise_type {
57 MyTask get_return_object() { return {}; }
58 std::suspend_never initial_suspend() { return {}; }
59 std::suspend_always final_suspend() noexcept { return {}; }
60 void unhandled_exception();
62 Awaiter await_transform(SomeAwaitable&& awaitable) {
63 return Awaiter{static_cast<SomeAwaitable&&>(awaitable)};
68 MyTask FooBar() {
69 co_await SomeAwaitable();
72 // CHECK-LABEL: @_Z6FooBarv
73 // CHECK: %[[to_resume:.*]] = {{.*}}call ptr @_ZN13SomeAwaitable8RegisterESt16coroutine_handleIvE
74 // CHECK-NEXT: %[[to_bool:.*]] = icmp eq ptr %[[to_resume]], null
75 // CHECK-NEXT: br i1 %[[to_bool]], label %[[then:.*]], label %[[else:.*]]
77 // CHECK: [[then]]:
78 // We only access the coroutine frame conditionally as the sources did.
79 // CHECK: store i8 0,
80 // CHECK-NEXT: br label %[[else]]
82 // CHECK: [[else]]:
83 // No more access to the coroutine frame until suspended.
84 // CHECK-NOT: store
85 // CHECK: }