1 ; Tests that coro-split pass splits the coroutine into f, f.resume and f.destroy
2 ; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
4 define ptr @f() presplitcoroutine !func_sanitize !0 {
6 %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
7 %need.alloc = call i1 @llvm.coro.alloc(token %id)
8 br i1 %need.alloc, label %dyn.alloc, label %begin
11 %size = call i32 @llvm.coro.size.i32()
12 %alloc = call ptr @malloc(i32 %size)
16 %phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
17 %hdl = call ptr @llvm.coro.begin(token %id, ptr %phi)
18 call void @print(i32 0)
19 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
20 switch i8 %0, label %suspend [i8 0, label %resume
23 call void @print(i32 1)
27 %mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
28 call void @free(ptr %mem)
31 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
35 ; Make a safe_elide call to f and CoroSplit should generate the .noalloc variant
36 define void @caller() presplitcoroutine {
38 %ptr = call ptr @f() #1
42 ; CHECK-LABEL: @f() !func_sanitize !0 {
43 ; CHECK: call ptr @malloc
44 ; CHECK: @llvm.coro.begin(token %id, ptr %phi)
45 ; CHECK: store ptr @f.resume, ptr %hdl
46 ; CHECK: %[[SEL:.+]] = select i1 %need.alloc, ptr @f.destroy, ptr @f.cleanup
47 ; CHECK: store ptr %[[SEL]], ptr %destroy.addr
48 ; CHECK: call void @print(i32 0)
49 ; CHECK-NOT: call void @print(i32 1)
50 ; CHECK-NOT: call void @free(
53 ; CHECK-LABEL: @f.resume({{.*}}) {
54 ; CHECK-NOT: call ptr @malloc
55 ; CHECK-NOT: call void @print(i32 0)
56 ; CHECK: call void @print(i32 1)
57 ; CHECK-NOT: call void @print(i32 0)
58 ; CHECK: call void @free(
61 ; CHECK-LABEL: @f.destroy({{.*}}) {
62 ; CHECK-NOT: call ptr @malloc
63 ; CHECK-NOT: call void @print(
64 ; CHECK: call void @free(
67 ; CHECK-LABEL: @f.cleanup({{.*}}) {
68 ; CHECK-NOT: call ptr @malloc
69 ; CHECK-NOT: call void @print(
70 ; CHECK-NOT: call void @free(
73 ; CHECK-LABEL: @f.noalloc(ptr noundef nonnull align 8 dereferenceable(24) %{{.*}})
74 ; CHECK-NOT: call ptr @malloc
75 ; CHECK: call void @print(i32 0)
76 ; CHECK-NOT: call void @print(i32 1)
77 ; CHECK-NOT: call void @free(
78 ; CHECK: ret ptr %{{.*}}
80 declare ptr @llvm.coro.free(token, ptr)
81 declare i32 @llvm.coro.size.i32()
82 declare i8 @llvm.coro.suspend(token, i1)
83 declare void @llvm.coro.resume(ptr)
84 declare void @llvm.coro.destroy(ptr)
86 declare token @llvm.coro.id(i32, ptr, ptr, ptr)
87 declare i1 @llvm.coro.alloc(token)
88 declare ptr @llvm.coro.begin(token, ptr)
89 declare i1 @llvm.coro.end(ptr, i1, token)
91 declare noalias ptr @malloc(i32) allockind("alloc,uninitialized") "alloc-family"="malloc"
92 declare void @print(i32)
93 declare void @free(ptr) willreturn allockind("free") "alloc-family"="malloc"
95 !0 = !{i32 846595819, ptr null}
96 attributes #1 = { coro_elide_safe }