1 ;; Test callsite context graph generation for simple call graph with
2 ;; two memprof contexts and no inlining, as well as graph and IR cloning.
4 ;; Original code looks like:
7 ;; return new char[10];
18 ;; int main(int argc, char **argv) {
29 ;; Code compiled with -mllvm -memprof-ave-lifetime-cold-threshold=5 so that the
30 ;; memory freed after sleep(10) results in cold lifetimes.
32 ;; The IR was then reduced using llvm-reduce with the expected FileCheck input.
34 ;; -stats requires asserts
37 ; RUN: opt -passes=memprof-context-disambiguation -supports-hot-cold-new \
38 ; RUN: -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
39 ; RUN: -memprof-export-to-dot -memprof-dot-file-path-prefix=%t. \
40 ; RUN: -stats -pass-remarks=memprof-context-disambiguation \
41 ; RUN: %s -S 2>&1 | FileCheck %s --check-prefix=DUMP --check-prefix=IR \
42 ; RUN: --check-prefix=STATS --check-prefix=REMARKS
44 ; RUN: cat %t.ccg.postbuild.dot | FileCheck %s --check-prefix=DOT
45 ;; We should have cloned bar, baz, and foo, for the cold memory allocation.
46 ; RUN: cat %t.ccg.cloned.dot | FileCheck %s --check-prefix=DOTCLONED
48 ;; Check again without -supports-hot-cold-new and ensure all MIB are cold and
49 ;; that there is no cloning.
50 ; RUN: opt -passes=memprof-context-disambiguation \
51 ; RUN: -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
52 ; RUN: -memprof-export-to-dot -memprof-dot-file-path-prefix=%t. \
53 ; RUN: -stats -pass-remarks=memprof-context-disambiguation \
54 ; RUN: %s -S 2>&1 | FileCheck %s --implicit-check-not="Callsite Context Graph" \
55 ; RUN: --implicit-check-not="created clone"
57 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
58 target triple = "x86_64-unknown-linux-gnu"
60 define i32 @main() #0 {
62 %call = call noundef ptr @_Z3foov(), !callsite !0
63 %call1 = call noundef ptr @_Z3foov(), !callsite !1
67 ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
68 declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1
70 ; Function Attrs: nobuiltin
71 declare void @_ZdaPv() #2
73 define internal ptr @_Z3barv() #3 {
75 %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6, !memprof !2, !callsite !7
79 declare ptr @_Znam(i64)
81 define internal ptr @_Z3bazv() #4 {
83 %call = call noundef ptr @_Z3barv(), !callsite !8
87 ; Function Attrs: noinline
88 define internal ptr @_Z3foov() #5 {
90 %call = call noundef ptr @_Z3bazv(), !callsite !9
94 ; uselistorder directives
95 uselistorder ptr @_Z3foov, { 1, 0 }
97 attributes #0 = { "tune-cpu"="generic" }
98 attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
99 attributes #2 = { nobuiltin }
100 attributes #3 = { "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" }
101 attributes #4 = { "stack-protector-buffer-size"="8" }
102 attributes #5 = { noinline }
103 attributes #6 = { builtin }
105 !0 = !{i64 8632435727821051414}
106 !1 = !{i64 -3421689549917153178}
108 !3 = !{!4, !"notcold"}
109 !4 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 8632435727821051414}
111 !6 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 -3421689549917153178}
112 !7 = !{i64 9086428284934609951}
113 !8 = !{i64 -5964873800580613432}
114 !9 = !{i64 2732490490862098848}
117 ; DUMP: CCG before cloning:
118 ; DUMP: Callsite Context Graph:
119 ; DUMP: Node [[BAR:0x[a-z0-9]+]]
120 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
121 ; DUMP: AllocTypes: NotColdCold
122 ; DUMP: ContextIds: 1 2
125 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
128 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
129 ; DUMP: AllocTypes: NotColdCold
130 ; DUMP: ContextIds: 1 2
132 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotColdCold ContextIds: 1 2
134 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
137 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
138 ; DUMP: AllocTypes: NotColdCold
139 ; DUMP: ContextIds: 1 2
141 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotColdCold ContextIds: 1 2
143 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
144 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 2
146 ; DUMP: Node [[MAIN1]]
147 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
148 ; DUMP: AllocTypes: NotCold
149 ; DUMP: ContextIds: 1
151 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
154 ; DUMP: Node [[MAIN2]]
155 ; DUMP: %call1 = call noundef ptr @_Z3foov() (clone 0)
156 ; DUMP: AllocTypes: Cold
157 ; DUMP: ContextIds: 2
159 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
162 ; DUMP: CCG after cloning:
163 ; DUMP: Callsite Context Graph:
164 ; DUMP: Node [[BAR:0x[a-z0-9]+]]
165 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
166 ; DUMP: AllocTypes: NotCold
167 ; DUMP: ContextIds: 1
170 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
171 ; DUMP: Clones: [[BAR2:0x[a-z0-9]+]]
174 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
175 ; DUMP: AllocTypes: NotCold
176 ; DUMP: ContextIds: 1
178 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotCold ContextIds: 1
180 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
181 ; DUMP: Clones: [[BAZ2:0x[a-z0-9]+]]
184 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
185 ; DUMP: AllocTypes: NotCold
186 ; DUMP: ContextIds: 1
188 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
190 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
191 ; DUMP: Clones: [[FOO2:0x[a-z0-9]+]]
193 ; DUMP: Node [[MAIN1]]
194 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
195 ; DUMP: AllocTypes: NotCold
196 ; DUMP: ContextIds: 1
198 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
201 ; DUMP: Node [[MAIN2]]
202 ; DUMP: %call1 = call noundef ptr @_Z3foov() (clone 0)
203 ; DUMP: AllocTypes: Cold
204 ; DUMP: ContextIds: 2
206 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
209 ; DUMP: Node [[FOO2]]
210 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
211 ; DUMP: AllocTypes: Cold
212 ; DUMP: ContextIds: 2
214 ; DUMP: Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
216 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 2
217 ; DUMP: Clone of [[FOO]]
219 ; DUMP: Node [[BAZ2]]
220 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
221 ; DUMP: AllocTypes: Cold
222 ; DUMP: ContextIds: 2
224 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
226 ; DUMP: Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
227 ; DUMP: Clone of [[BAZ]]
229 ; DUMP: Node [[BAR2]]
230 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
231 ; DUMP: AllocTypes: Cold
232 ; DUMP: ContextIds: 2
235 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
236 ; DUMP: Clone of [[BAR]]
239 ; REMARKS: created clone _Z3barv.memprof.1
240 ; REMARKS: created clone _Z3bazv.memprof.1
241 ; REMARKS: created clone _Z3foov.memprof.1
242 ; REMARKS: call in clone main assigned to call function clone _Z3foov.memprof.1
243 ; REMARKS: call in clone _Z3foov.memprof.1 assigned to call function clone _Z3bazv.memprof.1
244 ; REMARKS: call in clone _Z3bazv.memprof.1 assigned to call function clone _Z3barv.memprof.1
245 ; REMARKS: call in clone _Z3barv.memprof.1 marked with memprof allocation attribute cold
246 ; REMARKS: call in clone main assigned to call function clone _Z3foov
247 ; REMARKS: call in clone _Z3foov assigned to call function clone _Z3bazv
248 ; REMARKS: call in clone _Z3bazv assigned to call function clone _Z3barv
249 ; REMARKS: call in clone _Z3barv marked with memprof allocation attribute notcold
252 ; IR: define {{.*}} @main
253 ;; The first call to foo does not allocate cold memory. It should call the
254 ;; original functions, which ultimately call the original allocation decorated
255 ;; with a "notcold" attribute.
256 ; IR: call {{.*}} @_Z3foov()
257 ;; The second call to foo allocates cold memory. It should call cloned functions
258 ;; which ultimately call a cloned allocation decorated with a "cold" attribute.
259 ; IR: call {{.*}} @_Z3foov.memprof.1()
260 ; IR: define internal {{.*}} @_Z3barv()
261 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[NOTCOLD:[0-9]+]]
262 ; IR: define internal {{.*}} @_Z3bazv()
263 ; IR: call {{.*}} @_Z3barv()
264 ; IR: define internal {{.*}} @_Z3foov()
265 ; IR: call {{.*}} @_Z3bazv()
266 ; IR: define internal {{.*}} @_Z3barv.memprof.1()
267 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[COLD:[0-9]+]]
268 ; IR: define internal {{.*}} @_Z3bazv.memprof.1()
269 ; IR: call {{.*}} @_Z3barv.memprof.1()
270 ; IR: define internal {{.*}} @_Z3foov.memprof.1()
271 ; IR: call {{.*}} @_Z3bazv.memprof.1()
272 ; IR: attributes #[[NOTCOLD]] = { builtin "memprof"="notcold" }
273 ; IR: attributes #[[COLD]] = { builtin "memprof"="cold" }
276 ; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)
277 ; STATS: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned)
278 ; STATS: 3 memprof-context-disambiguation - Number of function clones created during whole program analysis
281 ; DOT: digraph "postbuild" {
282 ; DOT: label="postbuild";
283 ; DOT: Node[[BAR:0x[a-z0-9]+]] [shape=record,tooltip="N[[BAR]] ContextIds: 1 2",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: Alloc0\n_Z3barv -\> _Znam}"];
284 ; DOT: Node[[BAZ:0x[a-z0-9]+]] [shape=record,tooltip="N[[BAZ]] ContextIds: 1 2",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: 12481870273128938184\n_Z3bazv -\> _Z3barv}"];
285 ; DOT: Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
286 ; DOT: Node[[FOO:0x[a-z0-9]+]] [shape=record,tooltip="N[[FOO]] ContextIds: 1 2",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: 2732490490862098848\n_Z3foov -\> _Z3bazv}"];
287 ; DOT: Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
288 ; DOT: Node[[MAIN1:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN1]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 8632435727821051414\nmain -\> _Z3foov}"];
289 ; DOT: Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
290 ; DOT: Node[[MAIN2:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN2]] ContextIds: 2",fillcolor="cyan",style="filled",style="filled",label="{OrigId: 15025054523792398438\nmain -\> _Z3foov}"];
291 ; DOT: Node[[MAIN2]] -> Node[[FOO]][tooltip="ContextIds: 2",fillcolor="cyan"];
295 ; DOTCLONED: digraph "cloned" {
296 ; DOTCLONED: label="cloned";
297 ; DOTCLONED: Node[[BAR:0x[a-z0-9]+]] [shape=record,tooltip="N[[BAR]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: Alloc0\n_Z3barv -\> _Znam}"];
298 ; DOTCLONED: Node[[BAZ:0x[a-z0-9]+]] [shape=record,tooltip="N[[BAZ]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 12481870273128938184\n_Z3bazv -\> _Z3barv}"];
299 ; DOTCLONED: Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1",fillcolor="brown1"];
300 ; DOTCLONED: Node[[FOO:0x[a-z0-9]+]] [shape=record,tooltip="N[[FOO]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 2732490490862098848\n_Z3foov -\> _Z3bazv}"];
301 ; DOTCLONED: Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1",fillcolor="brown1"];
302 ; DOTCLONED: Node[[MAIN1:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN1]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 8632435727821051414\nmain -\> _Z3foov}"];
303 ; DOTCLONED: Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
304 ; DOTCLONED: Node[[MAIN2:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN2]] ContextIds: 2",fillcolor="cyan",style="filled",style="filled",label="{OrigId: 15025054523792398438\nmain -\> _Z3foov}"];
305 ; DOTCLONED: Node[[MAIN2]] -> Node[[FOO2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
306 ; DOTCLONED: Node[[FOO2]] [shape=record,tooltip="N[[FOO2]] ContextIds: 2",fillcolor="cyan",style="filled",color="blue",style="filled,bold,dashed",label="{OrigId: 0\n_Z3foov -\> _Z3bazv}"];
307 ; DOTCLONED: Node[[FOO2]] -> Node[[BAZ2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
308 ; DOTCLONED: Node[[BAZ2]] [shape=record,tooltip="N[[BAZ2]] ContextIds: 2",fillcolor="cyan",style="filled",color="blue",style="filled,bold,dashed",label="{OrigId: 0\n_Z3bazv -\> _Z3barv}"];
309 ; DOTCLONED: Node[[BAZ2]] -> Node[[BAR2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
310 ; DOTCLONED: Node[[BAR2]] [shape=record,tooltip="N[[BAR2]] ContextIds: 2",fillcolor="cyan",style="filled",color="blue",style="filled,bold,dashed",label="{OrigId: Alloc0\n_Z3barv -\> _Znam}"];