xref: /freebsd-src/contrib/llvm-project/llvm/lib/ProfileData/SampleProf.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //=-- SampleProf.cpp - Sample profiling format support --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains common definitions used in the reading and writing of
100b57cec5SDimitry Andric // sample profile data.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/ProfileData/SampleProf.h"
150b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
160b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
17e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h"
18e8d8bef9SDimitry Andric #include "llvm/ProfileData/SampleProfReader.h"
19fe6060f1SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
210b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
220b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
230b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
240b57cec5SDimitry Andric #include <string>
250b57cec5SDimitry Andric #include <system_error>
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric using namespace sampleprof;
290b57cec5SDimitry Andric 
30fe6060f1SDimitry Andric static cl::opt<uint64_t> ProfileSymbolListCutOff(
3181ad6265SDimitry Andric     "profile-symbol-list-cutoff", cl::Hidden, cl::init(-1),
32fe6060f1SDimitry Andric     cl::desc("Cutoff value about how many symbols in profile symbol list "
33fe6060f1SDimitry Andric              "will be used. This is very useful for performance debugging"));
34fe6060f1SDimitry Andric 
3506c3fb27SDimitry Andric static cl::opt<bool> GenerateMergedBaseProfiles(
3681ad6265SDimitry Andric     "generate-merged-base-profiles",
370eae32dcSDimitry Andric     cl::desc("When generating nested context-sensitive profiles, always "
380eae32dcSDimitry Andric              "generate extra base profile for function with all its context "
390eae32dcSDimitry Andric              "profiles merged into it."));
400eae32dcSDimitry Andric 
410b57cec5SDimitry Andric namespace llvm {
420b57cec5SDimitry Andric namespace sampleprof {
43e8d8bef9SDimitry Andric bool FunctionSamples::ProfileIsProbeBased = false;
4481ad6265SDimitry Andric bool FunctionSamples::ProfileIsCS = false;
4581ad6265SDimitry Andric bool FunctionSamples::ProfileIsPreInlined = false;
46fe6060f1SDimitry Andric bool FunctionSamples::UseMD5 = false;
47fe6060f1SDimitry Andric bool FunctionSamples::HasUniqSuffix = true;
48fe6060f1SDimitry Andric bool FunctionSamples::ProfileIsFS = false;
490b57cec5SDimitry Andric } // namespace sampleprof
500b57cec5SDimitry Andric } // namespace llvm
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric namespace {
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
550b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
560b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
570b57cec5SDimitry Andric class SampleProfErrorCategoryType : public std::error_category {
580b57cec5SDimitry Andric   const char *name() const noexcept override { return "llvm.sampleprof"; }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   std::string message(int IE) const override {
610b57cec5SDimitry Andric     sampleprof_error E = static_cast<sampleprof_error>(IE);
620b57cec5SDimitry Andric     switch (E) {
630b57cec5SDimitry Andric     case sampleprof_error::success:
640b57cec5SDimitry Andric       return "Success";
650b57cec5SDimitry Andric     case sampleprof_error::bad_magic:
660b57cec5SDimitry Andric       return "Invalid sample profile data (bad magic)";
670b57cec5SDimitry Andric     case sampleprof_error::unsupported_version:
680b57cec5SDimitry Andric       return "Unsupported sample profile format version";
690b57cec5SDimitry Andric     case sampleprof_error::too_large:
700b57cec5SDimitry Andric       return "Too much profile data";
710b57cec5SDimitry Andric     case sampleprof_error::truncated:
720b57cec5SDimitry Andric       return "Truncated profile data";
730b57cec5SDimitry Andric     case sampleprof_error::malformed:
740b57cec5SDimitry Andric       return "Malformed sample profile data";
750b57cec5SDimitry Andric     case sampleprof_error::unrecognized_format:
760b57cec5SDimitry Andric       return "Unrecognized sample profile encoding format";
770b57cec5SDimitry Andric     case sampleprof_error::unsupported_writing_format:
780b57cec5SDimitry Andric       return "Profile encoding format unsupported for writing operations";
790b57cec5SDimitry Andric     case sampleprof_error::truncated_name_table:
800b57cec5SDimitry Andric       return "Truncated function name table";
810b57cec5SDimitry Andric     case sampleprof_error::not_implemented:
820b57cec5SDimitry Andric       return "Unimplemented feature";
830b57cec5SDimitry Andric     case sampleprof_error::counter_overflow:
840b57cec5SDimitry Andric       return "Counter overflow";
850b57cec5SDimitry Andric     case sampleprof_error::ostream_seek_unsupported:
860b57cec5SDimitry Andric       return "Ostream does not support seek";
878bcb0991SDimitry Andric     case sampleprof_error::uncompress_failed:
888bcb0991SDimitry Andric       return "Uncompress failure";
898bcb0991SDimitry Andric     case sampleprof_error::zlib_unavailable:
908bcb0991SDimitry Andric       return "Zlib is unavailable";
91e8d8bef9SDimitry Andric     case sampleprof_error::hash_mismatch:
92e8d8bef9SDimitry Andric       return "Function hash mismatch";
930b57cec5SDimitry Andric     }
940b57cec5SDimitry Andric     llvm_unreachable("A value of sampleprof_error has no message.");
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric };
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric } // end anonymous namespace
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric const std::error_category &llvm::sampleprof_category() {
101753f127fSDimitry Andric   static SampleProfErrorCategoryType ErrorCategory;
102753f127fSDimitry Andric   return ErrorCategory;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric void LineLocation::print(raw_ostream &OS) const {
1060b57cec5SDimitry Andric   OS << LineOffset;
1070b57cec5SDimitry Andric   if (Discriminator > 0)
1080b57cec5SDimitry Andric     OS << "." << Discriminator;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
1120b57cec5SDimitry Andric                                           const LineLocation &Loc) {
1130b57cec5SDimitry Andric   Loc.print(OS);
1140b57cec5SDimitry Andric   return OS;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
117fe6060f1SDimitry Andric /// Merge the samples in \p Other into this record.
118fe6060f1SDimitry Andric /// Optionally scale sample counts by \p Weight.
119fe6060f1SDimitry Andric sampleprof_error SampleRecord::merge(const SampleRecord &Other,
120fe6060f1SDimitry Andric                                      uint64_t Weight) {
121fe6060f1SDimitry Andric   sampleprof_error Result;
122fe6060f1SDimitry Andric   Result = addSamples(Other.getSamples(), Weight);
123fe6060f1SDimitry Andric   for (const auto &I : Other.getCallTargets()) {
124*0fca6ea1SDimitry Andric     mergeSampleProfErrors(Result, addCalledTarget(I.first, I.second, Weight));
125fe6060f1SDimitry Andric   }
126fe6060f1SDimitry Andric   return Result;
127fe6060f1SDimitry Andric }
128fe6060f1SDimitry Andric 
1290b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1300b57cec5SDimitry Andric LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
1310b57cec5SDimitry Andric #endif
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric /// Print the sample record to the stream \p OS indented by \p Indent.
1340b57cec5SDimitry Andric void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
1350b57cec5SDimitry Andric   OS << NumSamples;
1360b57cec5SDimitry Andric   if (hasCalls()) {
1370b57cec5SDimitry Andric     OS << ", calls:";
1388bcb0991SDimitry Andric     for (const auto &I : getSortedCallTargets())
1398bcb0991SDimitry Andric       OS << " " << I.first << ":" << I.second;
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric   OS << "\n";
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1450b57cec5SDimitry Andric LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
1460b57cec5SDimitry Andric #endif
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
1490b57cec5SDimitry Andric                                           const SampleRecord &Sample) {
1500b57cec5SDimitry Andric   Sample.print(OS, 0);
1510b57cec5SDimitry Andric   return OS;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric /// Print the samples collected for a function on stream \p OS.
1550b57cec5SDimitry Andric void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
156e8d8bef9SDimitry Andric   if (getFunctionHash())
157e8d8bef9SDimitry Andric     OS << "CFG checksum " << getFunctionHash() << "\n";
158e8d8bef9SDimitry Andric 
1590b57cec5SDimitry Andric   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
1600b57cec5SDimitry Andric      << " sampled lines\n";
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   OS.indent(Indent);
1630b57cec5SDimitry Andric   if (!BodySamples.empty()) {
1640b57cec5SDimitry Andric     OS << "Samples collected in the function's body {\n";
1650b57cec5SDimitry Andric     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
1660b57cec5SDimitry Andric     for (const auto &SI : SortedBodySamples.get()) {
1670b57cec5SDimitry Andric       OS.indent(Indent + 2);
1680b57cec5SDimitry Andric       OS << SI->first << ": " << SI->second;
1690b57cec5SDimitry Andric     }
1700b57cec5SDimitry Andric     OS.indent(Indent);
1710b57cec5SDimitry Andric     OS << "}\n";
1720b57cec5SDimitry Andric   } else {
1730b57cec5SDimitry Andric     OS << "No samples collected in the function's body\n";
1740b57cec5SDimitry Andric   }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   OS.indent(Indent);
1770b57cec5SDimitry Andric   if (!CallsiteSamples.empty()) {
1780b57cec5SDimitry Andric     OS << "Samples collected in inlined callsites {\n";
1790b57cec5SDimitry Andric     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
1800b57cec5SDimitry Andric         CallsiteSamples);
1810b57cec5SDimitry Andric     for (const auto &CS : SortedCallsiteSamples.get()) {
1820b57cec5SDimitry Andric       for (const auto &FS : CS->second) {
1830b57cec5SDimitry Andric         OS.indent(Indent + 2);
1845f757f3fSDimitry Andric         OS << CS->first << ": inlined callee: " << FS.second.getFunction()
1855f757f3fSDimitry Andric            << ": ";
1860b57cec5SDimitry Andric         FS.second.print(OS, Indent + 4);
1870b57cec5SDimitry Andric       }
1880b57cec5SDimitry Andric     }
1898bcb0991SDimitry Andric     OS.indent(Indent);
1900b57cec5SDimitry Andric     OS << "}\n";
1910b57cec5SDimitry Andric   } else {
1920b57cec5SDimitry Andric     OS << "No inlined callsites in this function\n";
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
1970b57cec5SDimitry Andric                                           const FunctionSamples &FS) {
1980b57cec5SDimitry Andric   FS.print(OS);
1990b57cec5SDimitry Andric   return OS;
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
202349cc55cSDimitry Andric void sampleprof::sortFuncProfiles(
203349cc55cSDimitry Andric     const SampleProfileMap &ProfileMap,
204349cc55cSDimitry Andric     std::vector<NameFunctionSamples> &SortedProfiles) {
205349cc55cSDimitry Andric   for (const auto &I : ProfileMap) {
2065f757f3fSDimitry Andric     SortedProfiles.push_back(std::make_pair(I.first, &I.second));
207349cc55cSDimitry Andric   }
208349cc55cSDimitry Andric   llvm::stable_sort(SortedProfiles, [](const NameFunctionSamples &A,
209349cc55cSDimitry Andric                                        const NameFunctionSamples &B) {
210349cc55cSDimitry Andric     if (A.second->getTotalSamples() == B.second->getTotalSamples())
2115f757f3fSDimitry Andric       return A.second->getContext() < B.second->getContext();
212349cc55cSDimitry Andric     return A.second->getTotalSamples() > B.second->getTotalSamples();
213349cc55cSDimitry Andric   });
214349cc55cSDimitry Andric }
215349cc55cSDimitry Andric 
2160b57cec5SDimitry Andric unsigned FunctionSamples::getOffset(const DILocation *DIL) {
2170b57cec5SDimitry Andric   return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
2180b57cec5SDimitry Andric       0xffff;
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
2210eae32dcSDimitry Andric LineLocation FunctionSamples::getCallSiteIdentifier(const DILocation *DIL,
2220eae32dcSDimitry Andric                                                     bool ProfileIsFS) {
2230eae32dcSDimitry Andric   if (FunctionSamples::ProfileIsProbeBased) {
224e8d8bef9SDimitry Andric     // In a pseudo-probe based profile, a callsite is simply represented by the
225e8d8bef9SDimitry Andric     // ID of the probe associated with the call instruction. The probe ID is
226e8d8bef9SDimitry Andric     // encoded in the Discriminator field of the call instruction's debug
227e8d8bef9SDimitry Andric     // metadata.
228e8d8bef9SDimitry Andric     return LineLocation(PseudoProbeDwarfDiscriminator::extractProbeIndex(
229e8d8bef9SDimitry Andric                             DIL->getDiscriminator()),
230e8d8bef9SDimitry Andric                         0);
2310eae32dcSDimitry Andric   } else {
2320eae32dcSDimitry Andric     unsigned Discriminator =
2330eae32dcSDimitry Andric         ProfileIsFS ? DIL->getDiscriminator() : DIL->getBaseDiscriminator();
2340eae32dcSDimitry Andric     return LineLocation(FunctionSamples::getOffset(DIL), Discriminator);
2350eae32dcSDimitry Andric   }
2360eae32dcSDimitry Andric }
2370eae32dcSDimitry Andric 
238e8d8bef9SDimitry Andric const FunctionSamples *FunctionSamples::findFunctionSamples(
239*0fca6ea1SDimitry Andric     const DILocation *DIL, SampleProfileReaderItaniumRemapper *Remapper,
240*0fca6ea1SDimitry Andric     const HashKeyMap<std::unordered_map, FunctionId, FunctionId>
241*0fca6ea1SDimitry Andric         *FuncNameToProfNameMap) const {
2420b57cec5SDimitry Andric   assert(DIL);
2430b57cec5SDimitry Andric   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   const DILocation *PrevDIL = DIL;
2460b57cec5SDimitry Andric   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
247349cc55cSDimitry Andric     // Use C++ linkage name if possible.
248349cc55cSDimitry Andric     StringRef Name = PrevDIL->getScope()->getSubprogram()->getLinkageName();
249349cc55cSDimitry Andric     if (Name.empty())
250349cc55cSDimitry Andric       Name = PrevDIL->getScope()->getSubprogram()->getName();
2510eae32dcSDimitry Andric     S.emplace_back(FunctionSamples::getCallSiteIdentifier(
2520eae32dcSDimitry Andric                        DIL, FunctionSamples::ProfileIsFS),
2530eae32dcSDimitry Andric                    Name);
2540b57cec5SDimitry Andric     PrevDIL = DIL;
2550b57cec5SDimitry Andric   }
2560eae32dcSDimitry Andric 
2570b57cec5SDimitry Andric   if (S.size() == 0)
2580b57cec5SDimitry Andric     return this;
2590b57cec5SDimitry Andric   const FunctionSamples *FS = this;
2600b57cec5SDimitry Andric   for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
261*0fca6ea1SDimitry Andric     FS = FS->findFunctionSamplesAt(S[i].first, S[i].second, Remapper,
262*0fca6ea1SDimitry Andric                                    FuncNameToProfNameMap);
2630b57cec5SDimitry Andric   }
2640b57cec5SDimitry Andric   return FS;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2675f757f3fSDimitry Andric void FunctionSamples::findAllNames(DenseSet<FunctionId> &NameSet) const {
2685f757f3fSDimitry Andric   NameSet.insert(getFunction());
269e8d8bef9SDimitry Andric   for (const auto &BS : BodySamples)
270e8d8bef9SDimitry Andric     for (const auto &TS : BS.second.getCallTargets())
2715f757f3fSDimitry Andric       NameSet.insert(TS.first);
272e8d8bef9SDimitry Andric 
273e8d8bef9SDimitry Andric   for (const auto &CS : CallsiteSamples) {
274e8d8bef9SDimitry Andric     for (const auto &NameFS : CS.second) {
275e8d8bef9SDimitry Andric       NameSet.insert(NameFS.first);
276e8d8bef9SDimitry Andric       NameFS.second.findAllNames(NameSet);
277e8d8bef9SDimitry Andric     }
278e8d8bef9SDimitry Andric   }
279e8d8bef9SDimitry Andric }
280e8d8bef9SDimitry Andric 
281e8d8bef9SDimitry Andric const FunctionSamples *FunctionSamples::findFunctionSamplesAt(
282e8d8bef9SDimitry Andric     const LineLocation &Loc, StringRef CalleeName,
283*0fca6ea1SDimitry Andric     SampleProfileReaderItaniumRemapper *Remapper,
284*0fca6ea1SDimitry Andric     const HashKeyMap<std::unordered_map, FunctionId, FunctionId>
285*0fca6ea1SDimitry Andric         *FuncNameToProfNameMap) const {
286fe6060f1SDimitry Andric   CalleeName = getCanonicalFnName(CalleeName);
287fe6060f1SDimitry Andric 
288*0fca6ea1SDimitry Andric   auto I = CallsiteSamples.find(mapIRLocToProfileLoc(Loc));
289*0fca6ea1SDimitry Andric   if (I == CallsiteSamples.end())
290e8d8bef9SDimitry Andric     return nullptr;
291*0fca6ea1SDimitry Andric   auto FS = I->second.find(getRepInFormat(CalleeName));
292*0fca6ea1SDimitry Andric   if (FS != I->second.end())
293e8d8bef9SDimitry Andric     return &FS->second;
294*0fca6ea1SDimitry Andric 
295*0fca6ea1SDimitry Andric   if (FuncNameToProfNameMap && !FuncNameToProfNameMap->empty()) {
296*0fca6ea1SDimitry Andric     auto R = FuncNameToProfNameMap->find(FunctionId(CalleeName));
297*0fca6ea1SDimitry Andric     if (R != FuncNameToProfNameMap->end()) {
298*0fca6ea1SDimitry Andric       CalleeName = R->second.stringRef();
299*0fca6ea1SDimitry Andric       auto FS = I->second.find(getRepInFormat(CalleeName));
300*0fca6ea1SDimitry Andric       if (FS != I->second.end())
301*0fca6ea1SDimitry Andric         return &FS->second;
302*0fca6ea1SDimitry Andric     }
303*0fca6ea1SDimitry Andric   }
304*0fca6ea1SDimitry Andric 
305e8d8bef9SDimitry Andric   if (Remapper) {
306e8d8bef9SDimitry Andric     if (auto NameInProfile = Remapper->lookUpNameInProfile(CalleeName)) {
307*0fca6ea1SDimitry Andric       auto FS = I->second.find(getRepInFormat(*NameInProfile));
308*0fca6ea1SDimitry Andric       if (FS != I->second.end())
309e8d8bef9SDimitry Andric         return &FS->second;
310e8d8bef9SDimitry Andric     }
311e8d8bef9SDimitry Andric   }
312e8d8bef9SDimitry Andric   // If we cannot find exact match of the callee name, return the FS with
313e8d8bef9SDimitry Andric   // the max total count. Only do this when CalleeName is not provided,
314e8d8bef9SDimitry Andric   // i.e., only for indirect calls.
315e8d8bef9SDimitry Andric   if (!CalleeName.empty())
316e8d8bef9SDimitry Andric     return nullptr;
317e8d8bef9SDimitry Andric   uint64_t MaxTotalSamples = 0;
318e8d8bef9SDimitry Andric   const FunctionSamples *R = nullptr;
319*0fca6ea1SDimitry Andric   for (const auto &NameFS : I->second)
320e8d8bef9SDimitry Andric     if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
321e8d8bef9SDimitry Andric       MaxTotalSamples = NameFS.second.getTotalSamples();
322e8d8bef9SDimitry Andric       R = &NameFS.second;
323e8d8bef9SDimitry Andric     }
324e8d8bef9SDimitry Andric   return R;
325e8d8bef9SDimitry Andric }
326e8d8bef9SDimitry Andric 
3270b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3280b57cec5SDimitry Andric LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
3290b57cec5SDimitry Andric #endif
3308bcb0991SDimitry Andric 
3318bcb0991SDimitry Andric std::error_code ProfileSymbolList::read(const uint8_t *Data,
3328bcb0991SDimitry Andric                                         uint64_t ListSize) {
3338bcb0991SDimitry Andric   const char *ListStart = reinterpret_cast<const char *>(Data);
3348bcb0991SDimitry Andric   uint64_t Size = 0;
335fe6060f1SDimitry Andric   uint64_t StrNum = 0;
336fe6060f1SDimitry Andric   while (Size < ListSize && StrNum < ProfileSymbolListCutOff) {
3378bcb0991SDimitry Andric     StringRef Str(ListStart + Size);
3388bcb0991SDimitry Andric     add(Str);
3398bcb0991SDimitry Andric     Size += Str.size() + 1;
340fe6060f1SDimitry Andric     StrNum++;
3418bcb0991SDimitry Andric   }
342fe6060f1SDimitry Andric   if (Size != ListSize && StrNum != ProfileSymbolListCutOff)
3438bcb0991SDimitry Andric     return sampleprof_error::malformed;
3448bcb0991SDimitry Andric   return sampleprof_error::success;
3458bcb0991SDimitry Andric }
3468bcb0991SDimitry Andric 
347fe6060f1SDimitry Andric void SampleContextTrimmer::trimAndMergeColdContextProfiles(
348fe6060f1SDimitry Andric     uint64_t ColdCountThreshold, bool TrimColdContext, bool MergeColdContext,
349349cc55cSDimitry Andric     uint32_t ColdContextFrameLength, bool TrimBaseProfileOnly) {
350fe6060f1SDimitry Andric   if (!TrimColdContext && !MergeColdContext)
351fe6060f1SDimitry Andric     return;
352fe6060f1SDimitry Andric 
353fe6060f1SDimitry Andric   // Nothing to merge if sample threshold is zero
354fe6060f1SDimitry Andric   if (ColdCountThreshold == 0)
355fe6060f1SDimitry Andric     return;
356fe6060f1SDimitry Andric 
357349cc55cSDimitry Andric   // Trimming base profiles only is mainly to honor the preinliner decsion. When
358349cc55cSDimitry Andric   // MergeColdContext is true preinliner decsion is not honored anyway so turn
359349cc55cSDimitry Andric   // off TrimBaseProfileOnly.
360349cc55cSDimitry Andric   if (MergeColdContext)
361349cc55cSDimitry Andric     TrimBaseProfileOnly = false;
362349cc55cSDimitry Andric 
363fe6060f1SDimitry Andric   // Filter the cold profiles from ProfileMap and move them into a tmp
364fe6060f1SDimitry Andric   // container
3655f757f3fSDimitry Andric   std::vector<std::pair<hash_code, const FunctionSamples *>> ColdProfiles;
366fe6060f1SDimitry Andric   for (const auto &I : ProfileMap) {
3675f757f3fSDimitry Andric     const SampleContext &Context = I.second.getContext();
368fe6060f1SDimitry Andric     const FunctionSamples &FunctionProfile = I.second;
369349cc55cSDimitry Andric     if (FunctionProfile.getTotalSamples() < ColdCountThreshold &&
370349cc55cSDimitry Andric         (!TrimBaseProfileOnly || Context.isBaseContext()))
3715f757f3fSDimitry Andric       ColdProfiles.emplace_back(I.first, &I.second);
372fe6060f1SDimitry Andric   }
373fe6060f1SDimitry Andric 
374fe6060f1SDimitry Andric   // Remove the cold profile from ProfileMap and merge them into
375fe6060f1SDimitry Andric   // MergedProfileMap by the last K frames of context
376349cc55cSDimitry Andric   SampleProfileMap MergedProfileMap;
377fe6060f1SDimitry Andric   for (const auto &I : ColdProfiles) {
378fe6060f1SDimitry Andric     if (MergeColdContext) {
379349cc55cSDimitry Andric       auto MergedContext = I.second->getContext().getContextFrames();
380349cc55cSDimitry Andric       if (ColdContextFrameLength < MergedContext.size())
381349cc55cSDimitry Andric         MergedContext = MergedContext.take_back(ColdContextFrameLength);
3825f757f3fSDimitry Andric       // Need to set MergedProfile's context here otherwise it will be lost.
383*0fca6ea1SDimitry Andric       FunctionSamples &MergedProfile = MergedProfileMap.create(MergedContext);
384fe6060f1SDimitry Andric       MergedProfile.merge(*I.second);
385fe6060f1SDimitry Andric     }
386fe6060f1SDimitry Andric     ProfileMap.erase(I.first);
387fe6060f1SDimitry Andric   }
388fe6060f1SDimitry Andric 
389fe6060f1SDimitry Andric   // Move the merged profiles into ProfileMap;
390fe6060f1SDimitry Andric   for (const auto &I : MergedProfileMap) {
391fe6060f1SDimitry Andric     // Filter the cold merged profile
392fe6060f1SDimitry Andric     if (TrimColdContext && I.second.getTotalSamples() < ColdCountThreshold &&
3935f757f3fSDimitry Andric         ProfileMap.find(I.second.getContext()) == ProfileMap.end())
394fe6060f1SDimitry Andric       continue;
395fe6060f1SDimitry Andric     // Merge the profile if the original profile exists, otherwise just insert
3965f757f3fSDimitry Andric     // as a new profile. If inserted as a new profile from MergedProfileMap, it
3975f757f3fSDimitry Andric     // already has the right context.
3985f757f3fSDimitry Andric     auto Ret = ProfileMap.emplace(I.second.getContext(), FunctionSamples());
399fe6060f1SDimitry Andric     FunctionSamples &OrigProfile = Ret.first->second;
400fe6060f1SDimitry Andric     OrigProfile.merge(I.second);
401fe6060f1SDimitry Andric   }
402fe6060f1SDimitry Andric }
403fe6060f1SDimitry Andric 
4048bcb0991SDimitry Andric std::error_code ProfileSymbolList::write(raw_ostream &OS) {
4058bcb0991SDimitry Andric   // Sort the symbols before output. If doing compression.
4068bcb0991SDimitry Andric   // It will make the compression much more effective.
407e8d8bef9SDimitry Andric   std::vector<StringRef> SortedList(Syms.begin(), Syms.end());
4088bcb0991SDimitry Andric   llvm::sort(SortedList);
4098bcb0991SDimitry Andric 
4108bcb0991SDimitry Andric   std::string OutputString;
4118bcb0991SDimitry Andric   for (auto &Sym : SortedList) {
4128bcb0991SDimitry Andric     OutputString.append(Sym.str());
4138bcb0991SDimitry Andric     OutputString.append(1, '\0');
4148bcb0991SDimitry Andric   }
4158bcb0991SDimitry Andric 
4168bcb0991SDimitry Andric   OS << OutputString;
4178bcb0991SDimitry Andric   return sampleprof_error::success;
4188bcb0991SDimitry Andric }
4198bcb0991SDimitry Andric 
4208bcb0991SDimitry Andric void ProfileSymbolList::dump(raw_ostream &OS) const {
4218bcb0991SDimitry Andric   OS << "======== Dump profile symbol list ========\n";
422e8d8bef9SDimitry Andric   std::vector<StringRef> SortedList(Syms.begin(), Syms.end());
4238bcb0991SDimitry Andric   llvm::sort(SortedList);
4248bcb0991SDimitry Andric 
4258bcb0991SDimitry Andric   for (auto &Sym : SortedList)
4268bcb0991SDimitry Andric     OS << Sym << "\n";
4278bcb0991SDimitry Andric }
4280eae32dcSDimitry Andric 
42906c3fb27SDimitry Andric ProfileConverter::FrameNode *
43006c3fb27SDimitry Andric ProfileConverter::FrameNode::getOrCreateChildFrame(const LineLocation &CallSite,
4315f757f3fSDimitry Andric                                                    FunctionId CalleeName) {
4320eae32dcSDimitry Andric   uint64_t Hash = FunctionSamples::getCallSiteHash(CalleeName, CallSite);
4330eae32dcSDimitry Andric   auto It = AllChildFrames.find(Hash);
4340eae32dcSDimitry Andric   if (It != AllChildFrames.end()) {
4350eae32dcSDimitry Andric     assert(It->second.FuncName == CalleeName &&
4360eae32dcSDimitry Andric            "Hash collision for child context node");
4370eae32dcSDimitry Andric     return &It->second;
4380eae32dcSDimitry Andric   }
4390eae32dcSDimitry Andric 
4400eae32dcSDimitry Andric   AllChildFrames[Hash] = FrameNode(CalleeName, nullptr, CallSite);
4410eae32dcSDimitry Andric   return &AllChildFrames[Hash];
4420eae32dcSDimitry Andric }
4430eae32dcSDimitry Andric 
44406c3fb27SDimitry Andric ProfileConverter::ProfileConverter(SampleProfileMap &Profiles)
4450eae32dcSDimitry Andric     : ProfileMap(Profiles) {
4460eae32dcSDimitry Andric   for (auto &FuncSample : Profiles) {
4470eae32dcSDimitry Andric     FunctionSamples *FSamples = &FuncSample.second;
4480eae32dcSDimitry Andric     auto *NewNode = getOrCreateContextPath(FSamples->getContext());
4490eae32dcSDimitry Andric     assert(!NewNode->FuncSamples && "New node cannot have sample profile");
4500eae32dcSDimitry Andric     NewNode->FuncSamples = FSamples;
4510eae32dcSDimitry Andric   }
4520eae32dcSDimitry Andric }
4530eae32dcSDimitry Andric 
45406c3fb27SDimitry Andric ProfileConverter::FrameNode *
45506c3fb27SDimitry Andric ProfileConverter::getOrCreateContextPath(const SampleContext &Context) {
4560eae32dcSDimitry Andric   auto Node = &RootFrame;
4570eae32dcSDimitry Andric   LineLocation CallSiteLoc(0, 0);
4580eae32dcSDimitry Andric   for (auto &Callsite : Context.getContextFrames()) {
4595f757f3fSDimitry Andric     Node = Node->getOrCreateChildFrame(CallSiteLoc, Callsite.Func);
4600eae32dcSDimitry Andric     CallSiteLoc = Callsite.Location;
4610eae32dcSDimitry Andric   }
4620eae32dcSDimitry Andric   return Node;
4630eae32dcSDimitry Andric }
4640eae32dcSDimitry Andric 
46506c3fb27SDimitry Andric void ProfileConverter::convertCSProfiles(ProfileConverter::FrameNode &Node) {
4660eae32dcSDimitry Andric   // Process each child profile. Add each child profile to callsite profile map
4670eae32dcSDimitry Andric   // of the current node `Node` if `Node` comes with a profile. Otherwise
4680eae32dcSDimitry Andric   // promote the child profile to a standalone profile.
4690eae32dcSDimitry Andric   auto *NodeProfile = Node.FuncSamples;
4700eae32dcSDimitry Andric   for (auto &It : Node.AllChildFrames) {
4710eae32dcSDimitry Andric     auto &ChildNode = It.second;
47206c3fb27SDimitry Andric     convertCSProfiles(ChildNode);
4730eae32dcSDimitry Andric     auto *ChildProfile = ChildNode.FuncSamples;
4740eae32dcSDimitry Andric     if (!ChildProfile)
4750eae32dcSDimitry Andric       continue;
4760eae32dcSDimitry Andric     SampleContext OrigChildContext = ChildProfile->getContext();
4775f757f3fSDimitry Andric     uint64_t OrigChildContextHash = OrigChildContext.getHashCode();
4780eae32dcSDimitry Andric     // Reset the child context to be contextless.
4795f757f3fSDimitry Andric     ChildProfile->getContext().setFunction(OrigChildContext.getFunction());
4800eae32dcSDimitry Andric     if (NodeProfile) {
4810eae32dcSDimitry Andric       // Add child profile to the callsite profile map.
4820eae32dcSDimitry Andric       auto &SamplesMap = NodeProfile->functionSamplesAt(ChildNode.CallSiteLoc);
4835f757f3fSDimitry Andric       SamplesMap.emplace(OrigChildContext.getFunction(), *ChildProfile);
4840eae32dcSDimitry Andric       NodeProfile->addTotalSamples(ChildProfile->getTotalSamples());
48581ad6265SDimitry Andric       // Remove the corresponding body sample for the callsite and update the
48681ad6265SDimitry Andric       // total weight.
48781ad6265SDimitry Andric       auto Count = NodeProfile->removeCalledTargetAndBodySample(
48881ad6265SDimitry Andric           ChildNode.CallSiteLoc.LineOffset, ChildNode.CallSiteLoc.Discriminator,
4895f757f3fSDimitry Andric           OrigChildContext.getFunction());
49081ad6265SDimitry Andric       NodeProfile->removeTotalSamples(Count);
4910eae32dcSDimitry Andric     }
4920eae32dcSDimitry Andric 
4935f757f3fSDimitry Andric     uint64_t NewChildProfileHash = 0;
4940eae32dcSDimitry Andric     // Separate child profile to be a standalone profile, if the current parent
4950eae32dcSDimitry Andric     // profile doesn't exist. This is a duplicating operation when the child
4960eae32dcSDimitry Andric     // profile is already incorporated into the parent which is still useful and
4970eae32dcSDimitry Andric     // thus done optionally. It is seen that duplicating context profiles into
4980eae32dcSDimitry Andric     // base profiles improves the code quality for thinlto build by allowing a
4990eae32dcSDimitry Andric     // profile in the prelink phase for to-be-fully-inlined functions.
50081ad6265SDimitry Andric     if (!NodeProfile) {
5010eae32dcSDimitry Andric       ProfileMap[ChildProfile->getContext()].merge(*ChildProfile);
5025f757f3fSDimitry Andric       NewChildProfileHash = ChildProfile->getContext().getHashCode();
50381ad6265SDimitry Andric     } else if (GenerateMergedBaseProfiles) {
50481ad6265SDimitry Andric       ProfileMap[ChildProfile->getContext()].merge(*ChildProfile);
5055f757f3fSDimitry Andric       NewChildProfileHash = ChildProfile->getContext().getHashCode();
50681ad6265SDimitry Andric       auto &SamplesMap = NodeProfile->functionSamplesAt(ChildNode.CallSiteLoc);
5075f757f3fSDimitry Andric       SamplesMap[ChildProfile->getFunction()].getContext().setAttribute(
50881ad6265SDimitry Andric           ContextDuplicatedIntoBase);
50981ad6265SDimitry Andric     }
5100eae32dcSDimitry Andric 
5115f757f3fSDimitry Andric     // Remove the original child profile. Check if MD5 of new child profile
5125f757f3fSDimitry Andric     // collides with old profile, in this case the [] operator already
5135f757f3fSDimitry Andric     // overwritten it without the need of erase.
5145f757f3fSDimitry Andric     if (NewChildProfileHash != OrigChildContextHash)
5155f757f3fSDimitry Andric       ProfileMap.erase(OrigChildContextHash);
5160eae32dcSDimitry Andric   }
5170eae32dcSDimitry Andric }
5180eae32dcSDimitry Andric 
51906c3fb27SDimitry Andric void ProfileConverter::convertCSProfiles() { convertCSProfiles(RootFrame); }
520