Skip to content

Commit beb80ff

Browse files
wlei-llvmtstellar
authored andcommitted
[CSSPGO][llvm-profgen] Add brackets for context id to support extended binary format
To align with https://reviews.llvm.org/D95547, we need to add brackets for context id before initializing the `SampleContext`. Also added test cases for extended binary format from llvm-profgen side. Differential Revision: https://reviews.llvm.org/D95929
1 parent 989b5c9 commit beb80ff

File tree

9 files changed

+104
-76
lines changed

9 files changed

+104
-76
lines changed

llvm/lib/ProfileData/SampleProfWriter.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ std::error_code SampleProfileWriterCompactBinary::write(
360360
/// it needs to be parsed by the SampleProfileReaderText class.
361361
std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
362362
auto &OS = *OutputStream;
363-
if (FunctionSamples::ProfileIsCS)
364-
OS << "[" << S.getNameWithContext() << "]:" << S.getTotalSamples();
365-
else
366-
OS << S.getName() << ":" << S.getTotalSamples();
363+
OS << S.getNameWithContext(true) << ":" << S.getTotalSamples();
367364
if (Indent == 0)
368365
OS << ":" << S.getHeadSamples();
369366
OS << "\n";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; test for dwarf-based cs profile
2+
; RUN: llvm-profgen --format=extbinary --perfscript=%S/Inputs/recursion-compression-noprobe.perfscript --binary=%S/Inputs/recursion-compression-noprobe.perfbin --output=%t1 --csprof-cold-thres=0
3+
; RUN: llvm-profdata merge --sample --text --output=%t2 %t1
4+
; RUN: FileCheck %S/recursion-compression-noprobe.test --input-file %t2
5+
; RUN: llvm-profdata merge --sample --extbinary --output=%t3 %t2 && llvm-profdata merge --sample --text --output=%t4 %t3
6+
; RUN: diff -b %t2 %t4
7+
8+
9+
; test for probe-based cs profile
10+
; RUN: llvm-profgen --format=extbinary --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t5 --csprof-cold-thres=0
11+
; RUN: llvm-profdata merge --sample --text --output=%t6 %t5
12+
; RUN: FileCheck %S/recursion-compression-pseudoprobe.test --input-file %t6
13+
; RUN: llvm-profdata merge --sample --extbinary --output=%t7 %t6 && llvm-profdata merge --sample --text --output=%t8 %t7
14+
; RUN: diff -b %t6 %t8

llvm/test/tools/llvm-profgen/inline-cs-noprobe.test

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
; RUN: FileCheck %s --input-file %t
33

44
; CHECK:[main:1 @ foo]:44:0
5-
; CHECK: 2.2: 14
5+
; CHECK: 2.1: 14
66
; 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
7+
; CHECK: 3.1: 14 bar:14
8+
; CHECK: 3.2: 1
9+
; CHECK:[main:1 @ foo:3.1 @ bar]:14:0
1010
; CHECK: 1: 14
1111

1212
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Range Counter:
@@ -15,10 +15,9 @@
1515
; CHECK-UNWINDER: (67e, 69b): 1
1616
; CHECK-UNWINDER: (67e, 6ad): 13
1717
; CHECK-UNWINDER: (6bd, 6c8): 14
18-
; CHECK-UNWINDER: main:1 @ foo:3.2 @ bar
18+
; CHECK-UNWINDER: main:1 @ foo:3.1 @ bar
1919
; CHECK-UNWINDER: (6af, 6bb): 14
2020

21-
2221
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Branch Counter:
2322
; CHECK-UNWINDER: main:1 @ foo
2423
; CHECK-UNWINDER: (69b, 670): 1

llvm/test/tools/llvm-profgen/noinline-cs-noprobe.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737

3838

39+
40+
3941
; original code:
4042
; clang -O0 -g test.c -o a.out
4143
#include <stdio.h>

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb]:12:0
1111
; CHECK-UNCOMPRESS: 1: 11
1212
; CHECK-UNCOMPRESS: 2: 1 fa:1
13-
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa]:3:0
14-
; CHECK-UNCOMPRESS: 1: 1
15-
; CHECK-UNCOMPRESS: 2: 2 fb:1
1613
; CHECK-UNCOMPRESS:[main:1 @ foo]:3:0
1714
; CHECK-UNCOMPRESS: 2: 1
1815
; CHECK-UNCOMPRESS: 3: 2 fa:1
19-
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb:2 @ fa]:1:0
20-
; CHECK-UNCOMPRESS: 4: 1
16+
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa]:3:0
17+
; CHECK-UNCOMPRESS: 1: 1
18+
; CHECK-UNCOMPRESS: 2: 2 fb:1
2119
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb]:1:0
2220
; CHECK-UNCOMPRESS: 2: 1 fa:1
21+
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb:2 @ fa]:1:0
22+
; CHECK-UNCOMPRESS: 4: 1
23+
2324

