2017-04-01 08:22:47 +08:00
|
|
|
// Verify that coroutine promise and allocated memory are freed up on exception.
|
2020-02-04 02:09:39 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++1z -fcoroutines-ts -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -disable-llvm-passes | FileCheck %s
|
2017-04-01 08:22:47 +08:00
|
|
|
|
|
|
|
namespace std::experimental {
|
|
|
|
template <typename... T> struct coroutine_traits;
|
|
|
|
|
|
|
|
template <class Promise = void> struct coroutine_handle {
|
|
|
|
coroutine_handle() = default;
|
2017-05-23 06:33:17 +08:00
|
|
|
static coroutine_handle from_address(void *) noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
};
|
|
|
|
template <> struct coroutine_handle<void> {
|
2017-05-23 06:33:17 +08:00
|
|
|
static coroutine_handle from_address(void *) noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
coroutine_handle() = default;
|
|
|
|
template <class PromiseType>
|
2017-05-23 06:33:17 +08:00
|
|
|
coroutine_handle(coroutine_handle<PromiseType>) noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct suspend_always {
|
2017-05-23 06:33:17 +08:00
|
|
|
bool await_ready() noexcept;
|
|
|
|
void await_suspend(std::experimental::coroutine_handle<>) noexcept;
|
|
|
|
void await_resume() noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct std::experimental::coroutine_traits<void> {
|
|
|
|
struct promise_type {
|
2017-05-23 06:33:17 +08:00
|
|
|
void get_return_object() noexcept;
|
|
|
|
suspend_always initial_suspend() noexcept;
|
|
|
|
suspend_always final_suspend() noexcept;
|
|
|
|
void return_void() noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
promise_type();
|
|
|
|
~promise_type();
|
2017-05-23 06:33:17 +08:00
|
|
|
void unhandled_exception() noexcept;
|
2017-04-01 08:22:47 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Cleanup { ~Cleanup(); };
|
|
|
|
void may_throw();
|
|
|
|
|
2020-12-31 16:27:11 +08:00
|
|
|
// CHECK-LABEL: define{{.*}} void @_Z1fv(
|
2017-04-01 08:22:47 +08:00
|
|
|
void f() {
|
[clang] Annotating C++'s `operator new` with more attributes
Summary:
Right now we annotate C++'s `operator new` with `noalias` attribute,
which very much is healthy for optimizations.
However as per [[ http://eel.is/c++draft/basic.stc.dynamic.allocation | `[basic.stc.dynamic.allocation]` ]],
there are more promises on global `operator new`, namely:
* non-`std::nothrow_t` `operator new` *never* returns `nullptr`
* If `std::align_val_t align` parameter is taken, the pointer will also be `align`-aligned
* ~~global `operator new`-returned pointer is `__STDCPP_DEFAULT_NEW_ALIGNMENT__`-aligned ~~ It's more caveated than that.
Supplying this information may not cause immediate landslide effects
on any specific benchmarks, but it for sure will be healthy for optimizer
in the sense that the IR will better reflect the guarantees provided in the source code.
The caveat is `-fno-assume-sane-operator-new`, which currently prevents emitting `noalias`
attribute, and is automatically passed by Sanitizers ([[ https://bugs.llvm.org/show_bug.cgi?id=16386 | PR16386 ]]) - should it also cover these attributes?
The problem is that the flag is back-end-specific, as seen in `test/Modules/explicit-build-flags.cpp`.
But while it is okay to add `noalias` metadata in backend, we really should be adding at least
the alignment metadata to the AST, since that allows us to perform sema checks on it.
Reviewers: erichkeane, rjmccall, jdoerfert, eugenis, rsmith
Reviewed By: rsmith
Subscribers: xbolva00, jrtc27, atanasyan, nlopes, cfe-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D73380
2020-02-26 06:37:17 +08:00
|
|
|
// CHECK: call noalias nonnull i8* @_Znwm(i64
|
2017-04-01 08:22:47 +08:00
|
|
|
|
|
|
|
// If promise constructor throws, check that we free the memory.
|
|
|
|
|
|
|
|
// CHECK: invoke void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeC1Ev(
|
|
|
|
// CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]]
|
|
|
|
|
2017-05-23 13:04:01 +08:00
|
|
|
// CHECK: [[DeallocPad]]:
|
|
|
|
// CHECK-NEXT: landingpad
|
|
|
|
// CHECK-NEXT: cleanup
|
|
|
|
// CHECK: br label %[[Dealloc:.+]]
|
|
|
|
|
2017-04-01 08:22:47 +08:00
|
|
|
Cleanup cleanup;
|
|
|
|
may_throw();
|
|
|
|
|
|
|
|
// if may_throw throws, check that we destroy the promise and free the memory.
|
|
|
|
|
|
|
|
// CHECK: invoke void @_Z9may_throwv(
|
2017-05-23 06:33:17 +08:00
|
|
|
// CHECK-NEXT: to label %{{.+}} unwind label %[[CatchPad:.+]]
|
2017-04-01 08:22:47 +08:00
|
|
|
|
2017-05-23 06:33:17 +08:00
|
|
|
// CHECK: [[CatchPad]]:
|
|
|
|
// CHECK-NEXT: landingpad
|
|
|
|
// CHECK-NEXT: catch i8* null
|
|
|
|
// CHECK: call void @_ZN7CleanupD1Ev(
|
|
|
|
// CHECK: br label %[[Catch:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[Catch]]:
|
|
|
|
// CHECK: call i8* @__cxa_begin_catch(
|
|
|
|
// CHECK: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_type19unhandled_exceptionEv(
|
|
|
|
// CHECK: invoke void @__cxa_end_catch()
|
|
|
|
// CHECK-NEXT: to label %[[Cont:.+]] unwind
|
|
|
|
|
|
|
|
// CHECK: [[Cont]]:
|
|
|
|
// CHECK-NEXT: br label %[[Cont2:.+]]
|
|
|
|
// CHECK: [[Cont2]]:
|
|
|
|
// CHECK-NEXT: br label %[[Cleanup:.+]]
|
|
|
|
|
|
|
|
// CHECK: [[Cleanup]]:
|
2017-04-01 08:22:47 +08:00
|
|
|
// CHECK: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeD1Ev(
|
2017-05-23 06:33:17 +08:00
|
|
|
// CHECK: %[[Mem0:.+]] = call i8* @llvm.coro.free(
|
|
|
|
// CHECK: call void @_ZdlPv(i8* %[[Mem0]]
|
2017-04-01 08:22:47 +08:00
|
|
|
|
|
|
|
// CHECK: [[Dealloc]]:
|
|
|
|
// CHECK: %[[Mem:.+]] = call i8* @llvm.coro.free(
|
|
|
|
// CHECK: call void @_ZdlPv(i8* %[[Mem]])
|
|
|
|
|
|
|
|
co_return;
|
|
|
|
}
|
2017-05-23 06:33:17 +08:00
|
|
|
|
2020-12-31 16:27:11 +08:00
|
|
|
// CHECK-LABEL: define{{.*}} void @_Z1gv(
|
2017-05-23 06:33:17 +08:00
|
|
|
void g() {
|
|
|
|
for (;;)
|
|
|
|
co_await suspend_always{};
|
|
|
|
// Since this is the endless loop there should be no fallthrough handler (call to 'return_void').
|
|
|
|
// CHECK-NOT: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_type11return_voidEv
|
|
|
|
}
|