xref: /llvm-project/llvm/test/ThinLTO/X86/memprof-duplicate-context-ids.ll (revision 78a195e1002dbfdfaeb7b36d5699e58b47238cbb)
1fe27495bSTeresa Johnson;; Test callsite context graph generation for call graph with with MIBs
2fe27495bSTeresa Johnson;; that have pruned contexts that partially match multiple inlined
3fe27495bSTeresa Johnson;; callsite contexts, requiring duplication of context ids and nodes
404f3c5a7STeresa Johnson;; while matching callsite nodes onto the graph. Also tests graph and IR
504f3c5a7STeresa Johnson;; cloning.
6fe27495bSTeresa Johnson;;
7fe27495bSTeresa Johnson;; Original code looks like:
8fe27495bSTeresa Johnson;;
9fe27495bSTeresa Johnson;; char *D() {
10fe27495bSTeresa Johnson;;   return new char[10];
11fe27495bSTeresa Johnson;; }
12fe27495bSTeresa Johnson;;
13fe27495bSTeresa Johnson;; char *F() {
14fe27495bSTeresa Johnson;;   return D();
15fe27495bSTeresa Johnson;; }
16fe27495bSTeresa Johnson;;
17fe27495bSTeresa Johnson;; char *C() {
18fe27495bSTeresa Johnson;;   return D();
19fe27495bSTeresa Johnson;; }
20fe27495bSTeresa Johnson;;
21fe27495bSTeresa Johnson;; char *B() {
22fe27495bSTeresa Johnson;;   return C();
23fe27495bSTeresa Johnson;; }
24fe27495bSTeresa Johnson;;
25fe27495bSTeresa Johnson;; char *E() {
26fe27495bSTeresa Johnson;;   return C();
27fe27495bSTeresa Johnson;; }
28fe27495bSTeresa Johnson;; int main(int argc, char **argv) {
29fe27495bSTeresa Johnson;;   char *x = B(); // cold
30fe27495bSTeresa Johnson;;   char *y = E(); // cold
31fe27495bSTeresa Johnson;;   char *z = F(); // default
32fe27495bSTeresa Johnson;;   memset(x, 0, 10);
33fe27495bSTeresa Johnson;;   memset(y, 0, 10);
34fe27495bSTeresa Johnson;;   memset(z, 0, 10);
35fe27495bSTeresa Johnson;;   delete[] z;
36fe27495bSTeresa Johnson;;   sleep(10);
37fe27495bSTeresa Johnson;;   delete[] x;
38fe27495bSTeresa Johnson;;   delete[] y;
39fe27495bSTeresa Johnson;;   return 0;
40fe27495bSTeresa Johnson;; }
41fe27495bSTeresa Johnson;;
42a4bdb275STeresa Johnson;; Code compiled with -mllvm -memprof-ave-lifetime-cold-threshold=5 so that the
43fe27495bSTeresa Johnson;; memory freed after sleep(10) results in cold lifetimes.
44fe27495bSTeresa Johnson;;
45fe27495bSTeresa Johnson;; The code below was created by forcing inlining of C into both B and E.
46fe27495bSTeresa Johnson;; Since both allocation contexts via C are cold, the matched memprof
47fe27495bSTeresa Johnson;; metadata has the context pruned above C's callsite. This requires
48fe27495bSTeresa Johnson;; matching the stack node for C to callsites where it was inlined (i.e.
49fe27495bSTeresa Johnson;; the callsites in B and E that have callsite metadata that includes C's).
50fe27495bSTeresa Johnson;; It also requires duplication of that node in the graph as well as the
51fe27495bSTeresa Johnson;; duplication of the context ids along that path through the graph,
52fe27495bSTeresa Johnson;; so that we can represent the duplicated (via inlining) C callsite.
53fe27495bSTeresa Johnson;;
54fe27495bSTeresa Johnson;; The IR was then reduced using llvm-reduce with the expected FileCheck input.
55fe27495bSTeresa Johnson
56e3e6bc69STeresa Johnson;; -stats requires asserts
57e3e6bc69STeresa Johnson; REQUIRES: asserts
58e3e6bc69STeresa Johnson
59fe27495bSTeresa Johnson; RUN: opt -thinlto-bc %s >%t.o
60fe27495bSTeresa Johnson; RUN: llvm-lto2 run %t.o -enable-memprof-context-disambiguation \
6117688986STeresa Johnson; RUN:	-supports-hot-cold-new \
62fe27495bSTeresa Johnson; RUN:  -r=%t.o,main,plx \
63fe27495bSTeresa Johnson; RUN:  -r=%t.o,_ZdaPv, \
64fe27495bSTeresa Johnson; RUN:  -r=%t.o,sleep, \
65fe27495bSTeresa Johnson; RUN:  -r=%t.o,_Znam, \
66fe27495bSTeresa Johnson; RUN:  -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
6704f3c5a7STeresa Johnson; RUN:  -stats -pass-remarks=memprof-context-disambiguation -save-temps \
6804f3c5a7STeresa Johnson; RUN:  -o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP \
69cfad2d3aSTeresa Johnson; RUN:  --check-prefix=STATS --check-prefix=STATS-BE --check-prefix=REMARKS
70fe27495bSTeresa Johnson
71cfad2d3aSTeresa Johnson; RUN: llvm-dis %t.out.1.4.opt.bc -o - | FileCheck %s --check-prefix=IR
72cfad2d3aSTeresa Johnson
73fe27495bSTeresa Johnson
7404f3c5a7STeresa Johnson;; Try again but with distributed ThinLTO
7504f3c5a7STeresa Johnson; RUN: llvm-lto2 run %t.o -enable-memprof-context-disambiguation \
7617688986STeresa Johnson; RUN:	-supports-hot-cold-new \
7704f3c5a7STeresa Johnson; RUN:  -thinlto-distributed-indexes \
7804f3c5a7STeresa Johnson; RUN:  -r=%t.o,main,plx \
7904f3c5a7STeresa Johnson; RUN:  -r=%t.o,_ZdaPv, \
8004f3c5a7STeresa Johnson; RUN:  -r=%t.o,sleep, \
8104f3c5a7STeresa Johnson; RUN:  -r=%t.o,_Znam, \
8204f3c5a7STeresa Johnson; RUN:  -memprof-verify-ccg -memprof-verify-nodes -memprof-dump-ccg \
8304f3c5a7STeresa Johnson; RUN:  -stats -pass-remarks=memprof-context-disambiguation \
8404f3c5a7STeresa Johnson; RUN:  -o %t2.out 2>&1 | FileCheck %s --check-prefix=DUMP \
8504f3c5a7STeresa Johnson; RUN:  --check-prefix=STATS
8604f3c5a7STeresa Johnson
8704f3c5a7STeresa Johnson
8804f3c5a7STeresa Johnson;; Check distributed index
8904f3c5a7STeresa Johnson; RUN: llvm-dis %t.o.thinlto.bc -o - | FileCheck %s --check-prefix=DISTRIB
9004f3c5a7STeresa Johnson
91cfad2d3aSTeresa Johnson;; Run ThinLTO backend
92cfad2d3aSTeresa Johnson; RUN: opt -passes=memprof-context-disambiguation \
93cfad2d3aSTeresa Johnson; RUN:  -memprof-import-summary=%t.o.thinlto.bc \
94cfad2d3aSTeresa Johnson; RUN:  -stats -pass-remarks=memprof-context-disambiguation \
95cfad2d3aSTeresa Johnson; RUN:  %t.o -S 2>&1 | FileCheck %s --check-prefix=IR \
96cfad2d3aSTeresa Johnson; RUN:  --check-prefix=STATS-BE --check-prefix=REMARKS
97cfad2d3aSTeresa Johnson
98fe27495bSTeresa Johnsonsource_filename = "duplicate-context-ids.ll"
99fe27495bSTeresa Johnsontarget datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
100fe27495bSTeresa Johnsontarget triple = "x86_64-unknown-linux-gnu"
101fe27495bSTeresa Johnson
102cfad2d3aSTeresa Johnsondefine internal ptr @_Z1Dv() #0 {
103fe27495bSTeresa Johnsonentry:
104fe27495bSTeresa Johnson  %call = call ptr @_Znam(i64 0), !memprof !0, !callsite !5
105fe27495bSTeresa Johnson  ret ptr null
106fe27495bSTeresa Johnson}
107fe27495bSTeresa Johnson
108fe27495bSTeresa Johnsondeclare ptr @_Znam(i64)
109fe27495bSTeresa Johnson
110cfad2d3aSTeresa Johnsondefine internal ptr @_Z1Fv() #0 {
111fe27495bSTeresa Johnsonentry:
112fe27495bSTeresa Johnson  %call = call ptr @_Z1Dv(), !callsite !6
113fe27495bSTeresa Johnson  ret ptr null
114fe27495bSTeresa Johnson}
115fe27495bSTeresa Johnson
116cfad2d3aSTeresa Johnsondefine internal ptr @_Z1Cv() #0 {
117fe27495bSTeresa Johnsonentry:
118fe27495bSTeresa Johnson  %call = call ptr @_Z1Dv(), !callsite !7
119fe27495bSTeresa Johnson  ret ptr null
120fe27495bSTeresa Johnson}
121fe27495bSTeresa Johnson
122cfad2d3aSTeresa Johnsondefine internal ptr @_Z1Bv() #0 {
123fe27495bSTeresa Johnsonentry:
124fe27495bSTeresa Johnson  %call.i = call ptr @_Z1Dv(), !callsite !8
125fe27495bSTeresa Johnson  ret ptr null
126fe27495bSTeresa Johnson}
127fe27495bSTeresa Johnson
128cfad2d3aSTeresa Johnsondefine internal ptr @_Z1Ev() #0 {
129fe27495bSTeresa Johnsonentry:
130fe27495bSTeresa Johnson  %call.i = call ptr @_Z1Dv(), !callsite !9
131fe27495bSTeresa Johnson  ret ptr null
132fe27495bSTeresa Johnson}
133fe27495bSTeresa Johnson
134cfad2d3aSTeresa Johnsondefine i32 @main() #0 {
13504f3c5a7STeresa Johnsonentry:
13604f3c5a7STeresa Johnson  call ptr @_Z1Bv()
13704f3c5a7STeresa Johnson  call ptr @_Z1Ev()
13804f3c5a7STeresa Johnson  call ptr @_Z1Fv()
13904f3c5a7STeresa Johnson  ret i32 0
14004f3c5a7STeresa Johnson}
141fe27495bSTeresa Johnson
142fe27495bSTeresa Johnsondeclare void @_ZdaPv()
143fe27495bSTeresa Johnson
144fe27495bSTeresa Johnsondeclare i32 @sleep()
145fe27495bSTeresa Johnson
146cfad2d3aSTeresa Johnsonattributes #0 = { noinline optnone}
147cfad2d3aSTeresa Johnson
148fe27495bSTeresa Johnson!0 = !{!1, !3}
149fe27495bSTeresa Johnson!1 = !{!2, !"cold"}
150fe27495bSTeresa Johnson!2 = !{i64 6541423618768552252, i64 -6270142974039008131}
151fe27495bSTeresa Johnson!3 = !{!4, !"notcold"}
152fe27495bSTeresa Johnson!4 = !{i64 6541423618768552252, i64 -4903163940066524832}
153fe27495bSTeresa Johnson!5 = !{i64 6541423618768552252}
154fe27495bSTeresa Johnson!6 = !{i64 -4903163940066524832}
155fe27495bSTeresa Johnson!7 = !{i64 -6270142974039008131}
156fe27495bSTeresa Johnson!8 = !{i64 -6270142974039008131, i64 -184525619819294889}
157fe27495bSTeresa Johnson!9 = !{i64 -6270142974039008131, i64 1905834578520680781}
158fe27495bSTeresa Johnson
159fe27495bSTeresa Johnson
160fe27495bSTeresa Johnson;; After adding only the alloc node memprof metadata, we only have 2 contexts.
161fe27495bSTeresa Johnson
162fe27495bSTeresa Johnson; DUMP: CCG before updating call stack chains:
163fe27495bSTeresa Johnson; DUMP: Callsite Context Graph:
164fe27495bSTeresa Johnson; DUMP: Node [[D:0x[a-z0-9]+]]
165fe27495bSTeresa Johnson; DUMP: 	Versions: 1 MIB:
166fe27495bSTeresa Johnson; DUMP: 		AllocType 2 StackIds: 0
167fe27495bSTeresa Johnson; DUMP: 		AllocType 1 StackIds: 1
168fe27495bSTeresa Johnson; DUMP: 	(clone 0)
169fe27495bSTeresa Johnson; DUMP: 	AllocTypes: NotColdCold
170fe27495bSTeresa Johnson; DUMP: 	ContextIds: 1 2
171fe27495bSTeresa Johnson; DUMP: 	CalleeEdges:
172fe27495bSTeresa Johnson; DUMP: 	CallerEdges:
173fe27495bSTeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[C:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 1
174fe27495bSTeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[F:0x[a-z0-9]+]] AllocTypes: NotCold ContextIds: 2
175fe27495bSTeresa Johnson
176fe27495bSTeresa Johnson;; After updating for callsite metadata, we should have generated context ids 3 and 4,
177fe27495bSTeresa Johnson;; along with 2 new nodes for those callsites. All have the same allocation type
178fe27495bSTeresa Johnson;; behavior as the original C node.
179fe27495bSTeresa Johnson
180fe27495bSTeresa Johnson; DUMP: CCG before cloning:
181fe27495bSTeresa Johnson; DUMP: Callsite Context Graph:
182fe27495bSTeresa Johnson; DUMP: Node [[D]]
183fe27495bSTeresa Johnson; DUMP: 	Versions: 1 MIB:
184fe27495bSTeresa Johnson; DUMP: 		AllocType 2 StackIds: 0
185fe27495bSTeresa Johnson; DUMP: 		AllocType 1 StackIds: 1
186fe27495bSTeresa Johnson; DUMP: 	(clone 0)
187fe27495bSTeresa Johnson; DUMP: 	AllocTypes: NotColdCold
188fe27495bSTeresa Johnson; DUMP: 	ContextIds: 1 2 3 4
189fe27495bSTeresa Johnson; DUMP: 	CalleeEdges:
190fe27495bSTeresa Johnson; DUMP: 	CallerEdges:
191fe27495bSTeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[F]] AllocTypes: NotCold ContextIds: 2
19283aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[C1:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 3
19383aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[C2:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 4
19483aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[C0:0x[a-z0-9]+]] AllocTypes: Cold ContextIds: 1
195fe27495bSTeresa Johnson
196a104e270STeresa Johnson; DUMP: CCG after cloning:
197a104e270STeresa Johnson; DUMP: Callsite Context Graph:
198a104e270STeresa Johnson; DUMP: Node [[D]]
199a104e270STeresa Johnson; DUMP:         Versions: 1 MIB:
200a104e270STeresa Johnson; DUMP:                 AllocType 2 StackIds: 0
201a104e270STeresa Johnson; DUMP:                 AllocType 1 StackIds: 1
202a104e270STeresa Johnson; DUMP:         (clone 0)
203a104e270STeresa Johnson; DUMP: 	AllocTypes: NotCold
204a104e270STeresa Johnson; DUMP: 	ContextIds: 2
205a104e270STeresa Johnson; DUMP: 	CalleeEdges:
206a104e270STeresa Johnson; DUMP: 	CallerEdges:
207a104e270STeresa Johnson; DUMP: 		Edge from Callee [[D]] to Caller: [[F]] AllocTypes: NotCold ContextIds: 2
208a104e270STeresa Johnson; DUMP:         Clones: [[D2:0x[a-z0-9]+]]
209a104e270STeresa Johnson
210a104e270STeresa Johnson; DUMP: Node [[D2]]
211a104e270STeresa Johnson; DUMP:         Versions: 1 MIB:
212a104e270STeresa Johnson; DUMP:                 AllocType 2 StackIds: 0
213a104e270STeresa Johnson; DUMP:                 AllocType 1 StackIds: 1
214a104e270STeresa Johnson; DUMP:         (clone 0)
215a104e270STeresa Johnson; DUMP: 	AllocTypes: Cold
216a104e270STeresa Johnson; DUMP: 	ContextIds: 1 3 4
217a104e270STeresa Johnson; DUMP: 	CalleeEdges:
218a104e270STeresa Johnson; DUMP: 	CallerEdges:
21983aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D2]] to Caller: [[C0]] AllocTypes: Cold ContextIds: 1
22083aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D2]] to Caller: [[C1]] AllocTypes: Cold ContextIds: 3
22183aa7250STeresa Johnson; DUMP: 		Edge from Callee [[D2]] to Caller: [[C2]] AllocTypes: Cold ContextIds: 4
222a104e270STeresa Johnson; DUMP:         Clone of [[D]]
223a104e270STeresa Johnson
224cfad2d3aSTeresa Johnson; REMARKS: created clone _Z1Dv.memprof.1
225cfad2d3aSTeresa Johnson; REMARKS: call in clone _Z1Dv marked with memprof allocation attribute notcold
226cfad2d3aSTeresa Johnson; REMARKS: call in clone _Z1Dv.memprof.1 marked with memprof allocation attribute cold
227cfad2d3aSTeresa Johnson; REMARKS: call in clone _Z1Bv assigned to call function clone _Z1Dv.memprof.1
228cfad2d3aSTeresa Johnson; REMARKS: call in clone _Z1Ev assigned to call function clone _Z1Dv.memprof.1
229cfad2d3aSTeresa Johnson
230cfad2d3aSTeresa Johnson
231cfad2d3aSTeresa Johnson;; The allocation via F does not allocate cold memory. It should call the
232cfad2d3aSTeresa Johnson;; original D, which ultimately call the original allocation decorated
233cfad2d3aSTeresa Johnson;; with a "notcold" attribute.
234cfad2d3aSTeresa Johnson; IR: define internal {{.*}} @_Z1Dv()
235cfad2d3aSTeresa Johnson; IR:   call {{.*}} @_Znam(i64 0) #[[NOTCOLD:[0-9]+]]
236cfad2d3aSTeresa Johnson; IR: define internal {{.*}} @_Z1Fv()
237cfad2d3aSTeresa Johnson; IR:   call {{.*}} @_Z1Dv()
238cfad2d3aSTeresa Johnson;; The allocations via B and E allocate cold memory. They should call the
239cfad2d3aSTeresa Johnson;; cloned D, which ultimately call the cloned allocation decorated with a
240cfad2d3aSTeresa Johnson;; "cold" attribute.
241cfad2d3aSTeresa Johnson; IR: define internal {{.*}} @_Z1Bv()
242cfad2d3aSTeresa Johnson; IR:   call {{.*}} @_Z1Dv.memprof.1()
243cfad2d3aSTeresa Johnson; IR: define internal {{.*}} @_Z1Ev()
244cfad2d3aSTeresa Johnson; IR:   call {{.*}} @_Z1Dv.memprof.1()
245cfad2d3aSTeresa Johnson; IR: define internal {{.*}} @_Z1Dv.memprof.1()
246cfad2d3aSTeresa Johnson; IR:   call {{.*}} @_Znam(i64 0) #[[COLD:[0-9]+]]
247cfad2d3aSTeresa Johnson; IR: attributes #[[NOTCOLD]] = { "memprof"="notcold" }
248cfad2d3aSTeresa Johnson; IR: attributes #[[COLD]] = { "memprof"="cold" }
249cfad2d3aSTeresa Johnson
250a104e270STeresa Johnson
25104f3c5a7STeresa Johnson; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)
252cfad2d3aSTeresa Johnson; STATS-BE: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned) during ThinLTO backend
25304f3c5a7STeresa Johnson; STATS: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned)
254cfad2d3aSTeresa Johnson; STATS-BE: 1 memprof-context-disambiguation - Number of not cold static allocations (possibly cloned) during ThinLTO backend
255cfad2d3aSTeresa Johnson; STATS-BE: 2 memprof-context-disambiguation - Number of allocation versions (including clones) during ThinLTO backend
25604f3c5a7STeresa Johnson; STATS: 1 memprof-context-disambiguation - Number of function clones created during whole program analysis
257cfad2d3aSTeresa Johnson; STATS-BE: 1 memprof-context-disambiguation - Number of function clones created during ThinLTO backend
258cfad2d3aSTeresa Johnson; STATS-BE: 1 memprof-context-disambiguation - Number of functions that had clones created during ThinLTO backend
259cfad2d3aSTeresa Johnson; STATS-BE: 2 memprof-context-disambiguation - Maximum number of allocation versions created for an original allocation during ThinLTO backend
260cfad2d3aSTeresa Johnson; STATS-BE: 1 memprof-context-disambiguation - Number of original (not cloned) allocations with memprof profiles during ThinLTO backend
26104f3c5a7STeresa Johnson
26204f3c5a7STeresa Johnson
263*78a195e1SMingming Liu; DISTRIB: ^[[E:[0-9]+]] = gv: (guid: 331966645857188136, {{.*}} callsites: ((callee: ^[[D:[0-9]+]], clones: (1)
264*78a195e1SMingming Liu; DISTRIB: ^[[D]] = gv: (guid: 11079124245221721799, {{.*}} allocs: ((versions: (notcold, cold)
265*78a195e1SMingming Liu; DISTRIB: ^[[F:[0-9]+]] = gv: (guid: 11254287701717398916, {{.*}} callsites: ((callee: ^[[D]], clones: (0)
266*78a195e1SMingming Liu; DISTRIB: ^[[B:[0-9]+]] = gv: (guid: 13579056193435805313, {{.*}} callsites: ((callee: ^[[D]], clones: (1)
267*78a195e1SMingming Liu; DISTRIB: ^[[C:[0-9]+]] = gv: (guid: 15101436305866936160, {{.*}} callsites: ((callee: ^[[D:[0-9]+]], clones: (1)
268