Skip to content

Commit

Permalink
[AutoFDO]Merge called target in body samples when flattening profiles
Browse files Browse the repository at this point in the history
- Body samples could have call targets, merge them as well.

Differential Revision: https://reviews.llvm.org/D147297
  • Loading branch information
minglotus-6 committed Mar 31, 2023
1 parent 74dee47 commit f91b0f2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 8 additions & 4 deletions llvm/include/llvm/ProfileData/SampleProf.h
Expand Up @@ -777,6 +777,11 @@ class FunctionSamples {
FName, Num, Weight);
}

sampleprof_error addSampleRecord(LineLocation Location,
const SampleRecord &SampleRecord, uint64_t Weight = 1) {
return BodySamples[Location].merge(SampleRecord, Weight);
}

// Remove a call target and decrease the body sample correspondingly. Return
// the number of body samples actually decreased.
uint64_t removeCalledTargetAndBodySample(uint32_t LineOffset,
Expand Down Expand Up @@ -1342,7 +1347,7 @@ class ProfileConverter {
// To retain the context, checksum, attributes of the original profile, make
// a copy of it if no profile is found.
SampleContext &Context = FS.getContext();
auto Ret = OutputProfiles.emplace(Context, FS);
auto Ret = OutputProfiles.try_emplace(Context, FS);
FunctionSamples &Profile = Ret.first->second;
if (Ret.second) {
// When it's the copy of the old profile, just clear all the inlinees'
Expand All @@ -1351,9 +1356,8 @@ class ProfileConverter {
// We recompute TotalSamples later, so here set to zero.
Profile.setTotalSamples(0);
} else {
for (const auto &Line : FS.getBodySamples()) {
Profile.addBodySamples(Line.first.LineOffset, Line.first.Discriminator,
Line.second.getSamples());
for (const auto &[LineLocation, SampleRecord] : FS.getBodySamples()) {
Profile.addSampleRecord(LineLocation, SampleRecord);
}
}

Expand Down
@@ -1,6 +1,8 @@
baz:160:10
1: 10
3: 20
4: 21 qux:5 quux:6 corge:10
4.1: 12 quux:3 grault:4 thud:5
5: foo:30
1: 20
3: bar:10
Expand All @@ -25,6 +27,7 @@ main:110:1
!CFGChecksum: 3
!Attributes: 3
10: baz:20
4: 15 qux:3 quux:7 corge:5
10: 1
6: bar:3
1: 2
Expand Down
6 changes: 4 additions & 2 deletions llvm/test/tools/llvm-profdata/sample-flatten-profile.test
Expand Up @@ -7,6 +7,8 @@
; CHECK:baz:169:10
; CHECK-NEXT: 1: 10
; CHECK-NEXT: 3: 20
; CHECK-NEXT: 4: 36 corge:15 quux:13 qux:8
; CHECK-NEXT: 4.1: 12 thud:5 grault:4 quux:3
; CHECK-NEXT: 5: 20 foo:20
; CHECK-NEXT: 6: 2 bar:2
; CHECK-NEXT: 10: 1
Expand All @@ -18,12 +20,12 @@
; CHECK-NEXT: 4: 1
; CHECK-NEXT: !CFGChecksum: 3
; CHECK-NEXT: !Attributes: 3
; CHECK-NEXT:main:91:1
; CHECK-NEXT:main:104:1
; CHECK-NEXT: 4: 1
; CHECK-NEXT: 4.2: 1
; CHECK-NEXT: 7: 1
; CHECK-NEXT: 9: 3 bar:2 foo:1
; CHECK-NEXT: 10: 3 baz:2 foo:1
; CHECK-NEXT: 10: 16 baz:15 foo:1
; CHECK-NEXT: !CFGChecksum: 2
; CHECK-NEXT: !Attributes: 2
; CHECK-NEXT:bar:15:14
Expand Down

0 comments on commit f91b0f2

Please sign in to comment.