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