105 lines · cpp
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,THROWEND3// 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,NOTHROWEND4 5namespace std {6template <typename... T> struct coroutine_traits;7 8template <class Promise = void> struct coroutine_handle {9 coroutine_handle() = default;10 static coroutine_handle from_address(void *) noexcept;11};12template <> 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 std19 20struct suspend_always {21 bool await_ready() noexcept;22 void await_suspend(std::coroutine_handle<>) noexcept;23 void await_resume() noexcept;24};25 26template <> 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 38struct Cleanup { ~Cleanup(); };39void may_throw();40 41// CHECK-LABEL: define{{.*}} void @_Z1fv(42void f() {43 // CHECK: call noalias noundef nonnull ptr @_Znwm(i6444 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: landingpad52 // CHECK-NEXT: cleanup53 // THROWEND: br label %[[Dealloc:.+]]54 // NOTHROWEND: icmp ne ptr %[[#]], null55 // NOTHROWEND-NEXT: br i1 %[[#]], label %[[Dealloc:.+]], label56 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: landingpad67 // CHECK-NEXT: catch ptr null68 // 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:.+]] unwind76 // 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(99void 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_voidEv104}105