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 @@ -1603,10 +1603,22 @@ ? TotalSamples - CalleeProfile.getTotalSamples() : 0; TotalSamples += CalleeProfile.getHeadSamplesEstimate(); + + } + } + + // FlattenNestedProfile adds new FunctionSamples to the output, which may + // invalidate the reference Profile, so it needs to be reset after all + // flattening. + for (const auto &I : FS.getCallsiteSamples()) { + for (const auto &Callee : I.second) { + const auto &CalleeProfile = Callee.second; // Recursively convert callee profile. flattenNestedProfile(OutputProfiles, CalleeProfile); } } + Profile = OutputProfiles[Context]; + Profile.addTotalSamples(TotalSamples); Profile.setHeadSamples(Profile.getHeadSamplesEstimate());