xref: /llvm-project/clang/test/CodeGenCoroutines/coro-gro.cpp (revision 130e93cc26ca9d3ac50ec5a92e3109577ca2e702)
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
4 
5 #include "Inputs/coroutine.h"
6 
7 using namespace std;
8 
9 struct GroType {
10   ~GroType();
11   operator int() noexcept;
12 };
13 
14 template <> struct std::coroutine_traits<int> {
15   struct promise_type {
16     GroType get_return_object() noexcept;
17     suspend_always initial_suspend() noexcept;
18     suspend_always final_suspend() noexcept;
19     void return_void() noexcept;
20     promise_type();
21     ~promise_type();
22     void unhandled_exception() noexcept;
23   };
24 };
25 
26 struct Cleanup { ~Cleanup(); };
27 void doSomething() noexcept;
28 
29 // CHECK: define{{.*}} i32 @_Z1fv(
f()30 int f() {
31   // CHECK: %[[RetVal:.+]] = alloca i32
32   // CHECK: %[[GroActive:.+]] = alloca i1
33   // CHECK: %[[CoroGro:.+]] = alloca %struct.GroType, {{.*}} !coro.outside.frame ![[OutFrameMetadata:.+]]
34 
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]]
41 
42   Cleanup cleanup;
43   doSomething();
44   co_return;
45 
46   // CHECK: call void @_Z11doSomethingv(
47   // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_type11return_voidEv(
48   // CHECK: call void @_ZN7CleanupD1Ev(
49 
50   // Destroy promise and free the memory.
51 
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]])
56 
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).
60 
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:.+]]
65 
66   // CHECK: [[CleanupGro]]:
67   // CHECK:   call void @_ZN7GroTypeD1Ev(
68   // CHECK:   br label %[[Done]]
69 
70   // CHECK: [[Done]]:
71   // CHECK:   %[[LoadRet:.+]] = load i32, ptr %[[RetVal]]
72   // CHECK:   ret i32 %[[LoadRet]]
73 }
74 
75 class invoker {
76 public:
77   class invoker_promise {
78   public:
get_return_object()79     invoker get_return_object() { return invoker{}; }
initial_suspend()80     auto initial_suspend() { return suspend_always{}; }
final_suspend()81     auto final_suspend() noexcept { return suspend_always{}; }
return_void()82     void return_void() {}
unhandled_exception()83     void unhandled_exception() {}
84   };
85   using promise_type = invoker_promise;
invoker()86   invoker() {}
87   invoker(const invoker &) = delete;
88   invoker &operator=(const invoker &) = delete;
89   invoker(invoker &&) = delete;
90   invoker &operator=(invoker &&) = delete;
91 };
92 
93 // According to cwg2563, matching GRO and function return type must allow
94 // for eager initialization and RVO.
95 // CHECK: define{{.*}} void @_Z1gv({{.*}} %[[AggRes:.+]])
g()96 invoker g() {
97   // CHECK: %[[ResultPtr:.+]] = alloca ptr
98   // CHECK-NEXT: %[[Promise:.+]] = alloca %"class.invoker::invoker_promise"
99 
100   // CHECK: store ptr %[[AggRes]], ptr %[[ResultPtr]]
101   // CHECK: coro.init:
102   // CHECK: = call ptr @llvm.coro.begin
103 
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]]
107   co_return;
108 }
109 // CHECK: ![[OutFrameMetadata]] = !{}