2425
; CHECK: [main:1 @ foo:3 @ fa]:14:0
2526
; CHECK: 1: 1

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,25 @@
44
; RUN: llvm-profgen --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --show-unwinder-output --csprof-cold-thres=0 | FileCheck %s --check-prefix=CHECK-UNWINDER
55
; RUN: FileCheck %s --input-file %t
66

7-
; 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:8 @ fa:7 @ fb:6 @ fa]:4:1
7+
; 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
88
; CHECK-UNCOMPRESS: 1: 1
99
; CHECK-UNCOMPRESS: 3: 1
10-
; CHECK-UNCOMPRESS: 4: 1
11-
; CHECK-UNCOMPRESS: 7: 1 fb:1
10+
; CHECK-UNCOMPRESS: 5: 1
11+
; CHECK-UNCOMPRESS: 8: 1 fa:1
1212
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
1313
; 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:8 @ fa]:4:1
1414
; CHECK-UNCOMPRESS: 1: 1
1515
; CHECK-UNCOMPRESS: 3: 1
1616
; CHECK-UNCOMPRESS: 4: 1
1717
; CHECK-UNCOMPRESS: 7: 1 fb:1
1818
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
19-
; 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
19+
; 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:8 @ fa:7 @ fb:6 @ fa]:4:1
2020
; CHECK-UNCOMPRESS: 1: 1
2121
; CHECK-UNCOMPRESS: 3: 1
22-
; CHECK-UNCOMPRESS: 5: 1
23-
; CHECK-UNCOMPRESS: 8: 1 fa:1
22+
; CHECK-UNCOMPRESS: 4: 1
23+
; CHECK-UNCOMPRESS: 7: 1 fb:1
2424
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
25-
; 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:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
26-
; CHECK-UNCOMPRESS: 1: 1
27-
; CHECK-UNCOMPRESS: 3: 1
28-
; CHECK-UNCOMPRESS: 6: 1 fa:1
29-
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
30-
; 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:8 @ fa:7 @ fb]:3:1
31-
; CHECK-UNCOMPRESS: 1: 1
32-
; CHECK-UNCOMPRESS: 3: 1
33-
; CHECK-UNCOMPRESS: 6: 1 fa:1
34-
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
35-
; 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]:3:1
36-
; CHECK-UNCOMPRESS: 1: 1
37-
; CHECK-UNCOMPRESS: 3: 1
38-
; CHECK-UNCOMPRESS: 6: 1 fa:1
39-
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
40-
; 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]:3:1
25+
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
4126
; CHECK-UNCOMPRESS: 1: 1
4227
; CHECK-UNCOMPRESS: 2: 1
4328
; CHECK-UNCOMPRESS: 5: 1 fb:1
@@ -47,11 +32,26 @@
4732
; CHECK-UNCOMPRESS: 2: 1
4833
; CHECK-UNCOMPRESS: 5: 1 fb:1
4934
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
50-
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
35+
; 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]:3:1
5136
; CHECK-UNCOMPRESS: 1: 1
5237
; CHECK-UNCOMPRESS: 2: 1
5338
; CHECK-UNCOMPRESS: 5: 1 fb:1
5439
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
40+
; 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]:3:1
41+
; CHECK-UNCOMPRESS: 1: 1
42+
; CHECK-UNCOMPRESS: 3: 1
43+
; CHECK-UNCOMPRESS: 6: 1 fa:1
44+
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
45+
; 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:8 @ fa:7 @ fb]:3:1
46+
; CHECK-UNCOMPRESS: 1: 1
47+
; CHECK-UNCOMPRESS: 3: 1
48+
; CHECK-UNCOMPRESS: 6: 1 fa:1
49+
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
50+
; 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:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
51+
; CHECK-UNCOMPRESS: 1: 1
52+
; CHECK-UNCOMPRESS: 3: 1
53+
; CHECK-UNCOMPRESS: 6: 1 fa:1
54+
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
5555
; 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:8 @ fa:7 @ fb:6 @ fa:7 @ fb:6 @ fa]:2:1
5656
; CHECK-UNCOMPRESS: 1: 1
5757
; CHECK-UNCOMPRESS: 3: 1
@@ -74,30 +74,31 @@
7474
; CHECK: 4: 1
7575
; CHECK: 7: 1 fb:1
7676
; CHECK: !CFGChecksum: 120515930909
77-
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa]:4:1
78-
; CHECK: 1: 1
79-
; CHECK: 3: 1
80-
; CHECK: 4: 1
81-
; CHECK: 7: 1 fb:1
82-
; CHECK: !CFGChecksum: 120515930909
8377
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa]:4:1
8478
; CHECK: 1: 1
8579
; CHECK: 3: 1
8680
; CHECK: 5: 1
8781
; CHECK: 8: 1 fa:1
8882
; CHECK: !CFGChecksum: 120515930909
89-
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
83+
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa]:4:1
84+
; CHECK: 1: 1
85+
; CHECK: 3: 1
86+
; CHECK: 4: 1
87+
; CHECK: 7: 1 fb:1
88+
; CHECK: !CFGChecksum: 120515930909
89+
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb]:3:1
9090
; CHECK: 1: 1
9191
; CHECK: 3: 1
9292
; CHECK: 6: 1 fa:1
9393
; CHECK: !CFGChecksum: 72617220756
94-
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb]:3:1
94+
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
9595
; CHECK: 1: 1
9696
; CHECK: 3: 1
9797
; CHECK: 6: 1 fa:1
9898
; CHECK: !CFGChecksum: 72617220756
9999

