1 // Verify that coroutine promise and allocated memory are freed up on exception.
2 // RUN: %clang_cc1 -no-opaque-pointers -std=c++1z -fcoroutines-ts -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -disable-llvm-passes | FileCheck %s
3 
4 namespace std::experimental {
5 template <typename... T> struct coroutine_traits;
6 
7 template <class Promise = void> struct coroutine_handle {
8   coroutine_handle() = default;
9   static coroutine_handle from_address(void *) noexcept;
10 };
11 template <> struct coroutine_handle<void> {
12   static coroutine_handle from_address(void *) noexcept;
13   coroutine_handle() = default;
14   template <class PromiseType>
15   coroutine_handle(coroutine_handle<PromiseType>) noexcept;
16 };
17 } // namespace std::experimental
18 
19 struct suspend_always {
20   bool await_ready() noexcept;
21   void await_suspend(std::experimental::coroutine_handle<>) noexcept;
22   void await_resume() noexcept;
23 };
24 
25 template <> struct std::experimental::coroutine_traits<void> {
26   struct promise_type {
27     void get_return_object() noexcept;
28     suspend_always initial_suspend() noexcept;
29     suspend_always final_suspend() noexcept;
30     void return_void() noexcept;
31     promise_type();
32     ~promise_type();
33     void unhandled_exception() noexcept;
34   };
35 };
36 
37 struct Cleanup {
38   ~Cleanup();
39 };
40 void may_throw();
41 
42 // CHECK-LABEL: define{{.*}} void @_Z1fv(
43 void f() {
44   // CHECK: call noalias noundef nonnull i8* @_Znwm(i64
45 
46   // If promise constructor throws, check that we free the memory.
47 
48   // CHECK: invoke void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeC1Ev(
49   // CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]]
50 
51   // CHECK: [[DeallocPad]]:
52   // CHECK-NEXT: landingpad
53   // CHECK-NEXT:   cleanup
54   // CHECK: br label %[[Dealloc:.+]]
55 
56   Cleanup cleanup;
57   may_throw();
58 
59   // if may_throw throws, check that we destroy the promise and free the memory.
60 
61   // CHECK: invoke void @_Z9may_throwv(
62   // CHECK-NEXT: to label %{{.+}} unwind label %[[CatchPad:.+]]
63 
64   // CHECK: [[CatchPad]]:
65   // CHECK-NEXT:  landingpad
66   // CHECK-NEXT:       catch i8* null
67   // CHECK:  call void @_ZN7CleanupD1Ev(
68   // CHECK:  br label %[[Catch:.+]]
69 
70   // CHECK: [[Catch]]:
71   // CHECK:    call i8* @__cxa_begin_catch(
72   // CHECK:    call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_type19unhandled_exceptionEv(
73   // CHECK:    invoke void @__cxa_end_catch()
74   // CHECK-NEXT:    to label %[[Cont:.+]] unwind
75 
76   // CHECK: [[Cont]]:
77   // CHECK-NEXT: br label %[[Cont2:.+]]
78   // CHECK: [[Cont2]]:
79   // CHECK-NEXT: br label %[[Cleanup:.+]]
80 
81   // CHECK: [[Cleanup]]:
82   // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeD1Ev(
83   // CHECK: %[[Mem0:.+]] = call i8* @llvm.coro.free(
84   // CHECK: call void @_ZdlPv(i8* noundef %[[Mem0]]
85 
86   // CHECK: [[Dealloc]]:
87   // CHECK:   %[[Mem:.+]] = call i8* @llvm.coro.free(
88   // CHECK:   call void @_ZdlPv(i8* noundef %[[Mem]])
89 
90   co_return;
91 }
92 
93 // CHECK-LABEL: define{{.*}} void @_Z1gv(
94 void g() {
95   for (;;)
96     co_await suspend_always{};
97   // Since this is the endless loop there should be no fallthrough handler (call to 'return_void').
98   // CHECK-NOT: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_type11return_voidEv
99 }
100