Skip to content

Commit 6c3c90b

Browse files
authored
[CSSPGO]Add a flag to limit unsymbolized context depth (llvm#121531)
Adding a new flag(`--csprof-max-unsymbolized-context-depth`) to only limit unsymbolized context depth. Currently,`--csprof-max-context-depth` applies to both symbolized and unsymbolized profile context, there are scenarios where `--csprof-max-context-depth` may not be flexible enough, e.g. if we want to limit the context but still keep all the inlinings from the leaf frame, we could set the value csprof-max-unsymbolized-context-depth >= 1.
1 parent e2c1b1f commit 6c3c90b

File tree

3 files changed

+143
-21
lines changed

3 files changed

+143
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
; Test --csprof-max-context-depth and --csprof-max-unsymbolized-context-depth
2+
3+
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --compress-recursion=0 --profile-summary-hot-count=0 --csprof-max-context-depth=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
4+
; RUN: FileCheck %s --input-file %t -check-prefix=CHECK-MAX-CTX-DEPTH
5+
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --compress-recursion=0 --profile-summary-hot-count=0 --csprof-max-unsymbolized-context-depth=2 --csspgo-preinliner=0 --gen-cs-nested-profile=0 --skip-symbolization
6+
; RUN: FileCheck %s --input-file %t -check-prefix=CHECK-MAX-UNSYM-CTX-DEPTH
7+
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --compress-recursion=0 --profile-summary-hot-count=0 --csprof-max-unsymbolized-context-depth=2 --csspgo-preinliner=0 --gen-cs-nested-profile=0
8+
; RUN: FileCheck %s --input-file %t -check-prefix=CHECK-MAX-UNSYM-CTX-DEPTH-PROF
9+
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --compress-recursion=0 --profile-summary-hot-count=0 --csprof-max-unsymbolized-context-depth=2 --csprof-max-context-depth=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
10+
; RUN: FileCheck %s --input-file %t -check-prefix=CHECK-MAX-CTX-DEPTH
11+
12+
13+
; CHECK-MAX-CTX-DEPTH: [fb]:19:6
14+
; CHECK-MAX-CTX-DEPTH: 1: 6
15+
; CHECK-MAX-CTX-DEPTH: 2: 3
16+
; CHECK-MAX-CTX-DEPTH: 3: 3
17+
; CHECK-MAX-CTX-DEPTH: 4: 0
18+
; CHECK-MAX-CTX-DEPTH: 5: 4 fb:4
19+
; CHECK-MAX-CTX-DEPTH: 6: 3 fa:3
20+
; CHECK-MAX-CTX-DEPTH: !CFGChecksum: 563022570642068
21+
; CHECK-MAX-CTX-DEPTH: [fa]:14:4
22+
; CHECK-MAX-CTX-DEPTH: 1: 4
23+
; CHECK-MAX-CTX-DEPTH: 3: 4
24+
; CHECK-MAX-CTX-DEPTH: 4: 2
25+
; CHECK-MAX-CTX-DEPTH: 5: 1
26+
; CHECK-MAX-CTX-DEPTH: 6: 0
27+
; CHECK-MAX-CTX-DEPTH: 7: 2 fb:2
28+
; CHECK-MAX-CTX-DEPTH: 8: 1 fa:1
29+
; CHECK-MAX-CTX-DEPTH: !CFGChecksum: 563070469352221
30+
31+
32+
; CHECK-MAX-UNSYM-CTX-DEPTH: [0x7ab @ 0x7ab]
33+
; CHECK-MAX-UNSYM-CTX-DEPTH: 3
34+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7a0-7a7:1
35+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7a0-7ab:3
36+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7b2-7b5:1
37+
; CHECK-MAX-UNSYM-CTX-DEPTH: 3
38+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7a7->7b2:1
39+
; CH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->7b2:1
66+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7b5->7c0:1
67+
; CHECK-MAX-UNSYM-CTX-DEPTH: [0x7e0 @ 0x7b5]
68+
; CHECK-MAX-UNSYM-CTX-DEPTH: 2
69+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7c0-7cd:2
70+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7db-7e0:1
71+
; CHECK-MAX-UNSYM-CTX-DEPTH: 2
72+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7cd->7db:2
73+
; CHECK-MAX-UNSYM-CTX-DEPTH: 7e0->7a0:1
74+
75+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fb:5 @ fb:5 @ fb]:13:4
76+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 4
77+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 2: 3
78+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 1
79+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 0
80+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 4 fb:4
81+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 1 fa:1
82+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563022570642068
83+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fa:7 @ fb:6 @ fa]:6:2
84+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 2
85+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 2
86+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 1
87+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 0
88+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 0
89+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 7: 1 fb:1
90+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 8: 0
91+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563070469352221
92+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fb:5 @ fb:6 @ fa]:4:1
93+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 1
94+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 1
95+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 0
96+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 1
97+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 0
98+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 7: 0
99+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 8: 1 fa:1
100+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563070469352221
101+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fb:6 @ fa:8 @ fa]:4:1
102+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 1
103+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 1
104+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 1
105+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 0
106+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 0
107+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 7: 1 fb:1
108+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 8: 0
109+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563070469352221
110+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fa:8 @ fa:7 @ fb]:3:1
111+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 1
112+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 2: 0
113+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 1
114+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 0
115+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 0
116+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 1 fa:1
117+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563022570642068
118+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: [fb:6 @ fa:7 @ fb]:3:1
119+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 1: 1
120+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 2: 0
121+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 3: 1
122+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 4: 0
123+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 5: 0
124+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: 6: 1 fa:1
125+
; CHECK-MAX-UNSYM-CTX-DEPTH-PROF: !CFGChecksum: 563022570642068

