1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Test for PR56919. Tests the we won't contain the resumption of final suspend point.
//
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -O3 -emit-llvm -o - | FileCheck %s
// This test is expected to fail on PowerPC.
// XFAIL: target=powerpc{{.*}}
#include "Inputs/coroutine.h"
void _exit(int status) __attribute__ ((__noreturn__));
class Promise;
// An object that can be co_awaited, but we always resume immediately from
// await_suspend.
struct ResumeFromAwaitSuspend{};
struct Task {
using promise_type = Promise;
Promise& promise;
};
struct Promise {
static std::coroutine_handle<Promise> GetHandle(Promise& promise) {
return std::coroutine_handle<Promise>::from_promise(promise);
}
void unhandled_exception() {}
Task get_return_object() { return Task{*this}; }
void return_void() {}
// Always suspend before starting the coroutine body. We actually run the body
// when we are co_awaited.
std::suspend_always initial_suspend() { return {}; }
// We support awaiting tasks. We do so by configuring them to resume us when
// they are finished, and then resuming them from their initial suspend.
auto await_transform(Task&& task) {
struct Awaiter {
bool await_ready() { return false; }
std::coroutine_handle<> await_suspend(
const std::coroutine_handle<> handle) {
// Tell the child to resume the parent once it finishes.
child.resume_at_final_suspend = GetHandle(parent);
// Run the child.
return GetHandle(child);
}
void await_resume() {
// The child is now at its final suspend point, and can be destroyed.
return GetHandle(child).destroy();
}
Promise& parent;
Promise& child;
};
return Awaiter{
.parent = *this,
.child = task.promise,
};
}
// Make evaluation of `co_await ResumeFromAwaitSuspend{}` go through the
// await_suspend path, but cause it to resume immediately by returning our own
// handle to resume.
auto await_transform(ResumeFromAwaitSuspend) {
struct Awaiter {
bool await_ready() { return false; }
std::coroutine_handle<> await_suspend(const std::coroutine_handle<> h) {
return h;
}
void await_resume() {}
};
return Awaiter{};
}
// Always suspend at the final suspend point, transferring control back to our
// caller. We expect never to be resumed from the final suspend.
auto final_suspend() noexcept {
struct FinalSuspendAwaitable final {
bool await_ready() noexcept { return false; }
std::coroutine_handle<> await_suspend(std::coroutine_handle<>) noexcept {
return promise.resume_at_final_suspend;
}
void await_resume() noexcept {
_exit(1);
}
Promise& promise;
};
return FinalSuspendAwaitable{.promise = *this};
}
// The handle we will resume once we hit final suspend.
std::coroutine_handle<> resume_at_final_suspend;
};
Task Inner();
Task Outer() {
co_await ResumeFromAwaitSuspend();
co_await Inner();
}
// CHECK: define{{.*}}@_Z5Outerv.resume(
// CHECK-NOT: }
// CHECK-NOT: _exit
// CHECK: musttail call
// CHECK: musttail call
// CHECK: musttail call
// CHECK-NEXT: ret void
// CHECK-EMPTY:
// CHECK-NEXT: unreachable:
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
|