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