2017-04-18 07:28:02 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcoroutines-ts -std=c++14 \
|
|
|
|
// 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 {
|
|
|
|
namespace experimental {
|
|
|
|
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;
|
|
|
|
static coroutine_handle from_address(void *) { return {}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct coroutine_handle<void> {
|
|
|
|
static coroutine_handle from_address(void *) { return {}; }
|
|
|
|
coroutine_handle() = default;
|
|
|
|
template <class PromiseType>
|
|
|
|
coroutine_handle(coroutine_handle<PromiseType>) {}
|
|
|
|
};
|
|
|
|
|
2017-04-18 11:12:48 +08:00
|
|
|
} // end namespace experimental
|
|
|
|
|
|
|
|
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 {
|
|
|
|
bool await_ready() { return false; }
|
2017-03-09 11:09:43 +08:00
|
|
|
void await_suspend(std::experimental::coroutine_handle<>) {}
|
2016-10-28 00:28:31 +08:00
|
|
|
void await_resume() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct global_new_delete_tag {};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct std::experimental::coroutine_traits<void, global_new_delete_tag> {
|
|
|
|
struct promise_type {
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
|
|
|
suspend_always final_suspend() { return {}; }
|
|
|
|
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()
|
2017-05-23 09:13:17 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @_Znwm(i64 %[[SIZE]])
|
|
|
|
// 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]]:
|
2016-10-28 00:28:31 +08:00
|
|
|
// CHECK: call void @_ZdlPv(i8* %[[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 {};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct std::experimental::coroutine_traits<void, promise_new_tag> {
|
|
|
|
struct promise_type {
|
|
|
|
void *operator new(unsigned long);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
|
|
|
suspend_always final_suspend() { return {}; }
|
|
|
|
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()
|
|
|
|
// CHECK: call i8* @_ZNSt12experimental16coroutine_traitsIJv15promise_new_tagEE12promise_typenwEm(i64 %[[SIZE]])
|
|
|
|
|
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: call void @_ZdlPv(i8* %[[MEM]])
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct promise_delete_tag {};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct std::experimental::coroutine_traits<void, promise_delete_tag> {
|
|
|
|
struct promise_type {
|
|
|
|
void operator delete(void*);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
|
|
|
suspend_always final_suspend() { return {}; }
|
|
|
|
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()
|
|
|
|
// CHECK: call i8* @_Znwm(i64 %[[SIZE]])
|
|
|
|
|
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: call void @_ZNSt12experimental16coroutine_traitsIJv18promise_delete_tagEE12promise_typedlEPv(i8* %[[MEM]])
|
2017-03-07 05:12:54 +08:00
|
|
|
co_return;
|
2016-10-28 00:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct promise_sized_delete_tag {};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct std::experimental::coroutine_traits<void, promise_sized_delete_tag> {
|
|
|
|
struct promise_type {
|
|
|
|
void operator delete(void*, unsigned long);
|
|
|
|
void get_return_object() {}
|
|
|
|
suspend_always initial_suspend() { return {}; }
|
|
|
|
suspend_always final_suspend() { return {}; }
|
|
|
|
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()
|
|
|
|
// CHECK: call i8* @_Znwm(i64 %[[SIZE]])
|
|
|
|
|
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()
|
|
|
|
// CHECK: call void @_ZNSt12experimental16coroutine_traitsIJv24promise_sized_delete_tagEE12promise_typedlEPvm(i8* %[[MEM]], i64 %[[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 {};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct std::experimental::coroutine_traits<int, promise_on_alloc_failure_tag> {
|
|
|
|
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 {}; }
|
|
|
|
suspend_always final_suspend() { return {}; }
|
|
|
|
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
|
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()
|
2017-04-18 11:12:48 +08:00
|
|
|
// CHECK: %[[MEM:.+]] = call i8* @_ZnwmRKSt9nothrow_t(i64 %[[SIZE]], %"struct.std::nothrow_t"* 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]]:
|
2017-05-23 04:22:23 +08:00
|
|
|
// CHECK: %[[FailRet:.+]] = call i32 @_ZNSt12experimental16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type39get_return_object_on_allocation_failureEv(
|
|
|
|
// CHECK: store i32 %[[FailRet]], i32* %[[RetVal]]
|
|
|
|
// CHECK: br label %[[RetBB:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[OKBB]]:
|
|
|
|
// CHECK: %[[OkRet:.+]] = call i32 @_ZNSt12experimental16coroutine_traitsIJi28promise_on_alloc_failure_tagEE12promise_type17get_return_objectEv(
|
|
|
|
// CHECK: store i32 %[[OkRet]], i32* %[[RetVal]]
|
|
|
|
|
|
|
|
// CHECK: [[RetBB]]:
|
|
|
|
// CHECK: %[[LoadRet:.+]] = load i32, i32* %[[RetVal]], align 4
|
|
|
|
// CHECK: ret i32 %[[LoadRet]]
|
2017-03-28 07:36:59 +08:00
|
|
|
co_return;
|
|
|
|
}
|