[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGenCoroutines / pr65054.cpp
blob2d8b6dfe18d5521aa0e9ee810f7021be921ac9d3
1 // The output of O0 is highly redundant and hard to test. Also it is not good
2 // limit the output of O0. So we test the optimized output from O0. The idea
3 // is the optimizations shouldn't change the semantics of the program.
4 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 \
5 // RUN: -O0 -emit-llvm %s -o - -disable-O0-optnone \
6 // RUN: | opt -passes='sroa,mem2reg,simplifycfg' -S | FileCheck %s --check-prefix=CHECK-O0
8 #include "Inputs/coroutine.h"
10 // A simple awaiter type with an await_suspend method that can't be
11 // inlined.
12 struct Awaiter {
13 const int& x;
15 bool await_ready() { return false; }
16 std::coroutine_handle<> await_suspend(const std::coroutine_handle<> h);
17 void await_resume() {}
20 struct MyTask {
21 // A lazy promise with an await_transform method that supports awaiting
22 // integer references using the Awaiter struct above.
23 struct promise_type {
24 MyTask get_return_object() {
25 return MyTask{
26 std::coroutine_handle<promise_type>::from_promise(*this),
30 std::suspend_always initial_suspend() { return {}; }
31 std::suspend_always final_suspend() noexcept { return {}; }
32 void unhandled_exception();
34 auto await_transform(const int& x) { return Awaiter{x}; }
37 std::coroutine_handle<> h;
40 // A global array of integers.
41 int g_array[32];
43 // A coroutine that awaits each integer in the global array.
44 MyTask FooBar() {
45 for (const int& x : g_array) {
46 co_await x;
50 // CHECK-O0: define{{.*}}@_Z6FooBarv.resume
51 // CHECK-O0: call{{.*}}@_Z6FooBarv.__await_suspend_wrapper__await(
52 // CHECK-O0-NOT: store
53 // CHECK-O0: ret void