1 ; RUN: opt < %s -passes='default<O0>' -S | FileCheck %s
2 ; RUN: opt --try-experimental-debuginfo-iterators < %s -passes='default<O0>' -S | FileCheck %s
4 ; Define a function 'f' that resembles the Clang frontend's output for the
5 ; following C++ coroutine:
11 ; print(i); // Prints '1'
13 ; co_await suspend_always();
19 ; print(i); // Prints '2'
21 ; print(j); // Prints '1'
22 ; print(x); // Print '1'
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.
31 ; CHECK-LABEL: define void @f() {{.*}} {
33 ; CHECK: %j = alloca i32, align 4
34 ; CHECK: #dbg_declare(ptr %j, ![[JVAR:[0-9]+]], !DIExpression(), ![[JDBGLOC:[0-9]+]]
35 ; CHECK: %[[MEMORY:.*]] = call ptr @new({{.+}}), !dbg ![[IDBGLOC:[0-9]+]]
36 ; CHECK: #dbg_declare(ptr %[[MEMORY]], ![[XVAR:[0-9]+]], !DIExpression(DW_OP_plus_uconst, 32), ![[IDBGLOC]]
37 ; CHECK: #dbg_declare(ptr %[[MEMORY]], ![[IVAR:[0-9]+]], !DIExpression(DW_OP_plus_uconst, 20), ![[IDBGLOC]]
40 ; CHECK-LABEL: define internal fastcc void @f.resume({{.*}}) {{.*}} {
41 ; CHECK: entry.resume:
42 ; CHECK-NEXT: %[[DBG_PTR:.*]] = alloca ptr
43 ; CHECK-NEXT: #dbg_declare(ptr %[[DBG_PTR]], ![[XVAR_RESUME:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 32),
44 ; CHECK-NEXT: #dbg_declare(ptr %[[DBG_PTR]], ![[IVAR_RESUME:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 20), ![[IDBGLOC_RESUME:[0-9]+]]
45 ; CHECK-NEXT: store ptr {{.*}}, ptr %[[DBG_PTR]]
46 ; CHECK: %[[J:.*]] = alloca i32, align 4
47 ; CHECK-NEXT: #dbg_declare(ptr %[[J]], ![[JVAR_RESUME:[0-9]+]], !DIExpression(), ![[JDBGLOC_RESUME:[0-9]+]]
51 ; CHECK-DAG: ![[IVAR]] = !DILocalVariable(name: "i"
52 ; CHECK-DAG: ![[PROG_SCOPE:[0-9]+]] = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov"
53 ; CHECK-DAG: ![[BLK_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[PROG_SCOPE]], file: !1, line: 23, column: 12)
54 ; CHECK-DAG: ![[IDBGLOC]] = !DILocation(line: 23, column: 6, scope: ![[PROG_SCOPE]])
55 ; CHECK-DAG: ![[XVAR]] = !DILocalVariable(name: "x"
56 ; CHECK-DAG: ![[JVAR]] = !DILocalVariable(name: "j"
57 ; CHECK-DAG: ![[JDBGLOC]] = !DILocation(line: 32, column: 7, scope: ![[BLK_SCOPE]])
59 ; CHECK-DAG: ![[XVAR_RESUME]] = !DILocalVariable(name: "x"
60 ; CHECK-DAG: ![[RESUME_PROG_SCOPE:[0-9]+]] = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov"
61 ; CHECK-DAG: ![[IDBGLOC_RESUME]] = !DILocation(line: 24, column: 7, scope: ![[RESUME_BLK_SCOPE:[0-9]+]])
62 ; CHECK-DAG: ![[RESUME_BLK_SCOPE]] = distinct !DILexicalBlock(scope: ![[RESUME_PROG_SCOPE]], file: !1, line: 23, column: 12)
63 ; CHECK-DAG: ![[IVAR_RESUME]] = !DILocalVariable(name: "i"
64 ; CHECK-DAG: ![[JVAR_RESUME]] = !DILocalVariable(name: "j"
65 ; CHECK-DAG: ![[JDBGLOC_RESUME]] = !DILocation(line: 32, column: 7, scope: ![[RESUME_BLK_SCOPE]])
66 define void @f() presplitcoroutine !dbg !8 {
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, ptr %__promise, ptr null, ptr null)
73 %alloc = call i1 @llvm.coro.alloc(token %id)
74 br i1 %alloc, label %coro.alloc, label %coro.init
76 coro.alloc: ; preds = %entry
77 %size = call i64 @llvm.coro.size.i64(), !dbg !23
78 %memory = call ptr @new(i64 %size), !dbg !23
79 br label %coro.init, !dbg !23
81 coro.init: ; preds = %coro.alloc, %entry
82 %phi.entry.alloc = phi ptr [ null, %entry ], [ %memory, %coro.alloc ], !dbg !23
83 %begin = call ptr @llvm.coro.begin(token %id, ptr %phi.entry.alloc), !dbg !23
84 %ready = call i1 @await_ready()
85 br i1 %ready, label %init.ready, label %init.suspend
87 init.suspend: ; preds = %coro.init
88 %save = call token @llvm.coro.save(ptr 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
96 init.cleanup: ; preds = %init.suspend
99 init.ready: ; preds = %init.suspend, %coro.init
100 call void @await_resume()
101 call void @llvm.dbg.declare(metadata ptr %i, metadata !6, metadata !DIExpression()), !dbg !11
102 store i32 0, ptr %i, align 4
103 %i.init.ready.load = load i32, ptr %i, align 4
104 %i.init.ready.inc = add nsw i32 %i.init.ready.load, 1
105 store i32 %i.init.ready.inc, ptr %i, align 4
106 call void @llvm.dbg.declare(metadata ptr %x, metadata !14, metadata !DIExpression()), !dbg !11
107 call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 40, i1 false), !dbg !11
108 %i.init.ready.reload = load i32, ptr %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
113 await.suspend: ; preds = %init.ready
114 %save.again = call token @llvm.coro.save(ptr null)
115 %from.address = call ptr @from_address(ptr %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
123 await.cleanup: ; preds = %await.suspend
126 await.ready: ; preds = %await.suspend, %init.ready
127 call void @await_resume()
128 call void @llvm.dbg.declare(metadata ptr %j, metadata !12, metadata !DIExpression()), !dbg !13
129 store i32 0, ptr %j, align 4
130 store i32 1, ptr %x, align 16, !dbg !19
131 %arrayidx1 = getelementptr inbounds [10 x i32], ptr %x, i64 0, i64 1, !dbg !20
132 store i32 2, ptr %arrayidx1, align 4, !dbg !21
133 %i.await.ready.load = load i32, ptr %i, align 4
134 %i.await.ready.inc = add nsw i32 %i.await.ready.load, 1
135 store i32 %i.await.ready.inc, ptr %i, align 4
136 %j.await.ready.load = load i32, ptr %j, align 4
137 %j.await.ready.inc = add nsw i32 %j.await.ready.load, 1
138 store i32 %j.await.ready.inc, ptr %j, align 4
139 %i.await.ready.reload = load i32, ptr %i, align 4
140 call void @print(i32 %i.await.ready.reload)
141 %j.await.ready.reload = load i32, ptr %j, align 4
142 call void @print(i32 %j.await.ready.reload)
143 call void @return_void()
146 coro.final: ; preds = %await.ready
147 call void @final_suspend()
148 %coro.final.await_ready = call i1 @await_ready()
149 br i1 %coro.final.await_ready, label %final.ready, label %final.suspend
151 final.suspend: ; preds = %coro.final
152 %final.suspend.coro.save = call token @llvm.coro.save(ptr null)
153 %final.suspend.from_address = call ptr @from_address(ptr %begin)
154 call void @await_suspend()
155 %final.suspend.coro.suspend = call i8 @llvm.coro.suspend(token %final.suspend.coro.save, i1 true)
156 switch i8 %final.suspend.coro.suspend, label %coro.ret [
157 i8 0, label %final.ready
158 i8 1, label %final.cleanup
161 final.cleanup: ; preds = %final.suspend
164 final.ready: ; preds = %final.suspend, %coro.final
165 call void @await_resume()
168 cleanup: ; preds = %final.ready, %final.cleanup, %await.cleanup, %init.cleanup
169 %cleanup.dest.slot.0 = phi i32 [ 0, %final.ready ], [ 2, %final.cleanup ], [ 2, %await.cleanup ], [ 2, %init.cleanup ]
170 %free.memory = call ptr @llvm.coro.free(token %id, ptr %begin)
171 %free = icmp ne ptr %free.memory, null
172 br i1 %free, label %coro.free, label %after.coro.free
174 coro.free: ; preds = %cleanup
175 call void @delete(ptr %free.memory)
176 br label %after.coro.free
178 after.coro.free: ; preds = %coro.free, %cleanup
179 switch i32 %cleanup.dest.slot.0, label %unreachable [
180 i32 0, label %cleanup.cont
181 i32 2, label %coro.ret
184 cleanup.cont: ; preds = %after.coro.free
187 coro.ret: ; preds = %cleanup.cont, %after.coro.free, %final.suspend, %await.suspend, %init.suspend
188 %end = call i1 @llvm.coro.end(ptr null, i1 false, token none)
191 unreachable: ; preds = %after.coro.free
195 declare void @llvm.dbg.declare(metadata, metadata, metadata)
196 declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr)
197 declare i1 @llvm.coro.alloc(token)
198 declare i64 @llvm.coro.size.i64()
199 declare token @llvm.coro.save(ptr)
200 declare ptr @llvm.coro.begin(token, ptr writeonly)
201 declare i8 @llvm.coro.suspend(token, i1)
202 declare ptr @llvm.coro.free(token, ptr nocapture readonly)
203 declare i1 @llvm.coro.end(ptr, i1, token)
205 declare ptr @new(i64)
206 declare void @delete(ptr)
207 declare i1 @await_ready()
208 declare void @await_suspend()
209 declare void @await_resume()
210 declare void @print(i32)
211 declare ptr @from_address(ptr)
212 declare void @return_void()
213 declare void @final_suspend()
215 declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
218 !llvm.linker.options = !{}
219 !llvm.module.flags = !{!3, !4}
222 !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)
223 !1 = !DIFile(filename: "repro.cpp", directory: ".")
225 !3 = !{i32 7, !"Dwarf Version", i32 4}
226 !4 = !{i32 2, !"Debug Info Version", i32 3}
227 !5 = !{!"clang version 11.0.0"}
228 !6 = !DILocalVariable(name: "i", scope: !7, file: !1, line: 24, type: !10)
229 !7 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
230 !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)
231 !9 = !DISubroutineType(types: !2)
232 !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
233 !11 = !DILocation(line: 24, column: 7, scope: !7)
234 !12 = !DILocalVariable(name: "j", scope: !7, file: !1, line: 32, type: !10)
235 !13 = !DILocation(line: 32, column: 7, scope: !7)
236 !14 = !DILocalVariable(name: "x", scope: !22, file: !1, line: 34, type: !15)
237 !15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 320, elements: !16)
239 !17 = !DISubrange(count: 10)
240 !18 = !DILocation(line: 42, column: 3, scope: !7)
241 !19 = !DILocation(line: 42, column: 8, scope: !7)
242 !20 = !DILocation(line: 43, column: 3, scope: !7)
243 !21 = !DILocation(line: 43, column: 8, scope: !7)
244 !22 = distinct !DILexicalBlock(scope: !8, file: !1, line: 23, column: 12)
245 !23 = !DILocation(line: 23, column: 6, scope: !8)