Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCoroutines / coro-halo.cpp
blobe75bedaf81fa228fc27b9f5fdad9645060b369f9
1 // This tests that the coroutine heap allocation elision optimization could happen succesfully.
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s \
4 // RUN: -fcxx-exceptions -fexceptions -o - | FileCheck %s
6 #include "Inputs/coroutine.h"
7 #include "Inputs/numeric.h"
9 template <typename T> struct generator {
10 struct promise_type {
11 T current_value;
12 std::suspend_always yield_value(T value) {
13 this->current_value = value;
14 return {};
16 std::suspend_always initial_suspend() { return {}; }
17 std::suspend_always final_suspend() noexcept { return {}; }
18 generator get_return_object() { return generator{this}; };
19 void unhandled_exception() {}
20 void return_void() {}
23 struct iterator {
24 std::coroutine_handle<promise_type> _Coro;
25 bool _Done;
27 iterator(std::coroutine_handle<promise_type> Coro, bool Done)
28 : _Coro(Coro), _Done(Done) {}
30 iterator &operator++() {
31 _Coro.resume();
32 _Done = _Coro.done();
33 return *this;
36 bool operator==(iterator const &_Right) const {
37 return _Done == _Right._Done;
40 bool operator!=(iterator const &_Right) const { return !(*this == _Right); }
41 T const &operator*() const { return _Coro.promise().current_value; }
42 T const *operator->() const { return &(operator*()); }
45 iterator begin() {
46 p.resume();
47 return {p, p.done()};
50 iterator end() { return {p, true}; }
52 generator(generator const &) = delete;
53 generator(generator &&rhs) : p(rhs.p) { rhs.p = nullptr; }
55 ~generator() {
56 if (p)
57 p.destroy();
60 private:
61 explicit generator(promise_type *p)
62 : p(std::coroutine_handle<promise_type>::from_promise(*p)) {}
64 std::coroutine_handle<promise_type> p;
67 template <typename T>
68 generator<T> seq() {
69 for (T i = {};; ++i)
70 co_yield i;
73 template <typename T>
74 generator<T> take_until(generator<T> &g, T limit) {
75 for (auto &&v : g)
76 if (v < limit)
77 co_yield v;
78 else
79 break;
82 template <typename T>
83 generator<T> multiply(generator<T> &g, T factor) {
84 for (auto &&v : g)
85 co_yield v *factor;
88 template <typename T>
89 generator<T> add(generator<T> &g, T adder) {
90 for (auto &&v : g)
91 co_yield v + adder;
94 int main() {
95 auto s = seq<int>();
96 auto t = take_until(s, 10);
97 auto m = multiply(t, 2);
98 auto a = add(m, 110);
99 return std::accumulate(a.begin(), a.end(), 0);
102 // CHECK-LABEL: define{{.*}} i32 @main(
103 // CHECK: ret i32 1190
104 // CHECK-NOT: call{{.*}}_Znwm