1 ;; Test callsite context graph generation for call graph with two memprof
2 ;; contexts and partial inlining, requiring generation of a new fused node to
3 ;; represent the inlined sequence while matching callsite nodes onto the graph.
4 ;; Also tests graph and IR cloning.
6 ;; Original code looks like:
9 ;; return new char[10];
20 ;; int main(int argc, char **argv) {
31 ;; Code compiled with -mllvm -memprof-ave-lifetime-cold-threshold=5 so that the
32 ;; memory freed after sleep(10) results in cold lifetimes.
34 ;; The code below was created by forcing inlining of baz into foo, and
35 ;; bar into baz. Due to the inlining of bar we will initially have two
36 ;; allocation nodes in the graph. This tests that we correctly match
37 ;; foo (with baz inlined) onto the graph nodes first, and generate a new
38 ;; fused node for it. We should then not match baz (with bar inlined) as that
39 ;; is not reached by the MIB contexts (since all calls from main will look
40 ;; like main -> foo(+baz) -> bar after the inlining reflected in this IR).
42 ;; The IR was then reduced using llvm-reduce with the expected FileCheck input.
44 ;; -stats requires asserts
47 ; RUN: opt -passes=memprof-context-disambiguation -supports-hot-cold-new \
48 ; RUN: -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
49 ; RUN: -memprof-export-to-dot -memprof-dot-file-path-prefix=%t. \
50 ; RUN: -stats -pass-remarks=memprof-context-disambiguation \
51 ; RUN: %s -S 2>&1 | FileCheck %s --check-prefix=DUMP --check-prefix=IR \
52 ; RUN: --check-prefix=STATS --check-prefix=REMARKS
54 ; RUN: cat %t.ccg.postbuild.dot | FileCheck %s --check-prefix=DOT
55 ;; We should create clones for foo and bar for the call from main to allocate
57 ; RUN: cat %t.ccg.cloned.dot | FileCheck %s --check-prefix=DOTCLONED
60 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
61 target triple = "x86_64-unknown-linux-gnu"
63 define internal ptr @_Z3barv() {
65 %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7, !memprof !0, !callsite !5
69 ; Function Attrs: nobuiltin
70 declare ptr @_Znam(i64) #0
72 ; Function Attrs: mustprogress
73 define internal ptr @_Z3bazv() #1 {
75 %call.i = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7, !memprof !0, !callsite !6
79 ; Function Attrs: noinline
80 define internal ptr @_Z3foov() #2 {
82 %call.i = call noundef ptr @_Z3barv(), !callsite !7
86 define i32 @main() #3 {
88 %call = call noundef ptr @_Z3foov(), !callsite !8
89 %call1 = call noundef ptr @_Z3foov(), !callsite !9
93 ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
94 declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4
96 ; Function Attrs: nounwind
97 declare void @_ZdaPv() #5
99 declare i32 @sleep() #6
101 attributes #0 = { nobuiltin }
102 attributes #1 = { mustprogress }
103 attributes #2 = { noinline }
104 attributes #3 = { "tune-cpu"="generic" }
105 attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
106 attributes #5 = { nounwind }
107 attributes #6 = { "disable-tail-calls"="true" }
108 attributes #7 = { builtin }
111 !1 = !{!2, !"notcold"}
112 !2 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 8632435727821051414}
114 !4 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 -3421689549917153178}
115 !5 = !{i64 9086428284934609951}
116 !6 = !{i64 9086428284934609951, i64 -5964873800580613432}
117 !7 = !{i64 -5964873800580613432, i64 2732490490862098848}
118 !8 = !{i64 8632435727821051414}
119 !9 = !{i64 -3421689549917153178}
122 ; DUMP: CCG before cloning:
123 ; DUMP: Callsite Context Graph:
124 ; DUMP: Node [[BAR:0x[a-z0-9]+]]
125 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7 (clone 0)
126 ; DUMP: AllocTypes: NotColdCold
127 ; DUMP: ContextIds: 1 2
130 ; DUMP: Edge from Callee [[BAR]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
132 ;; This is leftover from the MIB on the alloc inlined into baz. It is not
133 ;; matched with any call, since there is no such node in the IR. Due to the
134 ;; null call it will not participate in any context transformations.
135 ; DUMP: Node [[FOO2:0x[a-z0-9]+]]
137 ; DUMP: AllocTypes: NotColdCold
138 ; DUMP: ContextIds: 3 4
140 ; DUMP: Edge from Callee [[BAZ:0x[a-z0-9]+]] to Caller: [[FOO2]] AllocTypes: NotColdCold ContextIds: 3 4
142 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 3
143 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 4
145 ; DUMP: Node [[MAIN1]]
146 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
147 ; DUMP: AllocTypes: NotCold
148 ; DUMP: ContextIds: 1 3
150 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 3
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 4
159 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 4
160 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
164 ; DUMP: %call.i = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7 (clone 0)
165 ; DUMP: AllocTypes: NotColdCold
166 ; DUMP: ContextIds: 3 4
169 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO2]] AllocTypes: NotColdCold ContextIds: 3 4
171 ;; This is the node synthesized for the call to bar in foo that was created
172 ;; by inlining baz into foo.
174 ; DUMP: %call.i = call noundef ptr @_Z3barv() (clone 0)
175 ; DUMP: AllocTypes: NotColdCold
176 ; DUMP: ContextIds: 1 2
178 ; DUMP: Edge from Callee [[BAR]] to Caller: [[FOO]] AllocTypes: NotColdCold ContextIds: 1 2
180 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
181 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
183 ; DUMP: CCG after cloning:
184 ; DUMP: Callsite Context Graph:
186 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7 (clone 0)
187 ; DUMP: AllocTypes: NotCold
188 ; DUMP: ContextIds: 1
191 ; DUMP: Edge from Callee [[BAR]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
192 ; DUMP: Clones: [[BAR2:0x[a-z0-9]+]]
194 ; DUMP: Node [[FOO2]]
196 ; DUMP: AllocTypes: NotColdCold
197 ; DUMP: ContextIds: 3 4
199 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO2]] AllocTypes: NotColdCold ContextIds: 3 4
201 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 3
202 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 4
204 ; DUMP: Node [[MAIN1]]
205 ; DUMP: %call = call noundef ptr @_Z3foov() (clone 0)
206 ; DUMP: AllocTypes: NotCold
207 ; DUMP: ContextIds: 1 3
209 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 3
210 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
213 ; DUMP: Node [[MAIN2]]
214 ; DUMP: %call1 = call noundef ptr @_Z3foov() (clone 0)
215 ; DUMP: AllocTypes: Cold
216 ; DUMP: ContextIds: 2 4
218 ; DUMP: Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 4
219 ; DUMP: Edge from Callee [[FOO3:0x[a-z0-9]+]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
223 ; DUMP: %call.i = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7 (clone 0)
224 ; DUMP: AllocTypes: NotColdCold
225 ; DUMP: ContextIds: 3 4
228 ; DUMP: Edge from Callee [[BAZ]] to Caller: [[FOO2]] AllocTypes: NotColdCold ContextIds: 3 4
231 ; DUMP: %call.i = call noundef ptr @_Z3barv() (clone 0)
232 ; DUMP: AllocTypes: NotCold
233 ; DUMP: ContextIds: 1
235 ; DUMP: Edge from Callee [[BAR]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
237 ; DUMP: Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
238 ; DUMP: Clones: [[FOO3]]
240 ; DUMP: Node [[FOO3]]
241 ; DUMP: %call.i = call noundef ptr @_Z3barv() (clone 0)
242 ; DUMP: AllocTypes: Cold
243 ; DUMP: ContextIds: 2
245 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[FOO3]] AllocTypes: Cold ContextIds: 2
247 ; DUMP: Edge from Callee [[FOO3]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
248 ; DUMP: Clone of [[FOO]]
250 ; DUMP: Node [[BAR2]]
251 ; DUMP: %call = call noalias noundef nonnull ptr @_Znam(i64 noundef 10) #7 (clone 0)
252 ; DUMP: AllocTypes: Cold
253 ; DUMP: ContextIds: 2
256 ; DUMP: Edge from Callee [[BAR2]] to Caller: [[FOO3]] AllocTypes: Cold ContextIds: 2
257 ; DUMP: Clone of [[BAR]]
260 ; REMARKS: created clone _Z3barv.memprof.1
261 ; REMARKS: created clone _Z3foov.memprof.1
262 ; REMARKS: call in clone main assigned to call function clone _Z3foov.memprof.1
263 ; REMARKS: call in clone _Z3foov.memprof.1 assigned to call function clone _Z3barv.memprof.1
264 ; REMARKS: call in clone _Z3barv.memprof.1 marked with memprof allocation attribute cold
265 ; REMARKS: call in clone main assigned to call function clone _Z3foov
266 ; REMARKS: call in clone _Z3foov assigned to call function clone _Z3barv
267 ; REMARKS: call in clone _Z3barv marked with memprof allocation attribute notcold
268 ; REMARKS: call in clone _Z3bazv marked with memprof allocation attribute notcold
271 ; IR: define internal {{.*}} @_Z3barv()
272 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[NOTCOLD:[0-9]+]]
273 ; IR: define internal {{.*}} @_Z3foov()
274 ; IR: call {{.*}} @_Z3barv()
275 ; IR: define {{.*}} @main()
276 ;; The first call to foo does not allocate cold memory. It should call the
277 ;; original functions, which ultimately call the original allocation decorated
278 ;; with a "notcold" attribute.
279 ; IR: call {{.*}} @_Z3foov()
280 ;; The second call to foo allocates cold memory. It should call cloned functions
281 ;; which ultimately call a cloned allocation decorated with a "cold" attribute.
282 ; IR: call {{.*}} @_Z3foov.memprof.1()
283 ; IR: define internal {{.*}} @_Z3barv.memprof.1()
284 ; IR: call {{.*}} @_Znam(i64 noundef 10) #[[COLD:[0-9]+]]
285 ; IR: define internal {{.*}} @_Z3foov.memprof.1()
286 ; IR: call {{.*}} @_Z3barv.memprof.1()
287 ; IR: attributes #[[NOTCOLD]] = { builtin "memprof"="notcold" }
288 ; IR: attributes #[[COLD]] = { builtin "memprof"="cold" }
291 ; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)
292 ; STATS: 2 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned)
293 ; STATS: 2 memprof-context-disambiguation - Number of function clones created during whole program analysis
296 ; DOT: digraph "postbuild" {
297 ; DOT: label="postbuild";
298 ; 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}"];
299 ; DOT: Node[[FOO:0x[a-z0-9]+]] [shape=record,tooltip="N[[FOO]] ContextIds: 3 4",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: 2732490490862098848\nnull call (external)}"];
300 ; DOT: Node[[FOO]] -> Node[[BAZ:0x[a-z0-9]+]][tooltip="ContextIds: 3 4",fillcolor="mediumorchid1"];
301 ; DOT: Node[[MAIN1:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN1]] ContextIds: 1 3",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 8632435727821051414\nmain -\> _Z3foov}"];
302 ; DOT: Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 3",fillcolor="brown1"];
303 ; DOT: Node[[MAIN1]] -> Node[[FOO2:0x[a-z0-9]+]][tooltip="ContextIds: 1",fillcolor="brown1"];
304 ; DOT: Node[[MAIN2:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN2]] ContextIds: 2 4",fillcolor="cyan",style="filled",style="filled",label="{OrigId: 15025054523792398438\nmain -\> _Z3foov}"];
305 ; DOT: Node[[MAIN2]] -> Node[[FOO]][tooltip="ContextIds: 4",fillcolor="cyan"];
306 ; DOT: Node[[MAIN2]] -> Node[[FOO2]][tooltip="ContextIds: 2",fillcolor="cyan"];
307 ; DOT: Node[[BAZ]] [shape=record,tooltip="N[[BAZ]] ContextIds: 3 4",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: Alloc2\n_Z3bazv -\> _Znam}"];
308 ; DOT: Node[[FOO2]] [shape=record,tooltip="N[[FOO2]] ContextIds: 1 2",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: 0\n_Z3foov -\> _Z3barv}"];
309 ; DOT: Node[[FOO2]] -> Node[[BAR]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
313 ; DOTCLONED: digraph "cloned" {
314 ; DOTCLONED: label="cloned";
315 ; 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}"];
316 ; DOTCLONED: Node[[FOO2:0x[a-z0-9]+]] [shape=record,tooltip="N[[FOO2]] ContextIds: 3 4",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: 2732490490862098848\nnull call (external)}"];
317 ; DOTCLONED: Node[[FOO2]] -> Node[[BAZ:0x[a-z0-9]+]][tooltip="ContextIds: 3 4",fillcolor="mediumorchid1"];
318 ; DOTCLONED: Node[[MAIN1:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN1]] ContextIds: 1 3",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 8632435727821051414\nmain -\> _Z3foov}"];
319 ; DOTCLONED: Node[[MAIN1]] -> Node[[FOO2]][tooltip="ContextIds: 3",fillcolor="brown1"];
320 ; DOTCLONED: Node[[MAIN1]] -> Node[[FOO:0x[a-z0-9]+]][tooltip="ContextIds: 1",fillcolor="brown1"];
321 ; DOTCLONED: Node[[MAIN2:0x[a-z0-9]+]] [shape=record,tooltip="N[[MAIN2]] ContextIds: 2 4",fillcolor="cyan",style="filled",style="filled",label="{OrigId: 15025054523792398438\nmain -\> _Z3foov}"];
322 ; DOTCLONED: Node[[MAIN2]] -> Node[[FOO2]][tooltip="ContextIds: 4",fillcolor="cyan"];
323 ; DOTCLONED: Node[[MAIN2]] -> Node[[FOO3:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
324 ; DOTCLONED: Node[[BAZ]] [shape=record,tooltip="N[[BAZ]] ContextIds: 3 4",fillcolor="mediumorchid1",style="filled",style="filled",label="{OrigId: Alloc2\n_Z3bazv -\> _Znam}"];
325 ; DOTCLONED: Node[[FOO]] [shape=record,tooltip="N[[FOO]] ContextIds: 1",fillcolor="brown1",style="filled",style="filled",label="{OrigId: 0\n_Z3foov -\> _Z3barv}"];
326 ; DOTCLONED: Node[[FOO]] -> Node[[BAR]][tooltip="ContextIds: 1",fillcolor="brown1"];
327 ; DOTCLONED: Node[[FOO3]] [shape=record,tooltip="N[[FOO3]] ContextIds: 2",fillcolor="cyan",style="filled",color="blue",style="filled,bold,dashed",label="{OrigId: 0\n_Z3foov -\> _Z3barv}"];
328 ; DOTCLONED: Node[[FOO3]] -> Node[[BAR2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
329 ; 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}"];