| 1 |
// Verifies that parameters are copied with move constructors |
| 2 |
// Verifies that parameter copies are destroyed |
| 3 |
// Vefifies that parameter copies are used in the body of the coroutine |
| 4 |
// Verifies that parameter copies are used to construct the promise type, if that type has a matching constructor |
| 5 |
// RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes -fexceptions | FileCheck %s |
| 6 |
// RUN: %clang_cc1 -std=c++20 -triple=x86_64-pc-win32 -emit-llvm -o - %s -disable-llvm-passes -fexceptions | FileCheck %s --check-prefix=MSABI |
| 7 |
|
| 8 |
namespace std { |
| 9 |
template <typename... T> struct coroutine_traits; |
| 10 |
|
| 11 |
template <class Promise = void> struct coroutine_handle { |
| 12 |
coroutine_handle() = default; |
| 13 |
static coroutine_handle from_address(void *) noexcept; |
| 14 |
}; |
| 15 |
template <> struct coroutine_handle<void> { |
| 16 |
static coroutine_handle from_address(void *) noexcept; |
| 17 |
coroutine_handle() = default; |
| 18 |
template <class PromiseType> |
| 19 |
coroutine_handle(coroutine_handle<PromiseType>) noexcept; |
| 20 |
}; |
| 21 |
} // namespace std |
| 22 |
|
| 23 |
struct suspend_always { |
| 24 |
bool await_ready() noexcept; |
| 25 |
void await_suspend(std::coroutine_handle<>) noexcept; |
| 26 |
void await_resume() noexcept; |
| 27 |
}; |
| 28 |
|
| 29 |
template <typename... Args> struct std::coroutine_traits<void, Args...> { |
| 30 |
struct promise_type { |
| 31 |
void get_return_object() noexcept; |
| 32 |
suspend_always initial_suspend() noexcept; |
| 33 |
suspend_always final_suspend() noexcept; |
| 34 |
void return_void() noexcept; |
| 35 |
promise_type(); |
| 36 |
~promise_type() noexcept; |
| 37 |
void unhandled_exception() noexcept; |
| 38 |
}; |
| 39 |
}; |
| 40 |
|
| 41 |
// TODO: Not supported yet |
| 42 |
struct CopyOnly { |
| 43 |
int val; |
| 44 |
CopyOnly(const CopyOnly&) noexcept; |
| 45 |
CopyOnly(CopyOnly&&) = delete; |
| 46 |
~CopyOnly(); |
| 47 |
}; |
| 48 |
|
| 49 |
struct MoveOnly { |
| 50 |
int val; |
| 51 |
MoveOnly(const MoveOnly&) = delete; |
| 52 |
MoveOnly(MoveOnly&&) noexcept; |
| 53 |
~MoveOnly(); |
| 54 |
}; |
| 55 |
|
| 56 |
struct MoveAndCopy { |
| 57 |
int val; |
| 58 |
MoveAndCopy(const MoveAndCopy&)noexcept; |
| 59 |
MoveAndCopy(MoveAndCopy&&) noexcept; |
| 60 |
~MoveAndCopy(); |
| 61 |
}; |
| 62 |
|
| 63 |
struct [[clang::trivial_abi]] TrivialABI { |
| 64 |
int val; |
| 65 |
TrivialABI(TrivialABI&&) noexcept; |
| 66 |
~TrivialABI(); |
| 67 |
}; |
| 68 |
|
| 69 |
void consume(int,int,int,int) noexcept; |
| 70 |
|
| 71 |
// TODO: Add support for CopyOnly params |
| 72 |
// CHECK: define{{.*}} void @_Z1fi8MoveOnly11MoveAndCopy10TrivialABI(i32 noundef %val, ptr noundef %[[MoParam:.+]], ptr noundef %[[McParam:.+]], i32 %[[TrivialParam:.+]]) #0 personality ptr @__gxx_personality_v0 |
| 73 |
void f(int val, MoveOnly moParam, MoveAndCopy mcParam, TrivialABI trivialParam) { |
| 74 |
// CHECK: %[[TrivialAlloca:.+]] = alloca %struct.TrivialABI, |
| 75 |
// CHECK-SAME: !coro.outside.frame |
| 76 |
// CHECK: %[[MoCopy:.+]] = alloca %struct.MoveOnly, |
| 77 |
// CHECK: %[[McCopy:.+]] = alloca %struct.MoveAndCopy, |
| 78 |
// CHECK: %[[TrivialCopy:.+]] = alloca %struct.TrivialABI, |
| 79 |
// CHECK: store i32 %val, ptr %[[ValAddr:.+]] |
| 80 |
|
| 81 |
// CHECK: call ptr @llvm.coro.begin( |
| 82 |
// CHECK: call void @_ZN8MoveOnlyC1EOS_(ptr {{[^,]*}} %[[MoCopy]], ptr noundef nonnull align 4 dereferenceable(4) %[[MoParam]]) |
| 83 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 84 |
// CHECK-NEXT: call void @_ZN11MoveAndCopyC1EOS_(ptr {{[^,]*}} %[[McCopy]], ptr noundef nonnull align 4 dereferenceable(4) %[[McParam]]) # |
| 85 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 86 |
// CHECK-NEXT: call void @_ZN10TrivialABIC1EOS_(ptr {{[^,]*}} %[[TrivialCopy]], ptr {{[^,]*}} %[[TrivialAlloca]]) |
| 87 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 88 |
// CHECK-NEXT: invoke void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopy10TrivialABIEE12promise_typeC1Ev( |
| 89 |
|
| 90 |
// CHECK: call void @_ZN14suspend_always12await_resumeEv( |
| 91 |
// CHECK: %[[IntParam:.+]] = load i32, ptr %{{.*}} |
| 92 |
// CHECK: %[[MoGep:.+]] = getelementptr inbounds nuw %struct.MoveOnly, ptr %[[MoCopy]], i32 0, i32 0 |
| 93 |
// CHECK: %[[MoVal:.+]] = load i32, ptr %[[MoGep]] |
| 94 |
// CHECK: %[[McGep:.+]] = getelementptr inbounds nuw %struct.MoveAndCopy, ptr %[[McCopy]], i32 0, i32 0 |
| 95 |
// CHECK: %[[McVal:.+]] = load i32, ptr %[[McGep]] |
| 96 |
// CHECK: %[[TrivialGep:.+]] = getelementptr inbounds nuw %struct.TrivialABI, ptr %[[TrivialCopy]], i32 0, i32 0 |
| 97 |
// CHECK: %[[TrivialVal:.+]] = load i32, ptr %[[TrivialGep]] |
| 98 |
// CHECK: call void @_Z7consumeiiii(i32 noundef %[[IntParam]], i32 noundef %[[MoVal]], i32 noundef %[[McVal]], i32 noundef %[[TrivialVal]]) |
| 99 |
|
| 100 |
consume(val, moParam.val, mcParam.val, trivialParam.val); |
| 101 |
co_return; |
| 102 |
|
| 103 |
// Skip to final suspend: |
| 104 |
// CHECK: call void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopy10TrivialABIEE12promise_type13final_suspendEv( |
| 105 |
// CHECK: call void @_ZN14suspend_always12await_resumeEv( |
| 106 |
|
| 107 |
// Destroy promise, then parameter copies: |
| 108 |
// CHECK: call void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopy10TrivialABIEE12promise_typeD1Ev(ptr {{[^,]*}} %__promise) |
| 109 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 110 |
// CHECK-NEXT: call void @_ZN10TrivialABID1Ev(ptr {{[^,]*}} %[[TrivialCopy]]) |
| 111 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 112 |
// CHECK-NEXT: call void @_ZN11MoveAndCopyD1Ev(ptr {{[^,]*}} %[[McCopy]]) |
| 113 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 114 |
// CHECK-NEXT: call void @_ZN8MoveOnlyD1Ev(ptr {{[^,]*}} %[[MoCopy]] |
| 115 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 116 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 117 |
// CHECK-NEXT: call ptr @llvm.coro.free( |
| 118 |
|
| 119 |
// The original trivial_abi parameter is destroyed when returning from the ramp. |
| 120 |
// CHECK: call void @llvm.coro.end |
| 121 |
// CHECK: call void @_ZN10TrivialABID1Ev(ptr {{[^,]*}} %[[TrivialAlloca]]) |
| 122 |
} |
| 123 |
|
| 124 |
// CHECK-LABEL: void @_Z16dependent_paramsI1A1BEvT_T0_S3_(ptr noundef %x, ptr noundef %0, ptr noundef %y) |
| 125 |
template <typename T, typename U> |
| 126 |
void dependent_params(T x, U, U y) { |
| 127 |
// CHECK: %[[x_copy:.+]] = alloca %struct.A, |
| 128 |
// CHECK-NEXT: %[[unnamed_copy:.+]] = alloca %struct.B |
| 129 |
// CHECK-NEXT: %[[y_copy:.+]] = alloca %struct.B |
| 130 |
|
| 131 |
// CHECK: call ptr @llvm.coro.begin |
| 132 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 133 |
// CHECK-NEXT: call void @_ZN1AC1EOS_(ptr {{[^,]*}} %[[x_copy]], ptr noundef nonnull align 4 dereferenceable(512) %x) |
| 134 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 135 |
// CHECK-NEXT: call void @_ZN1BC1EOS_(ptr {{[^,]*}} %[[unnamed_copy]], ptr noundef nonnull align 4 dereferenceable(512) %0) |
| 136 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 137 |
// CHECK-NEXT: call void @_ZN1BC1EOS_(ptr {{[^,]*}} %[[y_copy]], ptr noundef nonnull align 4 dereferenceable(512) %y) |
| 138 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 139 |
// CHECK-NEXT: invoke void @_ZNSt16coroutine_traitsIJv1A1BS1_EE12promise_typeC1Ev( |
| 140 |
|
| 141 |
co_return; |
| 142 |
} |
| 143 |
|
| 144 |
struct A { |
| 145 |
int WontFitIntoRegisterForSure[128]; |
| 146 |
A(); |
| 147 |
A(A&&) noexcept; |
| 148 |
~A(); |
| 149 |
}; |
| 150 |
|
| 151 |
struct B { |
| 152 |
int WontFitIntoRegisterForSure[128]; |
| 153 |
B(); |
| 154 |
B(B&&) noexcept; |
| 155 |
~B(); |
| 156 |
}; |
| 157 |
|
| 158 |
void call_dependent_params() { |
| 159 |
dependent_params(A{}, B{}, B{}); |
| 160 |
} |
| 161 |
|
| 162 |
// Test that, when the promise type has a constructor whose signature matches |
| 163 |
// that of the coroutine function, that constructor is used. |
| 164 |
|
| 165 |
struct promise_matching_constructor {}; |
| 166 |
|
| 167 |
template <> |
| 168 |
struct std::coroutine_traits<void, promise_matching_constructor, int, float, double> { |
| 169 |
struct promise_type { |
| 170 |
promise_type(promise_matching_constructor, int, float, double) {} |
| 171 |
promise_type() = delete; |
| 172 |
void get_return_object() {} |
| 173 |
suspend_always initial_suspend() { return {}; } |
| 174 |
suspend_always final_suspend() noexcept { return {}; } |
| 175 |
void return_void() {} |
| 176 |
void unhandled_exception() {} |
| 177 |
}; |
| 178 |
}; |
| 179 |
|
| 180 |
// CHECK-LABEL: void @_Z38coroutine_matching_promise_constructor28promise_matching_constructorifd(i32 noundef %0, float noundef %1, double noundef %2) |
| 181 |
void coroutine_matching_promise_constructor(promise_matching_constructor, int, float, double) { |
| 182 |
// CHECK: %[[INT:.+]] = load i32, ptr %5, align 4 |
| 183 |
// CHECK: %[[FLOAT:.+]] = load float, ptr %6, align 4 |
| 184 |
// CHECK: %[[DOUBLE:.+]] = load double, ptr %7, align 8 |
| 185 |
// CHECK: invoke void @_ZNSt16coroutine_traitsIJv28promise_matching_constructorifdEE12promise_typeC1ES0_ifd(ptr {{[^,]*}} %__promise, i32 noundef %[[INT]], float noundef %[[FLOAT]], double noundef %[[DOUBLE]]) |
| 186 |
co_return; |
| 187 |
} |
| 188 |
|
| 189 |
struct some_class; |
| 190 |
|
| 191 |
struct method {}; |
| 192 |
|
| 193 |
template <typename... Args> struct std::coroutine_traits<method, Args...> { |
| 194 |
struct promise_type { |
| 195 |
promise_type(some_class&, float); |
| 196 |
method get_return_object(); |
| 197 |
suspend_always initial_suspend(); |
| 198 |
suspend_always final_suspend() noexcept; |
| 199 |
void return_void(); |
| 200 |
void unhandled_exception(); |
| 201 |
}; |
| 202 |
}; |
| 203 |
|
| 204 |
struct some_class { |
| 205 |
method good_coroutine_calls_custom_constructor(float); |
| 206 |
}; |
| 207 |
|
| 208 |
// CHECK-LABEL: define{{.*}} void @_ZN10some_class39good_coroutine_calls_custom_constructorEf(ptr |
| 209 |
method some_class::good_coroutine_calls_custom_constructor(float) { |
| 210 |
// CHECK: invoke void @_ZNSt16coroutine_traitsIJ6methodR10some_classfEE12promise_typeC1ES2_f(ptr {{[^,]*}} %__promise, ptr noundef nonnull align 1 dereferenceable(1) %{{.+}}, float |
| 211 |
co_return; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
struct MSParm { |
| 216 |
int val; |
| 217 |
~MSParm(); |
| 218 |
}; |
| 219 |
|
| 220 |
void consume(int) noexcept; |
| 221 |
|
| 222 |
// Similarly to the [[clang::trivial_abi]] parameters, with the MSVC ABI |
| 223 |
// parameters are also destroyed by the callee, and on x86-64 such parameters |
| 224 |
// may get passed in registers. In that case it's again important that the |
| 225 |
// parameter's local alloca does not become part of the coro frame since that |
| 226 |
// may be destroyed before the destructor call. |
| 227 |
void msabi(MSParm p) { |
| 228 |
// MSABI: define{{.*}} void @"?msabi@@YAXUMSParm@@@Z"(i32 %[[Param:.+]]) |
| 229 |
|
| 230 |
// The parameter's local alloca is marked not part of the frame. |
| 231 |
// MSABI: %[[ParamAlloca:.+]] = alloca %struct.MSParm |
| 232 |
// MSABI-SAME: !coro.outside.frame |
| 233 |
|
| 234 |
// MSABI: %[[ParamCopy:.+]] = alloca %struct.MSParm |
| 235 |
|
| 236 |
consume(p.val); |
| 237 |
// The parameter's copy is used by the coroutine. |
| 238 |
// MSABI: %[[ValPtr:.+]] = getelementptr inbounds nuw %struct.MSParm, ptr %[[ParamCopy]], i32 0, i32 0 |
| 239 |
// MSABI: %[[Val:.+]] = load i32, ptr %[[ValPtr]] |
| 240 |
// MSABI: call void @"?consume@@YAXH@Z"(i32{{.*}} %[[Val]]) |
| 241 |
|
| 242 |
co_return; |
| 243 |
|
| 244 |
// The local alloca is used for the destructor call at the end of the ramp. |
| 245 |
// MSABI: call void @llvm.coro.end |
| 246 |
// MSABI: call void @"??1MSParm@@QEAA@XZ"(ptr{{.*}} %[[ParamAlloca]]) |
| 247 |
} |