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