Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCoroutines / coro-gro.cpp
blobd4c3ff589e340a85f8ce79bb210ad4eeb2e529d5
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"
7 using namespace std;
9 struct GroType {
10 ~GroType();
11 operator int() noexcept;
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;
26 struct Cleanup { ~Cleanup(); };
27 void doSomething() noexcept;
29 // CHECK: define{{.*}} i32 @_Z1fv(
30 int f() {
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]]
42 Cleanup cleanup;
43 doSomething();
44 co_return;
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: call void @_ZdlPv(ptr noundef %[[Mem]])
56 // Initialize retval from Gro and destroy Gro
57 // Note this also tests delaying initialization when Gro and function return
58 // types mismatch (see cwg2563).
60 // CHECK: %[[Conv:.+]] = call noundef i32 @_ZN7GroTypecviEv(
61 // CHECK: store i32 %[[Conv]], ptr %[[RetVal]]
62 // CHECK: %[[IsActive:.+]] = load i1, ptr %[[GroActive]]
63 // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]]
65 // CHECK: [[CleanupGro]]:
66 // CHECK: call void @_ZN7GroTypeD1Ev(
67 // CHECK: br label %[[Done]]
69 // CHECK: [[Done]]:
70 // CHECK: %[[LoadRet:.+]] = load i32, ptr %[[RetVal]]
71 // CHECK: ret i32 %[[LoadRet]]
74 class invoker {
75 public:
76 class invoker_promise {
77 public:
78 invoker get_return_object() { return invoker{}; }
79 auto initial_suspend() { return suspend_always{}; }
80 auto final_suspend() noexcept { return suspend_always{}; }
81 void return_void() {}
82 void unhandled_exception() {}
84 using promise_type = invoker_promise;
85 invoker() {}
86 invoker(const invoker &) = delete;
87 invoker &operator=(const invoker &) = delete;
88 invoker(invoker &&) = delete;
89 invoker &operator=(invoker &&) = delete;
92 // According to cwg2563, matching GRO and function return type must allow
93 // for eager initialization and RVO.
94 // CHECK: define{{.*}} void @_Z1gv({{.*}} %[[AggRes:.+]])
95 invoker g() {
96 // CHECK: %[[ResultPtr:.+]] = alloca ptr
97 // CHECK-NEXT: %[[Promise:.+]] = alloca %"class.invoker::invoker_promise"
99 // CHECK: store ptr %[[AggRes]], ptr %[[ResultPtr]]
100 // CHECK: coro.init:
101 // CHECK: = call ptr @llvm.coro.begin
103 // delayed GRO pattern stores a GRO active flag, make sure to not emit it.
104 // CHECK-NOT: store i1 false, ptr
105 // CHECK: call void @_ZN7invoker15invoker_promise17get_return_objectEv({{.*}} %[[AggRes]]
106 co_return;
108 // CHECK: ![[OutFrameMetadata]] = !{}