coro-gro.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Verifies lifetime of __gro local variable
  2. // Verify that coroutine promise and allocated memory are freed up on exception.
  3. // RUN: %clang_cc1 -std=c++1z -fcoroutines-ts -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes | FileCheck %s
  4. namespace std::experimental {
  5. template <typename... T> struct coroutine_traits;
  6. template <class Promise = void> struct coroutine_handle {
  7. coroutine_handle() = default;
  8. static coroutine_handle from_address(void *) noexcept;
  9. };
  10. template <> struct coroutine_handle<void> {
  11. static coroutine_handle from_address(void *) noexcept;
  12. coroutine_handle() = default;
  13. template <class PromiseType>
  14. coroutine_handle(coroutine_handle<PromiseType>) noexcept;
  15. };
  16. }
  17. struct suspend_always {
  18. bool await_ready() noexcept;
  19. void await_suspend(std::experimental::coroutine_handle<>) noexcept;
  20. void await_resume() noexcept;
  21. };
  22. struct GroType {
  23. ~GroType();
  24. operator int() noexcept;
  25. };
  26. template <> struct std::experimental::coroutine_traits<int> {
  27. struct promise_type {
  28. GroType get_return_object() noexcept;
  29. suspend_always initial_suspend() noexcept;
  30. suspend_always final_suspend() noexcept;
  31. void return_void() noexcept;
  32. promise_type();
  33. ~promise_type();
  34. void unhandled_exception() noexcept;
  35. };
  36. };
  37. struct Cleanup { ~Cleanup(); };
  38. void doSomething() noexcept;
  39. // CHECK: define i32 @_Z1fv(
  40. int f() {
  41. // CHECK: %[[RetVal:.+]] = alloca i32
  42. // CHECK: %[[GroActive:.+]] = alloca i1
  43. // CHECK: %[[Size:.+]] = call i64 @llvm.coro.size.i64()
  44. // CHECK: call i8* @_Znwm(i64 %[[Size]])
  45. // CHECK: store i1 false, i1* %[[GroActive]]
  46. // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeC1Ev(
  47. // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type17get_return_objectEv(
  48. // CHECK: store i1 true, i1* %[[GroActive]]
  49. Cleanup cleanup;
  50. doSomething();
  51. co_return;
  52. // CHECK: call void @_Z11doSomethingv(
  53. // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_type11return_voidEv(
  54. // CHECK: call void @_ZN7CleanupD1Ev(
  55. // Destroy promise and free the memory.
  56. // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJiEE12promise_typeD1Ev(
  57. // CHECK: %[[Mem:.+]] = call i8* @llvm.coro.free(
  58. // CHECK: call void @_ZdlPv(i8* %[[Mem]])
  59. // Initialize retval from Gro and destroy Gro
  60. // CHECK: %[[Conv:.+]] = call i32 @_ZN7GroTypecviEv(
  61. // CHECK: store i32 %[[Conv]], i32* %[[RetVal]]
  62. // CHECK: %[[IsActive:.+]] = load i1, i1* %[[GroActive]]
  63. // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]]
  64. // CHECK: [[CleanupGro]]:
  65. // CHECK: call void @_ZN7GroTypeD1Ev(
  66. // CHECK: br label %[[Done]]
  67. // CHECK: [[Done]]:
  68. // CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]]
  69. // CHECK: ret i32 %[[LoadRet]]
  70. }