1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; First example from Doc/Coroutines.rst (two block loop) converted to retcon
3 ; RUN: opt < %s -passes='default<O2>' -S | FileCheck %s
4 ; RUN: opt < %s -passes='module(coro-early),cgscc(coro-split),module(coro-cleanup)' -S | FileCheck --check-prefix=CORO %s
6 define ptr @f(ptr %buffer, i32 %n) {
8 ; CHECK-NEXT: coro.return:
9 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[BUFFER:%.*]], align 4
10 ; CHECK-NEXT: tail call void @print(i32 [[N]])
11 ; CHECK-NEXT: ret ptr @f.resume.0
15 ; CORO-NEXT: [[N_VAL_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME:%.*]], ptr [[BUFFER:%.*]], i32 0, i32 0
16 ; CORO-NEXT: store i32 [[N:%.*]], ptr [[N_VAL_SPILL_ADDR]], align 4
17 ; CORO-NEXT: call void @print(i32 [[N]])
18 ; CORO-NEXT: ret ptr @f.resume.0
21 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @prototype, ptr @allocate, ptr @deallocate)
22 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
26 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
27 call void @print(i32 %n.val)
28 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1()
29 br i1 %unwind0, label %cleanup, label %resume
32 %inc = add i32 %n.val, 1
36 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
45 ; CHECK-NEXT: tail call void @print(i32 4)
46 ; CHECK-NEXT: tail call void @print(i32 5), !noalias !0
47 ; CHECK-NEXT: tail call void @print(i32 6), !noalias !3
48 ; CHECK-NEXT: ret i32 0
52 ; CORO-NEXT: [[TMP0:%.*]] = alloca [8 x i8], align 4
53 ; CORO-NEXT: [[CONT0:%.*]] = call ptr @f(ptr [[TMP0]], i32 4)
54 ; CORO-NEXT: [[CONT1:%.*]] = call ptr [[CONT0]](ptr [[TMP0]], i1 zeroext false)
55 ; CORO-NEXT: [[CONT2:%.*]] = call ptr [[CONT1]](ptr [[TMP0]], i1 zeroext false)
56 ; CORO-NEXT: [[TMP1:%.*]] = call ptr [[CONT2]](ptr [[TMP0]], i1 zeroext true)
57 ; CORO-NEXT: ret i32 0
60 %0 = alloca [8 x i8], align 4
61 %prepare = call ptr @llvm.coro.prepare.retcon(ptr @f)
62 %cont0 = call ptr %prepare(ptr %0, i32 4)
63 %cont1 = call ptr %cont0(ptr %0, i1 zeroext false)
64 %cont2 = call ptr %cont1(ptr %0, i1 zeroext false)
65 call ptr %cont2(ptr %0, i1 zeroext true)
69 ; Unfortunately, we don't seem to fully optimize this right now due
70 ; to some sort of phase-ordering thing.
72 define hidden { ptr, ptr } @g(ptr %buffer, ptr %ptr) {
74 ; CHECK-NEXT: coro.return:
75 ; CHECK-NEXT: [[TMP0:%.*]] = tail call ptr @allocate(i32 8)
76 ; CHECK-NEXT: store ptr [[TMP0]], ptr [[BUFFER:%.*]], align 8
77 ; CHECK-NEXT: store ptr [[PTR:%.*]], ptr [[TMP0]], align 8
78 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { ptr, ptr } { ptr @g.resume.0, ptr poison }, ptr [[PTR]], 1
79 ; CHECK-NEXT: ret { ptr, ptr } [[TMP1]]
83 ; CORO-NEXT: [[TMP0:%.*]] = call ptr @allocate(i32 8)
84 ; CORO-NEXT: store ptr [[TMP0]], ptr [[BUFFER:%.*]], align 8
85 ; CORO-NEXT: [[PTR_SPILL_ADDR:%.*]] = getelementptr inbounds [[G_FRAME:%.*]], ptr [[TMP0]], i32 0, i32 0
86 ; CORO-NEXT: store ptr [[PTR:%.*]], ptr [[PTR_SPILL_ADDR]], align 8
87 ; CORO-NEXT: [[PTR_RELOAD_ADDR:%.*]] = getelementptr inbounds [[G_FRAME]], ptr [[TMP0]], i32 0, i32 0
88 ; CORO-NEXT: [[PTR_RELOAD:%.*]] = load ptr, ptr [[PTR_RELOAD_ADDR]], align 8
89 ; CORO-NEXT: [[TMP1:%.*]] = insertvalue { ptr, ptr } poison, ptr @g.resume.0, 0
90 ; CORO-NEXT: [[TMP2:%.*]] = insertvalue { ptr, ptr } [[TMP1]], ptr [[PTR_RELOAD]], 1
91 ; CORO-NEXT: ret { ptr, ptr } [[TMP2]]
94 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @g_prototype, ptr @allocate, ptr @deallocate)
95 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
99 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1(ptr %ptr)
100 br i1 %unwind0, label %cleanup, label %resume
106 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
110 define ptr @nosuspend(ptr %buffer, i32 %n) {
111 ; CHECK-LABEL: @nosuspend(
113 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
114 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[A]], align 4
115 ; CHECK-NEXT: call void @use_var_ptr(ptr nonnull [[A]])
116 ; CHECK-NEXT: [[AL:%.*]] = load i32, ptr [[A]], align 4
117 ; CHECK-NEXT: call void @use_var(i32 [[AL]])
118 ; CHECK-NEXT: ret ptr null
120 ; CORO-LABEL: @nosuspend(
122 ; CORO-NEXT: [[A:%.*]] = alloca i32, align 4
123 ; CORO-NEXT: store i32 [[N:%.*]], ptr [[A]], align 4
124 ; CORO-NEXT: call void @use_var_ptr(ptr [[A]])
125 ; CORO-NEXT: [[AL:%.*]] = load i32, ptr [[A]], align 4
126 ; CORO-NEXT: call void @use_var(i32 [[AL]])
127 ; CORO-NEXT: ret ptr null
130 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @prototype, ptr @allocate, ptr @deallocate)
131 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
137 call void @use_var_ptr(ptr %a)
138 %al = load i32, ptr %a
139 call void @use_var(i32 %al)
140 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
144 declare token @llvm.coro.id.retcon(i32, i32, ptr, ptr, ptr, ptr)
145 declare ptr @llvm.coro.begin(token, ptr)
146 declare i1 @llvm.coro.suspend.retcon.i1(...)
147 declare i1 @llvm.coro.end(ptr, i1, token)
148 declare ptr @llvm.coro.prepare.retcon(ptr)
150 declare void @use_var(i32)
151 declare void @use_var_ptr(ptr)
153 declare ptr @prototype(ptr, i1 zeroext)
154 declare {ptr,ptr} @g_prototype(ptr, i1 zeroext)
156 declare noalias ptr @allocate(i32 %size)
157 declare void @deallocate(ptr %ptr)
159 declare void @print(i32)