1 // Verifies lifetime of __gro local variable 2 // Verify that coroutine promise and allocated memory are freed up on exception. 3 // RUN: %clang_cc1 -std=c++1z -fcoroutines-ts -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes | FileCheck %s 4 5 namespace std::experimental { 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::experimental 19 20 struct suspend_always { 21 bool await_ready() noexcept; 22 void await_suspend(std::experimental::coroutine_handle<>) noexcept; 23 void await_resume() noexcept; 24 }; 25 26 struct GroType { 27 ~GroType(); 28 operator int() noexcept; 29 }; 30 31 template <> struct std::experimental::coroutine_traits<int> { 32 struct promise_type { 33 GroType get_return_object() noexcept; 34 suspend_always initial_suspend() noexcept; 35 suspend_always final_suspend() noexcept; 36 void return_void() noexcept; 37 promise_type(); 38 ~promise_type(); 39 void unhandled_exception() noexcept; 40 }; 41 }; 42 43 struct Cleanup { 44 ~Cleanup(); 45 }; 46 void doSomething() noexcept; 47 48 // CHECK: define{{.*}} i32 @_Z1fv( 49 int f() { 50 // CHECK: %[[RetVal:.+]] = alloca i32 51 // CHECK: %[[GroActive:.+]] = alloca i1 52 53 // CHECK: %[[Size:.+]] = call i64 @llvm.coro.size.i64() 54 // CHECK: call noalias nonnull i8* @_Znwm(i64 %[[Size]]) 55 // CHECK: store i1 false, i1* %[[GroActive]] 56 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeC1Ev( 57 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type17get_return_objectEv( 58 // CHECK: store i1 true, i1* %[[GroActive]] 59 60 Cleanup cleanup; 61 doSomething(); 62 co_return; 63 64 // CHECK: call void @_Z11doSomethingv( 65 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type11return_voidEv( 66 // CHECK: call void @_ZN7CleanupD1Ev( 67 68 // Destroy promise and free the memory. 69 70 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeD1Ev( 71 // CHECK: %[[Mem:.+]] = call i8* @llvm.coro.free( 72 // CHECK: call void @_ZdlPv(i8* %[[Mem]]) 73 74 // Initialize retval from Gro and destroy Gro 75 76 // CHECK: %[[Conv:.+]] = call i32 @_ZN7GroTypecviEv( 77 // CHECK: store i32 %[[Conv]], i32* %[[RetVal]] 78 // CHECK: %[[IsActive:.+]] = load i1, i1* %[[GroActive]] 79 // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]] 80 81 // CHECK: [[CleanupGro]]: 82 // CHECK: call void @_ZN7GroTypeD1Ev( 83 // CHECK: br label %[[Done]] 84 85 // CHECK: [[Done]]: 86 // CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]] 87 // CHECK: ret i32 %[[LoadRet]] 88 } 89