[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / CodeGenCoroutines / coro-alloc.cpp
blobadcf7a1254f60ccf5792cd3e11e970874e3d9757
1 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-unknown-linux-gnu -std=c++20 \
2 // RUN: -Wno-coroutine-missing-unhandled-exception -emit-llvm %s -o - -disable-llvm-passes \
3 // RUN: | FileCheck %s
5 namespace std {
6 template <typename... T>
7 struct coroutine_traits; // expected-note {{declared here}}
9 template <class Promise = void>
10 struct coroutine_handle {
11 coroutine_handle() = default;
12 static coroutine_handle from_address(void *) noexcept { return {}; }
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 {}
23 struct nothrow_t {};
24 constexpr nothrow_t nothrow = {};
26 } // end namespace std
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;
35 struct suspend_always {
36 bool await_ready() noexcept { return false; }
37 void await_suspend(std::coroutine_handle<>) noexcept {}
38 void await_resume() noexcept {}
41 struct global_new_delete_tag {};
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() {}
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:.+]]
59 // CHECK: [[AllocBB]]:
60 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
61 // CHECK: %[[MEM:.+]] = call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
62 // CHECK: br label %[[InitBB]]
64 // CHECK: [[InitBB]]:
65 // CHECK: %[[PHI:.+]] = phi i8* [ null, %{{.+}} ], [ %call, %[[AllocBB]] ]
66 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(token %[[ID]], i8* %[[PHI]])
68 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
69 // CHECK: %[[NeedDealloc:.+]] = icmp ne i8* %[[MEM]], null
70 // CHECK: br i1 %[[NeedDealloc]], label %[[FreeBB:.+]], label %[[Afterwards:.+]]
72 // CHECK: [[FreeBB]]:
73 // CHECK: call void @_ZdlPv(i8* noundef %[[MEM]])
74 // CHECK: br label %[[Afterwards]]
76 // CHECK: [[Afterwards]]:
77 // CHECK: ret void
78 co_return;
81 struct promise_new_tag {};
83 template <>
84 struct std::coroutine_traits<void, promise_new_tag> {
85 struct promise_type {
86 void *operator new(unsigned long);
87 void get_return_object() {}
88 suspend_always initial_suspend() { return {}; }
89 suspend_always final_suspend() noexcept { return {}; }
90 void return_void() {}
94 // CHECK-LABEL: f1(
95 extern "C" void f1(promise_new_tag ) {
96 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
97 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
98 // CHECK: call noundef i8* @_ZNSt16coroutine_traitsIJv15promise_new_tagEE12promise_typenwEm(i64 noundef %[[SIZE]])
100 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
101 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
102 // CHECK: call void @_ZdlPv(i8* noundef %[[MEM]])
103 co_return;
106 struct promise_matching_placement_new_tag {};
108 template <>
109 struct std::coroutine_traits<void, promise_matching_placement_new_tag, int, float, double> {
110 struct promise_type {
111 void *operator new(unsigned long, promise_matching_placement_new_tag,
112 int, float, double);
113 void get_return_object() {}
114 suspend_always initial_suspend() { return {}; }
115 suspend_always final_suspend() noexcept { return {}; }
116 void return_void() {}
120 // CHECK-LABEL: f1a(
121 extern "C" void f1a(promise_matching_placement_new_tag, int x, float y , double z) {
122 // CHECK: store i32 %x, i32* %x.addr, align 4
123 // CHECK: store float %y, float* %y.addr, align 4
124 // CHECK: store double %z, double* %z.addr, align 8
125 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
126 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
127 // CHECK: %[[INT:.+]] = load i32, i32* %x.addr, align 4
128 // CHECK: %[[FLOAT:.+]] = load float, float* %y.addr, align 4
129 // CHECK: %[[DOUBLE:.+]] = load double, double* %z.addr, align 8
130 // CHECK: call noundef i8* @_ZNSt16coroutine_traitsIJv34promise_matching_placement_new_tagifdEE12promise_typenwEmS0_ifd(i64 noundef %[[SIZE]], i32 noundef %[[INT]], float noundef %[[FLOAT]], double noundef %[[DOUBLE]])
131 co_return;
134 // Declare a placement form operator new, such as the one described in
135 // C++ 18.6.1.3.1, which takes a void* argument.
136 void* operator new(SizeT __sz, void *__p) noexcept;
138 struct promise_matching_global_placement_new_tag {};
139 struct dummy {};
140 template <>
141 struct std::coroutine_traits<void, promise_matching_global_placement_new_tag, dummy *> {
142 struct promise_type {
143 void get_return_object() {}
144 suspend_always initial_suspend() { return {}; }
145 suspend_always final_suspend() noexcept { return {}; }
146 void return_void() {}
150 // A coroutine that takes a single pointer argument should not invoke this
151 // placement form operator. [dcl.fct.def.coroutine]/7 dictates that lookup for
152 // allocation functions matching the coroutine function's signature be done
153 // within the scope of the promise type's class.
154 // CHECK-LABEL: f1b(
155 extern "C" void f1b(promise_matching_global_placement_new_tag, dummy *) {
156 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64
157 co_return;
160 struct promise_delete_tag {};
162 template <>
163 struct std::coroutine_traits<void, promise_delete_tag> {
164 struct promise_type {
165 void operator delete(void*);
166 void get_return_object() {}
167 suspend_always initial_suspend() { return {}; }
168 suspend_always final_suspend() noexcept { return {}; }
169 void return_void() {}
173 // CHECK-LABEL: f2(
174 extern "C" void f2(promise_delete_tag) {
175 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
176 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
177 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
179 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
180 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
181 // CHECK: call void @_ZNSt16coroutine_traitsIJv18promise_delete_tagEE12promise_typedlEPv(i8* noundef %[[MEM]])
182 co_return;
185 struct promise_sized_delete_tag {};
187 template <>
188 struct std::coroutine_traits<void, promise_sized_delete_tag> {
189 struct promise_type {
190 void operator delete(void*, unsigned long);
191 void get_return_object() {}
192 suspend_always initial_suspend() { return {}; }
193 suspend_always final_suspend() noexcept { return {}; }
194 void return_void() {}
198 // CHECK-LABEL: f3(
199 extern "C" void f3(promise_sized_delete_tag) {
200 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
201 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
202 // CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
204 // CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
205 // CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
206 // CHECK: %[[SIZE2:.+]] = call i64 @llvm.coro.size.i64()
207 // CHECK: call void @_ZNSt16coroutine_traitsIJv24promise_sized_delete_tagEE12promise_typedlEPvm(i8* noundef %[[MEM]], i64 noundef %[[SIZE2]])
208 co_return;
211 struct promise_on_alloc_failure_tag {};
213 template <>
214 struct std::coroutine_traits<int, promise_on_alloc_failure_tag> {
215 struct promise_type {
216 int get_return_object() { return 0; }
217 suspend_always initial_suspend() { return {}; }
218 suspend_always final_suspend() noexcept { return {}; }
219 void return_void() {}
220 static int get_return_object_on_allocation_failure() { return -1; }
224 // CHECK-LABEL: f4(
225 extern "C" int f4(promise_on_alloc_failure_tag) {
226 // CHECK: %[[RetVal:.+]] = alloca i32
227 // CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
228 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
229 // CHECK: %[[MEM:.+]] = call noalias noundef i8* @_ZnwmRKSt9nothrow_t(i64 noundef %[[SIZE]], %"struct.std::nothrow_t"* noundef nonnull align 1 dereferenceable(1) @_ZStL7nothrow)
230 // CHECK: %[[OK:.+]] = icmp ne i8* %[[MEM]], null
231 // CHECK: br i1 %[[OK]], label %[[OKBB:.+]], label %[[ERRBB:.+]]
233 // CHECK: [[ERRBB]]:
234 // CHECK: %[[FailRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type39get_return_object_on_allocation_failureEv(
235 // CHECK: store i32 %[[FailRet]], i32* %[[RetVal]]
236 // CHECK: br label %[[RetBB:.+]]
238 // CHECK: [[OKBB]]:
239 // CHECK: %[[OkRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type17get_return_objectEv(
241 // CHECK: [[RetBB]]:
242 // CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]], align 4
243 // CHECK: ret i32 %[[LoadRet]]
244 co_return;