[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGenCoroutines / coro-elide.cpp
blobd7569c3b4d087a6720d7a92faa9bd36d445fdb89
1 // This tests that the coroutine elide optimization could happen succesfully.
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s
4 #include "Inputs/coroutine.h"
6 struct Task {
7 struct promise_type {
8 struct FinalAwaiter {
9 bool await_ready() const noexcept { return false; }
10 template <typename PromiseType>
11 std::coroutine_handle<> await_suspend(std::coroutine_handle<PromiseType> h) noexcept {
12 if (!h)
13 return std::noop_coroutine();
14 return h.promise().continuation;
16 void await_resume() noexcept {}
18 Task get_return_object() noexcept {
19 return std::coroutine_handle<promise_type>::from_promise(*this);
21 std::suspend_always initial_suspend() noexcept { return {}; }
22 FinalAwaiter final_suspend() noexcept { return {}; }
23 void unhandled_exception() noexcept {}
24 void return_value(int x) noexcept {
25 _value = x;
27 std::coroutine_handle<> continuation;
28 int _value;
31 Task(std::coroutine_handle<promise_type> handle) : handle(handle) {}
32 ~Task() {
33 if (handle)
34 handle.destroy();
37 struct Awaiter {
38 bool await_ready() const noexcept { return false; }
39 void await_suspend(std::coroutine_handle<void> continuation) noexcept {}
40 int await_resume() noexcept {
41 return 43;
45 auto operator co_await() {
46 return Awaiter{};
49 private:
50 std::coroutine_handle<promise_type> handle;
53 Task task0() {
54 co_return 43;
57 Task task1() {
58 co_return co_await task0();
61 // CHECK-LABEL: define{{.*}} void @_Z5task1v.resume
62 // CHECK-NOT: call{{.*}}_Znwm