Bruno Cardoso Lopes 43f5085fa8 [Coroutines] Fix premature conversion of return object
Fix https://github.com/llvm/llvm-project/issues/56532

Effectively, this reverts behavior introduced in https://reviews.llvm.org/D117087,
which did two things:

1. Change delayed to early conversion of return object.
2. Introduced RVO possibilities because of early conversion.

This patches fixes (1) and removes (2). I already worked on a follow up for (2)
in a separated patch. I believe it's important to split these two because if the RVO
causes any problems we can explore reverting (2) while maintaining (1).

Notes on some testcase changes:
- `pr59221.cpp` changed to `-O1` so we can check that the front-end honors
  the value checked for. Sounds like `-O3` without RVO is more likely
  to work with LLVM optimizations...
- Comment out delete members `coroutine-no-move-ctor.cpp` since behavior
  now requires copies again.

Differential Revision: https://reviews.llvm.org/D145639
2023-03-21 21:42:25 -07:00

89 lines
2.0 KiB
C++

// Test for PR59221. Tests the compiler wouldn't misoptimize the final result.
//
// REQUIRES: x86-registered-target
//
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -O1 -S -emit-llvm -o - | FileCheck %s
#include "Inputs/coroutine.h"
template <typename T> struct task {
struct promise_type {
T value{123};
std::coroutine_handle<> caller{std::noop_coroutine()};
struct final_awaiter: std::suspend_always {
auto await_suspend(std::coroutine_handle<promise_type> me) const noexcept {
return me.promise().caller;
}
};
constexpr auto initial_suspend() const noexcept {
return std::suspend_always();
}
constexpr auto final_suspend() const noexcept {
return final_awaiter{};
}
auto unhandled_exception() noexcept {
// ignore
}
constexpr void return_value(T v) noexcept {
value = v;
}
constexpr auto & get_return_object() noexcept {
return *this;
}
};
using coroutine_handle = std::coroutine_handle<promise_type>;
promise_type & promise{nullptr};
task(promise_type & p) noexcept: promise{p} { }
~task() noexcept {
coroutine_handle::from_promise(promise).destroy();
}
auto await_ready() noexcept {
return false;
}
auto await_suspend(std::coroutine_handle<> caller) noexcept {
promise.caller = caller;
return coroutine_handle::from_promise(promise);
}
constexpr auto await_resume() const noexcept {
return promise.value;
}
// non-coroutine access to result
auto get() noexcept {
const auto handle = coroutine_handle::from_promise(promise);
if (!handle.done()) {
handle.resume();
}
return promise.value;
}
};
static inline auto a() noexcept -> task<int> {
co_return 42;
}
static inline auto test() noexcept -> task<int> {
co_return co_await a();
}
int foo() {
return test().get();
}
// Checks that the store for the result value 42 is not misoptimized out.
// CHECK: define{{.*}}_Z3foov(
// CHECK: store i32 42, ptr %{{.*}}
// CHECK: }