| 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 |
| 4 |
|
| 5 |
namespace std { |
| 6 |
template <typename... T> struct coroutine_traits; |
| 7 |
|
| 8 |
template <class Promise = void> struct coroutine_handle { |
| 9 |
coroutine_handle() = default; |
| 10 |
static coroutine_handle from_address(void *) noexcept; |
| 11 |
}; |
| 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; |
| 17 |
}; |
| 18 |
} // namespace std |
| 19 |
|
| 20 |
struct suspend_always { |
| 21 |
bool await_ready() noexcept; |
| 22 |
void await_suspend(std::coroutine_handle<>) noexcept; |
| 23 |
void await_resume() noexcept; |
| 24 |
}; |
| 25 |
|
| 26 |
template <> struct std::coroutine_traits<void> { |
| 27 |
struct promise_type { |
| 28 |
void get_return_object() noexcept; |
| 29 |
suspend_always initial_suspend() noexcept; |
| 30 |
suspend_always final_suspend() noexcept; |
| 31 |
void return_void() noexcept; |
| 32 |
promise_type(); |
| 33 |
~promise_type(); |
| 34 |
void unhandled_exception() noexcept; |
| 35 |
}; |
| 36 |
}; |
| 37 |
|
| 38 |
struct Cleanup { ~Cleanup(); }; |
| 39 |
void may_throw(); |
| 40 |
|
| 41 |
// CHECK-LABEL: define{{.*}} void @_Z1fv( |
| 42 |
void f() { |
| 43 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 |
| 44 |
|
| 45 |
// If promise constructor throws, check that we free the memory. |
| 46 |
|
| 47 |
// CHECK: invoke void @_ZNSt16coroutine_traitsIJvEE12promise_typeC1Ev( |
| 48 |
// CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]] |
| 49 |
|
| 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 |
| 56 |
|
| 57 |
Cleanup cleanup; |
| 58 |
may_throw(); |
| 59 |
|
| 60 |
// if may_throw throws, check that we destroy the promise and free the memory. |
| 61 |
|
| 62 |
// CHECK: invoke void @_Z9may_throwv( |
| 63 |
// CHECK-NEXT: to label %{{.+}} unwind label %[[CatchPad:.+]] |
| 64 |
|
| 65 |
// CHECK: [[CatchPad]]: |
| 66 |
// CHECK-NEXT: landingpad |
| 67 |
// CHECK-NEXT: catch ptr null |
| 68 |
// CHECK: call void @_ZN7CleanupD1Ev( |
| 69 |
// CHECK: br label %[[Catch:.+]] |
| 70 |
|
| 71 |
// CHECK: [[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:.+]] |
| 78 |
|
| 79 |
// THROWEND: [[Cont]]: |
| 80 |
// THROWEND-NEXT: br label %[[Cont2:.+]] |
| 81 |
// CHECK: [[Cont2]]: |
| 82 |
// CHECK-NEXT: br label %[[Cleanup:.+]] |
| 83 |
|
| 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]]) |
| 89 |
|
| 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]]) |
| 94 |
|
| 95 |
co_return; |
| 96 |
} |
| 97 |
|
| 98 |
// CHECK-LABEL: define{{.*}} void @_Z1gv( |
| 99 |
void g() { |
| 100 |
for (;;) |
| 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 |
| 104 |
} |