137 lines · cpp
1// Verifies lifetime of __gro local variable2// Verify that coroutine promise and allocated memory are freed up on exception.3// RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -disable-llvm-passes | FileCheck %s4 5#include "Inputs/coroutine.h"6 7using namespace std;8 9struct GroType {10 ~GroType();11 operator int() noexcept;12};13 14template <> struct std::coroutine_traits<int> {15 struct promise_type {16 GroType get_return_object() noexcept;17 suspend_always initial_suspend() noexcept;18 suspend_always final_suspend() noexcept;19 void return_void() noexcept;20 promise_type();21 ~promise_type();22 void unhandled_exception() noexcept;23 };24};25 26struct Cleanup { ~Cleanup(); };27void doSomething() noexcept;28 29// CHECK: define{{.*}} i32 @_Z1fv(30int f() {31 // CHECK: %[[RetVal:.+]] = alloca i3232 // CHECK: %[[GroActive:.+]] = alloca i133 // CHECK: %[[CoroGro:.+]] = alloca %struct.GroType, {{.*}} !coro.outside.frame ![[OutFrameMetadata:.+]]34 35 // CHECK: %[[Size:.+]] = call i64 @llvm.coro.size.i64()36 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef %[[Size]])37 // CHECK: store i1 false, ptr %[[GroActive]]38 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_typeC1Ev(39 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_type17get_return_objectEv({{.*}} %[[CoroGro]]40 // CHECK: store i1 true, ptr %[[GroActive]]41 42 Cleanup cleanup;43 doSomething();44 co_return;45 46 // CHECK: call void @_Z11doSomethingv(47 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_type11return_voidEv(48 // CHECK: call void @_ZN7CleanupD1Ev(49 50 // Destroy promise and free the memory.51 52 // CHECK: call void @_ZNSt16coroutine_traitsIiJEE12promise_typeD1Ev(53 // CHECK: %[[Mem:.+]] = call ptr @llvm.coro.free(54 // CHECK: %[[SIZE:.+]] = call i64 @llvm.coro.size.i64()55 // CHECK: call void @_ZdlPvm(ptr noundef %[[Mem]], i64 noundef %[[SIZE]])56 57 // Initialize retval from Gro and destroy Gro58 // Note this also tests delaying initialization when Gro and function return59 // types mismatch (see cwg2563).60 61 // CHECK: %[[Conv:.+]] = call noundef i32 @_ZN7GroTypecviEv(62 // CHECK: store i32 %[[Conv]], ptr %[[RetVal]]63 // CHECK: %[[IsActive:.+]] = load i1, ptr %[[GroActive]]64 // CHECK: br i1 %[[IsActive]], label %[[CleanupGro:.+]], label %[[Done:.+]]65 66 // CHECK: [[CleanupGro]]:67 // CHECK: call void @_ZN7GroTypeD1Ev(68 // CHECK: br label %[[Done]]69 70 // CHECK: [[Done]]:71 // CHECK: %[[LoadRet:.+]] = load i32, ptr %[[RetVal]]72 // CHECK: ret i32 %[[LoadRet]]73}74 75class invoker {76public:77 class invoker_promise {78 public:79 invoker get_return_object() { return invoker{}; }80 auto initial_suspend() { return suspend_always{}; }81 auto final_suspend() noexcept { return suspend_always{}; }82 void return_void() {}83 void unhandled_exception() {}84 };85 using promise_type = invoker_promise;86 invoker() {}87 invoker(const invoker &) = delete;88 invoker &operator=(const invoker &) = delete;89 invoker(invoker &&) = delete;90 invoker &operator=(invoker &&) = delete;91};92 93// According to cwg2563, matching GRO and function return type must allow94// for eager initialization and RVO.95// CHECK: define{{.*}} void @_Z1gv({{.*}} %[[AggRes:.+]])96invoker g() {97 // CHECK: %[[ResultPtr:.+]] = alloca ptr98 // CHECK-NEXT: %[[Promise:.+]] = alloca %"class.invoker::invoker_promise"99 100 // CHECK: store ptr %[[AggRes]], ptr %[[ResultPtr]]101 // CHECK: coro.init:102 // CHECK: = call ptr @llvm.coro.begin103 104 // delayed GRO pattern stores a GRO active flag, make sure to not emit it.105 // CHECK-NOT: store i1 false, ptr106 // CHECK: call void @_ZN7invoker15invoker_promise17get_return_objectEv({{.*}} %[[AggRes]]107 co_return;108}109 110namespace gh148953 {111 112struct Task {113 struct promise_type {114 Task get_return_object();115 std::suspend_always initial_suspend() { return {}; }116 std::suspend_always final_suspend() noexcept { return {}; }117 void return_void() {}118 void unhandled_exception() {}119 };120 Task() {}121 // Different from `invoker`, this Task is copy constructible.122 Task(const Task&) {};123};124 125// NRVO on const qualified return type should work.126// CHECK: define{{.*}} void @_ZN8gh1489537exampleEv({{.*}} sret(%"struct.gh148953::Task") align 1 %[[NrvoRes:.+]])127const Task example() {128 // CHECK: %[[ResultPtr:.+]] = alloca ptr129 // CHECK: store ptr %[[NrvoRes]], ptr %[[ResultPtr]]130 // CHECK: coro.init:131 // CHECK: call void @_ZN8gh1489534Task12promise_type17get_return_objectEv({{.*}} %[[NrvoRes:.+]], {{.*}})132 co_return;133}134 135} // namespace gh148953136// CHECK: ![[OutFrameMetadata]] = !{}137