xref: /llvm-project/llvm/test/ThinLTO/X86/memprof-basic.ll (revision becc02ce93cd14f07f444fff6f7433c1ecf13664)
1;; Test callsite context graph generation for simple call graph with
2;; two memprof contexts and no inlining.
3;;
4;; Original code looks like:
5;;
6;; char *bar() {
7;;   return new char[10];
8;; }
9;;
10;; char *baz() {
11;;   return bar();
12;; }
13;;
14;; char *foo() {
15;;   return baz();
16;; }
17;;
18;; int main(int argc, char **argv) {
19;;   char *x = foo();
20;;   char *y = foo();
21;;   memset(x, 0, 10);
22;;   memset(y, 0, 10);
23;;   delete[] x;
24;;   sleep(10);
25;;   delete[] y;
26;;   return 0;
27;; }
28;;
29;; Code compiled with -mllvm -memprof-ave-lifetime-cold-threshold=5 so that the
30;; memory freed after sleep(10) results in cold lifetimes.
31;;
32;; The IR was then reduced using llvm-reduce with the expected FileCheck input.
33
34;; -stats requires asserts
35; REQUIRES: asserts
36
37; RUN: opt -thinlto-bc -memprof-report-hinted-sizes %s >%t.o
38; RUN: llvm-lto2 run %t.o -enable-memprof-context-disambiguation \
39; RUN:	-supports-hot-cold-new \
40; RUN:	-r=%t.o,main,plx \
41; RUN:	-r=%t.o,_ZdaPv, \
42; RUN:	-r=%t.o,sleep, \
43; RUN:	-r=%t.o,_Znam, \
44; RUN:	-memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
45; RUN:	-memprof-export-to-dot -memprof-dot-file-path-prefix=%t. \
46; RUN:	-memprof-report-hinted-sizes \
47; RUN:	-stats -pass-remarks=memprof-context-disambiguation -save-temps \
48; RUN:	-o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP \
49; RUN:	--check-prefix=STATS --check-prefix=STATS-BE --check-prefix=REMARKS \
50; RUN:  --check-prefix=SIZES
51
52; RUN:	cat %t.ccg.postbuild.dot | FileCheck %s --check-prefix=DOT
53;; We should have cloned bar, baz, and foo, for the cold memory allocation.
54; RUN:	cat %t.ccg.cloned.dot | FileCheck %s --check-prefix=DOTCLONED
55
56; RUN: llvm-dis %t.out.1.4.opt.bc -o - | FileCheck %s --check-prefix=IR
57
58
59;; Try again but with distributed ThinLTO
60; RUN: llvm-lto2 run %t.o -enable-memprof-context-disambiguation \
61; RUN:	-supports-hot-cold-new \
62; RUN:  -thinlto-distributed-indexes \
63; RUN:	-r=%t.o,main,plx \
64; RUN:	-r=%t.o,_ZdaPv, \
65; RUN:	-r=%t.o,sleep, \
66; RUN:	-r=%t.o,_Znam, \
67; RUN:	-memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
68; RUN:	-memprof-export-to-dot -memprof-dot-file-path-prefix=%t2. \
69; RUN:	-memprof-report-hinted-sizes \
70; RUN:	-stats -pass-remarks=memprof-context-disambiguation \
71; RUN:	-o %t2.out 2>&1 | FileCheck %s --check-prefix=DUMP \
72; RUN:	--check-prefix=STATS --check-prefix=SIZES
73
74; RUN:	cat %t2.ccg.postbuild.dot | FileCheck %s --check-prefix=DOT
75;; We should have cloned bar, baz, and foo, for the cold memory allocation.
76; RUN:	cat %t2.ccg.cloned.dot | FileCheck %s --check-prefix=DOTCLONED
77
78;; Check distributed index
79; RUN: llvm-dis %t.o.thinlto.bc -o - | FileCheck %s --check-prefix=DISTRIB
80
81;; Run ThinLTO backend
82; RUN: opt -passes=memprof-context-disambiguation \
83; RUN:	-memprof-import-summary=%t.o.thinlto.bc \
84; RUN:  -stats -pass-remarks=memprof-context-disambiguation \
85; RUN:  %t.o -S 2>&1 | FileCheck %s --check-prefix=IR \
86; RUN:  --check-prefix=STATS-BE --check-prefix=REMARKS
87
88source_filename = "memprof-basic.ll"
89target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
90target triple = "x86_64-unknown-linux-gnu"
91
92define i32 @main() #0 {
93entry:
94  %call = call ptr @_Z3foov(), !callsite !0
95  %call1 = call ptr @_Z3foov(), !callsite !1
96  ret i32 0
97}
98
99declare void @_ZdaPv()
100
101declare i32 @sleep()
102
103define internal ptr @_Z3barv() #0 {
104entry:
105  %call = call ptr @_Znam(i64 0), !memprof !2, !callsite !7
106  ret ptr null
107}
108
109declare ptr @_Znam(i64)
110
111define internal ptr @_Z3bazv() #0 {
112entry:
113  %call = call ptr @_Z3barv(), !callsite !8
114  ret ptr null
115}
116
117define internal ptr @_Z3foov() #0 {
118entry:
119  %call = call ptr @_Z3bazv(), !callsite !9
120  ret ptr null
121}
122
123; uselistorder directives
124uselistorder ptr @_Z3foov, { 1, 0 }
125
126attributes #0 = { noinline optnone }
127
128!0 = !{i64 8632435727821051414}
129!1 = !{i64 -3421689549917153178}
130!2 = !{!3, !5}
131!3 = !{!4, !"notcold", i64 100}
132!4 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 8632435727821051414}
133!5 = !{!6, !"cold", i64 400}
134!6 = !{i64 9086428284934609951, i64 -5964873800580613432, i64 2732490490862098848, i64 -3421689549917153178}
135!7 = !{i64 9086428284934609951}
136!8 = !{i64 -5964873800580613432}
137!9 = !{i64 2732490490862098848}
138
139
140; DUMP: CCG before cloning:
141; DUMP: Callsite Context Graph:
142; DUMP: Node [[BAR:0x[a-z0-9]+]]
143; DUMP: 	Versions: 1 MIB:
144; DUMP: 		AllocType 1 StackIds: 2, 3, 0
145; DUMP: 		AllocType 2 StackIds: 2, 3, 1
146; DUMP: 	(clone 0)
147; DUMP: 	AllocTypes: NotColdCold
148; DUMP: 	ContextIds: 1 2
149; DUMP: 	CalleeEdges:
150; DUMP: 	CallerEdges:
151; DUMP: 		Edge from Callee [[BAR]] to Caller: [[BAZ:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
152
153; DUMP: Node [[BAZ]]
154; DUMP: 	Callee: 11481133863268513686 (_Z3barv) Clones: 0 StackIds: 2	(clone 0)
155; DUMP: 	AllocTypes: NotColdCold
156; DUMP: 	ContextIds: 1 2
157; DUMP: 	CalleeEdges:
158; DUMP: 		Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotColdCold ContextIds: 1 2
159; DUMP: 	CallerEdges:
160; DUMP: 		Edge from Callee [[BAZ]] to Caller: [[FOO:0x[a-z0-9]+]] AllocTypes: NotColdCold ContextIds: 1 2
161
162; DUMP: Node [[FOO]]
163; DUMP: 	Callee: 1807954217441101578 (_Z3bazv) Clones: 0 StackIds: 3	(clone 0)
164; DUMP: 	AllocTypes: NotColdCold
165; DUMP: 	ContextIds: 1 2
166; DUMP: 	CalleeEdges:
167; DUMP: 		Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotColdCold ContextIds: 1 2
168; DUMP: 	CallerEdges:
169; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN1:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 1
170; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 2
171
172; DUMP: Node [[MAIN1]]
173; DUMP: 	Callee: 8107868197919466657 (_Z3foov) Clones: 0 StackIds: 0	(clone 0)
174; DUMP: 	AllocTypes: NotCold
175; DUMP: 	ContextIds: 1
176; DUMP: 	CalleeEdges:
177; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
178; DUMP: 	CallerEdges:
179
180; DUMP: Node [[MAIN2]]
181; DUMP: 	Callee: 8107868197919466657 (_Z3foov) Clones: 0 StackIds: 1	(clone 0)
182; DUMP: 	AllocTypes: Cold
183; DUMP: 	ContextIds: 2
184; DUMP: 	CalleeEdges:
185; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
186; DUMP: 	CallerEdges:
187
188; DUMP: CCG after cloning:
189; DUMP: Callsite Context Graph:
190; DUMP: Node [[BAR]]
191; DUMP: 	Versions: 1 MIB:
192; DUMP:                 AllocType 1 StackIds: 2, 3, 0
193; DUMP:                 AllocType 2 StackIds: 2, 3, 1
194; DUMP:         (clone 0)
195; DUMP: 	AllocTypes: NotCold
196; DUMP: 	ContextIds: 1
197; DUMP: 	CalleeEdges:
198; DUMP: 	CallerEdges:
199; DUMP: 		Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotCold ContextIds: 1
200; DUMP:		Clones: [[BAR2:0x[a-z0-9]+]]
201
202; DUMP: Node [[BAZ]]
203; DUMP: 	Callee: 11481133863268513686 (_Z3barv) Clones: 0 StackIds: 2    (clone 0)
204; DUMP: 	AllocTypes: NotCold
205; DUMP: 	ContextIds: 1
206; DUMP: 	CalleeEdges:
207; DUMP: 		Edge from Callee [[BAR]] to Caller: [[BAZ]] AllocTypes: NotCold ContextIds: 1
208; DUMP: 	CallerEdges:
209; DUMP: 		Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
210; DUMP:		Clones: [[BAZ2:0x[a-z0-9]+]]
211
212; DUMP: Node [[FOO]]
213; DUMP: 	Callee: 1807954217441101578 (_Z3bazv) Clones: 0 StackIds: 3    (clone 0)
214; DUMP: 	AllocTypes: NotCold
215; DUMP: 	ContextIds: 1
216; DUMP: 	CalleeEdges:
217; DUMP: 		Edge from Callee [[BAZ]] to Caller: [[FOO]] AllocTypes: NotCold ContextIds: 1
218; DUMP: 	CallerEdges:
219; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
220; DUMP:		Clones: [[FOO2:0x[a-z0-9]+]]
221
222; DUMP: Node [[MAIN1]]
223; DUMP: 	Callee: 8107868197919466657 (_Z3foov) Clones: 0 StackIds: 0     (clone 0)
224; DUMP: 	AllocTypes: NotCold
225; DUMP: 	ContextIds: 1
226; DUMP: 	CalleeEdges:
227; DUMP: 		Edge from Callee [[FOO]] to Caller: [[MAIN1]] AllocTypes: NotCold ContextIds: 1
228; DUMP: 	CallerEdges:
229
230; DUMP: Node [[MAIN2]]
231; DUMP: 	Callee: 8107868197919466657 (_Z3foov) Clones: 0 StackIds: 1     (clone 0)
232; DUMP: 	AllocTypes: Cold
233; DUMP: 	ContextIds: 2
234; DUMP: 	CalleeEdges:
235; DUMP: 		Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
236; DUMP: 	CallerEdges:
237
238; DUMP: Node [[FOO2]]
239; DUMP: 	Callee: 1807954217441101578 (_Z3bazv) Clones: 0 StackIds: 3    (clone 0)
240; DUMP: 	AllocTypes: Cold
241; DUMP: 	ContextIds: 2
242; DUMP: 	CalleeEdges:
243; DUMP: 		Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
244; DUMP: 	CallerEdges:
245; DUMP: 		Edge from Callee [[FOO2]] to Caller: [[MAIN2]] AllocTypes: Cold ContextIds: 2
246; DUMP:		Clone of [[FOO]]
247
248; DUMP: Node [[BAZ2]]
249; DUMP: 	Callee: 11481133863268513686 (_Z3barv) Clones: 0 StackIds: 2    (clone 0)
250; DUMP: 	AllocTypes: Cold
251; DUMP: 	ContextIds: 2
252; DUMP: 	CalleeEdges:
253; DUMP: 		Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
254; DUMP: 	CallerEdges:
255; DUMP: 		Edge from Callee [[BAZ2]] to Caller: [[FOO2]] AllocTypes: Cold ContextIds: 2
256; DUMP:		Clone of [[BAZ]]
257
258; DUMP: Node [[BAR2]]
259; DUMP: 	Versions: 1 MIB:
260; DUMP:                 AllocType 1 StackIds: 2, 3, 0
261; DUMP:                 AllocType 2 StackIds: 2, 3, 1
262; DUMP:         (clone 0)
263; DUMP: 	AllocTypes: Cold
264; DUMP: 	ContextIds: 2
265; DUMP: 	CalleeEdges:
266; DUMP: 	CallerEdges:
267; DUMP: 		Edge from Callee [[BAR2]] to Caller: [[BAZ2]] AllocTypes: Cold ContextIds: 2
268; DUMP:		Clone of [[BAR]]
269
270; SIZES: NotCold context 1 with total size 100 is NotCold after cloning
271; SIZES: Cold context 2 with total size 400 is Cold after cloning
272
273; REMARKS: call in clone main assigned to call function clone _Z3foov.memprof.1
274; REMARKS: created clone _Z3barv.memprof.1
275; REMARKS: call in clone _Z3barv marked with memprof allocation attribute notcold
276; REMARKS: call in clone _Z3barv.memprof.1 marked with memprof allocation attribute cold
277; REMARKS: created clone _Z3bazv.memprof.1
278; REMARKS: call in clone _Z3bazv.memprof.1 assigned to call function clone _Z3barv.memprof.1
279; REMARKS: created clone _Z3foov.memprof.1
280; REMARKS: call in clone _Z3foov.memprof.1 assigned to call function clone _Z3bazv.memprof.1
281
282
283; IR: define {{.*}} @main
284;; The first call to foo does not allocate cold memory. It should call the
285;; original functions, which ultimately call the original allocation decorated
286;; with a "notcold" attribute.
287; IR:   call {{.*}} @_Z3foov()
288;; The second call to foo allocates cold memory. It should call cloned functions
289;; which ultimately call a cloned allocation decorated with a "cold" attribute.
290; IR:   call {{.*}} @_Z3foov.memprof.1()
291; IR: define internal {{.*}} @_Z3barv()
292; IR:   call {{.*}} @_Znam(i64 0) #[[NOTCOLD:[0-9]+]]
293; IR: define internal {{.*}} @_Z3bazv()
294; IR:   call {{.*}} @_Z3barv()
295; IR: define internal {{.*}} @_Z3foov()
296; IR:   call {{.*}} @_Z3bazv()
297; IR: define internal {{.*}} @_Z3barv.memprof.1()
298; IR:   call {{.*}} @_Znam(i64 0) #[[COLD:[0-9]+]]
299; IR: define internal {{.*}} @_Z3bazv.memprof.1()
300; IR:   call {{.*}} @_Z3barv.memprof.1()
301; IR: define internal {{.*}} @_Z3foov.memprof.1()
302; IR:   call {{.*}} @_Z3bazv.memprof.1()
303; IR: attributes #[[NOTCOLD]] = { "memprof"="notcold" }
304; IR: attributes #[[COLD]] = { "memprof"="cold" }
305
306
307; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)
308; STATS-BE: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned) during ThinLTO backend
309; STATS: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned)
310; STATS-BE: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned) during ThinLTO backend
311; STATS-BE: 2 memprof-context-disambiguation - Number of allocation versions (including clones) during ThinLTO backend
312; STATS: 3 memprof-context-disambiguation - Number of function clones created during whole program analysis
313; STATS-BE: 3 memprof-context-disambiguation - Number of function clones created during ThinLTO backend
314; STATS-BE: 3 memprof-context-disambiguation - Number of functions that had clones created during ThinLTO backend
315; STATS-BE: 2 memprof-context-disambiguation - Maximum number of allocation versions created for an original allocation during ThinLTO backend
316; STATS-BE: 1 memprof-context-disambiguation - Number of original (not cloned) allocations with memprof profiles during ThinLTO backend
317
318
319; DOT: digraph "postbuild" {
320; DOT: 	label="postbuild";
321; 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 -\> alloc}"];
322; 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}"];
323; DOT: 	Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
324; 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}"];
325; DOT: 	Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1 2",fillcolor="mediumorchid1"];
326; 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}"];
327; DOT: 	Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
328; 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}"];
329; DOT: 	Node[[MAIN2]] -> Node[[FOO]][tooltip="ContextIds: 2",fillcolor="cyan"];
330; DOT: }
331
332
333; DOTCLONED: digraph "cloned" {
334; DOTCLONED: 	label="cloned";
335; 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 -\> alloc}"];
336; 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}"];
337; DOTCLONED: 	Node[[BAZ]] -> Node[[BAR]][tooltip="ContextIds: 1",fillcolor="brown1"];
338; 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}"];
339; DOTCLONED: 	Node[[FOO]] -> Node[[BAZ]][tooltip="ContextIds: 1",fillcolor="brown1"];
340; 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}"];
341; DOTCLONED: 	Node[[MAIN1]] -> Node[[FOO]][tooltip="ContextIds: 1",fillcolor="brown1"];
342; 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}"];
343; DOTCLONED: 	Node[[MAIN2]] -> Node[[FOO2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
344; 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}"];
345; DOTCLONED: 	Node[[FOO2]] -> Node[[BAZ2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
346; 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}"];
347; DOTCLONED: 	Node[[BAZ2]] -> Node[[BAR2:0x[a-z0-9]+]][tooltip="ContextIds: 2",fillcolor="cyan"];
348; 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 -\> alloc}"];
349; DOTCLONED: }
350
351
352; DISTRIB: ^[[BAZ:[0-9]+]] = gv: (guid: 1807954217441101578, {{.*}} callsites: ((callee: ^[[BAR:[0-9]+]], clones: (0, 1)
353; DISTRIB: ^[[FOO:[0-9]+]] = gv: (guid: 8107868197919466657, {{.*}} callsites: ((callee: ^[[BAZ]], clones: (0, 1)
354; DISTRIB: ^[[BAR]] = gv: (guid: 11481133863268513686, {{.*}} allocs: ((versions: (notcold, cold)
355; DISTRIB: ^[[MAIN:[0-9]+]] = gv: (guid: 15822663052811949562, {{.*}} callsites: ((callee: ^[[FOO]], clones: (0), {{.*}} (callee: ^[[FOO]], clones: (1)
356