1; Test for CSSPGO's SampleContextTracker to make sure context profile tree is promoted and merged properly
2; based on inline decision, so post inline counts are accurate.
3
4; Note that we need new pass manager to enable top-down processing for sample profile loader
5; Testwe we inlined the following in top-down order and entry counts accurate reflects post-inline base profile
6;   main:3 @ _Z5funcAi
7;   main:3 @ _Z5funcAi:1 @ _Z8funcLeafi
8;   _Z5funcBi:1 @ _Z8funcLeafi
9; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profile-context-tracker.prof -sample-profile-inline-size -profile-sample-accurate -S | FileCheck %s --check-prefix=INLINE-ALL
10
11; Testwe we inlined the following in top-down order and entry counts accurate reflects post-inline base profile
12;   main:3 @ _Z5funcAi
13;   _Z5funcAi:1 @ _Z8funcLeafi
14;   _Z5funcBi:1 @ _Z8funcLeafi
15; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profile-context-tracker.prof -profile-sample-accurate -S | FileCheck %s --check-prefix=INLINE-HOT
16
17
18@factor = dso_local global i32 3, align 4, !dbg !0
19
20define dso_local i32 @main() local_unnamed_addr #0 !dbg !18 {
21; INLINE-ALL: @main{{.*}}!prof ![[MAIN_PROF:[0-9]+]]
22; INLINE-HOT: @main{{.*}}!prof ![[MAIN_PROF:[0-9]+]]
23entry:
24  br label %for.body, !dbg !25
25
26for.cond.cleanup:                                 ; preds = %for.body
27  ret i32 %add3, !dbg !27
28
29for.body:                                         ; preds = %for.body, %entry
30  %x.011 = phi i32 [ 300000, %entry ], [ %dec, %for.body ]
31  %r.010 = phi i32 [ 0, %entry ], [ %add3, %for.body ]
32  %call = tail call i32 @_Z5funcBi(i32 %x.011), !dbg !32
33; _Z5funcBi is marked noinline
34; INLINE-ALL: call i32 @_Z5funcBi
35; INLINE-HOT: call i32 @_Z5funcBi
36  %add = add nuw nsw i32 %x.011, 1, !dbg !31
37  %call1 = tail call i32 @_Z5funcAi(i32 %add), !dbg !28
38; INLINE-ALL-NOT: call i32 @_Z5funcAi
39; INLINE-HOT: call i32 @_Z5funcAi
40  %add2 = add i32 %call, %r.010, !dbg !34
41  %add3 = add i32 %add2, %call1, !dbg !35
42  %dec = add nsw i32 %x.011, -1, !dbg !36
43  %cmp = icmp eq i32 %x.011, 0, !dbg !38
44  br i1 %cmp, label %for.cond.cleanup, label %for.body, !dbg !25
45}
46
47define dso_local i32 @_Z5funcAi(i32 %x) local_unnamed_addr #1 !dbg !40 {
48; _Z5funcAi is inlined, so outline remainder should have zero counts
49; INLINE-ALL: @_Z5funcAi{{.*}}!prof ![[FUNCA_PROF:[0-9]+]]
50; INLINE-HOT: @_Z5funcAi{{.*}}!prof ![[FUNCA_PROF:[0-9]+]]
51entry:
52  %add = add nsw i32 %x, 100000, !dbg !44
53; _Z8funcLeafi is already inlined on main->_Z5funcAi->_Z8funcLeafi,
54; so it should not be inlined on _Z5funcAi->_Z8funcLeafi based on updated
55; (merged and promoted) context profile
56; INLINE-ALL: call i32 @_Z8funcLeafi
57; INLINE-HOT-NOT: call i32 @_Z8funcLeafi
58  %call = tail call i32 @_Z8funcLeafi(i32 %add), !dbg !45
59  ret i32 %call, !dbg !46
60}
61
62define dso_local i32 @_Z8funcLeafi(i32 %x) local_unnamed_addr #1 !dbg !54 {
63; main->_Z5funcAi->_Z8funcLeafi is inlined, and _Z5funcBi->_Z8funcLeafi is also
64; inlined, so outline remainder should have empty profile
65; INLINE-ALL: @_Z8funcLeafi{{.*}}!prof ![[LEAF_PROF:[0-9]+]]
66; INLINE-HOT: @_Z8funcLeafi{{.*}}!prof ![[LEAF_PROF:[0-9]+]]
67entry:
68  %cmp = icmp sgt i32 %x, 0, !dbg !57
69  br i1 %cmp, label %while.body, label %while.cond2.preheader, !dbg !59
70
71while.cond2.preheader:                            ; preds = %entry
72  %cmp313 = icmp slt i32 %x, 0, !dbg !60
73  br i1 %cmp313, label %while.body4, label %if.end, !dbg !63
74
75while.body:                                       ; preds = %while.body, %entry
76  %x.addr.016 = phi i32 [ %sub, %while.body ], [ %x, %entry ]
77  %tmp = load volatile i32, i32* @factor, align 4, !dbg !64
78  %call = tail call i32 @_Z3fibi(i32 %tmp), !dbg !67
79  %sub = sub nsw i32 %x.addr.016, %call, !dbg !68
80  %cmp1 = icmp sgt i32 %sub, 0, !dbg !69
81  br i1 %cmp1, label %while.body, label %if.end, !dbg !71
82
83while.body4:                                      ; preds = %while.body4, %while.cond2.preheader
84  %x.addr.114 = phi i32 [ %add, %while.body4 ], [ %x, %while.cond2.preheader ]
85  %tmp1 = load volatile i32, i32* @factor, align 4, !dbg !72
86  %call5 = tail call i32 @_Z3fibi(i32 %tmp1), !dbg !74
87  %add = add nsw i32 %call5, %x.addr.114, !dbg !75
88  %cmp3 = icmp slt i32 %add, 0, !dbg !60
89  br i1 %cmp3, label %while.body4, label %if.end, !dbg !63
90
91if.end:                                           ; preds = %while.body4, %while.body, %while.cond2.preheader
92  %x.addr.2 = phi i32 [ 0, %while.cond2.preheader ], [ %sub, %while.body ], [ %add, %while.body4 ]
93  ret i32 %x.addr.2, !dbg !76
94}
95
96define dso_local i32 @_Z5funcBi(i32 %x) local_unnamed_addr #0 !dbg !47 {
97; _Z5funcBi is marked noinline, so outline remainder has promoted context profile
98; INLINE-ALL: @_Z5funcBi{{.*}}!prof ![[FUNCB_PROF:[0-9]+]]
99; INLINE-HOT: @_Z5funcBi{{.*}}!prof ![[FUNCB_PROF:[0-9]+]]
100entry:
101  %sub = add nsw i32 %x, -100000, !dbg !51
102  %call = tail call i32 @_Z8funcLeafi(i32 %sub), !dbg !52
103; _Z5funcBi is not inlined into main, so we main->_Z5funcBi->_Z8funcLeafi
104; should be inlined based on promoted context profile
105; INLINE-ALL-NOT: call i32 @_Z8funcLeafi
106; INLINE-HOT-NOT: call i32 @_Z8funcLeafi
107  ret i32 %call, !dbg !53
108}
109
110; INLINE-ALL-DAG: [[MAIN_PROF]] = !{!"function_entry_count", i64 13}
111; INLINE-ALL-DAG: [[FUNCA_PROF]] = !{!"function_entry_count", i64 0}
112; INLINE-ALL-DAG-SAME: [[LEAF_PROF]] = !{!"function_entry_count", i64 0}
113; INLINE-ALL-DAG: [[FUNCB_PROF]] = !{!"function_entry_count", i64 33}
114
115; INLINE-HOT-DAG: [[MAIN_PROF]] = !{!"function_entry_count", i64 13}
116; INLINE-HOT-DAG: [[FUNCA_PROF]] = !{!"function_entry_count", i64 12}
117; INLINE-HOT-DAG-SAME: [[LEAF_PROF]] = !{!"function_entry_count", i64 0}
118; INLINE-HOT-DAG: [[FUNCB_PROF]] = !{!"function_entry_count", i64 33}
119
120declare i32 @_Z3fibi(i32)
121
122attributes #0 = { nofree noinline norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" "use-sample-profile" }
123attributes #1 = { nofree norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" "use-sample-profile" }
124
125!llvm.dbg.cu = !{!2}
126!llvm.module.flags = !{!14, !15, !16}
127!llvm.ident = !{!17}
128
129!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
130!1 = distinct !DIGlobalVariable(name: "factor", scope: !2, file: !3, line: 21, type: !13, isLocal: false, isDefinition: true)
131!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !5, globals: !12, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None)
132!3 = !DIFile(filename: "merged.cpp", directory: "/local/autofdo")
133!4 = !{}
134!5 = !{!6, !10, !11}
135!6 = !DISubprogram(name: "funcA", linkageName: "_Z5funcAi", scope: !3, file: !3, line: 6, type: !7, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
136!7 = !DISubroutineType(types: !8)
137!8 = !{!9, !9}
138!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
139!10 = !DISubprogram(name: "funcB", linkageName: "_Z5funcBi", scope: !3, file: !3, line: 7, type: !7, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
140!11 = !DISubprogram(name: "funcLeaf", linkageName: "_Z8funcLeafi", scope: !3, file: !3, line: 22, type: !7, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
141!12 = !{!0}
142!13 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !9)
143!14 = !{i32 7, !"Dwarf Version", i32 4}
144!15 = !{i32 2, !"Debug Info Version", i32 3}
145!16 = !{i32 1, !"wchar_size", i32 4}
146!17 = !{!"clang version 11.0.0"}
147!18 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 11, type: !19, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !21)
148!19 = !DISubroutineType(types: !20)
149!20 = !{!9}
150!21 = !{!22, !23}
151!22 = !DILocalVariable(name: "r", scope: !18, file: !3, line: 12, type: !9)
152!23 = !DILocalVariable(name: "x", scope: !24, file: !3, line: 13, type: !9)
153!24 = distinct !DILexicalBlock(scope: !18, file: !3, line: 13, column: 3)
154!25 = !DILocation(line: 13, column: 3, scope: !26)
155!26 = !DILexicalBlockFile(scope: !24, file: !3, discriminator: 2)
156!27 = !DILocation(line: 17, column: 3, scope: !18)
157!28 = !DILocation(line: 14, column: 10, scope: !29)
158!29 = distinct !DILexicalBlock(scope: !30, file: !3, line: 13, column: 37)
159!30 = distinct !DILexicalBlock(scope: !24, file: !3, line: 13, column: 3)
160!31 = !DILocation(line: 14, column: 29, scope: !29)
161!32 = !DILocation(line: 14, column: 21, scope: !33)
162!33 = !DILexicalBlockFile(scope: !29, file: !3, discriminator: 2)
163!34 = !DILocation(line: 14, column: 19, scope: !29)
164!35 = !DILocation(line: 14, column: 7, scope: !29)
165!36 = !DILocation(line: 13, column: 33, scope: !37)
166!37 = !DILexicalBlockFile(scope: !30, file: !3, discriminator: 6)
167!38 = !DILocation(line: 13, column: 26, scope: !39)
168!39 = !DILexicalBlockFile(scope: !30, file: !3, discriminator: 2)
169!40 = distinct !DISubprogram(name: "funcA", linkageName: "_Z5funcAi", scope: !3, file: !3, line: 26, type: !7, scopeLine: 26, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2)
170!44 = !DILocation(line: 27, column: 22, scope: !40)
171!45 = !DILocation(line: 27, column: 11, scope: !40)
172!46 = !DILocation(line: 29, column: 3, scope: !40)
173!47 = distinct !DISubprogram(name: "funcB", linkageName: "_Z5funcBi", scope: !3, file: !3, line: 32, type: !7, scopeLine: 32, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2)
174!51 = !DILocation(line: 33, column: 22, scope: !47)
175!52 = !DILocation(line: 33, column: 11, scope: !47)
176!53 = !DILocation(line: 35, column: 3, scope: !47)
177!54 = distinct !DISubprogram(name: "funcLeaf", linkageName: "_Z8funcLeafi", scope: !3, file: !3, line: 48, type: !7, scopeLine: 48, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2)
178!57 = !DILocation(line: 49, column: 9, scope: !58)
179!58 = distinct !DILexicalBlock(scope: !54, file: !3, line: 49, column: 7)
180!59 = !DILocation(line: 49, column: 7, scope: !54)
181!60 = !DILocation(line: 58, column: 14, scope: !61)
182!61 = !DILexicalBlockFile(scope: !62, file: !3, discriminator: 2)
183!62 = distinct !DILexicalBlock(scope: !58, file: !3, line: 56, column: 8)
184!63 = !DILocation(line: 58, column: 5, scope: !61)
185!64 = !DILocation(line: 52, column: 16, scope: !65)
186!65 = distinct !DILexicalBlock(scope: !66, file: !3, line: 51, column: 19)
187!66 = distinct !DILexicalBlock(scope: !58, file: !3, line: 49, column: 14)
188!67 = !DILocation(line: 52, column: 12, scope: !65)
189!68 = !DILocation(line: 52, column: 9, scope: !65)
190!69 = !DILocation(line: 51, column: 14, scope: !70)
191!70 = !DILexicalBlockFile(scope: !66, file: !3, discriminator: 2)
192!71 = !DILocation(line: 51, column: 5, scope: !70)
193!72 = !DILocation(line: 59, column: 16, scope: !73)
194!73 = distinct !DILexicalBlock(scope: !62, file: !3, line: 58, column: 19)
195!74 = !DILocation(line: 59, column: 12, scope: !73)
196!75 = !DILocation(line: 59, column: 9, scope: !73)
197!76 = !DILocation(line: 63, column: 3, scope: !54)
198