Skip to content

Commit 1f05b1a

Browse files
committed
[CSSPGO][llvm-profgen] Context-sensitive profile data generation
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC. This change supports context-sensitive profile data generation into llvm-profgen. With simultaneous sampling for LBR and call stack, we can identify leaf of LBR sample with calling context from stack sample . During the process of deriving fall through path from LBR entries, we unwind LBR by replaying all the calls and returns (including implicit calls/returns due to inlining) backwards on top of the sampled call stack. Then the state of call stack as we unwind through LBR always represents the calling context of current fall through path. we have two types of virtual unwinding 1) LBR unwinding and 2) linear range unwinding. Specifically, for each LBR entry which can be classified into call, return, regular branch, LBR unwinding will replay the operation by pushing, popping or switching leaf frame towards the call stack and since the initial call stack is most recently sampled, the replay should be in anti-execution order, i.e. for the regular case, pop the call stack when LBR is call, push frame on call stack when LBR is return. After each LBR processed, it also needs to align with the next LBR by going through instructions from previous LBR's target to current LBR's source, which we named linear unwinding. As instruction from linear range can come from different function by inlining, linear unwinding will do the range splitting and record counters through the range with same inline context. With each fall through path from LBR unwinding, we aggregate each sample into counters by the calling context and eventually generate full context sensitive profile (without relying on inlining) to driver compiler's PGO/FDO. A breakdown of noteworthy changes: - Added `HybridSample` class as the abstraction perf sample including LBR stack and call stack * Extended `PerfReader` to implement auto-detect whether input perf script output contains CS profile, then do the parsing. Multiple `HybridSample` are extracted * Speed up by aggregating `HybridSample` into `AggregatedSamples` * Added VirtualUnwinder that consumes aggregated `HybridSample` and implements unwinding of calls, returns, and linear path that contains implicit call/return from inlining. Ranges and branches counters are aggregated by the calling context.
 Here calling context is string type, each context is a pair of function name and callsite location info, the whole context is like `main:1 @ foo:2 @ bar`. * Added PorfileGenerater that accumulates counters by ranges unfolding or branch target mapping, then generates context-sensitive function profile including function body, inferring callee's head sample, callsite target samples, eventually records into ProfileMap.
 * Leveraged LLVM build-in(`SampleProfWriter`) writer to support different serialization format with no stop - `getCanonicalFnName` for callee name and name from ELF section - Added regression test for both unwinding and profile generation Test Plan: ninja & ninja check-llvm Reviewed By: hoy, wenlei, wmi Differential Revision: https://reviews.llvm.org/D89723
1 parent 3e1cb0d commit 1f05b1a

17 files changed

+1463
-45
lines changed

llvm/docs/CommandGuide/llvm-profgen.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ OPTIONS
3636
-------
3737
:program:`llvm-profgen` supports the following options:
3838

39+
.. option:: --format=[text|binary|extbinary|compbinary|gcc]
40+
41+
Specify the format of the generated profile. Supported <format> are `text`,
42+
`binary`, `extbinary`, `compbinary`, `gcc`, see `llvm-profdata` for more
43+
descriptions of the format.
44+
3945
.. option:: --show-mmap-events
4046

4147
Print mmap events.

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ struct LineLocation {
246246
return LineOffset == O.LineOffset && Discriminator == O.Discriminator;
247247
}
248248

