| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 \ |
| 2 |
// RUN: -Wno-coroutine-missing-unhandled-exception -emit-llvm %s -o - -disable-llvm-passes \ |
| 3 |
// RUN: | FileCheck %s |
| 4 |
|
| 5 |
namespace std { |
| 6 |
template <typename... T> |
| 7 |
struct coroutine_traits; // expected-note {{declared here}} |
| 8 |
|
| 9 |
template <class Promise = void> |
| 10 |
struct coroutine_handle { |
| 11 |
coroutine_handle() = default; |
| 12 |
static coroutine_handle from_address(void *) noexcept { return {}; } |
| 13 |
}; |
| 14 |
|
| 15 |
template <> |
| 16 |
struct coroutine_handle<void> { |
| 17 |
static coroutine_handle from_address(void *) { return {}; } |
| 18 |
coroutine_handle() = default; |
| 19 |
template <class PromiseType> |
| 20 |
coroutine_handle(coroutine_handle<PromiseType>) noexcept {} |
| 21 |
}; |
| 22 |
|
| 23 |
struct nothrow_t {}; |
| 24 |
constexpr nothrow_t nothrow = {}; |
| 25 |
|
| 26 |
} // end namespace std |
| 27 |
|
| 28 |
// Required when get_return_object_on_allocation_failure() is defined by |
| 29 |
// the promise. |
| 30 |
using SizeT = decltype(sizeof(int)); |
| 31 |
void* operator new(SizeT __sz, const std::nothrow_t&) noexcept; |
| 32 |
void operator delete(void* __p, const std::nothrow_t&) noexcept; |
| 33 |
|
| 34 |
|
| 35 |
struct suspend_always { |
| 36 |
bool await_ready() noexcept { return false; } |
| 37 |
void await_suspend(std::coroutine_handle<>) noexcept {} |
| 38 |
void await_resume() noexcept {} |
| 39 |
}; |
| 40 |
|
| 41 |
struct global_new_delete_tag {}; |
| 42 |
|
| 43 |
template <> |
| 44 |
struct std::coroutine_traits<void, global_new_delete_tag> { |
| 45 |
struct promise_type { |
| 46 |
void get_return_object() {} |
| 47 |
suspend_always initial_suspend() { return {}; } |
| 48 |
suspend_always final_suspend() noexcept { return {}; } |
| 49 |
void return_void() {} |
| 50 |
}; |
| 51 |
}; |
| 52 |
|
| 53 |
// CHECK-LABEL: f0( |
| 54 |
extern "C" void f0(global_new_delete_tag) { |
| 55 |
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 |
| 56 |
// CHECK: %[[NeedAlloc:.+]] = call i1 @llvm.coro.alloc(token %[[ID]]) |
| 57 |
// CHECK: br i1 %[[NeedAlloc]], label %[[AllocBB:.+]], label %[[InitBB:.+]] |
| 58 |
|
| 59 |
// CHECK: [[AllocBB]]: |
| 60 |
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() |
| 61 |
// CHECK: %[[MEM:.+]] = call noalias noundef nonnull ptr @_Znwm(i64 noundef %[[SIZE]]) |
| 62 |
// CHECK: br label %[[InitBB]] |
| 63 |
|
| 64 |
// CHECK: [[InitBB]]: |
| 65 |
// CHECK: %[[PHI:.+]] = phi ptr [ null, %{{.+}} ], [ %call, %[[AllocBB]] ] |
| 66 |
// CHECK: %[[FRAME:.+]] = call ptr @llvm.coro.begin(token %[[ID]], ptr %[[PHI]]) |
| 67 |
|
| 68 |
// CHECK: %[[MEM:.+]] = call ptr @llvm.coro.free(token %[[ID]], ptr %[[FRAME]]) |
| 69 |
// CHECK: %[[NeedDealloc:.+]] = icmp ne ptr %[[MEM]], null |
| 70 |
// CHECK: br i1 %[[NeedDealloc]], label %[[FreeBB:.+]], label %[[Afterwards:.+]] |
| 71 |
|
| 72 |
// CHECK: [[FreeBB]]: |
| 73 |
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() |
| 74 |
// CHECK: call void @_ZdlPvm(ptr noundef %[[MEM]], i64 noundef %[[SIZE]]) |
| 75 |
// CHECK: br label %[[Afterwards]] |
| 76 |
|
| 77 |
// CHECK: [[Afterwards]]: |
| 78 |
// CHECK: ret void |
| 79 |
co_return; |
| 80 |
} |
| 81 |
|
| 82 |
struct promise_new_tag {}; |
| 83 |
|
| 84 |
template <> |
| 85 |
struct std::coroutine_traits<void, promise_new_tag> { |
| 86 |
struct promise_type { |
| 87 |
void *operator new(unsigned long); |
| 88 |
void get_return_object() {} |
| 89 |
suspend_always initial_suspend() { return {}; } |
| 90 |
suspend_always final_suspend() noexcept { return {}; } |
| 91 |
void return_void() {} |
| 92 |
}; |
| 93 |
}; |
| 94 |
|
| 95 |
// CHECK-LABEL: f1( |
| 96 |
extern "C" void f1(promise_new_tag ) { |
| 97 |
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 |
| 98 |
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() |
| 99 |
// CHECK: call noundef ptr @_ZNSt16coroutine_traitsIJv15promise_new_tagEE12promise_typenwEm(i64 noundef %[[SIZE]]) |
| 100 |
|
| 101 |
// CHECK: %[[FRAME:.+]] = call ptr @llvm.coro.begin( |
| 102 |
// CHECK: %[[MEM:.+]] = call ptr @llvm.coro.free(token %[[ID]], ptr %[[FRAME]]) |
| 103 |
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() |
| 104 |
// CHECK: call void @_ZdlPvm(ptr noundef %[[MEM]], i64 noundef %[[SIZE]]) |
| 105 |
co_return; |
| 106 |
} |
| 107 |
|
| 108 |
struct promise_matching_placement_new_tag {}; |
| 109 |
|
| 110 |
template <> |
| 111 |
struct std::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, ptr %x.addr, align 4 |
| 125 |
// CHECK: store float %y, ptr %y.addr, align 4 |
| 126 |
// CHECK: store double %z, ptr %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, ptr %x.addr, align 4 |
| 130 |
// CHECK: %[[FLOAT:.+]] = load float, ptr %y.addr, align 4 |
| 131 |
// CHECK: %[[DOUBLE:.+]] = load double, ptr %z.addr, align 8 |
| 132 |
// CHECK: call noundef ptr @_ZNSt16coroutine_traitsIJv34promise_matching_placement_new_tagifdEE12promise_typenwEmS0_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 ptr 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::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 ptr @_Znwm(i64 |
| 159 |
co_return; |
| 160 |
} |
| 161 |
|
| 162 |
struct promise_delete_tag {}; |
| 163 |
|
| 164 |
template <> |
| 165 |
struct std::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 ptr @_Znwm(i64 noundef %[[SIZE]]) |
| 180 |
|
| 181 |
// CHECK: %[[FRAME:.+]] = call ptr @llvm.coro.begin( |
| 182 |
// CHECK: %[[MEM:.+]] = call ptr @llvm.coro.free(token %[[ID]], ptr %[[FRAME]]) |
| 183 |
// CHECK: call void @_ZNSt16coroutine_traitsIJv18promise_delete_tagEE12promise_typedlEPv(ptr noundef %[[MEM]]) |
| 184 |
co_return; |
| 185 |
} |
| 186 |
|
| 187 |
struct promise_sized_delete_tag {}; |
| 188 |
|
| 189 |
template <> |
| 190 |
struct std::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 ptr @_Znwm(i64 noundef %[[SIZE]]) |
| 205 |
|
| 206 |
// CHECK: %[[FRAME:.+]] = call ptr @llvm.coro.begin( |
| 207 |
// CHECK: %[[MEM:.+]] = call ptr @llvm.coro.free(token %[[ID]], ptr %[[FRAME]]) |
| 208 |
// CHECK: %[[SIZE2:.+]] = call i64 @llvm.coro.size.i64() |
| 209 |
// CHECK: call void @_ZNSt16coroutine_traitsIJv24promise_sized_delete_tagEE12promise_typedlEPvm(ptr noundef %[[MEM]], i64 noundef %[[SIZE2]]) |
| 210 |
co_return; |
| 211 |
} |
| 212 |
|
| 213 |
struct promise_on_alloc_failure_tag {}; |
| 214 |
|
| 215 |
template <> |
| 216 |
struct std::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 ptr @_ZnwmRKSt9nothrow_t(i64 noundef %[[SIZE]], ptr noundef nonnull align 1 dereferenceable(1) @_ZStL7nothrow) |
| 232 |
// CHECK: %[[OK:.+]] = icmp ne ptr %[[MEM]], null |
| 233 |
// CHECK: call i1 @llvm.expect.i1(i1 %[[OK]], i1 true) |
| 234 |
// CHECK: br i1 %[[OK]], label %[[OKBB:.+]], label %[[ERRBB:.+]] |
| 235 |
|
| 236 |
// CHECK: [[ERRBB]]: |
| 237 |
// CHECK: %[[FailRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type39get_return_object_on_allocation_failureEv( |
| 238 |
// CHECK: store i32 %[[FailRet]], ptr %[[RetVal]] |
| 239 |
// CHECK: br label %[[RetBB:.+]] |
| 240 |
|
| 241 |
// CHECK: [[OKBB]]: |
| 242 |
// CHECK: %[[OkRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type17get_return_objectEv( |
| 243 |
|
| 244 |
// CHECK: [[RetBB]]: |
| 245 |
// CHECK: %[[LoadRet:.+]] = load i32, ptr %[[RetVal]], align 4 |
| 246 |
// CHECK: ret i32 %[[LoadRet]] |
| 247 |
co_return; |
| 248 |
} |
| 249 |
|
| 250 |
struct promise_sized_delete_tag2 {}; |
| 251 |
|
| 252 |
template <> |
| 253 |
struct std::coroutine_traits<void, promise_sized_delete_tag2> { |
| 254 |
struct promise_type { |
| 255 |
// Tests that the compiler can choose the correct operator delete |
| 256 |
// when we have multiple operator delete |
| 257 |
void operator delete(void*, unsigned long); |
| 258 |
void operator delete(void*); |
| 259 |
void get_return_object() {} |
| 260 |
suspend_always initial_suspend() { return {}; } |
| 261 |
suspend_always final_suspend() noexcept { return {}; } |
| 262 |
void return_void() {} |
| 263 |
}; |
| 264 |
}; |
| 265 |
|
| 266 |
// CHECK-LABEL: f5( |
| 267 |
extern "C" void f5(promise_sized_delete_tag2) { |
| 268 |
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16 |
| 269 |
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64() |
| 270 |
// CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef %[[SIZE]]) |
| 271 |
|
| 272 |
// CHECK: %[[FRAME:.+]] = call ptr @llvm.coro.begin( |
| 273 |
// CHECK: %[[MEM:.+]] = call ptr @llvm.coro.free(token %[[ID]], ptr %[[FRAME]]) |
| 274 |
// CHECK: %[[SIZE2:.+]] = call i64 @llvm.coro.size.i64() |
| 275 |
// CHECK: call void @_ZNSt16coroutine_traitsIJv25promise_sized_delete_tag2EE12promise_typedlEPvm(ptr noundef %[[MEM]], i64 noundef %[[SIZE2]]) |
| 276 |
co_return; |
| 277 |
} |