xref: /llvm-project/clang/test/CodeGenCoroutines/coro-await-resume-eh.cpp (revision 9466b49171dc4b21f56a48594fc82b1e52f5358a)
1 // Test the behavior of http://wg21.link/P0664, a proposal to catch any
2 // exceptions thrown after the initial suspend point of a coroutine by
3 // executing the handler specified by the promise type's 'unhandled_exception'
4 // member function.
5 //
6 // RUN: %clang_cc1 -std=c++20 \
7 // RUN:   -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s \
8 // RUN:   -fexceptions -fcxx-exceptions -disable-llvm-passes \
9 // RUN:   | FileCheck %s
10 
11 #include "Inputs/coroutine.h"
12 
13 struct throwing_awaitable {
await_readythrowing_awaitable14   bool await_ready() { return true; }
await_suspendthrowing_awaitable15   void await_suspend(std::coroutine_handle<>) {}
await_resumethrowing_awaitable16   void await_resume() { throw 42; }
17 };
18 
19 struct throwing_task {
20   struct promise_type {
get_return_objectthrowing_task::promise_type21     auto get_return_object() { return throwing_task{}; }
initial_suspendthrowing_task::promise_type22     auto initial_suspend() { return throwing_awaitable{}; }
final_suspendthrowing_task::promise_type23     auto final_suspend() noexcept { return std::suspend_never{}; }
return_voidthrowing_task::promise_type24     void return_void() {}
unhandled_exceptionthrowing_task::promise_type25     void unhandled_exception() {}
26   };
27 };
28 
29 // CHECK-LABEL: define{{.*}} void @_Z1fv()
f()30 throwing_task f() {
31   // A variable RESUMETHREW is used to keep track of whether the body
32   // of 'await_resume' threw an exception. Exceptions thrown in
33   // 'await_resume' are unwound to RESUMELPAD.
34   // CHECK: init.ready:
35   // CHECK-NEXT: store i1 true, ptr %[[RESUMETHREW:.+]], align 1
36   // CHECK-NEXT: invoke void @_ZN18throwing_awaitable12await_resumeEv
37   // CHECK-NEXT: to label %[[RESUMECONT:.+]] unwind label %[[RESUMELPAD:.+]]
38 
39   // If 'await_resume' does not throw an exception, 'false' is stored in
40   // variable RESUMETHREW.
41   // CHECK: [[RESUMECONT]]:
42   // CHECK-NEXT: store i1 false, ptr %[[RESUMETHREW]]
43   // CHECK-NEXT: br label %[[RESUMETRYCONT:.+]]
44 
45   // 'unhandled_exception' is called for the exception thrown in
46   // 'await_resume'. The variable RESUMETHREW is never set to false,
47   // and a jump is made to RESUMETRYCONT.
48   // CHECK: [[RESUMELPAD]]:
49   // CHECK: br label %[[RESUMECATCH:.+]]
50   // CHECK: [[RESUMECATCH]]:
51   // CHECK: invoke void @_ZN13throwing_task12promise_type19unhandled_exceptionEv
52   // CHECK-NEXT: to label %[[RESUMEENDCATCH:.+]] unwind label
53   // CHECK: [[RESUMEENDCATCH]]:
54   // CHECK-NEXT: invoke void @__cxa_end_catch()
55   // CHECK-NEXT: to label %[[RESUMEENDCATCHCONT:.+]] unwind label
56   // CHECK: [[RESUMEENDCATCHCONT]]:
57   // CHECK-NEXT: br label %[[RESUMETRYCONT]]
58   // CHECK: [[RESUMETRYCONT]]:
59   // CHECK-NEXT: br label %[[CLEANUP:.+]]
60   // CHECK: [[CLEANUP]]:
61   // CHECK: switch i32 %{{.+}}, label %{{.+}} [
62   // CHECK-NEXT: i32 0, label %[[CLEANUPCONT:.+]]
63   // CHECK-NEXT: ]
64 
65   // The variable RESUMETHREW is loaded and if true, then 'await_resume'
66   // threw an exception and the coroutine body is skipped, and the final
67   // suspend is executed immediately. Otherwise, the coroutine body is
68   // executed, and then the final suspend.
69   // CHECK: [[CLEANUPCONT]]:
70   // CHECK-NEXT: %[[RESUMETHREWLOAD:.+]] = load i1, ptr %[[RESUMETHREW]]
71   // CHECK-NEXT: br i1 %[[RESUMETHREWLOAD]], label %[[RESUMEDCONT:.+]], label %[[RESUMEDBODY:.+]]
72 
73   // CHECK: [[RESUMEDBODY]]:
74   // CHECK: invoke void @_ZN13throwing_task12promise_type11return_voidEv
75   // CHECK-NEXT: to label %[[REDUMEDBODYCONT:.+]] unwind label
76   // CHECK: [[REDUMEDBODYCONT]]:
77   // CHECK-NEXT: br label %[[COROFINAL:.+]]
78 
79   // CHECK: [[RESUMEDCONT]]:
80   // CHECK-NEXT: br label %[[COROFINAL]]
81 
82   // CHECK: [[COROFINAL]]:
83   // CHECK: call void @_ZN13throwing_task12promise_type13final_suspendEv
84   co_return;
85 }
86 
87 struct noexcept_awaitable {
await_readynoexcept_awaitable88   bool await_ready() { return true; }
await_suspendnoexcept_awaitable89   void await_suspend(std::coroutine_handle<>) {}
await_resumenoexcept_awaitable90   void await_resume() noexcept {}
91 };
92 
93 struct noexcept_task {
94   struct promise_type {
get_return_objectnoexcept_task::promise_type95     auto get_return_object() { return noexcept_task{}; }
initial_suspendnoexcept_task::promise_type96     auto initial_suspend() { return noexcept_awaitable{}; }
final_suspendnoexcept_task::promise_type97     auto final_suspend() noexcept { return std::suspend_never{}; }
return_voidnoexcept_task::promise_type98     void return_void() {}
unhandled_exceptionnoexcept_task::promise_type99     void unhandled_exception() {}
100   };
101 };
102 
103 // CHECK-LABEL: define{{.*}} void @_Z1gv()
g()104 noexcept_task g() {
105   // If the await_resume function is marked as noexcept, none of the additional
106   // conditions that are present in f() above are added to the IR.
107   // This means that no i1 are stored before or after calling await_resume:
108   // CHECK: init.ready:
109   // CHECK-NEXT: call void @_ZN18noexcept_awaitable12await_resumeEv
110   // CHECK-NOT: store i1 false, ptr
111   co_return;
112 }
113