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.declare(metadata i32* %j, metadata ![[JVAR:[0-9]+]], metadata !DIExpression()), !dbg ![[JDBGLOC:[0-9]+]]
41;
42; CHECK-LABEL: define internal fastcc void @f.resume({{.*}}) {
43; CHECK:       entry.resume:
44; CHECK-NEXT:    %[[DBG_PTR:.*]] = alloca %f.Frame*
45; CHECK-NEXT:    call void @llvm.dbg.declare(metadata %f.Frame** %[[DBG_PTR]], metadata ![[XVAR_RESUME:[0-9]+]],   metadata !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 32)), !dbg
46; CHECK-NEXT:    call void @llvm.dbg.declare(metadata %f.Frame** %[[DBG_PTR]], metadata ![[IVAR_RESUME:[0-9]+]], metadata !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 20)), !dbg ![[IDBGLOC_RESUME:[0-9]+]]
47; CHECK-NEXT:    store %f.Frame* {{.*}}, %f.Frame** %[[DBG_PTR]]
48; CHECK:         %[[J:.*]] = alloca i32, align 4
49; CHECK-NEXT:    call void @llvm.dbg.declare(metadata i32* %[[J]], metadata ![[JVAR_RESUME:[0-9]+]], metadata !DIExpression()), !dbg ![[JDBGLOC_RESUME:[0-9]+]]
50; CHECK:       init.ready:
51; CHECK:       await.ready:
52;
53; CHECK: ![[IVAR]] = !DILocalVariable(name: "i"
54; CHECK: ![[SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
55; CHECK: ![[IDBGLOC]] = !DILocation(line: 24, column: 7, scope: ![[SCOPE]])
56; CHECK: ![[XVAR]] = !DILocalVariable(name: "x"
57; CHECK: ![[JVAR]] = !DILocalVariable(name: "j"
58; CHECK: ![[JDBGLOC]] = !DILocation(line: 32, column: 7, scope: ![[SCOPE]])
59
60; CHECK: ![[XVAR_RESUME]] = !DILocalVariable(name: "x"
61; CHECK: ![[IDBGLOC_RESUME]] = !DILocation(line: 24, column: 7, scope: ![[RESUME_SCOPE:[0-9]+]])
62; CHECK: ![[RESUME_SCOPE]] = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
63; CHECK: ![[IVAR_RESUME]] = !DILocalVariable(name: "i"
64; CHECK: ![[JVAR_RESUME]] = !DILocalVariable(name: "j"
65; CHECK: ![[JDBGLOC_RESUME]] = !DILocation(line: 32, column: 7, scope: ![[RESUME_SCOPE]])
66define void @f() {
67entry:
68  %__promise = alloca i8, align 8
69  %i = alloca i32, align 4
70  %j = alloca i32, align 4
71  %x = alloca [10 x i32], align 16
72  %id = call token @llvm.coro.id(i32 16, i8* %__promise, i8* null, i8* null)
73  %alloc = call i1 @llvm.coro.alloc(token %id)
74  br i1 %alloc, label %coro.alloc, label %coro.init
75
76coro.alloc:                                       ; preds = %entry
77  %size = call i64 @llvm.coro.size.i64()
78  %memory = call i8* @new(i64 %size)
79  br label %coro.init
80
81coro.init:                                        ; preds = %coro.alloc, %entry
82  %phi.entry.alloc = phi i8* [ null, %entry ], [ %memory, %coro.alloc ]
83  %begin = call i8* @llvm.coro.begin(token %id, i8* %phi.entry.alloc)
84  %ready = call i1 @await_ready()
85  br i1 %ready, label %init.ready, label %init.suspend
86
87init.suspend:                                     ; preds = %coro.init
88  %save = call token @llvm.coro.save(i8* null)
89  call void @await_suspend()
90  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
91  switch i8 %suspend, label %coro.ret [
92    i8 0, label %init.ready
93    i8 1, label %init.cleanup
94  ]
95
96init.cleanup:                                     ; preds = %init.suspend
97  br label %cleanup
98
99init.ready:                                       ; preds = %init.suspend, %coro.init
100  call void @await_resume()
101  call void @llvm.dbg.declare(metadata i32* %i, metadata !6, metadata !DIExpression()), !dbg !11
102  store i32 0, i32* %i, align 4
103  %i.init.ready.load = load i32, i32* %i, align 4
104  %i.init.ready.inc = add nsw i32 %i.init.ready.load, 1
105  store i32 %i.init.ready.inc, i32* %i, align 4
106  call void @llvm.dbg.declare(metadata [10 x i32]* %x, metadata !14, metadata !DIExpression()), !dbg !11
107  %memset = bitcast [10 x i32]* %x to i8*, !dbg !11
108  call void @llvm.memset.p0i8.i64(i8* align 16 %memset, i8 0, i64 40, i1 false), !dbg !11
109  %i.init.ready.reload = load i32, i32* %i, align 4
110  call void @print(i32 %i.init.ready.reload)
111  %ready.again = call zeroext i1 @await_ready()
112  br i1 %ready.again, label %await.ready, label %await.suspend
113
114await.suspend:                                    ; preds = %init.ready
115  %save.again = call token @llvm.coro.save(i8* null)
116  %from.address = call i8* @from_address(i8* %begin)
117  call void @await_suspend()
118  %suspend.again = call i8 @llvm.coro.suspend(token %save.again, i1 false)
119  switch i8 %suspend.again, label %coro.ret [
120    i8 0, label %await.ready
121    i8 1, label %await.cleanup
122  ]
123
124await.cleanup:                                    ; preds = %await.suspend
125  br label %cleanup
126
127await.ready:                                      ; preds = %await.suspend, %init.ready
128  call void @await_resume()
129  call void @llvm.dbg.declare(metadata i32* %j, metadata !12, metadata !DIExpression()), !dbg !13
130  store i32 0, i32* %j, align 4
131  %arrayidx0 = getelementptr inbounds [10 x i32], [10 x i32]* %x, i64 0, i64 0, !dbg !18
132  store i32 1, i32* %arrayidx0, align 16, !dbg !19
133  %arrayidx1 = getelementptr inbounds [10 x i32], [10 x i32]* %x, i64 0, i64 1, !dbg !20
134  store i32 2, i32* %arrayidx1, align 4, !dbg !21
135  %i.await.ready.load = load i32, i32* %i, align 4
136  %i.await.ready.inc = add nsw i32 %i.await.ready.load, 1
137  store i32 %i.await.ready.inc, i32* %i, align 4
138  %j.await.ready.load = load i32, i32* %j, align 4
139  %j.await.ready.inc = add nsw i32 %j.await.ready.load, 1
140  store i32 %j.await.ready.inc, i32* %j, align 4
141  %i.await.ready.reload = load i32, i32* %i, align 4
142  call void @print(i32 %i.await.ready.reload)
143  %j.await.ready.reload = load i32, i32* %j, align 4
144  call void @print(i32 %j.await.ready.reload)
145  call void @return_void()
146  br label %coro.final
147
148coro.final:                                       ; preds = %await.ready
149  call void @final_suspend()
150  %coro.final.await_ready = call i1 @await_ready()
151  br i1 %coro.final.await_ready, label %final.ready, label %final.suspend
152
153final.suspend:                                    ; preds = %coro.final
154  %final.suspend.coro.save = call token @llvm.coro.save(i8* null)
155  %final.suspend.from_address = call i8* @from_address(i8* %begin)
156  call void @await_suspend()
157  %final.suspend.coro.suspend = call i8 @llvm.coro.suspend(token %final.suspend.coro.save, i1 true)
158  switch i8 %final.suspend.coro.suspend, label %coro.ret [
159    i8 0, label %final.ready
160    i8 1, label %final.cleanup
161  ]
162
163final.cleanup:                                    ; preds = %final.suspend
164  br label %cleanup
165
166final.ready:                                      ; preds = %final.suspend, %coro.final
167  call void @await_resume()
168  br label %cleanup
169
170cleanup:                                          ; preds = %final.ready, %final.cleanup, %await.cleanup, %init.cleanup
171  %cleanup.dest.slot.0 = phi i32 [ 0, %final.ready ], [ 2, %final.cleanup ], [ 2, %await.cleanup ], [ 2, %init.cleanup ]
172  %free.memory = call i8* @llvm.coro.free(token %id, i8* %begin)
173  %free = icmp ne i8* %free.memory, null
174  br i1 %free, label %coro.free, label %after.coro.free
175
176coro.free:                                        ; preds = %cleanup
177  call void @delete(i8* %free.memory)
178  br label %after.coro.free
179
180after.coro.free:                                  ; preds = %coro.free, %cleanup
181  switch i32 %cleanup.dest.slot.0, label %unreachable [
182    i32 0, label %cleanup.cont
183    i32 2, label %coro.ret
184  ]
185
186cleanup.cont:                                     ; preds = %after.coro.free
187  br label %coro.ret
188
189coro.ret:                                         ; preds = %cleanup.cont, %after.coro.free, %final.suspend, %await.suspend, %init.suspend
190  %end = call i1 @llvm.coro.end(i8* null, i1 false)
191  ret void
192
193unreachable:                                      ; preds = %after.coro.free
194  unreachable
195}
196
197declare void @llvm.dbg.declare(metadata, metadata, metadata)
198declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*)
199declare i1 @llvm.coro.alloc(token)
200declare i64 @llvm.coro.size.i64()
201declare token @llvm.coro.save(i8*)
202declare i8* @llvm.coro.begin(token, i8* writeonly)
203declare i8 @llvm.coro.suspend(token, i1)
204declare i8* @llvm.coro.free(token, i8* nocapture readonly)
205declare i1 @llvm.coro.end(i8*, i1)
206
207declare i8* @new(i64)
208declare void @delete(i8*)
209declare i1 @await_ready()
210declare void @await_suspend()
211declare void @await_resume()
212declare void @print(i32)
213declare i8* @from_address(i8*)
214declare void @return_void()
215declare void @final_suspend()
216
217declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg)
218
219!llvm.dbg.cu = !{!0}
220!llvm.linker.options = !{}
221!llvm.module.flags = !{!3, !4}
222!llvm.ident = !{!5}
223
224!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)
225!1 = !DIFile(filename: "repro.cpp", directory: ".")
226!2 = !{}
227!3 = !{i32 7, !"Dwarf Version", i32 4}
228!4 = !{i32 2, !"Debug Info Version", i32 3}
229!5 = !{!"clang version 11.0.0"}
230!6 = !DILocalVariable(name: "i", scope: !7, file: !1, line: 24, type: !10)
231!7 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
232!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)
233!9 = !DISubroutineType(types: !2)
234!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
235!11 = !DILocation(line: 24, column: 7, scope: !7)
236!12 = !DILocalVariable(name: "j", scope: !7, file: !1, line: 32, type: !10)
237!13 = !DILocation(line: 32, column: 7, scope: !7)
238!14 = !DILocalVariable(name: "x", scope: !22, file: !1, line: 34, type: !15)
239!15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 320, elements: !16)
240!16 = !{!17}
241!17 = !DISubrange(count: 10)
242!18 = !DILocation(line: 42, column: 3, scope: !7)
243!19 = !DILocation(line: 42, column: 8, scope: !7)
244!20 = !DILocation(line: 43, column: 3, scope: !7)
245!21 = !DILocation(line: 43, column: 8, scope: !7)
246!22 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
247