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 @llvm.experimental.noalias.scope.decl(metadata [[META0:![0-9]+]])
47 ; CHECK-NEXT: tail call void @print(i32 5), !noalias [[META0]]
48 ; CHECK-NEXT: tail call void @llvm.experimental.noalias.scope.decl(metadata [[META3:![0-9]+]])
49 ; CHECK-NEXT: tail call void @print(i32 6), !noalias [[META3]]
50 ; CHECK-NEXT: ret i32 0
54 ; CORO-NEXT: [[TMP0:%.*]] = alloca [8 x i8], align 4
55 ; CORO-NEXT: [[CONT0:%.*]] = call ptr @f(ptr [[TMP0]], i32 4)
56 ; CORO-NEXT: [[CONT1:%.*]] = call ptr [[CONT0]](ptr [[TMP0]], i1 zeroext false)
57 ; CORO-NEXT: [[CONT2:%.*]] = call ptr [[CONT1]](ptr [[TMP0]], i1 zeroext false)
58 ; CORO-NEXT: [[TMP1:%.*]] = call ptr [[CONT2]](ptr [[TMP0]], i1 zeroext true)
59 ; CORO-NEXT: ret i32 0
62 %0 = alloca [8 x i8], align 4
63 %prepare = call ptr @llvm.coro.prepare.retcon(ptr @f)
64 %cont0 = call ptr %prepare(ptr %0, i32 4)
65 %cont1 = call ptr %cont0(ptr %0, i1 zeroext false)
66 %cont2 = call ptr %cont1(ptr %0, i1 zeroext false)
67 call ptr %cont2(ptr %0, i1 zeroext true)
71 ; Unfortunately, we don't seem to fully optimize this right now due
72 ; to some sort of phase-ordering thing.
74 define hidden { ptr, ptr } @g(ptr %buffer, ptr %ptr) {
76 ; CHECK-NEXT: coro.return:
77 ; CHECK-NEXT: [[TMP0:%.*]] = tail call ptr @allocate(i32 8)
78 ; CHECK-NEXT: store ptr [[TMP0]], ptr [[BUFFER:%.*]], align 8
79 ; CHECK-NEXT: store ptr [[PTR:%.*]], ptr [[TMP0]], align 8
80 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { ptr, ptr } { ptr @g.resume.0, ptr poison }, ptr [[PTR]], 1
81 ; CHECK-NEXT: ret { ptr, ptr } [[TMP1]]
85 ; CORO-NEXT: [[TMP0:%.*]] = call ptr @allocate(i32 8)
86 ; CORO-NEXT: store ptr [[TMP0]], ptr [[BUFFER:%.*]], align 8
87 ; CORO-NEXT: [[PTR_SPILL_ADDR:%.*]] = getelementptr inbounds [[G_FRAME:%.*]], ptr [[TMP0]], i32 0, i32 0
88 ; CORO-NEXT: store ptr [[PTR:%.*]], ptr [[PTR_SPILL_ADDR]], align 8
89 ; CORO-NEXT: [[PTR_RELOAD_ADDR:%.*]] = getelementptr inbounds [[G_FRAME]], ptr [[TMP0]], i32 0, i32 0
90 ; CORO-NEXT: [[PTR_RELOAD:%.*]] = load ptr, ptr [[PTR_RELOAD_ADDR]], align 8
91 ; CORO-NEXT: [[TMP1:%.*]] = insertvalue { ptr, ptr } poison, ptr @g.resume.0, 0
92 ; CORO-NEXT: [[TMP2:%.*]] = insertvalue { ptr, ptr } [[TMP1]], ptr [[PTR_RELOAD]], 1
93 ; CORO-NEXT: ret { ptr, ptr } [[TMP2]]
96 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @g_prototype, ptr @allocate, ptr @deallocate)
97 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
101 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1(ptr %ptr)
102 br i1 %unwind0, label %cleanup, label %resume
108 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
112 define ptr @nosuspend(ptr %buffer, i32 %n) {
113 ; CHECK-LABEL: @nosuspend(
115 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
116 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[A]], align 4
117 ; CHECK-NEXT: call void @use_var_ptr(ptr nonnull [[A]])
118 ; CHECK-NEXT: [[AL:%.*]] = load i32, ptr [[A]], align 4
119 ; CHECK-NEXT: call void @use_var(i32 [[AL]])
120 ; CHECK-NEXT: ret ptr null
122 ; CORO-LABEL: @nosuspend(
124 ; CORO-NEXT: [[A:%.*]] = alloca i32, align 4
125 ; CORO-NEXT: store i32 [[N:%.*]], ptr [[A]], align 4
126 ; CORO-NEXT: call void @use_var_ptr(ptr [[A]])
127 ; CORO-NEXT: [[AL:%.*]] = load i32, ptr [[A]], align 4
128 ; CORO-NEXT: call void @use_var(i32 [[AL]])
129 ; CORO-NEXT: ret ptr null
132 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @prototype, ptr @allocate, ptr @deallocate)
133 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
139 call void @use_var_ptr(ptr %a)
140 %al = load i32, ptr %a
141 call void @use_var(i32 %al)
142 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
146 declare token @llvm.coro.id.retcon(i32, i32, ptr, ptr, ptr, ptr)
147 declare ptr @llvm.coro.begin(token, ptr)
148 declare i1 @llvm.coro.suspend.retcon.i1(...)
149 declare i1 @llvm.coro.end(ptr, i1, token)
150 declare ptr @llvm.coro.prepare.retcon(ptr)
152 declare void @use_var(i32)
153 declare void @use_var_ptr(ptr)
155 declare ptr @prototype(ptr, i1 zeroext)
156 declare {ptr,ptr} @g_prototype(ptr, i1 zeroext)
158 declare noalias ptr @allocate(i32 %size)
159 declare void @deallocate(ptr %ptr)
161 declare void @print(i32)