1 ; RUN: opt < %s -O0 -S | FileCheck --check-prefixes=CHECK %s
3 target datalayout = "p:64:64:64"
5 %async.task = type { i64 }
6 %async.actor = type { i64 }
7 %async.fp = type <{ i32, i32 }>
9 %async.ctxt = type { ptr, ptr }
12 @my_other_async_function_fp = external global <{ i32, i32 }>
13 declare void @my_other_async_function(ptr %async.ctxt)
15 @my_async_function_fp = constant <{ i32, i32 }>
16 <{ i32 trunc ( ; Relative pointer to async function
18 i64 ptrtoint (ptr @my_async_function to i64),
19 i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, <{ i32, i32 }>* @my_async_function_fp, i32 0, i32 1) to i64)
22 i32 128 ; Initial async context size without space for frame
25 define swiftcc void @my_other_async_function_fp.apply(ptr %fnPtr, ptr %async.ctxt) {
26 tail call swiftcc void %fnPtr(ptr %async.ctxt)
30 declare void @escape(ptr)
31 declare void @store_resume(ptr)
32 declare i1 @exitLoop()
33 define ptr @resume_context_projection(ptr %ctxt) {
35 %resume_ctxt = load ptr, ptr %ctxt, align 8
39 define swiftcc void @my_async_function(ptr swiftasync %async.ctxt) {
41 %escaped_addr = alloca i64
43 %id = call token @llvm.coro.id.async(i32 128, i32 16, i32 0,
44 ptr bitcast (<{i32, i32}>* @my_async_function_fp to ptr))
45 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
46 call void @llvm.lifetime.start.p0(i64 4, ptr %escaped_addr)
52 %callee_context = call ptr @context_alloc()
54 %resume.func_ptr = call ptr @llvm.coro.async.resume()
55 call void @store_resume(ptr %resume.func_ptr)
56 %res = call {ptr, ptr, ptr} (i32, ptr, ptr, ...) @llvm.coro.suspend.async(i32 0,
58 ptr @resume_context_projection,
59 ptr @my_other_async_function_fp.apply,
60 ptr @asyncSuspend, ptr %callee_context)
61 call void @escape(ptr %escaped_addr)
62 %exitCond = call i1 @exitLoop()
64 ;; We used to move the lifetime.start intrinsic here =>
65 ;; This exposes two bugs:
66 ; 1.) The code should use the basic block start not end as insertion point
67 ; More problematically:
68 ; 2.) The code marks the stack object as not alive for part of the loop.
70 br i1 %exitCond, label %loop_exit, label %loop
71 %res2 = call {ptr, ptr, ptr} (i32, ptr, ptr, ...) @llvm.coro.suspend.async(i32 0,
73 ptr @resume_context_projection,
74 ptr @my_other_async_function_fp.apply,
75 ptr @asyncSuspend, ptr %callee_context)
77 %exitCond2 = call i1 @exitLoop()
78 br i1 %exitCond2, label %loop_exit, label %loop
84 call void @llvm.lifetime.end.p0(i64 4, ptr %escaped_addr)
85 call i1 (ptr, i1, ...) @llvm.coro.end.async(ptr %hdl, i1 false)
89 ; CHECK: define {{.*}} void @my_async_function.resume.0(
90 ; CHECK-NOT: call void @llvm.lifetime.start.p0(i64 4, ptr %3)
91 ; CHECK: br i1 %exitCond, label %loop_exit, label %loop
95 declare { ptr, ptr, ptr, ptr } @llvm.coro.suspend.async.sl_p0i8p0i8p0i8p0i8s(i32, ptr, ptr, ...)
96 declare ptr @llvm.coro.prepare.async(ptr)
97 declare token @llvm.coro.id.async(i32, i32, i32, ptr)
98 declare ptr @llvm.coro.begin(token, ptr)
99 declare i1 @llvm.coro.end.async(ptr, i1, ...)
100 declare i1 @llvm.coro.end(ptr, i1, token)
101 declare {ptr, ptr, ptr} @llvm.coro.suspend.async(i32, ptr, ptr, ...)
102 declare ptr @context_alloc()
103 declare void @llvm.coro.async.context.dealloc(ptr)
104 declare swiftcc void @asyncSuspend(ptr)
105 declare ptr @llvm.coro.async.resume()
106 declare void @llvm.coro.async.size.replace(ptr, ptr)
107 declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0
108 declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0
109 attributes #0 = { argmemonly nofree nosync nounwind willreturn }