llvm/test/tools/llvm-profgen/recursion-compression-pseudoprobe.test

-20
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-UNWINDER
1010
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe-nommap.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --profile-summary-hot-count=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
1111
; RUN: FileCheck %s --input-file %t
12-
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --compress-recursion=0 --profile-summary-hot-count=0 --csprof-max-context-depth=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
13-
; RUN: FileCheck %s --input-file %t -check-prefix=CHECK-MAX-CTX-DEPTH
14-
1512

1613
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa]:4:1
1714
; CHECK-UNCOMPRESS: 1: 1
@@ -68,23 +65,6 @@
6865
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:1:0
6966
; CHECK-UNCOMPRESS: 5: 1 fb:1
7067
; CHECK-UNCOMPRESS: !CFGChecksum: 563022570642068
71-
; CHECK-MAX-CTX-DEPTH: [fb]:19:6
72-
; CHECK-MAX-CTX-DEPTH: 1: 6
73-
; CHECK-MAX-CTX-DEPTH: 2: 3
74-
; CHECK-MAX-CTX-DEPTH: 3: 3
75-
; CHECK-MAX-CTX-DEPTH: 4: 0
76-
; CHECK-MAX-CTX-DEPTH: 5: 4 fb:4
77-
; CHECK-MAX-CTX-DEPTH: 6: 3 fa:3
78-
; CHECK-MAX-CTX-DEPTH: !CFGChecksum: 563022570642068
79-
; CHECK-MAX-CTX-DEPTH: [fa]:14:4
80-
; CHECK-MAX-CTX-DEPTH: 1: 4
81-
; CHECK-MAX-CTX-DEPTH: 3: 4
82-
; CHECK-MAX-CTX-DEPTH: 4: 2
83-
; CHECK-MAX-CTX-DEPTH: 5: 1
84-
; CHECK-MAX-CTX-DEPTH: 6: 0
85-
; CHECK-MAX-CTX-DEPTH: 7: 2 fb:2
86-
; CHECK-MAX-CTX-DEPTH: 8: 1 fa:1
87-
; CHECK-MAX-CTX-DEPTH: !CFGChecksum: 563070469352221
8868

8969

9070
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb]:13:4

llvm/tools/llvm-profgen/PerfReader.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static cl::opt<bool>
4242
cl::opt<bool> ShowDetailedWarning("show-detailed-warning",
4343
cl::desc("Show detailed warning message."));
4444

45+
static cl::opt<int> CSProfMaxUnsymbolizedCtxDepth(
46+
"csprof-max-unsymbolized-context-depth", cl::init(-1),
47+
cl::desc("Keep the last K contexts while merging unsymbolized profile. -1 "
48+
"means no depth limit."));
49+
4550
extern cl::opt<std::string> PerfTraceFilename;
4651
extern cl::opt<bool> ShowDisassemblyOnly;
4752
extern cl::opt<bool> ShowSourceLocations;
@@ -172,7 +177,19 @@ std::shared_ptr<AddrBasedCtxKey> AddressStack::getContextKey() {
172177
std::shared_ptr<AddrBasedCtxKey> KeyStr = std::make_shared<AddrBasedCtxKey>();
173178
KeyStr->Context = Stack;
174179
CSProfileGenerator::compressRecursionContext<uint64_t>(KeyStr->Context);
175-
CSProfileGenerator::trimContext<uint64_t>(KeyStr->Context);
180+
// MaxContextDepth(--csprof-max-context-depth) is used to trim both symbolized
181+
// and unsymbolized profile context. Sometimes we want to at least preserve
182+
// the inlinings for the leaf frame(the profiled binary inlining),
183+
// --csprof-max-context-depth may not be flexible enough, in this case,
184+
// --csprof-max-unsymbolized-context-depth is used to limit the context for
185+
// unsymbolized profile. If both are set, use the minimum of them.
186+
int Depth = CSProfileGenerator::MaxContextDepth != -1
187+
? CSProfileGenerator::MaxContextDepth
188+
: KeyStr->Context.size();
189+
Depth = CSProfMaxUnsymbolizedCtxDepth != -1
190+
? std::min(static_cast<int>(CSProfMaxUnsymbolizedCtxDepth), Depth)
191+
: Depth;
192+
CSProfileGenerator::trimContext<uint64_t>(KeyStr->Context, Depth);
176193
return KeyStr;
177194
}
178195

0 commit comments

Comments
 (0)