1 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-unknown-linux-gnu -fcoroutines-ts -std=c++14 \ 2 // RUN: -Wno-coroutine-missing-unhandled-exception -emit-llvm %s -o - -disable-llvm-passes \ 3 // RUN: | FileCheck %s 4 5 namespace std { 6 namespace experimental { 7 template <typename... T> 8 struct coroutine_traits; // expected-note {{declared here}} 9 10 template <class Promise = void> 11 struct coroutine_handle { 12 coroutine_handle() = default; 13 static coroutine_handle from_address(void *) noexcept { return {}; } 14 }; 15 16 template <> 17 struct coroutine_handle<void> { 18 static coroutine_handle from_address(void *) { return {}; } 19 coroutine_handle() = default; 20 template <class PromiseType> 21 coroutine_handle(coroutine_handle<PromiseType>) noexcept {} 22 }; 23 24 } // end namespace experimental 25 26 struct nothrow_t {}; 27 constexpr nothrow_t nothrow = {}; 28 29 } // end namespace std 30 31 // Required when get_return_object_on_allocation_failure() is defined by 32 // the promise. 33 using SizeT = decltype(sizeof(int)); 34 void *operator new(SizeT __sz, const std::nothrow_t &) noexcept; 35 void operator delete(void *__p, const std::nothrow_t &)noexcept; 36 37 struct suspend_always { 38 bool await_ready() noexcept { return false; } 39 void await_suspend(std::experimental::coroutine_handle<>) noexcept {} 40 void await_resume() noexcept {} 41 }; 42 43 struct global_new_delete_tag {}; 44 45 template <> 46 struct std::experimental::coroutine_traits<void, global_new_delete_tag> { 47 struct promise_type { 48 void get_return_object() {} 49 suspend_always initial_suspend() { return {}; } 50 suspend_always final_suspend() noexcept { return {}; } 51 void return_void() {} 52 }; 53 }; 54 55 // CHECK-LABEL: f0( 56 extern "C" void f0(global_new_delete_tag) { 57 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 58 // CHECK: %[[NeedAlloc:.+]] = call i1 @llvm.coro.alloc(token %[[ID]]) 59 // CHECK: br i1 %[[NeedAlloc]], label %[[AllocBB:.+]], label %[[InitBB:.+]] 60 61 // CHECK: [[AllocBB]]: 62 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 63 // CHECK: %[[MEM:.+]] = call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]]) 64 // CHECK: br label %[[InitBB]] 65 66 // CHECK: [[InitBB]]: 67 // CHECK: %[[PHI:.+]] = phi i8* [ null, %{{.+}} ], [ %call, %[[AllocBB]] ] 68 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(token %[[ID]], i8* %[[PHI]]) 69 70 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]]) 71 // CHECK: %[[NeedDealloc:.+]] = icmp ne i8* %[[MEM]], null 72 // CHECK: br i1 %[[NeedDealloc]], label %[[FreeBB:.+]], label %[[Afterwards:.+]] 73 74 // CHECK: [[FreeBB]]: 75 // CHECK: call void @_ZdlPv(i8* noundef %[[MEM]]) 76 // CHECK: br label %[[Afterwards]] 77 78 // CHECK: [[Afterwards]]: 79 // CHECK: ret void 80 co_return; 81 } 82 83 struct promise_new_tag {}; 84 85 template <> 86 struct std::experimental::coroutine_traits<void, promise_new_tag> { 87 struct promise_type { 88 void *operator new(unsigned long); 89 void get_return_object() {} 90 suspend_always initial_suspend() { return {}; } 91 suspend_always final_suspend() noexcept { return {}; } 92 void return_void() {} 93 }; 94 }; 95 96 // CHECK-LABEL: f1( 97 extern "C" void f1(promise_new_tag) { 98 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 99 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 100 // CHECK: call noundef i8* @_ZNSt12experimental16coroutine_traitsIJv15promise_new_tagEE12promise_typenwEm(i64 noundef %[[SIZE]]) 101 102 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin( 103 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]]) 104 // CHECK: call void @_ZdlPv(i8* noundef %[[MEM]]) 105 co_return; 106 } 107 108 struct promise_matching_placement_new_tag {}; 109 110 template <> 111 struct std::experimental::coroutine_traits<void, promise_matching_placement_new_tag, int, float, double> { 112 struct promise_type { 113 void *operator new(unsigned long, promise_matching_placement_new_tag, 114 int, float, double); 115 void get_return_object() {} 116 suspend_always initial_suspend() { return {}; } 117 suspend_always final_suspend() noexcept { return {}; } 118 void return_void() {} 119 }; 120 }; 121 122 // CHECK-LABEL: f1a( 123 extern "C" void f1a(promise_matching_placement_new_tag, int x, float y, double z) { 124 // CHECK: store i32 %x, i32* %x.addr, align 4 125 // CHECK: store float %y, float* %y.addr, align 4 126 // CHECK: store double %z, double* %z.addr, align 8 127 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 128 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 129 // CHECK: %[[INT:.+]] = load i32, i32* %x.addr, align 4 130 // CHECK: %[[FLOAT:.+]] = load float, float* %y.addr, align 4 131 // CHECK: %[[DOUBLE:.+]] = load double, double* %z.addr, align 8 132 // CHECK: call noundef i8* @_ZNSt12experimental16coroutine_traitsIJv34promise_matching_placement_new_tagifdEE12promise_typenwEmS1_ifd(i64 noundef %[[SIZE]], i32 noundef %[[INT]], float noundef %[[FLOAT]], double noundef %[[DOUBLE]]) 133 co_return; 134 } 135 136 // Declare a placement form operator new, such as the one described in 137 // C++ 18.6.1.3.1, which takes a void* argument. 138 void *operator new(SizeT __sz, void *__p) noexcept; 139 140 struct promise_matching_global_placement_new_tag {}; 141 struct dummy {}; 142 template <> 143 struct std::experimental::coroutine_traits<void, promise_matching_global_placement_new_tag, dummy *> { 144 struct promise_type { 145 void get_return_object() {} 146 suspend_always initial_suspend() { return {}; } 147 suspend_always final_suspend() noexcept { return {}; } 148 void return_void() {} 149 }; 150 }; 151 152 // A coroutine that takes a single pointer argument should not invoke this 153 // placement form operator. [dcl.fct.def.coroutine]/7 dictates that lookup for 154 // allocation functions matching the coroutine function's signature be done 155 // within the scope of the promise type's class. 156 // CHECK-LABEL: f1b( 157 extern "C" void f1b(promise_matching_global_placement_new_tag, dummy *) { 158 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64 159 co_return; 160 } 161 162 struct promise_delete_tag {}; 163 164 template <> 165 struct std::experimental::coroutine_traits<void, promise_delete_tag> { 166 struct promise_type { 167 void operator delete(void *); 168 void get_return_object() {} 169 suspend_always initial_suspend() { return {}; } 170 suspend_always final_suspend() noexcept { return {}; } 171 void return_void() {} 172 }; 173 }; 174 175 // CHECK-LABEL: f2( 176 extern "C" void f2(promise_delete_tag) { 177 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 178 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 179 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]]) 180 181 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin( 182 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]]) 183 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJv18promise_delete_tagEE12promise_typedlEPv(i8* noundef %[[MEM]]) 184 co_return; 185 } 186 187 struct promise_sized_delete_tag {}; 188 189 template <> 190 struct std::experimental::coroutine_traits<void, promise_sized_delete_tag> { 191 struct promise_type { 192 void operator delete(void *, unsigned long); 193 void get_return_object() {} 194 suspend_always initial_suspend() { return {}; } 195 suspend_always final_suspend() noexcept { return {}; } 196 void return_void() {} 197 }; 198 }; 199 200 // CHECK-LABEL: f3( 201 extern "C" void f3(promise_sized_delete_tag) { 202 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 203 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 204 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]]) 205 206 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin( 207 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]]) 208 // CHECK: %[[SIZE2:.+]] = call i64 @llvm.coro.size.i64() 209 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJv24promise_sized_delete_tagEE12promise_typedlEPvm(i8* noundef %[[MEM]], i64 noundef %[[SIZE2]]) 210 co_return; 211 } 212 213 struct promise_on_alloc_failure_tag {}; 214 215 template <> 216 struct std::experimental::coroutine_traits<int, promise_on_alloc_failure_tag> { 217 struct promise_type { 218 int get_return_object() { return 0; } 219 suspend_always initial_suspend() { return {}; } 220 suspend_always final_suspend() noexcept { return {}; } 221 void return_void() {} 222 static int get_return_object_on_allocation_failure() { return -1; } 223 }; 224 }; 225 226 // CHECK-LABEL: f4( 227 extern "C" int f4(promise_on_alloc_failure_tag) { 228 // CHECK: %[[RetVal:.+]] = alloca i32 229 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 230 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() 231 // CHECK: %[[MEM:.+]] = call noalias noundef i8* @_ZnwmRKSt9nothrow_t(i64 noundef %[[SIZE]], %"struct.std::nothrow_t"* noundef nonnull align 1 dereferenceable(1) @_ZStL7nothrow) 232 // CHECK: %[[OK:.+]] = icmp ne i8* %[[MEM]], null 233 // CHECK: br i1 %[[OK]], label %[[OKBB:.+]], label %[[ERRBB:.+]] 234 235 // CHECK: [[ERRBB]]: 236 // CHECK: %[[FailRet:.+]] = call noundef i32 @_ZNSt12experimental16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type39get_return_object_on_allocation_failureEv( 237 // CHECK: store i32 %[[FailRet]], i32* %[[RetVal]] 238 // CHECK: br label %[[RetBB:.+]] 239 240 // CHECK: [[OKBB]]: 241 // CHECK: %[[OkRet:.+]] = call noundef i32 @_ZNSt12experimental16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type17get_return_objectEv( 242 243 // CHECK: [[RetBB]]: 244 // CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]], align 4 245 // CHECK: ret i32 %[[LoadRet]] 246 co_return; 247 } 248