100100

101+
101102
; CHECK-UNWINDER: Binary(recursion-compression-pseudoprobe.perfbin)'s Range Counter:
102103
; CHECK-UNWINDER: main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5
103104
; CHECK-UNWINDER: (7a0, 7a7): 1

llvm/tools/llvm-profgen/ProfileGenerator.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,16 @@ ProfileGenerator::create(const BinarySampleCounterMap &BinarySampleCounters,
7676
return ProfileGenerator;
7777
}
7878

79+
void ProfileGenerator::write(std::unique_ptr<SampleProfileWriter> Writer,
80+
StringMap<FunctionSamples> &ProfileMap) {
81+
Writer->write(ProfileMap);
82+
}
83+
7984
void ProfileGenerator::write() {
8085
auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
8186
if (std::error_code EC = WriterOrErr.getError())
8287
exitWithError(EC, OutputFilename);
83-
auto Writer = std::move(WriterOrErr.get());
84-
mergeAndTrimColdProfile(ProfileMap);
85-
Writer->write(ProfileMap);
88+
write(std::move(WriterOrErr.get()), ProfileMap);
8689
}
8790

8891
void ProfileGenerator::findDisjointRanges(RangeSample &DisjointRanges,
@@ -188,7 +191,6 @@ CSProfileGenerator::getFunctionProfileForContext(StringRef ContextStr) {
188191
if (Ret.second) {
189192
SampleContext FContext(Ret.first->first(), RawContext);
190193
FunctionSamples &FProfile = Ret.first->second;
191-
FProfile.setName(FContext.getNameWithoutContext());
192194
FProfile.setContext(FContext);
193195
}
194196
return Ret.first->second;
@@ -268,16 +270,15 @@ void CSProfileGenerator::populateFunctionBoundarySamples(
268270
CalleeName, Count);
269271

270272
// Record head sample for called target(callee)
271-
// TODO: Cleanup ' @ '
272-
std::string CalleeContextId =
273-
getCallSite(LeafLoc) + " @ " + CalleeName.str();
273+
std::ostringstream OCalleeCtxStr;
274274
if (ContextId.find(" @ ") != StringRef::npos) {
275-
CalleeContextId =
276-
ContextId.rsplit(" @ ").first.str() + " @ " + CalleeContextId;
275+
OCalleeCtxStr << ContextId.rsplit(" @ ").first.str();
276+
OCalleeCtxStr << " @ ";
277277
}
278+
OCalleeCtxStr << getCallSite(LeafLoc) << " @ " << CalleeName.str();
278279

279280
FunctionSamples &CalleeProfile =
280-
getFunctionProfileForContext(CalleeContextId);
281+
getFunctionProfileForContext(OCalleeCtxStr.str());
281282
assert(Count != 0 && "Unexpected zero weight branch");
282283
CalleeProfile.addHeadSamples(Count);
283284
}
@@ -334,8 +335,8 @@ void CSProfileGenerator::populateInferredFunctionSamples() {
334335
EstimatedCallCount = 1;
335336
CallerProfile.addCalledTargetSamples(
336337
CallerLeafFrameLoc.second.LineOffset,
337-
CallerLeafFrameLoc.second.Discriminator, CalleeProfile.getName(),
338-
EstimatedCallCount);
338+
CallerLeafFrameLoc.second.Discriminator,
339+
CalleeProfile.getContext().getNameWithoutContext(), EstimatedCallCount);
339340
CallerProfile.addBodySamples(CallerLeafFrameLoc.second.LineOffset,
340341
CallerLeafFrameLoc.second.Discriminator,
341342
EstimatedCallCount);
@@ -362,8 +363,8 @@ void CSProfileGenerator::mergeAndTrimColdProfile(
362363
// Remove the code profile from ProfileMap and merge them into BaseProileMap
363364
StringMap<FunctionSamples> BaseProfileMap;
364365
for (const auto &I : ToRemoveVec) {
365-
auto Ret =
366-
BaseProfileMap.try_emplace(I.second->getName(), FunctionSamples());
366+
auto Ret = BaseProfileMap.try_emplace(
367+
I.second->getContext().getNameWithoutContext(), FunctionSamples());
367368
FunctionSamples &BaseProfile = Ret.first->second;
368369
BaseProfile.merge(*I.second);
369370
ProfileMap.erase(I.first);
@@ -378,14 +379,27 @@ void CSProfileGenerator::mergeAndTrimColdProfile(
378379
// Merge the profile if the original profile exists, otherwise just insert
379380
// as a new profile
380381
FunctionSamples &OrigProfile = getFunctionProfileForContext(I.getKey());
381-
StringRef TmpName = OrigProfile.getName();
382382
OrigProfile.merge(I.second);
383-
// Should use the name ref from ProfileMap's key to avoid name being freed
384-
// from BaseProfileMap
385-
OrigProfile.setName(TmpName);
386383
}
387384
}
388385

386+
void CSProfileGenerator::write(std::unique_ptr<SampleProfileWriter> Writer,
387+
StringMap<FunctionSamples> &ProfileMap) {
388+
mergeAndTrimColdProfile(ProfileMap);
389+
// Add bracket for context key to support different profile binary format
390+
StringMap<FunctionSamples> CxtWithBracketPMap;
391+
for (const auto &Item : ProfileMap) {
392+
std::string ContextWithBracket = "[" + Item.first().str() + "]";
393+
auto Ret = CxtWithBracketPMap.try_emplace(ContextWithBracket, Item.second);
394+
assert(Ret.second && "Must be a unique context");
395+
SampleContext FContext(Ret.first->first(), RawContext);
396+
FunctionSamples &FProfile = Ret.first->second;
397+
FProfile.setName(FContext.getNameWithContext(true));
398+
FProfile.setContext(FContext);
399+
}
400+
Writer->write(CxtWithBracketPMap);
401+
}
402+
389403
// Helper function to extract context prefix string stack
390404
// Extract context stack for reusing, leaf context stack will
391405
// be added compressed while looking up function profile
@@ -399,8 +413,7 @@ extractPrefixContextStack(SmallVectorImpl<std::string> &ContextStrStack,
399413
}
400414

401415
void PseudoProbeCSProfileGenerator::generateProfile() {
402-
// Enable CS and pseudo probe functionalities in SampleProf
403-
FunctionSamples::ProfileIsCS = true;
416+
// Enable pseudo probe functionalities in SampleProf
404417
FunctionSamples::ProfileIsProbeBased = true;
405418
for (const auto &BI : BinarySampleCounters) {
406419
ProfiledBinary *Binary = BI.first;
@@ -495,8 +508,9 @@ void PseudoProbeCSProfileGenerator::populateBodySamplesWithProbes(
495508
CallerProfile.setFunctionHash(InlinerDesc->FuncHash);
496509
CallerProfile.addBodySamples(CallerIndex, 0, Count);
497510
CallerProfile.addTotalSamples(Count);
498-
CallerProfile.addCalledTargetSamples(CallerIndex, 0,
499-
FunctionProfile.getName(), Count);
511+
CallerProfile.addCalledTargetSamples(
512+
CallerIndex, 0,
513+
FunctionProfile.getContext().getNameWithoutContext(), Count);
500514
}
501515
}
502516
}

llvm/tools/llvm-profgen/ProfileGenerator.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ class ProfileGenerator {
2828
create(const BinarySampleCounterMap &BinarySampleCounters,
2929
enum PerfScriptType SampleType);
3030
virtual void generateProfile() = 0;
31-
// Merge and trim profile with cold context before serialization,
32-
// only eligible for CS profile
33-
virtual void
34-
mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap){};
3531
// Use SampleProfileWriter to serialize profile map
32+
virtual void write(std::unique_ptr<SampleProfileWriter> Writer,
33+
StringMap<FunctionSamples> &ProfileMap);
3634
void write();
3735

3836
protected:
@@ -68,8 +66,6 @@ class CSProfileGenerator : public ProfileGenerator {
6866

6967
public:
7068
void generateProfile() override {
71-
// Enable context-sensitive functionalities in SampleProf
72-
FunctionSamples::ProfileIsCS = true;
7369
for (const auto &BI : BinarySampleCounters) {
7470
ProfiledBinary *Binary = BI.first;
7571
for (const auto &CI : BI.second) {
@@ -205,7 +201,9 @@ class CSProfileGenerator : public ProfileGenerator {
205201
FunctionSamples &getFunctionProfileForContext(StringRef ContextId);
206202
// Merge cold context profile whose total sample is below threshold
207203
// into base profile.
208-
void mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap) override;
204+
void mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap);
205+
void write(std::unique_ptr<SampleProfileWriter> Writer,
206+
StringMap<FunctionSamples> &ProfileMap) override;
209207

210208
private:
211209
// Helper function for updating body sample for a leaf location in

llvm/tools/llvm-profgen/ProfiledBinary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ProfileGenerator.h"
1212
#include "llvm/ADT/Triple.h"
1313
#include "llvm/Demangle/Demangle.h"
14+
#include "llvm/IR/DebugInfoMetadata.h"
1415
#include "llvm/Support/CommandLine.h"
1516
#include "llvm/Support/Format.h"
1617
#include "llvm/Support/TargetRegistry.h"
@@ -393,7 +394,8 @@ FrameLocationStack ProfiledBinary::symbolize(const InstructionPointer &IP,
393394
if (UseCanonicalFnName)
394395
FunctionName = FunctionSamples::getCanonicalFnName(FunctionName);
395396
LineLocation Line(CallerFrame.Line - CallerFrame.StartLine,
396-
CallerFrame.Discriminator);
397+
DILocation::getBaseDiscriminatorFromDiscriminator(
398+
CallerFrame.Discriminator));
397399
FrameLocation Callsite(FunctionName.str(), Line);
398400
CallStack.push_back(Callsite);
399401
}

0 commit comments

Comments
 (0)