249+
bool operator!=(const LineLocation &O) const {
250+
return LineOffset != O.LineOffset || Discriminator != O.Discriminator;
251+
}
252+
249253
uint32_t LineOffset;
250254
uint32_t Discriminator;
251255
};
@@ -585,6 +589,11 @@ class FunctionSamples {
585589
/// Return the sample count of the first instruction of the function.
586590
/// The function can be either a standalone symbol or an inlined function.
587591
uint64_t getEntrySamples() const {
592+
if (FunctionSamples::ProfileIsCS && getHeadSamples()) {
593+
// For CS profile, if we already have more accurate head samples
594+
// counted by branch sample from caller, use them as entry samples.
595+
return getHeadSamples();
596+
}
588597
uint64_t Count = 0;
589598
// Use either BodySamples or CallsiteSamples which ever has the smaller
590599
// lineno.
@@ -680,19 +689,28 @@ class FunctionSamples {
680689
/// Return the function name.
681690
StringRef getName() const { return Name; }
682691

692+
/// Return function name with context.
693+
StringRef getNameWithContext() const {
694+
return FunctionSamples::ProfileIsCS ? Context.getNameWithContext() : Name;
695+
}
696+
683697
/// Return the original function name.
684698
StringRef getFuncName() const { return getFuncName(Name); }
685699

686700
/// Return the canonical name for a function, taking into account
687701
/// suffix elision policy attributes.
688702
static StringRef getCanonicalFnName(const Function &F) {
689-
static const char *knownSuffixes[] = { ".llvm.", ".part." };
690703
auto AttrName = "sample-profile-suffix-elision-policy";
691704
auto Attr = F.getFnAttribute(AttrName).getValueAsString();
705+
return getCanonicalFnName(F.getName(), Attr);
706+
}
707+
708+
static StringRef getCanonicalFnName(StringRef FnName, StringRef Attr = "") {
709+
static const char *knownSuffixes[] = { ".llvm.", ".part." };
692710
if (Attr == "" || Attr == "all") {
693-
return F.getName().split('.').first;
711+
return FnName.split('.').first;
694712
} else if (Attr == "selected") {
695-
StringRef Cand(F.getName());
713+
StringRef Cand(FnName);
696714
for (const auto &Suf : knownSuffixes) {
697715
StringRef Suffix(Suf);
698716
auto It = Cand.rfind(Suffix);
@@ -704,11 +722,11 @@ class FunctionSamples {
704722
}
705723
return Cand;
706724
} else if (Attr == "none") {
707-
return F.getName();
725+
return FnName;
708726
} else {
709727
assert(false && "internal error: unknown suffix elision policy");
710728
}
711-
return F.getName();
729+
return FnName;
712730
}
713731

714732
/// Translate \p Name into its original name.

llvm/lib/ProfileData/SampleProfWriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ std::error_code SampleProfileWriterCompactBinary::write(
276276
/// it needs to be parsed by the SampleProfileReaderText class.
277277
std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
278278
auto &OS = *OutputStream;
279-
OS << S.getName() << ":" << S.getTotalSamples();
279+
if (FunctionSamples::ProfileIsCS)
280+
OS << "[" << S.getNameWithContext() << "]:" << S.getTotalSamples();
281+
else
282+
OS << S.getName() << ":" << S.getTotalSamples();
280283
if (Indent == 0)
281284
OS << ":" << S.getHeadSamples();
282285
OS << "\n";
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Using perf wrapper that supports hot-text. Try perf.real if you encounter any issues.
2+
PERF_RECORD_MMAP2 2854748/2854748: [0x400000(0x1000) @ 0 00:1d 123291722 526021]: r-xp /home/inline-cs-noprobe.perfbin
3+
4+
5+
40067e
6+
5541f689495641d7
7+
0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x40069b/0x400670/M/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0 0x4006c8/0x40067e/P/-/-/0
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Using perf wrapper that supports hot-text. Try perf.real if you encounter any issues.
2+
PERF_RECORD_MMAP2 2854748/2854748: [0x400000(0x1000) @ 0 00:1d 123291722 526021]: r-xp /home/noinline-cs-noprobe.perfbin
3+
4+
4005dc
5+
400634
6+
400684
7+
7f68c5788793
8+
0x4005c8/0x4005dc/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005d7/0x4005e5/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005d7/0x4005e5/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005c8/0x4005dc/P/-/-/0
9+
10+
// Test for leaf frame ending up in prolog
11+
4005b0
12+
400684
13+
7f68c5788793
14+
0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005c8/0x4005dc/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005d7/0x4005e5/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 0x400645/0x4005ff/P/-/-/0 0x400637/0x400645/P/-/-/0 0x4005e9/0x400634/P/-/-/0 0x4005d7/0x4005e5/P/-/-/0 0x40062f/0x4005b0/P/-/-/0
15+
16+
// Call stack:
17+
// 4005b0 -> start addr of bar
18+
// 400684 -> address in main
19+
// LBR Entry: | Source | Target
20+
// 0x40062f/0x4005b0/P/-/-/0 | callq -132 <bar> | start addr of bar
21+
// 0x400645/0x4005ff/P/-/-/0 | jmp -75 <foo+0xf> | movl -8(%rbp), %eax
22+
// 0x400637/0x400645/P/-/-/0 | jmp 9 <foo+0x55> | jmp -75 <foo+0xf>
23+
// 0x4005e9/0x400634/P/-/-/0 | (bar)retq | next addr of [callq -132 <bar>]
24+
// 0x4005d7/0x4005e5/P/-/-/0 | jmp 9 <bar+0x35> | movl -4(%rbp), %eax
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: llvm-profgen --perfscript=%S/Inputs/inline-cs-noprobe.perfscript --binary=%S/Inputs/inline-cs-noprobe.perfbin --output=%t --show-unwinder-output | FileCheck %s --check-prefix=CHECK-UNWINDER
2+
; RUN: FileCheck %s --input-file %t
3+
4+
; CHECK:[main:1 @ foo]:44:0
5+
; CHECK: 2.2: 14
6+
; CHECK: 3: 15
7+
; CHECK: 3.2: 14 bar:14
8+
; CHECK: 3.4: 1
9+
; CHECK:[main:1 @ foo:3.2 @ bar]:14:0
10+
; CHECK: 1: 14
11+
12+
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Range Counter:
13+
; CHECK-UNWINDER: main:1 @ foo:3.2 @ bar
14+
; CHECK-UNWINDER: (6af, 6bb): 14
15+
; CHECK-UNWINDER: main:1 @ foo
16+
; CHECK-UNWINDER: (670, 6ad): 1
17+
; CHECK-UNWINDER: (67e, 69b): 1
18+
; CHECK-UNWINDER: (67e, 6ad): 13
19+
; CHECK-UNWINDER: (6bd, 6c8): 14
20+
21+
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Branch Counter:
22+
; CHECK-UNWINDER: main:1 @ foo
23+
; CHECK-UNWINDER: (69b, 670): 1
24+
; CHECK-UNWINDER: (6c8, 67e): 15
25+
26+
; original code:
27+
; clang -O3 -g test.c -o a.out
28+
#include <stdio.h>
29+
30+
int bar(int x, int y) {
31+
if (x % 3) {
32+
return x - y;
33+
}
34+
return x + y;
35+
}
36+
37+
void foo() {
38+
int s, i = 0;
39+
while (i++ < 4000 * 4000)
40+
if (i % 91) s = bar(i, s); else s += 30;
41+
printf("sum is %d\n", s);
42+
}
43+
44+
int main() {
45+
foo();
46+
return 0;
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; RUN: llvm-profgen --perfscript=%S/Inputs/noinline-cs-noprobe.perfscript --binary=%S/Inputs/noinline-cs-noprobe.perfbin --output=%t --show-unwinder-output | FileCheck %s --check-prefix=CHECK-UNWINDER
2+
; RUN: FileCheck %s --input-file %t
3+
4+
; CHECK:[main:1 @ foo:3 @ bar]:12:3
5+
; CHECK: 0: 3
6+
; CHECK: 1: 3
7+
; CHECK: 2: 2
8+
; CHECK: 4: 1
9+
; CHECK: 5: 3
10+
; CHECK:[main:1 @ foo]:9:0
11+
; CHECK: 2: 3
12+
; CHECK: 3: 3 bar:3
13+
14+
; CHECK-UNWINDER: Binary(noinline-cs-noprobe.perfbin)'s Range Counter:
15+
; CHECK-UNWINDER: main:1 @ foo
16+
; CHECK-UNWINDER: (5ff, 62f): 3
17+
; CHECK-UNWINDER: (634, 637): 3
18+
; CHECK-UNWINDER: (645, 645): 3
19+
; CHECK-UNWINDER: main:1 @ foo:3 @ bar
20+
; CHECK-UNWINDER: (5b0, 5c8): 1
21+
; CHECK-UNWINDER: (5b0, 5d7): 2
22+
; CHECK-UNWINDER: (5dc, 5e9): 1
23+
; CHECK-UNWINDER: (5e5, 5e9): 2
24+
25+
; CHECK-UNWINDER: Binary(noinline-cs-noprobe.perfbin)'s Branch Counter:
26+
; CHECK-UNWINDER: main:1 @ foo
27+
; CHECK-UNWINDER: (62f, 5b0): 3
28+
; CHECK-UNWINDER: (637, 645): 3
29+
; CHECK-UNWINDER: (645, 5ff): 3
30+
; CHECK-UNWINDER: main:1 @ foo:3 @ bar
31+
; CHECK-UNWINDER: (5c8, 5dc): 2
32+
; CHECK-UNWINDER: (5d7, 5e5): 2
33+
; CHECK-UNWINDER: (5e9, 634): 3
34+
35+
36+
37+
38+
39+
; original code:
40+
; clang -O0 -g test.c -o a.out
41+
#include <stdio.h>
42+
43+
int bar(int x, int y) {
44+
if (x % 3) {
45+
return x - y;
46+
}
47+
return x + y;
48+
}
49+
50+
void foo() {
51+
int s, i = 0;
52+
while (i++ < 4000 * 4000)
53+
if (i % 91) s = bar(i, s); else s += 30;
54+
printf("sum is %d\n", s);
55+
}
56+
57+
int main() {
58+
foo();
59+
return 0;
60+
}

llvm/tools/llvm-profgen/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
77
MC
88
MCDisassembler
99
Object
10+
ProfileData
1011
Support
1112
Symbolize
1213
)
@@ -15,4 +16,5 @@ add_llvm_tool(llvm-profgen
1516
llvm-profgen.cpp
1617
PerfReader.cpp
1718
ProfiledBinary.cpp
19+
ProfileGenerator.cpp
1820
)

0 commit comments

Comments
 (0)