brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.7 KiB · 79e77a2 Raw
248 lines · cpp
1// Verifies that parameters are copied with move constructors2// Verifies that parameter copies are destroyed3// Vefifies that parameter copies are used in the body of the coroutine4// Verifies that parameter copies are used to construct the promise type, if that type has a matching constructor5// RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes -fexceptions | FileCheck %s6// RUN: %clang_cc1 -std=c++20 -triple=x86_64-pc-win32          -emit-llvm -o - %s -disable-llvm-passes -fexceptions | FileCheck %s --check-prefix=MSABI7 8namespace std {9template <typename... T> struct coroutine_traits;10 11template <class Promise = void> struct coroutine_handle {12  coroutine_handle() = default;13  static coroutine_handle from_address(void *) noexcept;14};15template <> 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 std22 23struct suspend_always {24  bool await_ready() noexcept;25  void await_suspend(std::coroutine_handle<>) noexcept;26  void await_resume() noexcept;27};28 29template <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 yet42struct CopyOnly {43  int val;44  CopyOnly(const CopyOnly&) noexcept;45  CopyOnly(CopyOnly&&) = delete;46  ~CopyOnly();47};48 49struct MoveOnly {50  int val;51  MoveOnly(const MoveOnly&) = delete;52  MoveOnly(MoveOnly&&) noexcept;53  ~MoveOnly();54};55 56struct MoveAndCopy {57  int val;58  MoveAndCopy(const MoveAndCopy&)noexcept;59  MoveAndCopy(MoveAndCopy&&) noexcept;60  ~MoveAndCopy();61};62 63struct [[clang::trivial_abi]] TrivialABI {64  int val;65  TrivialABI(TrivialABI&&) noexcept;66  ~TrivialABI();67};68 69void consume(int,int,int,int) noexcept;70 71// TODO: Add support for CopyOnly params72// CHECK: define{{.*}} void @_Z1fi8MoveOnly11MoveAndCopy10TrivialABI(i32 noundef %val, ptr noundef %[[MoParam:.+]], ptr noundef %[[McParam:.+]], i32 %[[TrivialParam:.+]]) #0 personality ptr @__gxx_personality_v073void f(int val, MoveOnly moParam, MoveAndCopy mcParam, TrivialABI trivialParam) {74  // CHECK: %[[TrivialAlloca:.+]] = alloca %struct.TrivialABI,75  // CHECK-SAME: !coro.outside.frame76  // 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 093  // CHECK: %[[MoVal:.+]] = load i32, ptr %[[MoGep]]94  // CHECK: %[[McGep:.+]] = getelementptr inbounds nuw %struct.MoveAndCopy, ptr %[[McCopy]], i32 0, i32 095  // CHECK: %[[McVal:.+]] = load i32, ptr %[[McGep]]96  // CHECK: %[[TrivialGep:.+]] = getelementptr inbounds nuw %struct.TrivialABI, ptr %[[TrivialCopy]], i32 0, i32 097  // 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.end121  // 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)125template <typename T, typename U>126void dependent_params(T x, U, U y) {127  // CHECK: %[[x_copy:.+]] = alloca %struct.A,128  // CHECK-NEXT: %[[unnamed_copy:.+]] = alloca %struct.B129  // CHECK-NEXT: %[[y_copy:.+]] = alloca %struct.B130 131  // CHECK: call ptr @llvm.coro.begin132  // 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 144struct A {145  int WontFitIntoRegisterForSure[128];146  A();147  A(A&&) noexcept;148  ~A();149};150 151struct B {152  int WontFitIntoRegisterForSure[128];153  B();154  B(B&&) noexcept;155  ~B();156};157 158void call_dependent_params() {159  dependent_params(A{}, B{}, B{});160}161 162// Test that, when the promise type has a constructor whose signature matches163// that of the coroutine function, that constructor is used.164 165struct promise_matching_constructor {};166 167template <>168struct 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)181void coroutine_matching_promise_constructor(promise_matching_constructor, int, float, double) {182  // CHECK: %[[INT:.+]] = load i32, ptr %5, align 4183  // CHECK: %[[FLOAT:.+]] = load float, ptr %6, align 4184  // CHECK: %[[DOUBLE:.+]] = load double, ptr %7, align 8185  // 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 189struct some_class;190 191struct method {};192 193template <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 204struct some_class {205  method good_coroutine_calls_custom_constructor(float);206};207 208// CHECK-LABEL: define{{.*}} void @_ZN10some_class39good_coroutine_calls_custom_constructorEf(ptr209method 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) %{{.+}}, float211  co_return;212}213 214 215struct MSParm {216  int val;217  ~MSParm();218};219 220void consume(int) noexcept;221 222// Similarly to the [[clang::trivial_abi]] parameters, with the MSVC ABI223// parameters are also destroyed by the callee, and on x86-64 such parameters224// may get passed in registers. In that case it's again important that the225// parameter's local alloca does not become part of the coro frame since that226// may be destroyed before the destructor call.227void 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.MSParm232  // MSABI-SAME: !coro.outside.frame233 234  // MSABI: %[[ParamCopy:.+]] = alloca %struct.MSParm235 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 0239  // 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.end246  // MSABI: call void @"??1MSParm@@QEAA@XZ"(ptr{{.*}} %[[ParamAlloca]])247}248