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