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: -memprof-report-hinted-sizes \
42 ; RUN: %s -S 2>&1 | FileCheck %s --check-prefix=DUMP --check-prefix=IR \
43 ; RUN: --check-prefix=STATS --check-prefix=REMARKS --check-prefix=SIZES
45 ; RUN: cat %t.ccg.postbuild.dot | FileCheck %s --check-prefix=DOT
46 ;; We should have cloned bar, baz, and foo, for the cold memory allocation.
47 ; RUN: cat %t.ccg.cloned.dot | FileCheck %s --check-prefix=DOTCLONED
49 ;; Check again without -supports-hot-cold-new and ensure all MIB are cold and
50 ;; that there is no cloning.
51 ; RUN: opt -passes=memprof-context-disambiguation \
52 ; RUN: -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
53 ; RUN: -memprof-export-to-dot -memprof-dot-file-path-prefix=%t. \
54 ; RUN: -stats -pass-remarks=memprof-context-disambiguation \
55 ; RUN: %s -S 2>&1 | FileCheck %s --implicit-check-not="Callsite Context Graph" \
56 ; RUN: --implicit-check-not="created clone"
58 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
59 target triple = "x86_64-unknown-linux-gnu"
61 define i32 @main() #0 {
63 %call = call noundef ptr @_Z3foov(), !callsite !0
64 %call1 = call noundef ptr @_Z3foov(), !callsite !1
68 ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
69 declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1
71 ; Function Attrs: nobuiltin
72 declare void @_ZdaPv() #2
74 define internal ptr @_Z3barv() #3 {
76 %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6, !memprof !2, !callsite !7
80 declare ptr @_Znam(i64)
82 define internal ptr @_Z3bazv() #4 {
84 %call = call noundef ptr @_Z3barv(), !callsite !8
88 ; Function Attrs: noinline
89 define internal ptr @_Z3foov() #5 {
91 %call = call noundef ptr @_Z3bazv(), !callsite !9
95 ; uselistorder directives
96 uselistorder ptr @_Z3foov, { 1, 0 }
98 attributes #0 = { "tune-cpu"="generic" }
99 attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
100 attributes #2 = { nobuiltin }
101 attributes #3 = { "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" }
102 attributes #4 = { "stack-protector-buffer-size"="8" }
103 attributes #5 = { noinline }
104 attributes #6 = { builtin }
106 !0 = !{i64 8632435727821051414}
107 !1 = !{i64 -3421689549917153178}
109 !3 = !{!4, !"notcold", i64 100}
110 !4 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 8632435727821051414}
111 !5 = !{!6, !"cold", i64 400}
112 !6 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 -3421689549917153178}
113 !7 = !{i64 9086428284934609951}
114 !8 = !{i64 -5964873800580613432}
115 !9 = !{i64 2732490490862098848}
118 ; DUMP: CCG before cloning:
119 ; DUMP: Callsite Context Graph:
120 ; DUMP: Node [[BAR:0x[a-z0-9]+]]
121 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
122 ; DUMP: AllocTypes: NotColdCold
123 ; DUMP: ContextIds: 1 2
126 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
129 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
130 ; DUMP: AllocTypes: NotColdCold
131 ; DUMP: ContextIds: 1 2
133 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotColdCold ContextIds: 1 2
135 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
138 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
139 ; DUMP: AllocTypes: NotColdCold
140 ; DUMP: ContextIds: 1 2
142 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotColdCold ContextIds: 1 2
144 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
145 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 2
147 ; DUMP: Node [[MAIN1]]
148 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
149 ; DUMP: AllocTypes: NotCold
150 ; DUMP: ContextIds: 1
152 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
155 ; DUMP: Node [[MAIN2]]
156 ; DUMP: %call1 = call noundef ptr @_Z3foov() (clone 0)
157 ; DUMP: AllocTypes: Cold
158 ; DUMP: ContextIds: 2
160 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
163 ; DUMP: CCG after cloning:
164 ; DUMP: Callsite Context Graph:
165 ; DUMP: Node [[BAR:0x[a-z0-9]+]]
166 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
167 ; DUMP: AllocTypes: NotCold
168 ; DUMP: ContextIds: 1
171 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
172 ; DUMP: Clones: [[BAR2:0x[a-z0-9]+]]
175 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
176 ; DUMP: AllocTypes: NotCold
177 ; DUMP: ContextIds: 1
179 ; DUMP: Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotCold ContextIds: 1
181 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
182 ; DUMP: Clones: [[BAZ2:0x[a-z0-9]+]]
185 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
186 ; DUMP: AllocTypes: NotCold
187 ; DUMP: ContextIds: 1
189 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
191 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
192 ; DUMP: Clones: [[FOO2:0x[a-z0-9]+]]
194 ; DUMP: Node [[MAIN1]]
195 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
196 ; DUMP: AllocTypes: NotCold
197 ; DUMP: ContextIds: 1
199 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
202 ; DUMP: Node [[MAIN2]]
203 ; DUMP: %call1 = call noundef ptr @_Z3foov() (clone 0)
204 ; DUMP: AllocTypes: Cold
205 ; DUMP: ContextIds: 2
207 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
210 ; DUMP: Node [[FOO2]]
211 ; DUMP: %call = call noundef ptr @_Z3bazv() (clone 0)
212 ; DUMP: AllocTypes: Cold
213 ; DUMP: ContextIds: 2
215 ; DUMP: Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
217 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 2
218 ; DUMP: Clone of [[FOO]]
220 ; DUMP: Node [[BAZ2]]
221 ; DUMP: %call = call noundef ptr @_Z3barv() (clone 0)
222 ; DUMP: AllocTypes: Cold
223 ; DUMP: ContextIds: 2
225 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
227 ; DUMP: Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
228 ; DUMP: Clone of [[BAZ]]
230 ; DUMP: Node [[BAR2]]
231 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #6 (clone 0)
232 ; DUMP: AllocTypes: Cold
233 ; DUMP: ContextIds: 2
236 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
237 ; DUMP: Clone of [[BAR]]
240 ; REMARKS: created clone _Z3barv.memprof.1
241 ; REMARKS: created clone _Z3bazv.memprof.1
242 ; REMARKS: created clone _Z3foov.memprof.1
243 ; REMARKS: call in clone main assigned to call function clone _Z3foov.memprof.1
244 ; REMARKS: call in clone _Z3foov.memprof.1 assigned to call function clone _Z3bazv.memprof.1
245 ; REMARKS: call in clone _Z3bazv.memprof.1 assigned to call function clone _Z3barv.memprof.1
246 ; REMARKS: call in clone _Z3barv.memprof.1 marked with memprof allocation attribute cold
247 ; REMARKS: call in clone main assigned to call function clone _Z3foov
248 ; REMARKS: call in clone _Z3foov assigned to call function clone _Z3bazv
249 ; REMARKS: call in clone _Z3bazv assigned to call function clone _Z3barv
250 ; REMARKS: call in clone _Z3barv marked with memprof allocation attribute notcold
252 ; SIZES: NotCold context 1 with total size 100 is NotCold after cloning
253 ; SIZES: Cold context 2 with total size 400 is Cold after cloning
255 ; IR: define {{.*}} @main
256 ;; The first call to foo does not allocate cold memory. It should call the
257 ;; original functions, which ultimately call the original allocation decorated
258 ;; with a "notcold" attribute.
259 ; IR: call {{.*}} @_Z3foov()
260 ;; The second call to foo allocates cold memory. It should call cloned functions
261 ;; which ultimately call a cloned allocation decorated with a "cold" attribute.
262 ; IR: call {{.*}} @_Z3foov.memprof.1()
263 ; IR: define internal {{.*}} @_Z3barv()
264 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[NOTCOLD:[0-9]+]]
265 ; IR: define internal {{.*}} @_Z3bazv()
266 ; IR: call {{.*}} @_Z3barv()
267 ; IR: define internal {{.*}} @_Z3foov()
268 ; IR: call {{.*}} @_Z3bazv()
269 ; IR: define internal {{.*}} @_Z3barv.memprof.1()
270 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[COLD:[0-9]+]]
271 ; IR: define internal {{.*}} @_Z3bazv.memprof.1()
272 ; IR: call {{.*}} @_Z3barv.memprof.1()
273 ; IR: define internal {{.*}} @_Z3foov.memprof.1()
274 ; IR: call {{.*}} @_Z3bazv.memprof.1()
275 ; IR: attributes #[[NOTCOLD]] = { builtin "memprof"="notcold" }
276 ; IR: attributes #[[COLD]] = { builtin "memprof"="cold" }
279 ; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)
280 ; STATS: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned)
281 ; STATS: 3 memprof-context-disambiguation - Number of function clones created during whole program analysis
284 ; DOT: digraph "postbuild" {
285 ; DOT: label="postbuild";
286 ; 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}"];
287 ; 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}"];
288 ; DOT: Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
289 ; 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}"];
290 ; DOT: Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
291 ; 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}"];
292 ; DOT: Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
293 ; 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}"];
294 ; DOT: Node[[MAIN2]] -> Node[[FOO]][tooltip="ContextIds: 2",fillcolor="cyan"];
298 ; DOTCLONED: digraph "cloned" {
299 ; DOTCLONED: label="cloned";
300 ; 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}"];
301 ; 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}"];
302 ; DOTCLONED: Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1",fillcolor="brown1"];
303 ; 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}"];
304 ; DOTCLONED: Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1",fillcolor="brown1"];
305 ; 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}"];
306 ; DOTCLONED: Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
307 ; 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}"];
308 ; DOTCLONED: Node[[MAIN2]] -> Node[[FOO2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
309 ; 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}"];
310 ; DOTCLONED: Node[[FOO2]] -> Node[[BAZ2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
311 ; 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}"];
312 ; DOTCLONED: Node[[BAZ2]] -> Node[[BAR2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
313 ; 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}"];