coro-gro-nrvo.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcoroutines-ts -std=c++14 -emit-llvm %s -o - -disable-llvm-passes | FileCheck %s
  2. #include "Inputs/coroutine.h"
  3. using namespace std::experimental;
  4. namespace std {
  5. struct nothrow_t {};
  6. constexpr nothrow_t nothrow = {};
  7. } // end namespace std
  8. // Required when get_return_object_on_allocation_failure() is defined by
  9. // the promise.
  10. void* operator new(__SIZE_TYPE__ __sz, const std::nothrow_t&) noexcept;
  11. void operator delete(void* __p, const std::nothrow_t&) noexcept;
  12. template <class RetObject>
  13. struct promise_type {
  14. RetObject get_return_object();
  15. suspend_always initial_suspend();
  16. suspend_never final_suspend();
  17. void return_void();
  18. static void unhandled_exception();
  19. };
  20. struct coro {
  21. using promise_type = promise_type<coro>;
  22. coro(coro const&);
  23. struct Impl;
  24. Impl *impl;
  25. };
  26. // Verify that the NRVO is applied to the Gro object.
  27. // CHECK-LABEL: define void @_Z1fi(%struct.coro* noalias sret %agg.result, i32 %0)
  28. coro f(int) {
  29. // CHECK: %call = call i8* @_Znwm(
  30. // CHECK-NEXT: br label %[[CoroInit:.*]]
  31. // CHECK: {{.*}}[[CoroInit]]:
  32. // CHECK: store i1 false, i1* %gro.active
  33. // CHECK: call void @{{.*get_return_objectEv}}(%struct.coro* sret %agg.result
  34. // CHECK-NEXT: store i1 true, i1* %gro.active
  35. co_return;
  36. }
  37. template <class RetObject>
  38. struct promise_type_with_on_alloc_failure {
  39. static RetObject get_return_object_on_allocation_failure();
  40. RetObject get_return_object();
  41. suspend_always initial_suspend();
  42. suspend_never final_suspend();
  43. void return_void();
  44. static void unhandled_exception();
  45. };
  46. struct coro_two {
  47. using promise_type = promise_type_with_on_alloc_failure<coro_two>;
  48. coro_two(coro_two const&);
  49. struct Impl;
  50. Impl *impl;
  51. };
  52. // Verify that the NRVO is applied to the Gro object.
  53. // CHECK-LABEL: define void @_Z1hi(%struct.coro_two* noalias sret %agg.result, i32 %0)
  54. coro_two h(int) {
  55. // CHECK: %call = call i8* @_ZnwmRKSt9nothrow_t
  56. // CHECK-NEXT: %[[CheckNull:.*]] = icmp ne i8* %call, null
  57. // CHECK-NEXT: br i1 %[[CheckNull]], label %[[InitOnSuccess:.*]], label %[[InitOnFailure:.*]]
  58. // CHECK: {{.*}}[[InitOnFailure]]:
  59. // CHECK-NEXT: call void @{{.*get_return_object_on_allocation_failureEv}}(%struct.coro_two* sret %agg.result
  60. // CHECK-NEXT: br label %[[RetLabel:.*]]
  61. // CHECK: {{.*}}[[InitOnSuccess]]:
  62. // CHECK: store i1 false, i1* %gro.active
  63. // CHECK: call void @{{.*get_return_objectEv}}(%struct.coro_two* sret %agg.result
  64. // CHECK-NEXT: store i1 true, i1* %gro.active
  65. // CHECK: [[RetLabel]]:
  66. // CHECK-NEXT: ret void
  67. co_return;
  68. }