1; RUN: opt < %s -O0 -enable-coroutines -S | FileCheck %s
2; RUN: opt < %s -passes='default<O0>' -enable-coroutines -S | FileCheck %s
3
4; Define a function 'f' that resembles the Clang frontend's output for the
5; following C++ coroutine:
6;
7;   void foo() {
8;     int i = 0;
9;     ++i;
10;     int x = {};
11;     print(i);  // Prints '1'
12;
13;     co_await suspend_always();
14;
15;     int j = 0;
16;     x[0] = 1;
17;     x[1] = 2;
18;     ++i;
19;     print(i);  // Prints '2'
20;     ++j;
21;     print(j);  // Prints '1'
22;     print(x);  // Print '1'
23;   }
24;
25; The CHECKs verify that dbg.declare intrinsics are created for the coroutine
26; funclet 'f.resume', and that they reference the address of the variables on
27; the coroutine frame. The debug locations for the original function 'f' are
28; static (!11 and !13), whereas the coroutine funclet will have its own new
29; ones with identical line and column numbers.
30;
31; CHECK-LABEL: define void @f() {
32; CHECK:       entry:
33; CHECK:         %j = alloca i32, align 4
34; CHECK:         [[IGEP:%.+]] = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 4
35; CHECK:         [[XGEP:%.+]] = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 6
36; CHECK:       init.ready:
37; CHECK:         call void @llvm.dbg.declare(metadata i32* [[IGEP]], metadata ![[IVAR:[0-9]+]], metadata !DIExpression()), !dbg ![[IDBGLOC:[0-9]+]]
38; CHECK:         call void @llvm.dbg.declare(metadata [10 x i32]* [[XGEP]], metadata ![[XVAR:[0-9]+]], metadata !DIExpression()), !dbg ![[IDBGLOC]]
39; CHECK:       await.ready:
40; CHECK:         call void @llvm.dbg.value(metadata [10 x i32]* [[XGEP]], metadata ![[XVAR]], metadata !DIExpression()), !dbg ![[IDBGLOC]]
41; CHECK:         call void @llvm.dbg.value(metadata i32* [[IGEP]], metadata ![[IVAR]], metadata !DIExpression()), !dbg ![[IDBGLOC]]
42; CHECK:         call void @llvm.dbg.declare(metadata i32* %j, metadata ![[JVAR:[0-9]+]], metadata !DIExpression()), !dbg ![[JDBGLOC:[0-9]+]]
43;
44; CHECK-LABEL: define internal fastcc void @f.resume({{.*}}) {
45; CHECK:       entry.resume:
46; CHECK:         %j = alloca i32, align 4
47; CHECK:         [[IGEP_RESUME:%.+]] = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 4
48; CHECK:         [[XGEP_RESUME:%.+]] = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 6
49; CHECK:       init.ready:
50; CHECK:         call void @llvm.dbg.declare(metadata i32* [[IGEP_RESUME]], metadata ![[IVAR_RESUME:[0-9]+]], metadata !DIExpression()), !dbg ![[IDBGLOC_RESUME:[0-9]+]]
51; CHECK:         call void @llvm.dbg.declare(metadata [10 x i32]* [[XGEP_RESUME]], metadata ![[XVAR_RESUME:[0-9]+]], metadata !DIExpression()), !dbg ![[IDBGLOC_RESUME]]
52; CHECK:       await.ready:
53; CHECK:         call void @llvm.dbg.value(metadata [10 x i32]* [[XGEP_RESUME]], metadata ![[XVAR_RESUME]], metadata !DIExpression()), !dbg ![[IDBGLOC_RESUME]]
54; CHECK:         call void @llvm.dbg.value(metadata i32* [[IGEP_RESUME]], metadata ![[IVAR_RESUME]], metadata !DIExpression()), !dbg ![[IDBGLOC_RESUME]]
55; CHECK:         call void @llvm.dbg.declare(metadata i32* %j, metadata ![[JVAR_RESUME:[0-9]+]], metadata !DIExpression()), !dbg ![[JDBGLOC_RESUME:[0-9]+]]
56;
57; CHECK: ![[IVAR]] = !DILocalVariable(name: "i"
58; CHECK: ![[SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
59; CHECK: ![[IDBGLOC]] = !DILocation(line: 24, column: 7, scope: ![[SCOPE]])
60; CHECK: ![[XVAR]] = !DILocalVariable(name: "x"
61; CHECK: ![[JVAR]] = !DILocalVariable(name: "j"
62; CHECK: ![[JDBGLOC]] = !DILocation(line: 32, column: 7, scope: ![[SCOPE]])
63
64; CHECK: ![[IVAR_RESUME]] = !DILocalVariable(name: "i"
65; CHECK: ![[RESUME_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
66; CHECK: ![[IDBGLOC_RESUME]] = !DILocation(line: 24, column: 7, scope: ![[RESUME_SCOPE]])
67; CHECK: ![[XVAR_RESUME]] = !DILocalVariable(name: "x"
68; CHECK: ![[JVAR_RESUME]] = !DILocalVariable(name: "j"
69; CHECK: ![[JDBGLOC_RESUME]] = !DILocation(line: 32, column: 7, scope: ![[RESUME_SCOPE]])
70define void @f() {
71entry:
72  %__promise = alloca i8, align 8
73  %i = alloca i32, align 4
74  %j = alloca i32, align 4
75  %x = alloca [10 x i32], align 16
76  %id = call token @llvm.coro.id(i32 16, i8* %__promise, i8* null, i8* null)
77  %alloc = call i1 @llvm.coro.alloc(token %id)
78  br i1 %alloc, label %coro.alloc, label %coro.init
79
80coro.alloc:                                       ; preds = %entry
81  %size = call i64 @llvm.coro.size.i64()
82  %memory = call i8* @new(i64 %size)
83  br label %coro.init
84
85coro.init:                                        ; preds = %coro.alloc, %entry
86  %phi.entry.alloc = phi i8* [ null, %entry ], [ %memory, %coro.alloc ]
87  %begin = call i8* @llvm.coro.begin(token %id, i8* %phi.entry.alloc)
88  %ready = call i1 @await_ready()
89  br i1 %ready, label %init.ready, label %init.suspend
90
91init.suspend:                                     ; preds = %coro.init
92  %save = call token @llvm.coro.save(i8* null)
93  call void @await_suspend()
94  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
95  switch i8 %suspend, label %coro.ret [
96    i8 0, label %init.ready
97    i8 1, label %init.cleanup
98  ]
99
100init.cleanup:                                     ; preds = %init.suspend
101  br label %cleanup
102
103init.ready:                                       ; preds = %init.suspend, %coro.init
104  call void @await_resume()
105  call void @llvm.dbg.declare(metadata i32* %i, metadata !6, metadata !DIExpression()), !dbg !11
106  store i32 0, i32* %i, align 4
107  %i.init.ready.load = load i32, i32* %i, align 4
108  %i.init.ready.inc = add nsw i32 %i.init.ready.load, 1
109  store i32 %i.init.ready.inc, i32* %i, align 4
110  call void @llvm.dbg.declare(metadata [10 x i32]* %x, metadata !14, metadata !DIExpression()), !dbg !11
111  %memset = bitcast [10 x i32]* %x to i8*, !dbg !11
112  call void @llvm.memset.p0i8.i64(i8* align 16 %memset, i8 0, i64 40, i1 false), !dbg !11
113  %i.init.ready.reload = load i32, i32* %i, align 4
114  call void @print(i32 %i.init.ready.reload)
115  %ready.again = call zeroext i1 @await_ready()
116  br i1 %ready.again, label %await.ready, label %await.suspend
117
118await.suspend:                                    ; preds = %init.ready
119  %save.again = call token @llvm.coro.save(i8* null)
120  %from.address = call i8* @from_address(i8* %begin)
121  call void @await_suspend()
122  %suspend.again = call i8 @llvm.coro.suspend(token %save.again, i1 false)
123  switch i8 %suspend.again, label %coro.ret [
124    i8 0, label %await.ready
125    i8 1, label %await.cleanup
126  ]
127
128await.cleanup:                                    ; preds = %await.suspend
129  br label %cleanup
130
131await.ready:                                      ; preds = %await.suspend, %init.ready
132  call void @await_resume()
133  call void @llvm.dbg.declare(metadata i32* %j, metadata !12, metadata !DIExpression()), !dbg !13
134  store i32 0, i32* %j, align 4
135  %arrayidx0 = getelementptr inbounds [10 x i32], [10 x i32]* %x, i64 0, i64 0, !dbg !18
136  store i32 1, i32* %arrayidx0, align 16, !dbg !19
137  %arrayidx1 = getelementptr inbounds [10 x i32], [10 x i32]* %x, i64 0, i64 1, !dbg !20
138  store i32 2, i32* %arrayidx1, align 4, !dbg !21
139  %i.await.ready.load = load i32, i32* %i, align 4
140  %i.await.ready.inc = add nsw i32 %i.await.ready.load, 1
141  store i32 %i.await.ready.inc, i32* %i, align 4
142  %j.await.ready.load = load i32, i32* %j, align 4
143  %j.await.ready.inc = add nsw i32 %j.await.ready.load, 1
144  store i32 %j.await.ready.inc, i32* %j, align 4
145  %i.await.ready.reload = load i32, i32* %i, align 4
146  call void @print(i32 %i.await.ready.reload)
147  %j.await.ready.reload = load i32, i32* %j, align 4
148  call void @print(i32 %j.await.ready.reload)
149  call void @return_void()
150  br label %coro.final
151
152coro.final:                                       ; preds = %await.ready
153  call void @final_suspend()
154  %coro.final.await_ready = call i1 @await_ready()
155  br i1 %coro.final.await_ready, label %final.ready, label %final.suspend
156
157final.suspend:                                    ; preds = %coro.final
158  %final.suspend.coro.save = call token @llvm.coro.save(i8* null)
159  %final.suspend.from_address = call i8* @from_address(i8* %begin)
160  call void @await_suspend()
161  %final.suspend.coro.suspend = call i8 @llvm.coro.suspend(token %final.suspend.coro.save, i1 true)
162  switch i8 %final.suspend.coro.suspend, label %coro.ret [
163    i8 0, label %final.ready
164    i8 1, label %final.cleanup
165  ]
166
167final.cleanup:                                    ; preds = %final.suspend
168  br label %cleanup
169
170final.ready:                                      ; preds = %final.suspend, %coro.final
171  call void @await_resume()
172  br label %cleanup
173
174cleanup:                                          ; preds = %final.ready, %final.cleanup, %await.cleanup, %init.cleanup
175  %cleanup.dest.slot.0 = phi i32 [ 0, %final.ready ], [ 2, %final.cleanup ], [ 2, %await.cleanup ], [ 2, %init.cleanup ]
176  %free.memory = call i8* @llvm.coro.free(token %id, i8* %begin)
177  %free = icmp ne i8* %free.memory, null
178  br i1 %free, label %coro.free, label %after.coro.free
179
180coro.free:                                        ; preds = %cleanup
181  call void @delete(i8* %free.memory)
182  br label %after.coro.free
183
184after.coro.free:                                  ; preds = %coro.free, %cleanup
185  switch i32 %cleanup.dest.slot.0, label %unreachable [
186    i32 0, label %cleanup.cont
187    i32 2, label %coro.ret
188  ]
189
190cleanup.cont:                                     ; preds = %after.coro.free
191  br label %coro.ret
192
193coro.ret:                                         ; preds = %cleanup.cont, %after.coro.free, %final.suspend, %await.suspend, %init.suspend
194  %end = call i1 @llvm.coro.end(i8* null, i1 false)
195  ret void
196
197unreachable:                                      ; preds = %after.coro.free
198  unreachable
199}
200
201declare void @llvm.dbg.declare(metadata, metadata, metadata)
202declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*)
203declare i1 @llvm.coro.alloc(token)
204declare i64 @llvm.coro.size.i64()
205declare token @llvm.coro.save(i8*)
206declare i8* @llvm.coro.begin(token, i8* writeonly)
207declare i8 @llvm.coro.suspend(token, i1)
208declare i8* @llvm.coro.free(token, i8* nocapture readonly)
209declare i1 @llvm.coro.end(i8*, i1)
210
211declare i8* @new(i64)
212declare void @delete(i8*)
213declare i1 @await_ready()
214declare void @await_suspend()
215declare void @await_resume()
216declare void @print(i32)
217declare i8* @from_address(i8*)
218declare void @return_void()
219declare void @final_suspend()
220
221declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg)
222
223!llvm.dbg.cu = !{!0}
224!llvm.linker.options = !{}
225!llvm.module.flags = !{!3, !4}
226!llvm.ident = !{!5}
227
228!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, splitDebugInlining: false, nameTableKind: None)
229!1 = !DIFile(filename: "repro.cpp", directory: ".")
230!2 = !{}
231!3 = !{i32 7, !"Dwarf Version", i32 4}
232!4 = !{i32 2, !"Debug Info Version", i32 3}
233!5 = !{!"clang version 11.0.0"}
234!6 = !DILocalVariable(name: "i", scope: !7, file: !1, line: 24, type: !10)
235!7 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
236!8 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 23, type: !9, scopeLine: 23, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
237!9 = !DISubroutineType(types: !2)
238!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
239!11 = !DILocation(line: 24, column: 7, scope: !7)
240!12 = !DILocalVariable(name: "j", scope: !7, file: !1, line: 32, type: !10)
241!13 = !DILocation(line: 32, column: 7, scope: !7)
242!14 = !DILocalVariable(name: "x", scope: !22, file: !1, line: 34, type: !15)
243!15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 320, elements: !16)
244!16 = !{!17}
245!17 = !DISubrange(count: 10)
246!18 = !DILocation(line: 42, column: 3, scope: !7)
247!19 = !DILocation(line: 42, column: 8, scope: !7)
248!20 = !DILocation(line: 43, column: 3, scope: !7)
249!21 = !DILocation(line: 43, column: 8, scope: !7)
250!22 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)