|
| 1 | +// Test for PR56919. Tests the destroy function contains the call to delete function only. |
| 2 | +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -O3 -S -o - | FileCheck %s |
| 3 | + |
| 4 | +#include "Inputs/coroutine.h" |
| 5 | + |
| 6 | +namespace std { |
| 7 | + |
| 8 | +template <typename T> struct remove_reference { using type = T; }; |
| 9 | +template <typename T> struct remove_reference<T &> { using type = T; }; |
| 10 | +template <typename T> struct remove_reference<T &&> { using type = T; }; |
| 11 | + |
| 12 | +template <typename T> |
| 13 | +constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept { |
| 14 | + return static_cast<typename std::remove_reference<T>::type &&>(t); |
| 15 | +} |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +template <typename T> |
| 20 | +class Task final { |
| 21 | + public: |
| 22 | + using value_type = T; |
| 23 | + |
| 24 | + class promise_type final { |
| 25 | + public: |
| 26 | + Task<void> get_return_object() { return Task<void>(std::coroutine_handle<promise_type>::from_promise(*this)); } |
| 27 | + |
| 28 | + void unhandled_exception(); |
| 29 | + |
| 30 | + std::suspend_always initial_suspend() { return {}; } |
| 31 | + |
| 32 | + auto await_transform(Task<void> co) { |
| 33 | + return await_transform(std::move(co.handle_.promise())); |
| 34 | + } |
| 35 | + |
| 36 | + auto await_transform(promise_type&& awaited) { |
| 37 | + struct Awaitable { |
| 38 | + promise_type&& awaited; |
| 39 | + |
| 40 | + bool await_ready() { return false; } |
| 41 | + |
| 42 | + std::coroutine_handle<> await_suspend( |
| 43 | + const std::coroutine_handle<> handle) { |
| 44 | + // Register our handle to be resumed once the awaited promise's coroutine |
| 45 | + // finishes, and then resume that coroutine. |
| 46 | + awaited.registered_handle_ = handle; |
| 47 | + return std::coroutine_handle<promise_type>::from_promise(awaited); |
| 48 | + } |
| 49 | + |
| 50 | + void await_resume() {} |
| 51 | + |
| 52 | + private: |
| 53 | + }; |
| 54 | + |
| 55 | + return Awaitable{std::move(awaited)}; |
| 56 | + } |
| 57 | + |
| 58 | + void return_void() {} |
| 59 | + |
| 60 | + // At final suspend resume our registered handle. |
| 61 | + auto final_suspend() noexcept { |
| 62 | + struct FinalSuspendAwaitable final { |
| 63 | + bool await_ready() noexcept { return false; } |
| 64 | + |
| 65 | + std::coroutine_handle<> await_suspend( |
| 66 | + std::coroutine_handle<> h) noexcept { |
| 67 | + return to_resume; |
| 68 | + } |
| 69 | + |
| 70 | + void await_resume() noexcept {} |
| 71 | + |
| 72 | + std::coroutine_handle<> to_resume; |
| 73 | + }; |
| 74 | + |
| 75 | + return FinalSuspendAwaitable{registered_handle_}; |
| 76 | + } |
| 77 | + |
| 78 | + private: |
| 79 | + std::coroutine_handle<promise_type> my_handle() { |
| 80 | + return std::coroutine_handle<promise_type>::from_promise(*this); |
| 81 | + } |
| 82 | + |
| 83 | + std::coroutine_handle<> registered_handle_; |
| 84 | + }; |
| 85 | + |
| 86 | + ~Task() { |
| 87 | + // Teach llvm that we are only ever destroyed when the coroutine body is done, |
| 88 | + // so there is no need for the jump table in the destroy function. Our coroutine |
| 89 | + // library doesn't expose handles to the user, so we know this constraint isn't |
| 90 | + // violated. |
| 91 | + if (!handle_.done()) { |
| 92 | + __builtin_unreachable(); |
| 93 | + } |
| 94 | + |
| 95 | + handle_.destroy(); |
| 96 | + } |
| 97 | + |
| 98 | + private: |
| 99 | + explicit Task(const std::coroutine_handle<promise_type> handle) |
| 100 | + : handle_(handle) {} |
| 101 | + |
| 102 | + const std::coroutine_handle<promise_type> handle_; |
| 103 | +}; |
| 104 | + |
| 105 | +Task<void> Qux() { co_return; } |
| 106 | +Task<void> Baz() { co_await Qux(); } |
| 107 | +Task<void> Bar() { co_await Baz(); } |
| 108 | + |
| 109 | +// CHECK: _Z3Quxv.destroy:{{.*}} |
| 110 | +// CHECK-NEXT: # |
| 111 | +// CHECK-NEXT: jmp _ZdlPv |
| 112 | + |
| 113 | +// CHECK: _Z3Bazv.destroy:{{.*}} |
| 114 | +// CHECK-NEXT: # |
| 115 | +// CHECK-NEXT: jmp _ZdlPv |
| 116 | + |
| 117 | +// CHECK: _Z3Barv.destroy:{{.*}} |
| 118 | +// CHECK-NEXT: # |
| 119 | +// CHECK-NEXT: jmp _ZdlPv |
0 commit comments