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