diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -777,6 +777,11 @@ 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, @@ -1342,7 +1347,7 @@ // 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' @@ -1351,9 +1356,8 @@ // 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); } } diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-flatten-profile.proftext b/llvm/test/tools/llvm-profdata/Inputs/sample-flatten-profile.proftext --- a/llvm/test/tools/llvm-profdata/Inputs/sample-flatten-profile.proftext +++ b/llvm/test/tools/llvm-profdata/Inputs/sample-flatten-profile.proftext @@ -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 @@ -25,6 +27,7 @@ !CFGChecksum: 3 !Attributes: 3 10: baz:20 + 4: 15 qux:3 quux:7 corge:5 10: 1 6: bar:3 1: 2 diff --git a/llvm/test/tools/llvm-profdata/sample-flatten-profile.test b/llvm/test/tools/llvm-profdata/sample-flatten-profile.test --- a/llvm/test/tools/llvm-profdata/sample-flatten-profile.test +++ b/llvm/test/tools/llvm-profdata/sample-flatten-profile.test @@ -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 @@ -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