llvm-project/clang/test/CodeGenCoroutines/coro-await-resume-eh.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
4.2 KiB
C++
Raw Normal View History

// Test the behavior of http://wg21.link/P0664, a proposal to catch any
// exceptions thrown after the initial suspend point of a coroutine by
// executing the handler specified by the promise type's 'unhandled_exception'
// member function.
//
// RUN: %clang_cc1 -std=c++14 -fcoroutines-ts \
// RUN: -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s \
// RUN: -fexceptions -fcxx-exceptions -disable-llvm-passes \
// RUN: | FileCheck %s
#include "Inputs/coroutine.h"
namespace coro = std::experimental::coroutines_v1;
struct throwing_awaitable {
bool await_ready() { return true; }
void await_suspend(coro::coroutine_handle<>) {}
void await_resume() { throw 42; }
};
struct throwing_task {
struct promise_type {
auto get_return_object() { return throwing_task{}; }
auto initial_suspend() { return throwing_awaitable{}; }
auto final_suspend() noexcept { return coro::suspend_never{}; }
void return_void() {}
void unhandled_exception() {}
};
};
// CHECK-LABEL: define{{.*}} void @_Z1fv()
throwing_task f() {
// A variable RESUMETHREW is used to keep track of whether the body
// of 'await_resume' threw an exception. Exceptions thrown in
// 'await_resume' are unwound to RESUMELPAD.
// CHECK: init.ready:
// CHECK-NEXT: store i1 true, i1* %[[RESUMETHREW:.+]], align 1
// CHECK-NEXT: invoke void @_ZN18throwing_awaitable12await_resumeEv
// CHECK-NEXT: to label %[[RESUMECONT:.+]] unwind label %[[RESUMELPAD:.+]]
// If 'await_resume' does not throw an exception, 'false' is stored in
// variable RESUMETHREW.
// CHECK: [[RESUMECONT]]:
// CHECK-NEXT: store i1 false, i1* %[[RESUMETHREW]]
// CHECK-NEXT: br label %[[RESUMETRYCONT:.+]]
// 'unhandled_exception' is called for the exception thrown in
// 'await_resume'. The variable RESUMETHREW is never set to false,
// and a jump is made to RESUMETRYCONT.
// CHECK: [[RESUMELPAD]]:
// CHECK: br label %[[RESUMECATCH:.+]]
// CHECK: [[RESUMECATCH]]:
// CHECK: invoke void @_ZN13throwing_task12promise_type19unhandled_exceptionEv
// CHECK-NEXT: to label %[[RESUMEENDCATCH:.+]] unwind label
// CHECK: [[RESUMEENDCATCH]]:
// CHECK-NEXT: invoke void @__cxa_end_catch()
// CHECK-NEXT: to label %[[RESUMEENDCATCHCONT:.+]] unwind label
// CHECK: [[RESUMEENDCATCHCONT]]:
// CHECK-NEXT: br label %[[RESUMETRYCONT]]
[Coroutine][Clang] Force emit lifetime intrinsics for Coroutines tl;dr Correct implementation of Corouintes requires having lifetime intrinsics available. Coroutine functions are functions that can be suspended and resumed latter. To do so, data that need to stay alive after suspension must be put on the heap (i.e. the coroutine frame). The optimizer is responsible for analyzing each AllocaInst and figure out whether it should be put on the stack or the frame. In most cases, for data that we are unable to accurately analyze lifetime, we can just conservatively put them on the heap. Unfortunately, there exists a few cases where certain data MUST be put on the stack, not on the heap. Without lifetime intrinsics, we are unable to correctly analyze those data's lifetime. To dig into more details, there exists cases where at certain code points, the current coroutine frame may have already been destroyed. Hence no frame access would be allowed beyond that point. The following is a common code pattern called "Symmetric Transfer" in coroutine: ``` auto tmp = await_suspend(); __builtin_coro_resume(tmp.address()); return; ``` In the above code example, `await_suspend()` returns a new coroutine handle, which we will obtain the address and then resume that coroutine. This essentially "transfered" from the current coroutine to a different coroutine. During the call to `await_suspend()`, the current coroutine may be destroyed, which should be fine because we are not accessing any data afterwards. However when LLVM is emitting IR for the above code, it needs to emit an AllocaInst for `tmp`. It will then call the `address` function on tmp. `address` function is a member function of coroutine, and there is no way for the LLVM optimizer to know that it does not capture the `tmp` pointer. So when the optimizer looks at it, it has to conservatively assume that `tmp` may escape and hence put it on the heap. Furthermore, in some cases `address` call would be inlined, which will generate a bunch of store/load instructions that move the `tmp` pointer around. Those stores will also make the compiler to think that `tmp` might escape. To summarize, it's really difficult for the mid-end to figure out that the `tmp` data is short-lived. I made some attempt in D98638, but it appears to be way too complex and is basically doing the same thing as inserting lifetime intrinsics in coroutines. Also, for reference, we already force emitting lifetime intrinsics in O0 for AlwaysInliner: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Passes/PassBuilder.cpp#L1893 Differential Revision: https://reviews.llvm.org/D99227
2021-03-26 04:46:20 +08:00
// CHECK: [[RESUMETRYCONT]]:
// CHECK-NEXT: br label %[[CLEANUP:.+]]
// CHECK: [[CLEANUP]]:
// CHECK: switch i32 %{{.+}}, label %{{.+}} [
// CHECK-NEXT: i32 0, label %[[CLEANUPCONT:.+]]
// CHECK-NEXT: ]
// The variable RESUMETHREW is loaded and if true, then 'await_resume'
// threw an exception and the coroutine body is skipped, and the final
// suspend is executed immediately. Otherwise, the coroutine body is
// executed, and then the final suspend.
[Coroutine][Clang] Force emit lifetime intrinsics for Coroutines tl;dr Correct implementation of Corouintes requires having lifetime intrinsics available. Coroutine functions are functions that can be suspended and resumed latter. To do so, data that need to stay alive after suspension must be put on the heap (i.e. the coroutine frame). The optimizer is responsible for analyzing each AllocaInst and figure out whether it should be put on the stack or the frame. In most cases, for data that we are unable to accurately analyze lifetime, we can just conservatively put them on the heap. Unfortunately, there exists a few cases where certain data MUST be put on the stack, not on the heap. Without lifetime intrinsics, we are unable to correctly analyze those data's lifetime. To dig into more details, there exists cases where at certain code points, the current coroutine frame may have already been destroyed. Hence no frame access would be allowed beyond that point. The following is a common code pattern called "Symmetric Transfer" in coroutine: ``` auto tmp = await_suspend(); __builtin_coro_resume(tmp.address()); return; ``` In the above code example, `await_suspend()` returns a new coroutine handle, which we will obtain the address and then resume that coroutine. This essentially "transfered" from the current coroutine to a different coroutine. During the call to `await_suspend()`, the current coroutine may be destroyed, which should be fine because we are not accessing any data afterwards. However when LLVM is emitting IR for the above code, it needs to emit an AllocaInst for `tmp`. It will then call the `address` function on tmp. `address` function is a member function of coroutine, and there is no way for the LLVM optimizer to know that it does not capture the `tmp` pointer. So when the optimizer looks at it, it has to conservatively assume that `tmp` may escape and hence put it on the heap. Furthermore, in some cases `address` call would be inlined, which will generate a bunch of store/load instructions that move the `tmp` pointer around. Those stores will also make the compiler to think that `tmp` might escape. To summarize, it's really difficult for the mid-end to figure out that the `tmp` data is short-lived. I made some attempt in D98638, but it appears to be way too complex and is basically doing the same thing as inserting lifetime intrinsics in coroutines. Also, for reference, we already force emitting lifetime intrinsics in O0 for AlwaysInliner: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Passes/PassBuilder.cpp#L1893 Differential Revision: https://reviews.llvm.org/D99227
2021-03-26 04:46:20 +08:00
// CHECK: [[CLEANUPCONT]]:
// CHECK-NEXT: %[[RESUMETHREWLOAD:.+]] = load i1, i1* %[[RESUMETHREW]]
// CHECK-NEXT: br i1 %[[RESUMETHREWLOAD]], label %[[RESUMEDCONT:.+]], label %[[RESUMEDBODY:.+]]
// CHECK: [[RESUMEDBODY]]:
// CHECK: invoke void @_ZN13throwing_task12promise_type11return_voidEv
// CHECK-NEXT: to label %[[REDUMEDBODYCONT:.+]] unwind label
// CHECK: [[REDUMEDBODYCONT]]:
// CHECK-NEXT: br label %[[COROFINAL:.+]]
// CHECK: [[RESUMEDCONT]]:
// CHECK-NEXT: br label %[[COROFINAL]]
// CHECK: [[COROFINAL]]:
[Coroutine][Clang] Force emit lifetime intrinsics for Coroutines tl;dr Correct implementation of Corouintes requires having lifetime intrinsics available. Coroutine functions are functions that can be suspended and resumed latter. To do so, data that need to stay alive after suspension must be put on the heap (i.e. the coroutine frame). The optimizer is responsible for analyzing each AllocaInst and figure out whether it should be put on the stack or the frame. In most cases, for data that we are unable to accurately analyze lifetime, we can just conservatively put them on the heap. Unfortunately, there exists a few cases where certain data MUST be put on the stack, not on the heap. Without lifetime intrinsics, we are unable to correctly analyze those data's lifetime. To dig into more details, there exists cases where at certain code points, the current coroutine frame may have already been destroyed. Hence no frame access would be allowed beyond that point. The following is a common code pattern called "Symmetric Transfer" in coroutine: ``` auto tmp = await_suspend(); __builtin_coro_resume(tmp.address()); return; ``` In the above code example, `await_suspend()` returns a new coroutine handle, which we will obtain the address and then resume that coroutine. This essentially "transfered" from the current coroutine to a different coroutine. During the call to `await_suspend()`, the current coroutine may be destroyed, which should be fine because we are not accessing any data afterwards. However when LLVM is emitting IR for the above code, it needs to emit an AllocaInst for `tmp`. It will then call the `address` function on tmp. `address` function is a member function of coroutine, and there is no way for the LLVM optimizer to know that it does not capture the `tmp` pointer. So when the optimizer looks at it, it has to conservatively assume that `tmp` may escape and hence put it on the heap. Furthermore, in some cases `address` call would be inlined, which will generate a bunch of store/load instructions that move the `tmp` pointer around. Those stores will also make the compiler to think that `tmp` might escape. To summarize, it's really difficult for the mid-end to figure out that the `tmp` data is short-lived. I made some attempt in D98638, but it appears to be way too complex and is basically doing the same thing as inserting lifetime intrinsics in coroutines. Also, for reference, we already force emitting lifetime intrinsics in O0 for AlwaysInliner: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Passes/PassBuilder.cpp#L1893 Differential Revision: https://reviews.llvm.org/D99227
2021-03-26 04:46:20 +08:00
// CHECK: call void @_ZN13throwing_task12promise_type13final_suspendEv
co_return;
}
struct noexcept_awaitable {
bool await_ready() { return true; }
void await_suspend(coro::coroutine_handle<>) {}
void await_resume() noexcept {}
};
struct noexcept_task {
struct promise_type {
auto get_return_object() { return noexcept_task{}; }
auto initial_suspend() { return noexcept_awaitable{}; }
auto final_suspend() noexcept { return coro::suspend_never{}; }
void return_void() {}
void unhandled_exception() {}
};
};
// CHECK-LABEL: define{{.*}} void @_Z1gv()
noexcept_task g() {
// If the await_resume function is marked as noexcept, none of the additional
// conditions that are present in f() above are added to the IR.
// This means that no i1 are stored before or after calling await_resume:
// CHECK: init.ready:
// CHECK-NEXT: call void @_ZN18noexcept_awaitable12await_resumeEv
// CHECK-NOT: store i1 false, i1*
co_return;
}