Implement `llvm.coro.await.suspend` intrinsics, to deal with performance regression after prohibiting `.await_suspend` inlining, as suggested in #64945. Actually, there are three new intrinsics, which directly correspond to each of three forms of `await_suspend`: ``` void llvm.coro.await.suspend.void(ptr %awaiter, ptr %frame, ptr @wrapperFunction) i1 llvm.coro.await.suspend.bool(ptr %awaiter, ptr %frame, ptr @wrapperFunction) ptr llvm.coro.await.suspend.handle(ptr %awaiter, ptr %frame, ptr @wrapperFunction) ``` There are three different versions instead of one, because in `bool` case it's result is used for resuming via a branch, and in `coroutine_handle` case exceptions from `await_suspend` are handled in the coroutine, and exceptions from the subsequent `.resume()` are propagated to the caller. Await-suspend block is simplified down to intrinsic calls only, for example for symmetric transfer: ``` %id = call token @llvm.coro.save(ptr null) %handle = call ptr @llvm.coro.await.suspend.handle(ptr %awaiter, ptr %frame, ptr @wrapperFunction) call void @llvm.coro.resume(%handle) %result = call i8 @llvm.coro.suspend(token %id, i1 false) switch i8 %result, ... ``` All await-suspend logic is moved out into a wrapper function, generated for each suspension point. The signature of the function is `<type> wrapperFunction(ptr %awaiter, ptr %frame)` where `<type>` is one of `void` `i1` or `ptr`, depending on the return type of `await_suspend`. Intrinsic calls are lowered during `CoroSplit` pass, right after the split. Because I'm new to LLVM, I'm not sure if the helper function generation, calls to them and lowering are implemented in the right way, especially with regard to various metadata and attributes, i. e. for TBAA. All things that seemed questionable are marked with `FIXME` comments. There is another detail: in case of symmetric transfer raw pointer to the frame of coroutine, that should be resumed, is returned from the helper function and a direct call to `@llvm.coro.resume` is generated. C++ standard demands, that `.resume()` method is evaluated. Not sure how important is this, because code has been generated in the same way before, sans helper function.
104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fsyntax-only -ast-dump %s | FileCheck %s
|
|
|
|
#include "Inputs/std-coroutine.h"
|
|
|
|
using namespace std;
|
|
|
|
struct Task {
|
|
struct promise_type {
|
|
Task get_return_object() noexcept {
|
|
return Task{coroutine_handle<promise_type>::from_promise(*this)};
|
|
}
|
|
|
|
void return_void() noexcept {}
|
|
|
|
struct final_awaiter {
|
|
bool await_ready() noexcept { return false; }
|
|
coroutine_handle<> await_suspend(coroutine_handle<promise_type> h) noexcept {
|
|
h.destroy();
|
|
return {};
|
|
}
|
|
void await_resume() noexcept {}
|
|
};
|
|
|
|
void unhandled_exception() noexcept {}
|
|
|
|
final_awaiter final_suspend() noexcept { return {}; }
|
|
|
|
suspend_always initial_suspend() noexcept { return {}; }
|
|
|
|
template <typename Awaitable>
|
|
auto await_transform(Awaitable &&awaitable) {
|
|
return awaitable.co_viaIfAsync();
|
|
}
|
|
};
|
|
|
|
using handle_t = coroutine_handle<promise_type>;
|
|
|
|
class Awaiter {
|
|
public:
|
|
explicit Awaiter(handle_t coro) noexcept;
|
|
Awaiter(Awaiter &&other) noexcept;
|
|
Awaiter(const Awaiter &) = delete;
|
|
~Awaiter();
|
|
|
|
bool await_ready() noexcept { return false; }
|
|
handle_t await_suspend(coroutine_handle<> continuation) noexcept;
|
|
void await_resume();
|
|
|
|
private:
|
|
handle_t coro_;
|
|
};
|
|
|
|
Task(handle_t coro) noexcept : coro_(coro) {}
|
|
|
|
handle_t coro_;
|
|
|
|
Task(const Task &t) = delete;
|
|
Task(Task &&t) noexcept;
|
|
~Task();
|
|
Task &operator=(Task t) noexcept;
|
|
|
|
Awaiter co_viaIfAsync();
|
|
};
|
|
|
|
static Task foo() {
|
|
co_return;
|
|
}
|
|
|
|
Task bar() {
|
|
auto mode = 2;
|
|
switch (mode) {
|
|
case 1:
|
|
co_await foo();
|
|
break;
|
|
case 2:
|
|
co_await foo();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// CHECK-LABEL: FunctionDecl {{.*}} bar 'Task ()'
|
|
// CHECK: SwitchStmt
|
|
// CHECK: CaseStmt
|
|
// CHECK: ExprWithCleanups {{.*}} 'void'
|
|
// CHECK-NEXT: CoawaitExpr
|
|
// CHECK-NEXT: CXXBindTemporaryExpr {{.*}} 'Task' (CXXTemporary {{.*}})
|
|
// CHECK: MaterializeTemporaryExpr {{.*}} 'Awaiter':'Task::Awaiter'
|
|
// CHECK: ExprWithCleanups {{.*}} 'bool'
|
|
// CHECK-NEXT: CXXMemberCallExpr {{.*}} 'bool'
|
|
// CHECK-NEXT: MemberExpr {{.*}} .await_ready
|
|
// CHECK: ExprWithCleanups {{.*}} 'void *'
|
|
|
|
// CHECK: CaseStmt
|
|
// CHECK: ExprWithCleanups {{.*}} 'void'
|
|
// CHECK-NEXT: CoawaitExpr
|
|
// CHECK-NEXT: CXXBindTemporaryExpr {{.*}} 'Task' (CXXTemporary {{.*}})
|
|
// CHECK: MaterializeTemporaryExpr {{.*}} 'Awaiter':'Task::Awaiter'
|
|
// CHECK: ExprWithCleanups {{.*}} 'bool'
|
|
// CHECK-NEXT: CXXMemberCallExpr {{.*}} 'bool'
|
|
// CHECK-NEXT: MemberExpr {{.*}} .await_ready
|
|
// CHECK: ExprWithCleanups {{.*}} 'void *'
|