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++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes | FileCheck %s
5 #include "Inputs/coroutine.h"
11 operator int() noexcept
;
14 template <> struct std::coroutine_traits
<int> {
16 GroType
get_return_object() noexcept
;
17 suspend_always
initial_suspend() noexcept
;
18 suspend_always
final_suspend() noexcept
;
19 void return_void() noexcept
;
22 void unhandled_exception() noexcept
;
26 struct Cleanup
{ ~Cleanup(); };
27 void doSomething() noexcept
;
29 // CHECK: define{{.*}} i32 @_Z1fv(
31 // CHECK: %[[RetVal:.+]] = alloca i32
32 // CHECK: %[[GroActive:.+]] = alloca i1
33 // CHECK: %[[CoroGro:.+]] = alloca %struct.GroType, {{.*}} !coro.outside.frame ![[OutFrameMetadata:.+]]
35 // CHECK: %[[Size:.+]] = call i64 @llvm.coro.size.i64()
36 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef %[[Size]])
37 // CHECK: store i1 false, ptr %[[GroActive]]
38 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_typeC1Ev(
39 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_type17get_return_objectEv({{.*}} %[[CoroGro]]
40 // CHECK: store i1 true, ptr %[[GroActive]]
46 // CHECK: call void @_Z11doSomethingv(
47 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_type11return_voidEv(
48 // CHECK: call void @_ZN7CleanupD1Ev(
50 // Destroy promise and free the memory.
52 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_typeD1Ev(
53 // CHECK: %[[Mem:.+]] = call ptr @llvm.coro.free(
54 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
55 // CHECK: call void @_ZdlPvm(ptr noundef %[[Mem]], i64 noundef %[[SIZE]])
57 // Initialize retval from Gro and destroy Gro
58 // Note this also tests delaying initialization when Gro and function return
59 // types mismatch (see cwg2563).
61 // CHECK: %[[Conv:.+]] = call noundef i32 @_ZN7GroTypecviEv(
62 // CHECK: store i32 %[[Conv]], ptr %[[RetVal]]
63 // CHECK: %[[IsActive:.+]] = load i1, ptr %[[GroActive]]
64 // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]]
66 // CHECK: [[CleanupGro]]:
67 // CHECK: call void @_ZN7GroTypeD1Ev(
68 // CHECK: br label %[[Done]]
71 // CHECK: %[[LoadRet:.+]] = load i32, ptr %[[RetVal]]
72 // CHECK: ret i32 %[[LoadRet]]
77 class invoker_promise
{
79 invoker
get_return_object() { return invoker
{}; }
80 auto initial_suspend() { return suspend_always
{}; }
81 auto final_suspend() noexcept
{ return suspend_always
{}; }
83 void unhandled_exception() {}
85 using promise_type
= invoker_promise
;
87 invoker(const invoker
&) = delete;
88 invoker
&operator=(const invoker
&) = delete;
89 invoker(invoker
&&) = delete;
90 invoker
&operator=(invoker
&&) = delete;
93 // According to cwg2563, matching GRO and function return type must allow
94 // for eager initialization and RVO.
95 // CHECK: define{{.*}} void @_Z1gv({{.*}} %[[AggRes:.+]])
97 // CHECK: %[[ResultPtr:.+]] = alloca ptr
98 // CHECK-NEXT: %[[Promise:.+]] = alloca %"class.invoker::invoker_promise"
100 // CHECK: store ptr %[[AggRes]], ptr %[[ResultPtr]]
102 // CHECK: = call ptr @llvm.coro.begin
104 // delayed GRO pattern stores a GRO active flag, make sure to not emit it.
105 // CHECK-NOT: store i1 false, ptr
106 // CHECK: call void @_ZN7invoker15invoker_promise17get_return_objectEv({{.*}} %[[AggRes]]
109 // CHECK: ![[OutFrameMetadata]] = !{}