2021-11-04 11:50:30 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 \
|
2017-04-18 07:28:02 +08:00
|
|
|
// RUN: -Wno-coroutine-missing-unhandled-exception -emit-llvm %s -o - -disable-llvm-passes \
|
|
|
|
// RUN: | FileCheck %s
|
2016-10-28 00:28:31 +08:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <typename... T>
|
|
|
|
struct coroutine_traits; // expected-note {{declared here}}
|
2017-03-09 11:09:43 +08:00
|
|
|
|
|
|
|
template <class Promise = void>
|
|
|
|
struct coroutine_handle {
|
|
|
|
coroutine_handle() = default;
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
static coroutine_handle from_address(void *) noexcept { return {}; }
|
2017-03-09 11:09:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct coroutine_handle<void> {
|
|
|
|
static coroutine_handle from_address(void *) { return {}; }
|
|
|
|
coroutine_handle() = default;
|
|
|
|
template <class PromiseType>
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
coroutine_handle(coroutine_handle<PromiseType>) noexcept {}
|
2017-03-09 11:09:43 +08:00
|
|
|
};
|
|
|
|
|
2017-04-18 11:12:48 +08:00
|
|
|
struct nothrow_t {};
|
|
|
|
constexpr nothrow_t nothrow = {};
|
|
|
|
|
|
|
|
} // end namespace std
|
|
|
|
|
|
|
|
// Required when get_return_object_on_allocation_failure() is defined by
|
|
|
|
// the promise.
|
|
|
|
using SizeT = decltype(sizeof(int));
|
|
|
|
void* operator new(SizeT __sz, const std::nothrow_t&) noexcept;
|
|
|
|
void operator delete(void* __p, const std::nothrow_t&) noexcept;
|
|
|
|
|
2016-10-28 00:28:31 +08:00
|
|
|
|
|
|
|
struct suspend_always {
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
bool await_ready() noexcept { return false; }
|
2021-11-04 11:50:30 +08:00
|
|
|
void await_suspend(std::coroutine_handle<>) noexcept {}
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
void await_resume() noexcept {}
|
2016-10-28 00:28:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct global_new_delete_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, global_new_delete_tag> {
|
2016-10-28 00:28:31 +08:00
|
|
|
struct promise_type {
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2016-10-28 00:28:31 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-28 07:36:59 +08:00
|
|
|
// CHECK-LABEL: f0(
|
2016-10-28 00:28:31 +08:00
|
|
|
extern "C" void f0(global_new_delete_tag) {
|
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
2017-05-23 09:13:17 +08:00
|
|
|
// CHECK: %[[NeedAlloc:.+]] = call i1 @llvm.coro.alloc(token %[[ID]])
|
|
|
|
// CHECK: br i1 %[[NeedAlloc]], label %[[AllocBB:.+]], label %[[InitBB:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[AllocBB]]:
|
2016-10-28 00:28:31 +08:00
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
|
2017-05-23 09:13:17 +08:00
|
|
|
// CHECK: br label %[[InitBB]]
|
|
|
|
|
|
|
|
// CHECK: [[InitBB]]:
|
|
|
|
// CHECK: %[[PHI:.+]] = phi i8* [ null, %{{.+}} ], [ %call, %[[AllocBB]] ]
|
2017-05-23 11:46:59 +08:00
|
|
|
// CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(token %[[ID]], i8* %[[PHI]])
|
2016-10-28 00:28:31 +08:00
|
|
|
|
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
|
2017-05-23 12:21:27 +08:00
|
|
|
// CHECK: %[[NeedDealloc:.+]] = icmp ne i8* %[[MEM]], null
|
|
|
|
// CHECK: br i1 %[[NeedDealloc]], label %[[FreeBB:.+]], label %[[Afterwards:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[FreeBB]]:
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call void @_ZdlPv(i8* noundef %[[MEM]])
|
2017-05-23 12:21:27 +08:00
|
|
|
// CHECK: br label %[[Afterwards]]
|
|
|
|
|
|
|
|
// CHECK: [[Afterwards]]:
|
|
|
|
// CHECK: ret void
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct promise_new_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, promise_new_tag> {
|
2016-10-28 00:28:31 +08:00
|
|
|
struct promise_type {
|
|
|
|
void *operator new(unsigned long);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2016-10-28 00:28:31 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-28 07:36:59 +08:00
|
|
|
// CHECK-LABEL: f1(
|
2016-10-28 00:28:31 +08:00
|
|
|
extern "C" void f1(promise_new_tag ) {
|
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call noundef i8* @_ZNSt16coroutine_traitsIJv15promise_new_tagEE12promise_typenwEm(i64 noundef %[[SIZE]])
|
2016-10-28 00:28:31 +08:00
|
|
|
|
2017-05-23 11:46:59 +08:00
|
|
|
// CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
|
2016-10-28 00:28:31 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call void @_ZdlPv(i8* noundef %[[MEM]])
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
struct promise_matching_placement_new_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, promise_matching_placement_new_tag, int, float, double> {
|
2018-02-16 04:37:22 +08:00
|
|
|
struct promise_type {
|
|
|
|
void *operator new(unsigned long, promise_matching_placement_new_tag,
|
|
|
|
int, float, double);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2018-02-16 04:37:22 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// CHECK-LABEL: f1a(
|
|
|
|
extern "C" void f1a(promise_matching_placement_new_tag, int x, float y , double z) {
|
|
|
|
// CHECK: store i32 %x, i32* %x.addr, align 4
|
|
|
|
// CHECK: store float %y, float* %y.addr, align 4
|
|
|
|
// CHECK: store double %z, double* %z.addr, align 8
|
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
|
|
|
// CHECK: %[[INT:.+]] = load i32, i32* %x.addr, align 4
|
|
|
|
// CHECK: %[[FLOAT:.+]] = load float, float* %y.addr, align 4
|
|
|
|
// CHECK: %[[DOUBLE:.+]] = load double, double* %z.addr, align 8
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call noundef i8* @_ZNSt16coroutine_traitsIJv34promise_matching_placement_new_tagifdEE12promise_typenwEmS0_ifd(i64 noundef %[[SIZE]], i32 noundef %[[INT]], float noundef %[[FLOAT]], double noundef %[[DOUBLE]])
|
2018-02-16 04:37:22 +08:00
|
|
|
co_return;
|
|
|
|
}
|
|
|
|
|
[Coroutines] Find custom allocators in class scope
Summary:
https://reviews.llvm.org/rL325291 implemented Coroutines TS N4723
section [dcl.fct.def.coroutine]/7, but it performed lookup of allocator
functions within both the global and class scope, whereas the specified
behavior is to perform lookup for custom allocators within just the
class scope.
To fix, add parameters to the `Sema::FindAllocationFunctions` function
such that it can be used to lookup allocators in global scope,
class scope, or both (instead of just being able to look up in just global
scope or in both global and class scope). Then, use those parameters
from within the coroutine Sema.
This incorrect behavior had the unfortunate side-effect of causing the
bug https://bugs.llvm.org/show_bug.cgi?id=36578 (or at least the reports
of that bug in C++ programs). That bug would occur for any C++ user with
a coroutine frame that took a single pointer argument, since it would
then find the global placement form `operator new`, described in the
C++ standard 18.6.1.3.1. This patch prevents Clang from generating code
that triggers the LLVM assert described in that bug report.
Test Plan: `check-clang`
Reviewers: GorNishanov, eric_niebler, lewissbaker
Reviewed By: GorNishanov
Subscribers: EricWF, cfe-commits
Differential Revision: https://reviews.llvm.org/D44552
llvm-svn: 328949
2018-04-02 06:59:22 +08:00
|
|
|
// Declare a placement form operator new, such as the one described in
|
|
|
|
// C++ 18.6.1.3.1, which takes a void* argument.
|
|
|
|
void* operator new(SizeT __sz, void *__p) noexcept;
|
|
|
|
|
|
|
|
struct promise_matching_global_placement_new_tag {};
|
|
|
|
struct dummy {};
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, promise_matching_global_placement_new_tag, dummy *> {
|
[Coroutines] Find custom allocators in class scope
Summary:
https://reviews.llvm.org/rL325291 implemented Coroutines TS N4723
section [dcl.fct.def.coroutine]/7, but it performed lookup of allocator
functions within both the global and class scope, whereas the specified
behavior is to perform lookup for custom allocators within just the
class scope.
To fix, add parameters to the `Sema::FindAllocationFunctions` function
such that it can be used to lookup allocators in global scope,
class scope, or both (instead of just being able to look up in just global
scope or in both global and class scope). Then, use those parameters
from within the coroutine Sema.
This incorrect behavior had the unfortunate side-effect of causing the
bug https://bugs.llvm.org/show_bug.cgi?id=36578 (or at least the reports
of that bug in C++ programs). That bug would occur for any C++ user with
a coroutine frame that took a single pointer argument, since it would
then find the global placement form `operator new`, described in the
C++ standard 18.6.1.3.1. This patch prevents Clang from generating code
that triggers the LLVM assert described in that bug report.
Test Plan: `check-clang`
Reviewers: GorNishanov, eric_niebler, lewissbaker
Reviewed By: GorNishanov
Subscribers: EricWF, cfe-commits
Differential Revision: https://reviews.llvm.org/D44552
llvm-svn: 328949
2018-04-02 06:59:22 +08:00
|
|
|
struct promise_type {
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
[Coroutines] Find custom allocators in class scope
Summary:
https://reviews.llvm.org/rL325291 implemented Coroutines TS N4723
section [dcl.fct.def.coroutine]/7, but it performed lookup of allocator
functions within both the global and class scope, whereas the specified
behavior is to perform lookup for custom allocators within just the
class scope.
To fix, add parameters to the `Sema::FindAllocationFunctions` function
such that it can be used to lookup allocators in global scope,
class scope, or both (instead of just being able to look up in just global
scope or in both global and class scope). Then, use those parameters
from within the coroutine Sema.
This incorrect behavior had the unfortunate side-effect of causing the
bug https://bugs.llvm.org/show_bug.cgi?id=36578 (or at least the reports
of that bug in C++ programs). That bug would occur for any C++ user with
a coroutine frame that took a single pointer argument, since it would
then find the global placement form `operator new`, described in the
C++ standard 18.6.1.3.1. This patch prevents Clang from generating code
that triggers the LLVM assert described in that bug report.
Test Plan: `check-clang`
Reviewers: GorNishanov, eric_niebler, lewissbaker
Reviewed By: GorNishanov
Subscribers: EricWF, cfe-commits
Differential Revision: https://reviews.llvm.org/D44552
llvm-svn: 328949
2018-04-02 06:59:22 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// A coroutine that takes a single pointer argument should not invoke this
|
|
|
|
// placement form operator. [dcl.fct.def.coroutine]/7 dictates that lookup for
|
|
|
|
// allocation functions matching the coroutine function's signature be done
|
|
|
|
// within the scope of the promise type's class.
|
|
|
|
// CHECK-LABEL: f1b(
|
|
|
|
extern "C" void f1b(promise_matching_global_placement_new_tag, dummy *) {
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call noalias noundef nonnull i8* @_Znwm(i64
|
[Coroutines] Find custom allocators in class scope
Summary:
https://reviews.llvm.org/rL325291 implemented Coroutines TS N4723
section [dcl.fct.def.coroutine]/7, but it performed lookup of allocator
functions within both the global and class scope, whereas the specified
behavior is to perform lookup for custom allocators within just the
class scope.
To fix, add parameters to the `Sema::FindAllocationFunctions` function
such that it can be used to lookup allocators in global scope,
class scope, or both (instead of just being able to look up in just global
scope or in both global and class scope). Then, use those parameters
from within the coroutine Sema.
This incorrect behavior had the unfortunate side-effect of causing the
bug https://bugs.llvm.org/show_bug.cgi?id=36578 (or at least the reports
of that bug in C++ programs). That bug would occur for any C++ user with
a coroutine frame that took a single pointer argument, since it would
then find the global placement form `operator new`, described in the
C++ standard 18.6.1.3.1. This patch prevents Clang from generating code
that triggers the LLVM assert described in that bug report.
Test Plan: `check-clang`
Reviewers: GorNishanov, eric_niebler, lewissbaker
Reviewed By: GorNishanov
Subscribers: EricWF, cfe-commits
Differential Revision: https://reviews.llvm.org/D44552
llvm-svn: 328949
2018-04-02 06:59:22 +08:00
|
|
|
co_return;
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:28:31 +08:00
|
|
|
struct promise_delete_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, promise_delete_tag> {
|
2016-10-28 00:28:31 +08:00
|
|
|
struct promise_type {
|
|
|
|
void operator delete(void*);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2016-10-28 00:28:31 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-28 07:36:59 +08:00
|
|
|
// CHECK-LABEL: f2(
|
2016-10-28 00:28:31 +08:00
|
|
|
extern "C" void f2(promise_delete_tag) {
|
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
|
2016-10-28 00:28:31 +08:00
|
|
|
|
2017-05-23 11:46:59 +08:00
|
|
|
// CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
|
2016-10-28 00:28:31 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call void @_ZNSt16coroutine_traitsIJv18promise_delete_tagEE12promise_typedlEPv(i8* noundef %[[MEM]])
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct promise_sized_delete_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<void, promise_sized_delete_tag> {
|
2016-10-28 00:28:31 +08:00
|
|
|
struct promise_type {
|
|
|
|
void operator delete(void*, unsigned long);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2016-10-28 00:28:31 +08:00
|
|
|
void return_void() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-07 05:12:54 +08:00
|
|
|
// CHECK-LABEL: f3(
|
2016-10-28 00:28:31 +08:00
|
|
|
extern "C" void f3(promise_sized_delete_tag) {
|
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call noalias noundef nonnull i8* @_Znwm(i64 noundef %[[SIZE]])
|
2016-10-28 00:28:31 +08:00
|
|
|
|
2017-05-23 11:46:59 +08:00
|
|
|
// CHECK: %[[FRAME:.+]] = call i8* @llvm.coro.begin(
|
2016-10-28 00:28:31 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @llvm.coro.free(token %[[ID]], i8* %[[FRAME]])
|
|
|
|
// CHECK: %[[SIZE2:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: call void @_ZNSt16coroutine_traitsIJv24promise_sized_delete_tagEE12promise_typedlEPvm(i8* noundef %[[MEM]], i64 noundef %[[SIZE2]])
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
|
|
struct promise_on_alloc_failure_tag {};
|
|
|
|
|
2021-11-04 11:50:30 +08:00
|
|
|
template <>
|
|
|
|
struct std::coroutine_traits<int, promise_on_alloc_failure_tag> {
|
2017-03-28 07:36:59 +08:00
|
|
|
struct promise_type {
|
2017-04-18 07:28:02 +08:00
|
|
|
int get_return_object() { return 0; }
|
2017-03-28 07:36:59 +08:00
|
|
|
suspend_always initial_suspend() { return {}; }
|
[Coroutines] Ensure co_await promise.final_suspend() does not throw
Summary:
This patch addresses https://bugs.llvm.org/show_bug.cgi?id=46256
The spec of coroutine requires that the expression co_await promise.final_suspend() shall not be potentially-throwing.
To check this, we recursively look at every call (including Call, MemberCall, OperatorCall and Constructor) in all code
generated by the final suspend, and ensure that the callees are declared with noexcept. We also look at any returned data
type that requires explicit destruction, and check their destructors for noexcept.
This patch does not check declarations with dependent types yet, which will be done in future patches.
Updated all tests to add noexcept to the required functions, and added a dedicated test for this patch.
This patch might start to cause existing codebase fail to compile because most people may not have been strict in tagging
all the related functions noexcept.
Reviewers: lewissbaker, modocache, junparser
Reviewed By: modocache
Subscribers: arphaman, junparser, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82029
2020-06-16 07:27:41 +08:00
|
|
|
suspend_always final_suspend() noexcept { return {}; }
|
2017-03-28 07:36:59 +08:00
|
|
|
void return_void() {}
|
|
|
|
static int get_return_object_on_allocation_failure() { return -1; }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// CHECK-LABEL: f4(
|
|
|
|
extern "C" int f4(promise_on_alloc_failure_tag) {
|
2017-05-23 04:22:23 +08:00
|
|
|
// CHECK: %[[RetVal:.+]] = alloca i32
|
2018-02-02 07:47:54 +08:00
|
|
|
// CHECK: %[[Gro:.+]] = alloca i32
|
2017-03-28 07:36:59 +08:00
|
|
|
// CHECK: %[[ID:.+]] = call token @llvm.coro.id(i32 16
|
|
|
|
// CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call noalias noundef i8* @_ZnwmRKSt9nothrow_t(i64 noundef %[[SIZE]], %"struct.std::nothrow_t"* noundef nonnull align 1 dereferenceable(1) @_ZStL7nothrow)
|
2017-03-28 07:36:59 +08:00
|
|
|
// CHECK: %[[OK:.+]] = icmp ne i8* %[[MEM]], null
|
|
|
|
// CHECK: br i1 %[[OK]], label %[[OKBB:.+]], label %[[ERRBB:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[ERRBB]]:
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: %[[FailRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type39get_return_object_on_allocation_failureEv(
|
2017-05-23 04:22:23 +08:00
|
|
|
// CHECK: store i32 %[[FailRet]], i32* %[[RetVal]]
|
|
|
|
// CHECK: br label %[[RetBB:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[OKBB]]:
|
2022-01-16 17:53:11 +08:00
|
|
|
// CHECK: %[[OkRet:.+]] = call noundef i32 @_ZNSt16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type17get_return_objectEv(
|
2018-02-02 07:47:54 +08:00
|
|
|
// CHECK: store i32 %[[OkRet]], i32* %[[Gro]]
|
|
|
|
|
|
|
|
// CHECK: %[[Tmp1:.*]] = load i32, i32* %[[Gro]]
|
|
|
|
// CHECK-NEXT: store i32 %[[Tmp1]], i32* %[[RetVal]]
|
[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-NEXT: %[[Gro_CAST:.+]] = bitcast i32* %[[Gro]] to i8*
|
|
|
|
// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 4, i8* %[[Gro_CAST]]) #2
|
2018-02-02 07:47:54 +08:00
|
|
|
// CHECK-NEXT: br label %[[RetBB]]
|
2017-05-23 04:22:23 +08:00
|
|
|
|
|
|
|
// CHECK: [[RetBB]]:
|
|
|
|
// CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]], align 4
|
|
|
|
// CHECK: ret i32 %[[LoadRet]]
|
2017-03-28 07:36:59 +08:00
|
|
|
co_return;
|
|
|
|
}
|