| 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 |
|
| 7 |
namespace std { |
| 8 |
template <typename... T> struct coroutine_traits; |
| 9 |
|
| 10 |
template <class Promise = void> struct coroutine_handle { |
| 11 |
coroutine_handle() = default; |
| 12 |
static coroutine_handle from_address(void *) noexcept; |
| 13 |
}; |
| 14 |
template <> struct coroutine_handle<void> { |
| 15 |
static coroutine_handle from_address(void *) noexcept; |
| 16 |
coroutine_handle() = default; |
| 17 |
template <class PromiseType> |
| 18 |
coroutine_handle(coroutine_handle<PromiseType>) noexcept; |
| 19 |
}; |
| 20 |
} // namespace std |
| 21 |
|
| 22 |
struct suspend_always { |
| 23 |
bool await_ready() noexcept; |
| 24 |
void await_suspend(std::coroutine_handle<>) noexcept; |
| 25 |
void await_resume() noexcept; |
| 26 |
}; |
| 27 |
|
| 28 |
template <typename... Args> struct std::coroutine_traits<void, Args...> { |
| 29 |
struct promise_type { |
| 30 |
void get_return_object() noexcept; |
| 31 |
suspend_always initial_suspend() noexcept; |
| 32 |
suspend_always final_suspend() noexcept; |
| 33 |
void return_void() noexcept; |
| 34 |
promise_type(); |
| 35 |
~promise_type() noexcept; |
| 36 |
void unhandled_exception() noexcept; |
| 37 |
}; |
| 38 |
}; |
| 39 |
|
| 40 |
// TODO: Not supported yet |
| 41 |
struct CopyOnly { |
| 42 |
int val; |
| 43 |
CopyOnly(const CopyOnly&) noexcept; |
| 44 |
CopyOnly(CopyOnly&&) = delete; |
| 45 |
~CopyOnly(); |
| 46 |
}; |
| 47 |
|
| 48 |
struct MoveOnly { |
| 49 |
int val; |
| 50 |
MoveOnly(const MoveOnly&) = delete; |
| 51 |
MoveOnly(MoveOnly&&) noexcept; |
| 52 |
~MoveOnly(); |
| 53 |
}; |
| 54 |
|
| 55 |
struct MoveAndCopy { |
| 56 |
int val; |
| 57 |
MoveAndCopy(const MoveAndCopy&)noexcept; |
| 58 |
MoveAndCopy(MoveAndCopy&&) noexcept; |
| 59 |
~MoveAndCopy(); |
| 60 |
}; |
| 61 |
|
| 62 |
void consume(int,int,int) noexcept; |
| 63 |
|
| 64 |
// TODO: Add support for CopyOnly params |
| 65 |
// CHECK: define{{.*}} void @_Z1fi8MoveOnly11MoveAndCopy(i32 noundef %val, ptr noundef %[[MoParam:.+]], ptr noundef %[[McParam:.+]]) #0 personality ptr @__gxx_personality_v0 |
| 66 |
void f(int val, MoveOnly moParam, MoveAndCopy mcParam) { |
| 67 |
// CHECK: %[[MoCopy:.+]] = alloca %struct.MoveOnly, |
| 68 |
// CHECK: %[[McCopy:.+]] = alloca %struct.MoveAndCopy, |
| 69 |
// CHECK: store i32 %val, ptr %[[ValAddr:.+]] |
| 70 |
|
| 71 |
// CHECK: call ptr @llvm.coro.begin( |
| 72 |
// CHECK: call void @_ZN8MoveOnlyC1EOS_(ptr {{[^,]*}} %[[MoCopy]], ptr noundef nonnull align 4 dereferenceable(4) %[[MoParam]]) |
| 73 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 74 |
// CHECK-NEXT: call void @_ZN11MoveAndCopyC1EOS_(ptr {{[^,]*}} %[[McCopy]], ptr noundef nonnull align 4 dereferenceable(4) %[[McParam]]) # |
| 75 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 76 |
// CHECK-NEXT: invoke void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopyEE12promise_typeC1Ev( |
| 77 |
|
| 78 |
// CHECK: call void @_ZN14suspend_always12await_resumeEv( |
| 79 |
// CHECK: %[[IntParam:.+]] = load i32, ptr %{{.*}} |
| 80 |
// CHECK: %[[MoGep:.+]] = getelementptr inbounds %struct.MoveOnly, ptr %[[MoCopy]], i32 0, i32 0 |
| 81 |
// CHECK: %[[MoVal:.+]] = load i32, ptr %[[MoGep]] |
| 82 |
// CHECK: %[[McGep:.+]] = getelementptr inbounds %struct.MoveAndCopy, ptr %[[McCopy]], i32 0, i32 0 |
| 83 |
// CHECK: %[[McVal:.+]] = load i32, ptr %[[McGep]] |
| 84 |
// CHECK: call void @_Z7consumeiii(i32 noundef %[[IntParam]], i32 noundef %[[MoVal]], i32 noundef %[[McVal]]) |
| 85 |
|
| 86 |
consume(val, moParam.val, mcParam.val); |
| 87 |
co_return; |
| 88 |
|
| 89 |
// Skip to final suspend: |
| 90 |
// CHECK: call void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopyEE12promise_type13final_suspendEv( |
| 91 |
// CHECK: call void @_ZN14suspend_always12await_resumeEv( |
| 92 |
|
| 93 |
// Destroy promise, then parameter copies: |
| 94 |
// CHECK: call void @_ZNSt16coroutine_traitsIJvi8MoveOnly11MoveAndCopyEE12promise_typeD1Ev(ptr {{[^,]*}} %__promise) |
| 95 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 96 |
// CHECK-NEXT: call void @_ZN11MoveAndCopyD1Ev(ptr {{[^,]*}} %[[McCopy]]) |
| 97 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 98 |
// CHECK-NEXT: call void @_ZN8MoveOnlyD1Ev(ptr {{[^,]*}} %[[MoCopy]] |
| 99 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 100 |
// CHECK-NEXT: call void @llvm.lifetime.end.p0( |
| 101 |
// CHECK-NEXT: call ptr @llvm.coro.free( |
| 102 |
} |
| 103 |
|
| 104 |
// CHECK-LABEL: void @_Z16dependent_paramsI1A1BEvT_T0_S3_(ptr noundef %x, ptr noundef %0, ptr noundef %y) |
| 105 |
template <typename T, typename U> |
| 106 |
void dependent_params(T x, U, U y) { |
| 107 |
// CHECK: %[[x_copy:.+]] = alloca %struct.A, |
| 108 |
// CHECK-NEXT: %[[unnamed_copy:.+]] = alloca %struct.B |
| 109 |
// CHECK-NEXT: %[[y_copy:.+]] = alloca %struct.B |
| 110 |
|
| 111 |
// CHECK: call ptr @llvm.coro.begin |
| 112 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 113 |
// CHECK-NEXT: call void @_ZN1AC1EOS_(ptr {{[^,]*}} %[[x_copy]], ptr noundef nonnull align 4 dereferenceable(512) %x) |
| 114 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 115 |
// CHECK-NEXT: call void @_ZN1BC1EOS_(ptr {{[^,]*}} %[[unnamed_copy]], ptr noundef nonnull align 4 dereferenceable(512) %0) |
| 116 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 117 |
// CHECK-NEXT: call void @_ZN1BC1EOS_(ptr {{[^,]*}} %[[y_copy]], ptr noundef nonnull align 4 dereferenceable(512) %y) |
| 118 |
// CHECK-NEXT: call void @llvm.lifetime.start.p0( |
| 119 |
// CHECK-NEXT: invoke void @_ZNSt16coroutine_traitsIJv1A1BS1_EE12promise_typeC1Ev( |
| 120 |
|
| 121 |
co_return; |
| 122 |
} |
| 123 |
|
| 124 |
struct A { |
| 125 |
int WontFitIntoRegisterForSure[128]; |
| 126 |
A(); |
| 127 |
A(A&&) noexcept; |
| 128 |
~A(); |
| 129 |
}; |
| 130 |
|
| 131 |
struct B { |
| 132 |
int WontFitIntoRegisterForSure[128]; |
| 133 |
B(); |
| 134 |
B(B&&) noexcept; |
| 135 |
~B(); |
| 136 |
}; |
| 137 |
|
| 138 |
void call_dependent_params() { |
| 139 |
dependent_params(A{}, B{}, B{}); |
| 140 |
} |
| 141 |
|
| 142 |
// Test that, when the promise type has a constructor whose signature matches |
| 143 |
// that of the coroutine function, that constructor is used. |
| 144 |
|
| 145 |
struct promise_matching_constructor {}; |
| 146 |
|
| 147 |
template <> |
| 148 |
struct std::coroutine_traits<void, promise_matching_constructor, int, float, double> { |
| 149 |
struct promise_type { |
| 150 |
promise_type(promise_matching_constructor, int, float, double) {} |
| 151 |
promise_type() = delete; |
| 152 |
void get_return_object() {} |
| 153 |
suspend_always initial_suspend() { return {}; } |
| 154 |
suspend_always final_suspend() noexcept { return {}; } |
| 155 |
void return_void() {} |
| 156 |
void unhandled_exception() {} |
| 157 |
}; |
| 158 |
}; |
| 159 |
|
| 160 |
// CHECK-LABEL: void @_Z38coroutine_matching_promise_constructor28promise_matching_constructorifd(i32 noundef %0, float noundef %1, double noundef %2) |
| 161 |
void coroutine_matching_promise_constructor(promise_matching_constructor, int, float, double) { |
| 162 |
// CHECK: %[[INT:.+]] = load i32, ptr %5, align 4 |
| 163 |
// CHECK: %[[FLOAT:.+]] = load float, ptr %6, align 4 |
| 164 |
// CHECK: %[[DOUBLE:.+]] = load double, ptr %7, align 8 |
| 165 |
// CHECK: invoke void @_ZNSt16coroutine_traitsIJv28promise_matching_constructorifdEE12promise_typeC1ES0_ifd(ptr {{[^,]*}} %__promise, i32 noundef %[[INT]], float noundef %[[FLOAT]], double noundef %[[DOUBLE]]) |
| 166 |
co_return; |
| 167 |
} |
| 168 |
|
| 169 |
struct some_class; |
| 170 |
|
| 171 |
struct method {}; |
| 172 |
|
| 173 |
template <typename... Args> struct std::coroutine_traits<method, Args...> { |
| 174 |
struct promise_type { |
| 175 |
promise_type(some_class&, float); |
| 176 |
method get_return_object(); |
| 177 |
suspend_always initial_suspend(); |
| 178 |
suspend_always final_suspend() noexcept; |
| 179 |
void return_void(); |
| 180 |
void unhandled_exception(); |
| 181 |
}; |
| 182 |
}; |
| 183 |
|
| 184 |
struct some_class { |
| 185 |
method good_coroutine_calls_custom_constructor(float); |
| 186 |
}; |
| 187 |
|
| 188 |
// CHECK-LABEL: define{{.*}} void @_ZN10some_class39good_coroutine_calls_custom_constructorEf(ptr |
| 189 |
method some_class::good_coroutine_calls_custom_constructor(float) { |
| 190 |
// CHECK: invoke void @_ZNSt16coroutine_traitsIJ6methodR10some_classfEE12promise_typeC1ES2_f(ptr {{[^,]*}} %__promise, ptr noundef nonnull align 1 dereferenceable(1) %{{.+}}, float |
| 191 |
co_return; |
| 192 |
} |