• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
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 { ~Cleanup(); };
44 void doSomething() noexcept;
45 
46 // CHECK: define i32 @_Z1fv(
f()47 int f() {
48   // CHECK: %[[RetVal:.+]] = alloca i32
49   // CHECK: %[[GroActive:.+]] = alloca i1
50 
51   // CHECK: %[[Size:.+]] = call i64 @llvm.coro.size.i64()
52   // CHECK: call noalias nonnull i8* @_Znwm(i64 %[[Size]])
53   // CHECK: store i1 false, i1* %[[GroActive]]
54   // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeC1Ev(
55   // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type17get_return_objectEv(
56   // CHECK: store i1 true, i1* %[[GroActive]]
57 
58   Cleanup cleanup;
59   doSomething();
60   co_return;
61 
62   // CHECK: call void @_Z11doSomethingv(
63   // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type11return_voidEv(
64   // CHECK: call void @_ZN7CleanupD1Ev(
65 
66   // Destroy promise and free the memory.
67 
68   // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeD1Ev(
69   // CHECK: %[[Mem:.+]] = call i8* @llvm.coro.free(
70   // CHECK: call void @_ZdlPv(i8* %[[Mem]])
71 
72   // Initialize retval from Gro and destroy Gro
73 
74   // CHECK: %[[Conv:.+]] = call i32 @_ZN7GroTypecviEv(
75   // CHECK: store i32 %[[Conv]], i32* %[[RetVal]]
76   // CHECK: %[[IsActive:.+]] = load i1, i1* %[[GroActive]]
77   // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]]
78 
79   // CHECK: [[CleanupGro]]:
80   // CHECK:   call void @_ZN7GroTypeD1Ev(
81   // CHECK:   br label %[[Done]]
82 
83   // CHECK: [[Done]]:
84   // CHECK:   %[[LoadRet:.+]] = load i32, i32* %[[RetVal]]
85   // CHECK:   ret i32 %[[LoadRet]]
86 }
87