1 // Verify that coroutine promise and allocated memory are freed up on exception.
2 // RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -disable-llvm-passes | FileCheck %s --check-prefixes=CHECK,THROWEND
3 // RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -fassume-nothrow-exception-dtor -disable-llvm-passes | FileCheck %s --check-prefixes=CHECK,NOTHROWEND
6 template <typename
... T
> struct coroutine_traits
;
8 template <class Promise
= void> struct coroutine_handle
{
9 coroutine_handle() = default;
10 static coroutine_handle
from_address(void *) noexcept
;
12 template <> struct coroutine_handle
<void> {
13 static coroutine_handle
from_address(void *) noexcept
;
14 coroutine_handle() = default;
15 template <class PromiseType
>
16 coroutine_handle(coroutine_handle
<PromiseType
>) noexcept
;
20 struct suspend_always
{
21 bool await_ready() noexcept
;
22 void await_suspend(std::coroutine_handle
<>) noexcept
;
23 void await_resume() noexcept
;
26 template <> struct std::coroutine_traits
<void> {
28 void get_return_object() noexcept
;
29 suspend_always
initial_suspend() noexcept
;
30 suspend_always
final_suspend() noexcept
;
31 void return_void() noexcept
;
34 void unhandled_exception() noexcept
;
38 struct Cleanup
{ ~Cleanup(); };
41 // CHECK-LABEL: define{{.*}} void @_Z1fv(
43 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64
45 // If promise constructor throws, check that we free the memory.
47 // CHECK: invoke void @_ZNSt16coroutine_traitsIJvEE12promise_typeC1Ev(
48 // CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]]
50 // CHECK: [[DeallocPad]]:
51 // CHECK-NEXT: landingpad
52 // CHECK-NEXT: cleanup
53 // THROWEND: br label %[[Dealloc:.+]]
54 // NOTHROWEND: icmp ne ptr %[[#]], null
55 // NOTHROWEND-NEXT: br i1 %[[#]], label %[[Dealloc:.+]], label
60 // if may_throw throws, check that we destroy the promise and free the memory.
62 // CHECK: invoke void @_Z9may_throwv(
63 // CHECK-NEXT: to label %{{.+}} unwind label %[[CatchPad:.+]]
65 // CHECK: [[CatchPad]]:
66 // CHECK-NEXT: landingpad
67 // CHECK-NEXT: catch ptr null
68 // CHECK: call void @_ZN7CleanupD1Ev(
69 // CHECK: br label %[[Catch:.+]]
72 // CHECK: call ptr @__cxa_begin_catch(
73 // CHECK: call void @_ZNSt16coroutine_traitsIJvEE12promise_type19unhandled_exceptionEv(
74 // THROWEND: invoke void @__cxa_end_catch()
75 // THROWEND-NEXT: to label %[[Cont:.+]] unwind
76 // NOTHROWEND: call void @__cxa_end_catch()
77 // NOTHROWEND-NEXT: br label %[[Cont2:.+]]
79 // THROWEND: [[Cont]]:
80 // THROWEND-NEXT: br label %[[Cont2:.+]]
82 // CHECK-NEXT: br label %[[Cleanup:.+]]
84 // CHECK: [[Cleanup]]:
85 // CHECK: call void @_ZNSt16coroutine_traitsIJvEE12promise_typeD1Ev(
86 // CHECK: %[[Mem0:.+]] = call ptr @llvm.coro.free(
87 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
88 // CHECK: call void @_ZdlPvm(ptr noundef %[[Mem0]], i64 noundef %[[SIZE]])
90 // CHECK: [[Dealloc]]:
91 // THROWEND: %[[Mem:.+]] = call ptr @llvm.coro.free(
92 // THROWEND: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
93 // THROWEND: call void @_ZdlPvm(ptr noundef %[[Mem]], i64 noundef %[[SIZE]])
98 // CHECK-LABEL: define{{.*}} void @_Z1gv(
101 co_await suspend_always
{};
102 // Since this is the endless loop there should be no fallthrough handler (call to 'return_void').
103 // CHECK-NOT: call void @_ZNSt16coroutine_traitsIJvEE12promise_type11return_voidEv