139d628a0SDimitry Andric //=-- SampleProf.cpp - Sample profiling format support --------------------===//
239d628a0SDimitry Andric //
339d628a0SDimitry Andric // The LLVM Compiler Infrastructure
439d628a0SDimitry Andric //
539d628a0SDimitry Andric // This file is distributed under the University of Illinois Open Source
639d628a0SDimitry Andric // License. See LICENSE.TXT for details.
739d628a0SDimitry Andric //
839d628a0SDimitry Andric //===----------------------------------------------------------------------===//
939d628a0SDimitry Andric //
1039d628a0SDimitry Andric // This file contains common definitions used in the reading and writing of
1139d628a0SDimitry Andric // sample profile data.
1239d628a0SDimitry Andric //
1339d628a0SDimitry Andric //===----------------------------------------------------------------------===//
1439d628a0SDimitry Andric
1539d628a0SDimitry Andric #include "llvm/ProfileData/SampleProf.h"
164ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
174ba319b5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
187a7e6055SDimitry Andric #include "llvm/Support/Compiler.h"
197a7e6055SDimitry Andric #include "llvm/Support/Debug.h"
2039d628a0SDimitry Andric #include "llvm/Support/ErrorHandling.h"
2139d628a0SDimitry Andric #include "llvm/Support/ManagedStatic.h"
227a7e6055SDimitry Andric #include "llvm/Support/raw_ostream.h"
237a7e6055SDimitry Andric #include <string>
247a7e6055SDimitry Andric #include <system_error>
2539d628a0SDimitry Andric
2639d628a0SDimitry Andric using namespace llvm;
277a7e6055SDimitry Andric using namespace sampleprof;
2839d628a0SDimitry Andric
29*b5893f02SDimitry Andric namespace llvm {
30*b5893f02SDimitry Andric namespace sampleprof {
31*b5893f02SDimitry Andric SampleProfileFormat FunctionSamples::Format;
32*b5893f02SDimitry Andric DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap;
33*b5893f02SDimitry Andric Module *FunctionSamples::CurrentModule;
34*b5893f02SDimitry Andric } // namespace sampleprof
35*b5893f02SDimitry Andric } // namespace llvm
36*b5893f02SDimitry Andric
3739d628a0SDimitry Andric namespace {
387a7e6055SDimitry Andric
393ca95b02SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
403ca95b02SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
413ca95b02SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
4239d628a0SDimitry Andric class SampleProfErrorCategoryType : public std::error_category {
name() const43d88c1a5aSDimitry Andric const char *name() const noexcept override { return "llvm.sampleprof"; }
447a7e6055SDimitry Andric
message(int IE) const4539d628a0SDimitry Andric std::string message(int IE) const override {
4639d628a0SDimitry Andric sampleprof_error E = static_cast<sampleprof_error>(IE);
4739d628a0SDimitry Andric switch (E) {
4839d628a0SDimitry Andric case sampleprof_error::success:
4939d628a0SDimitry Andric return "Success";
5039d628a0SDimitry Andric case sampleprof_error::bad_magic:
517d523365SDimitry Andric return "Invalid sample profile data (bad magic)";
5239d628a0SDimitry Andric case sampleprof_error::unsupported_version:
537d523365SDimitry Andric return "Unsupported sample profile format version";
5439d628a0SDimitry Andric case sampleprof_error::too_large:
5539d628a0SDimitry Andric return "Too much profile data";
5639d628a0SDimitry Andric case sampleprof_error::truncated:
5739d628a0SDimitry Andric return "Truncated profile data";
5839d628a0SDimitry Andric case sampleprof_error::malformed:
597d523365SDimitry Andric return "Malformed sample profile data";
6039d628a0SDimitry Andric case sampleprof_error::unrecognized_format:
617d523365SDimitry Andric return "Unrecognized sample profile encoding format";
627d523365SDimitry Andric case sampleprof_error::unsupported_writing_format:
637d523365SDimitry Andric return "Profile encoding format unsupported for writing operations";
647d523365SDimitry Andric case sampleprof_error::truncated_name_table:
657d523365SDimitry Andric return "Truncated function name table";
667d523365SDimitry Andric case sampleprof_error::not_implemented:
677d523365SDimitry Andric return "Unimplemented feature";
687d523365SDimitry Andric case sampleprof_error::counter_overflow:
697d523365SDimitry Andric return "Counter overflow";
70*b5893f02SDimitry Andric case sampleprof_error::ostream_seek_unsupported:
71*b5893f02SDimitry Andric return "Ostream does not support seek";
7239d628a0SDimitry Andric }
7339d628a0SDimitry Andric llvm_unreachable("A value of sampleprof_error has no message.");
7439d628a0SDimitry Andric }
7539d628a0SDimitry Andric };
767a7e6055SDimitry Andric
777a7e6055SDimitry Andric } // end anonymous namespace
7839d628a0SDimitry Andric
7939d628a0SDimitry Andric static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
8039d628a0SDimitry Andric
sampleprof_category()8139d628a0SDimitry Andric const std::error_category &llvm::sampleprof_category() {
8239d628a0SDimitry Andric return *ErrorCategory;
8339d628a0SDimitry Andric }
847d523365SDimitry Andric
print(raw_ostream & OS) const857d523365SDimitry Andric void LineLocation::print(raw_ostream &OS) const {
867d523365SDimitry Andric OS << LineOffset;
877d523365SDimitry Andric if (Discriminator > 0)
887d523365SDimitry Andric OS << "." << Discriminator;
897d523365SDimitry Andric }
907d523365SDimitry Andric
operator <<(raw_ostream & OS,const LineLocation & Loc)917d523365SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
927d523365SDimitry Andric const LineLocation &Loc) {
937d523365SDimitry Andric Loc.print(OS);
947d523365SDimitry Andric return OS;
957d523365SDimitry Andric }
967d523365SDimitry Andric
977a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const983ca95b02SDimitry Andric LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
997a7e6055SDimitry Andric #endif
1007d523365SDimitry Andric
1014ba319b5SDimitry Andric /// Print the sample record to the stream \p OS indented by \p Indent.
print(raw_ostream & OS,unsigned Indent) const1027d523365SDimitry Andric void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
1037d523365SDimitry Andric OS << NumSamples;
1047d523365SDimitry Andric if (hasCalls()) {
1057d523365SDimitry Andric OS << ", calls:";
1067d523365SDimitry Andric for (const auto &I : getCallTargets())
1077d523365SDimitry Andric OS << " " << I.first() << ":" << I.second;
1087d523365SDimitry Andric }
1097d523365SDimitry Andric OS << "\n";
1107d523365SDimitry Andric }
1117d523365SDimitry Andric
1127a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1133ca95b02SDimitry Andric LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
1147a7e6055SDimitry Andric #endif
1157d523365SDimitry Andric
operator <<(raw_ostream & OS,const SampleRecord & Sample)1167d523365SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
1177d523365SDimitry Andric const SampleRecord &Sample) {
1187d523365SDimitry Andric Sample.print(OS, 0);
1197d523365SDimitry Andric return OS;
1207d523365SDimitry Andric }
1217d523365SDimitry Andric
1224ba319b5SDimitry Andric /// Print the samples collected for a function on stream \p OS.
print(raw_ostream & OS,unsigned Indent) const1237d523365SDimitry Andric void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
1247d523365SDimitry Andric OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
1257d523365SDimitry Andric << " sampled lines\n";
1267d523365SDimitry Andric
1277d523365SDimitry Andric OS.indent(Indent);
1287a7e6055SDimitry Andric if (!BodySamples.empty()) {
1297d523365SDimitry Andric OS << "Samples collected in the function's body {\n";
1307d523365SDimitry Andric SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
1317d523365SDimitry Andric for (const auto &SI : SortedBodySamples.get()) {
1327d523365SDimitry Andric OS.indent(Indent + 2);
1337d523365SDimitry Andric OS << SI->first << ": " << SI->second;
1347d523365SDimitry Andric }
1357d523365SDimitry Andric OS.indent(Indent);
1367d523365SDimitry Andric OS << "}\n";
1377d523365SDimitry Andric } else {
1387d523365SDimitry Andric OS << "No samples collected in the function's body\n";
1397d523365SDimitry Andric }
1407d523365SDimitry Andric
1417d523365SDimitry Andric OS.indent(Indent);
1427a7e6055SDimitry Andric if (!CallsiteSamples.empty()) {
1437d523365SDimitry Andric OS << "Samples collected in inlined callsites {\n";
1447a7e6055SDimitry Andric SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
1457d523365SDimitry Andric CallsiteSamples);
1467d523365SDimitry Andric for (const auto &CS : SortedCallsiteSamples.get()) {
1477a7e6055SDimitry Andric for (const auto &FS : CS->second) {
1487d523365SDimitry Andric OS.indent(Indent + 2);
1497a7e6055SDimitry Andric OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
1507a7e6055SDimitry Andric FS.second.print(OS, Indent + 4);
1517a7e6055SDimitry Andric }
1527d523365SDimitry Andric }
1537d523365SDimitry Andric OS << "}\n";
1547d523365SDimitry Andric } else {
1557d523365SDimitry Andric OS << "No inlined callsites in this function\n";
1567d523365SDimitry Andric }
1577d523365SDimitry Andric }
1587d523365SDimitry Andric
operator <<(raw_ostream & OS,const FunctionSamples & FS)1597d523365SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
1607d523365SDimitry Andric const FunctionSamples &FS) {
1617d523365SDimitry Andric FS.print(OS);
1627d523365SDimitry Andric return OS;
1637d523365SDimitry Andric }
1647d523365SDimitry Andric
getOffset(const DILocation * DIL)1654ba319b5SDimitry Andric unsigned FunctionSamples::getOffset(const DILocation *DIL) {
1664ba319b5SDimitry Andric return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
1674ba319b5SDimitry Andric 0xffff;
1684ba319b5SDimitry Andric }
1694ba319b5SDimitry Andric
1704ba319b5SDimitry Andric const FunctionSamples *
findFunctionSamples(const DILocation * DIL) const1714ba319b5SDimitry Andric FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
1724ba319b5SDimitry Andric assert(DIL);
1734ba319b5SDimitry Andric SmallVector<std::pair<LineLocation, StringRef>, 10> S;
1744ba319b5SDimitry Andric
1754ba319b5SDimitry Andric const DILocation *PrevDIL = DIL;
1764ba319b5SDimitry Andric for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
1774ba319b5SDimitry Andric S.push_back(std::make_pair(
1784ba319b5SDimitry Andric LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
1794ba319b5SDimitry Andric PrevDIL->getScope()->getSubprogram()->getLinkageName()));
1804ba319b5SDimitry Andric PrevDIL = DIL;
1814ba319b5SDimitry Andric }
1824ba319b5SDimitry Andric if (S.size() == 0)
1834ba319b5SDimitry Andric return this;
1844ba319b5SDimitry Andric const FunctionSamples *FS = this;
1854ba319b5SDimitry Andric for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
1864ba319b5SDimitry Andric FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
1874ba319b5SDimitry Andric }
1884ba319b5SDimitry Andric return FS;
1894ba319b5SDimitry Andric }
1904ba319b5SDimitry Andric
1917a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1927a7e6055SDimitry Andric LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
1937a7e6055SDimitry Andric #endif
194