1 //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // llvm-profdata merges .profdata files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/ProfileData/InstrProfReader.h"
19 #include "llvm/ProfileData/InstrProfWriter.h"
20 #include "llvm/ProfileData/ProfileCommon.h"
21 #include "llvm/ProfileData/SampleProfReader.h"
22 #include "llvm/ProfileData/SampleProfWriter.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Errc.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/InitLLVM.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/ThreadPool.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 
34 using namespace llvm;
35 
36 enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
37 
38 static void warn(StringRef Prefix, Twine Message, std::string Whence = "",
39                  std::string Hint = "") {
40   errs() << Prefix;
41   if (!Whence.empty())
42     errs() << Whence << ": ";
43   errs() << Message << "\n";
44   if (!Hint.empty())
45     errs() << Hint << "\n";
46 }
47 
48 static void exitWithError(Twine Message, std::string Whence = "",
49                           std::string Hint = "") {
50   warn("error: ", Message, Whence, Hint);
51   ::exit(1);
52 }
53 
54 static void exitWithError(Error E, StringRef Whence = "") {
55   if (E.isA<InstrProfError>()) {
56     handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
57       instrprof_error instrError = IPE.get();
58       StringRef Hint = "";
59       if (instrError == instrprof_error::unrecognized_format) {
60         // Hint for common error of forgetting -sample for sample profiles.
61         Hint = "Perhaps you forgot to use the -sample option?";
62       }
63       exitWithError(IPE.message(), Whence, Hint);
64     });
65   }
66 
67   exitWithError(toString(std::move(E)), Whence);
68 }
69 
70 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
71   exitWithError(EC.message(), Whence);
72 }
73 
74 namespace {
75 enum ProfileKinds { instr, sample };
76 }
77 
78 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
79                                    StringRef WhenceFunction = "",
80                                    bool ShowHint = true) {
81   if (!WhenceFile.empty())
82     errs() << WhenceFile << ": ";
83   if (!WhenceFunction.empty())
84     errs() << WhenceFunction << ": ";
85 
86   auto IPE = instrprof_error::success;
87   E = handleErrors(std::move(E),
88                    [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
89                      IPE = E->get();
90                      return Error(std::move(E));
91                    });
92   errs() << toString(std::move(E)) << "\n";
93 
94   if (ShowHint) {
95     StringRef Hint = "";
96     if (IPE != instrprof_error::success) {
97       switch (IPE) {
98       case instrprof_error::hash_mismatch:
99       case instrprof_error::count_mismatch:
100       case instrprof_error::value_site_count_mismatch:
101         Hint = "Make sure that all profile data to be merged is generated "
102                "from the same binary.";
103         break;
104       default:
105         break;
106       }
107     }
108 
109     if (!Hint.empty())
110       errs() << Hint << "\n";
111   }
112 }
113 
114 struct WeightedFile {
115   std::string Filename;
116   uint64_t Weight;
117 };
118 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
119 
120 /// Keep track of merged data and reported errors.
121 struct WriterContext {
122   std::mutex Lock;
123   InstrProfWriter Writer;
124   Error Err;
125   std::string ErrWhence;
126   std::mutex &ErrLock;
127   SmallSet<instrprof_error, 4> &WriterErrorCodes;
128 
129   WriterContext(bool IsSparse, std::mutex &ErrLock,
130                 SmallSet<instrprof_error, 4> &WriterErrorCodes)
131       : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
132         ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
133 };
134 
135 /// Determine whether an error is fatal for profile merging.
136 static bool isFatalError(instrprof_error IPE) {
137   switch (IPE) {
138   default:
139     return true;
140   case instrprof_error::success:
141   case instrprof_error::eof:
142   case instrprof_error::unknown_function:
143   case instrprof_error::hash_mismatch:
144   case instrprof_error::count_mismatch:
145   case instrprof_error::counter_overflow:
146   case instrprof_error::value_site_count_mismatch:
147     return false;
148   }
149 }
150 
151 /// Load an input into a writer context.
152 static void loadInput(const WeightedFile &Input, WriterContext *WC) {
153   std::unique_lock<std::mutex> CtxGuard{WC->Lock};
154 
155   // If there's a pending hard error, don't do more work.
156   if (WC->Err)
157     return;
158 
159   // Copy the filename, because llvm::ThreadPool copied the input "const
160   // WeightedFile &" by value, making a reference to the filename within it
161   // invalid outside of this packaged task.
162   WC->ErrWhence = Input.Filename;
163 
164   auto ReaderOrErr = InstrProfReader::create(Input.Filename);
165   if (Error E = ReaderOrErr.takeError()) {
166     // Skip the empty profiles by returning sliently.
167     instrprof_error IPE = InstrProfError::take(std::move(E));
168     if (IPE != instrprof_error::empty_raw_profile)
169       WC->Err = make_error<InstrProfError>(IPE);
170     return;
171   }
172 
173   auto Reader = std::move(ReaderOrErr.get());
174   bool IsIRProfile = Reader->isIRLevelProfile();
175   if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
176     WC->Err = make_error<StringError>(
177         "Merge IR generated profile with Clang generated profile.",
178         std::error_code());
179     return;
180   }
181 
182   for (auto &I : *Reader) {
183     const StringRef FuncName = I.Name;
184     bool Reported = false;
185     WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
186       if (Reported) {
187         consumeError(std::move(E));
188         return;
189       }
190       Reported = true;
191       // Only show hint the first time an error occurs.
192       instrprof_error IPE = InstrProfError::take(std::move(E));
193       std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
194       bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
195       handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
196                              FuncName, firstTime);
197     });
198   }
199   if (Reader->hasError()) {
200     if (Error E = Reader->getError()) {
201       instrprof_error IPE = InstrProfError::take(std::move(E));
202       if (isFatalError(IPE))
203         WC->Err = make_error<InstrProfError>(IPE);
204     }
205   }
206 }
207 
208 /// Merge the \p Src writer context into \p Dst.
209 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
210   // If we've already seen a hard error, continuing with the merge would
211   // clobber it.
212   if (Dst->Err || Src->Err)
213     return;
214 
215   bool Reported = false;
216   Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
217     if (Reported) {
218       consumeError(std::move(E));
219       return;
220     }
221     Reported = true;
222     Dst->Err = std::move(E);
223   });
224 }
225 
226 static void mergeInstrProfile(const WeightedFileVector &Inputs,
227                               StringRef OutputFilename,
228                               ProfileFormat OutputFormat, bool OutputSparse,
229                               unsigned NumThreads) {
230   if (OutputFilename.compare("-") == 0)
231     exitWithError("Cannot write indexed profdata format to stdout.");
232 
233   if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
234     exitWithError("Unknown format is specified.");
235 
236   std::error_code EC;
237   raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
238   if (EC)
239     exitWithErrorCode(EC, OutputFilename);
240 
241   std::mutex ErrorLock;
242   SmallSet<instrprof_error, 4> WriterErrorCodes;
243 
244   // If NumThreads is not specified, auto-detect a good default.
245   if (NumThreads == 0)
246     NumThreads =
247         std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
248 
249   // Initialize the writer contexts.
250   SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
251   for (unsigned I = 0; I < NumThreads; ++I)
252     Contexts.emplace_back(llvm::make_unique<WriterContext>(
253         OutputSparse, ErrorLock, WriterErrorCodes));
254 
255   if (NumThreads == 1) {
256     for (const auto &Input : Inputs)
257       loadInput(Input, Contexts[0].get());
258   } else {
259     ThreadPool Pool(NumThreads);
260 
261     // Load the inputs in parallel (N/NumThreads serial steps).
262     unsigned Ctx = 0;
263     for (const auto &Input : Inputs) {
264       Pool.async(loadInput, Input, Contexts[Ctx].get());
265       Ctx = (Ctx + 1) % NumThreads;
266     }
267     Pool.wait();
268 
269     // Merge the writer contexts together (~ lg(NumThreads) serial steps).
270     unsigned Mid = Contexts.size() / 2;
271     unsigned End = Contexts.size();
272     assert(Mid > 0 && "Expected more than one context");
273     do {
274       for (unsigned I = 0; I < Mid; ++I)
275         Pool.async(mergeWriterContexts, Contexts[I].get(),
276                    Contexts[I + Mid].get());
277       Pool.wait();
278       if (End & 1) {
279         Pool.async(mergeWriterContexts, Contexts[0].get(),
280                    Contexts[End - 1].get());
281         Pool.wait();
282       }
283       End = Mid;
284       Mid /= 2;
285     } while (Mid > 0);
286   }
287 
288   // Handle deferred hard errors encountered during merging.
289   for (std::unique_ptr<WriterContext> &WC : Contexts) {
290     if (!WC->Err)
291       continue;
292     if (!WC->Err.isA<InstrProfError>())
293       exitWithError(std::move(WC->Err), WC->ErrWhence);
294 
295     instrprof_error IPE = InstrProfError::take(std::move(WC->Err));
296     if (isFatalError(IPE))
297       exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence);
298     else
299       warn("warning: ", toString(make_error<InstrProfError>(IPE)),
300            WC->ErrWhence);
301   }
302 
303   InstrProfWriter &Writer = Contexts[0]->Writer;
304   if (OutputFormat == PF_Text) {
305     if (Error E = Writer.writeText(Output))
306       exitWithError(std::move(E));
307   } else {
308     Writer.write(Output);
309   }
310 }
311 
312 static sampleprof::SampleProfileFormat FormatMap[] = {
313     sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
314     sampleprof::SPF_GCC};
315 
316 static void mergeSampleProfile(const WeightedFileVector &Inputs,
317                                StringRef OutputFilename,
318                                ProfileFormat OutputFormat) {
319   using namespace sampleprof;
320   auto WriterOrErr =
321       SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
322   if (std::error_code EC = WriterOrErr.getError())
323     exitWithErrorCode(EC, OutputFilename);
324 
325   auto Writer = std::move(WriterOrErr.get());
326   StringMap<FunctionSamples> ProfileMap;
327   SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
328   LLVMContext Context;
329   for (const auto &Input : Inputs) {
330     auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
331     if (std::error_code EC = ReaderOrErr.getError())
332       exitWithErrorCode(EC, Input.Filename);
333 
334     // We need to keep the readers around until after all the files are
335     // read so that we do not lose the function names stored in each
336     // reader's memory. The function names are needed to write out the
337     // merged profile map.
338     Readers.push_back(std::move(ReaderOrErr.get()));
339     const auto Reader = Readers.back().get();
340     if (std::error_code EC = Reader->read())
341       exitWithErrorCode(EC, Input.Filename);
342 
343     StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
344     for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
345                                               E = Profiles.end();
346          I != E; ++I) {
347       StringRef FName = I->first();
348       FunctionSamples &Samples = I->second;
349       sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
350       if (Result != sampleprof_error::success) {
351         std::error_code EC = make_error_code(Result);
352         handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
353       }
354     }
355   }
356   Writer->write(ProfileMap);
357 }
358 
359 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
360   StringRef WeightStr, FileName;
361   std::tie(WeightStr, FileName) = WeightedFilename.split(',');
362 
363   uint64_t Weight;
364   if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
365     exitWithError("Input weight must be a positive integer.");
366 
367   return {FileName, Weight};
368 }
369 
370 static std::unique_ptr<MemoryBuffer>
371 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
372   if (InputFilenamesFile == "")
373     return {};
374 
375   auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
376   if (!BufOrError)
377     exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
378 
379   return std::move(*BufOrError);
380 }
381 
382 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
383   StringRef Filename = WF.Filename;
384   uint64_t Weight = WF.Weight;
385 
386   // If it's STDIN just pass it on.
387   if (Filename == "-") {
388     WNI.push_back({Filename, Weight});
389     return;
390   }
391 
392   llvm::sys::fs::file_status Status;
393   llvm::sys::fs::status(Filename, Status);
394   if (!llvm::sys::fs::exists(Status))
395     exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
396                       Filename);
397   // If it's a source file, collect it.
398   if (llvm::sys::fs::is_regular_file(Status)) {
399     WNI.push_back({Filename, Weight});
400     return;
401   }
402 
403   if (llvm::sys::fs::is_directory(Status)) {
404     std::error_code EC;
405     for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
406          F != E && !EC; F.increment(EC)) {
407       if (llvm::sys::fs::is_regular_file(F->path())) {
408         addWeightedInput(WNI, {F->path(), Weight});
409       }
410     }
411     if (EC)
412       exitWithErrorCode(EC, Filename);
413   }
414 }
415 
416 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
417                                     WeightedFileVector &WFV) {
418   if (!Buffer)
419     return;
420 
421   SmallVector<StringRef, 8> Entries;
422   StringRef Data = Buffer->getBuffer();
423   Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
424   for (const StringRef &FileWeightEntry : Entries) {
425     StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
426     // Skip comments.
427     if (SanitizedEntry.startswith("#"))
428       continue;
429     // If there's no comma, it's an unweighted profile.
430     else if (SanitizedEntry.find(',') == StringRef::npos)
431       addWeightedInput(WFV, {SanitizedEntry, 1});
432     else
433       addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
434   }
435 }
436 
437 static int merge_main(int argc, const char *argv[]) {
438   cl::list<std::string> InputFilenames(cl::Positional,
439                                        cl::desc("<filename...>"));
440   cl::list<std::string> WeightedInputFilenames("weighted-input",
441                                                cl::desc("<weight>,<filename>"));
442   cl::opt<std::string> InputFilenamesFile(
443       "input-files", cl::init(""),
444       cl::desc("Path to file containing newline-separated "
445                "[<weight>,]<filename> entries"));
446   cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
447                                 cl::aliasopt(InputFilenamesFile));
448   cl::opt<bool> DumpInputFileList(
449       "dump-input-file-list", cl::init(false), cl::Hidden,
450       cl::desc("Dump the list of input files and their weights, then exit"));
451   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
452                                       cl::init("-"), cl::Required,
453                                       cl::desc("Output file"));
454   cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
455                             cl::aliasopt(OutputFilename));
456   cl::opt<ProfileKinds> ProfileKind(
457       cl::desc("Profile kind:"), cl::init(instr),
458       cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
459                  clEnumVal(sample, "Sample profile")));
460   cl::opt<ProfileFormat> OutputFormat(
461       cl::desc("Format of output profile"), cl::init(PF_Binary),
462       cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
463                  clEnumValN(PF_Text, "text", "Text encoding"),
464                  clEnumValN(PF_GCC, "gcc",
465                             "GCC encoding (only meaningful for -sample)")));
466   cl::opt<bool> OutputSparse("sparse", cl::init(false),
467       cl::desc("Generate a sparse profile (only meaningful for -instr)"));
468   cl::opt<unsigned> NumThreads(
469       "num-threads", cl::init(0),
470       cl::desc("Number of merge threads to use (default: autodetect)"));
471   cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
472                         cl::aliasopt(NumThreads));
473 
474   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
475 
476   WeightedFileVector WeightedInputs;
477   for (StringRef Filename : InputFilenames)
478     addWeightedInput(WeightedInputs, {Filename, 1});
479   for (StringRef WeightedFilename : WeightedInputFilenames)
480     addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
481 
482   // Make sure that the file buffer stays alive for the duration of the
483   // weighted input vector's lifetime.
484   auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
485   parseInputFilenamesFile(Buffer.get(), WeightedInputs);
486 
487   if (WeightedInputs.empty())
488     exitWithError("No input files specified. See " +
489                   sys::path::filename(argv[0]) + " -help");
490 
491   if (DumpInputFileList) {
492     for (auto &WF : WeightedInputs)
493       outs() << WF.Weight << "," << WF.Filename << "\n";
494     return 0;
495   }
496 
497   if (ProfileKind == instr)
498     mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
499                       OutputSparse, NumThreads);
500   else
501     mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
502 
503   return 0;
504 }
505 
506 typedef struct ValueSitesStats {
507   ValueSitesStats()
508       : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
509         TotalNumValues(0) {}
510   uint64_t TotalNumValueSites;
511   uint64_t TotalNumValueSitesWithValueProfile;
512   uint64_t TotalNumValues;
513   std::vector<unsigned> ValueSitesHistogram;
514 } ValueSitesStats;
515 
516 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
517                                   ValueSitesStats &Stats, raw_fd_ostream &OS,
518                                   InstrProfSymtab *Symtab) {
519   uint32_t NS = Func.getNumValueSites(VK);
520   Stats.TotalNumValueSites += NS;
521   for (size_t I = 0; I < NS; ++I) {
522     uint32_t NV = Func.getNumValueDataForSite(VK, I);
523     std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
524     Stats.TotalNumValues += NV;
525     if (NV) {
526       Stats.TotalNumValueSitesWithValueProfile++;
527       if (NV > Stats.ValueSitesHistogram.size())
528         Stats.ValueSitesHistogram.resize(NV, 0);
529       Stats.ValueSitesHistogram[NV - 1]++;
530     }
531     for (uint32_t V = 0; V < NV; V++) {
532       OS << "\t[ " << I << ", ";
533       if (Symtab == nullptr)
534         OS << VD[V].Value;
535       else
536         OS << Symtab->getFuncName(VD[V].Value);
537       OS << ", " << VD[V].Count << " ]\n";
538     }
539   }
540 }
541 
542 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
543                                 ValueSitesStats &Stats) {
544   OS << "  Total number of sites: " << Stats.TotalNumValueSites << "\n";
545   OS << "  Total number of sites with values: "
546      << Stats.TotalNumValueSitesWithValueProfile << "\n";
547   OS << "  Total number of profiled values: " << Stats.TotalNumValues << "\n";
548 
549   OS << "  Value sites histogram:\n\tNumTargets, SiteCount\n";
550   for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
551     if (Stats.ValueSitesHistogram[I] > 0)
552       OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
553   }
554 }
555 
556 static int showInstrProfile(const std::string &Filename, bool ShowCounts,
557                             uint32_t TopN, bool ShowIndirectCallTargets,
558                             bool ShowMemOPSizes, bool ShowDetailedSummary,
559                             std::vector<uint32_t> DetailedSummaryCutoffs,
560                             bool ShowAllFunctions,
561                             const std::string &ShowFunction, bool TextFormat,
562                             raw_fd_ostream &OS) {
563   auto ReaderOrErr = InstrProfReader::create(Filename);
564   std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
565   if (ShowDetailedSummary && Cutoffs.empty()) {
566     Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
567   }
568   InstrProfSummaryBuilder Builder(std::move(Cutoffs));
569   if (Error E = ReaderOrErr.takeError())
570     exitWithError(std::move(E), Filename);
571 
572   auto Reader = std::move(ReaderOrErr.get());
573   bool IsIRInstr = Reader->isIRLevelProfile();
574   size_t ShownFunctions = 0;
575   int NumVPKind = IPVK_Last - IPVK_First + 1;
576   std::vector<ValueSitesStats> VPStats(NumVPKind);
577 
578   auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
579                    const std::pair<std::string, uint64_t> &v2) {
580     return v1.second > v2.second;
581   };
582 
583   std::priority_queue<std::pair<std::string, uint64_t>,
584                       std::vector<std::pair<std::string, uint64_t>>,
585                       decltype(MinCmp)>
586       HottestFuncs(MinCmp);
587 
588   for (const auto &Func : *Reader) {
589     bool Show =
590         ShowAllFunctions || (!ShowFunction.empty() &&
591                              Func.Name.find(ShowFunction) != Func.Name.npos);
592 
593     bool doTextFormatDump = (Show && ShowCounts && TextFormat);
594 
595     if (doTextFormatDump) {
596       InstrProfSymtab &Symtab = Reader->getSymtab();
597       InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
598                                          OS);
599       continue;
600     }
601 
602     assert(Func.Counts.size() > 0 && "function missing entry counter");
603     Builder.addRecord(Func);
604 
605     if (TopN) {
606       uint64_t FuncMax = 0;
607       for (size_t I = 0, E = Func.Counts.size(); I < E; ++I)
608         FuncMax = std::max(FuncMax, Func.Counts[I]);
609 
610       if (HottestFuncs.size() == TopN) {
611         if (HottestFuncs.top().second < FuncMax) {
612           HottestFuncs.pop();
613           HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
614         }
615       } else
616         HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
617     }
618 
619     if (Show) {
620 
621       if (!ShownFunctions)
622         OS << "Counters:\n";
623 
624       ++ShownFunctions;
625 
626       OS << "  " << Func.Name << ":\n"
627          << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
628          << "    Counters: " << Func.Counts.size() << "\n";
629       if (!IsIRInstr)
630         OS << "    Function count: " << Func.Counts[0] << "\n";
631 
632       if (ShowIndirectCallTargets)
633         OS << "    Indirect Call Site Count: "
634            << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
635 
636       uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
637       if (ShowMemOPSizes && NumMemOPCalls > 0)
638         OS << "    Number of Memory Intrinsics Calls: " << NumMemOPCalls
639            << "\n";
640 
641       if (ShowCounts) {
642         OS << "    Block counts: [";
643         size_t Start = (IsIRInstr ? 0 : 1);
644         for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
645           OS << (I == Start ? "" : ", ") << Func.Counts[I];
646         }
647         OS << "]\n";
648       }
649 
650       if (ShowIndirectCallTargets) {
651         OS << "    Indirect Target Results:\n";
652         traverseAllValueSites(Func, IPVK_IndirectCallTarget,
653                               VPStats[IPVK_IndirectCallTarget], OS,
654                               &(Reader->getSymtab()));
655       }
656 
657       if (ShowMemOPSizes && NumMemOPCalls > 0) {
658         OS << "    Memory Intrinsic Size Results:\n";
659         traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
660                               nullptr);
661       }
662     }
663   }
664   if (Reader->hasError())
665     exitWithError(Reader->getError(), Filename);
666 
667   if (ShowCounts && TextFormat)
668     return 0;
669   std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
670   OS << "Instrumentation level: "
671      << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
672   if (ShowAllFunctions || !ShowFunction.empty())
673     OS << "Functions shown: " << ShownFunctions << "\n";
674   OS << "Total functions: " << PS->getNumFunctions() << "\n";
675   OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
676   OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
677 
678   if (TopN) {
679     std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
680     while (!HottestFuncs.empty()) {
681       SortedHottestFuncs.emplace_back(HottestFuncs.top());
682       HottestFuncs.pop();
683     }
684     OS << "Top " << TopN
685        << " functions with the largest internal block counts: \n";
686     for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
687       OS << "  " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
688   }
689 
690   if (ShownFunctions && ShowIndirectCallTargets) {
691     OS << "Statistics for indirect call sites profile:\n";
692     showValueSitesStats(OS, IPVK_IndirectCallTarget,
693                         VPStats[IPVK_IndirectCallTarget]);
694   }
695 
696   if (ShownFunctions && ShowMemOPSizes) {
697     OS << "Statistics for memory intrinsic calls sizes profile:\n";
698     showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
699   }
700 
701   if (ShowDetailedSummary) {
702     OS << "Detailed summary:\n";
703     OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
704     OS << "Total count: " << PS->getTotalCount() << "\n";
705     for (auto Entry : PS->getDetailedSummary()) {
706       OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
707          << " account for "
708          << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
709          << " percentage of the total counts.\n";
710     }
711   }
712   return 0;
713 }
714 
715 static int showSampleProfile(const std::string &Filename, bool ShowCounts,
716                              bool ShowAllFunctions,
717                              const std::string &ShowFunction,
718                              raw_fd_ostream &OS) {
719   using namespace sampleprof;
720   LLVMContext Context;
721   auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
722   if (std::error_code EC = ReaderOrErr.getError())
723     exitWithErrorCode(EC, Filename);
724 
725   auto Reader = std::move(ReaderOrErr.get());
726   if (std::error_code EC = Reader->read())
727     exitWithErrorCode(EC, Filename);
728 
729   if (ShowAllFunctions || ShowFunction.empty())
730     Reader->dump(OS);
731   else
732     Reader->dumpFunctionProfile(ShowFunction, OS);
733 
734   return 0;
735 }
736 
737 static int show_main(int argc, const char *argv[]) {
738   cl::opt<std::string> Filename(cl::Positional, cl::Required,
739                                 cl::desc("<profdata-file>"));
740 
741   cl::opt<bool> ShowCounts("counts", cl::init(false),
742                            cl::desc("Show counter values for shown functions"));
743   cl::opt<bool> TextFormat(
744       "text", cl::init(false),
745       cl::desc("Show instr profile data in text dump format"));
746   cl::opt<bool> ShowIndirectCallTargets(
747       "ic-targets", cl::init(false),
748       cl::desc("Show indirect call site target values for shown functions"));
749   cl::opt<bool> ShowMemOPSizes(
750       "memop-sizes", cl::init(false),
751       cl::desc("Show the profiled sizes of the memory intrinsic calls "
752                "for shown functions"));
753   cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
754                                     cl::desc("Show detailed profile summary"));
755   cl::list<uint32_t> DetailedSummaryCutoffs(
756       cl::CommaSeparated, "detailed-summary-cutoffs",
757       cl::desc(
758           "Cutoff percentages (times 10000) for generating detailed summary"),
759       cl::value_desc("800000,901000,999999"));
760   cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
761                                  cl::desc("Details for every function"));
762   cl::opt<std::string> ShowFunction("function",
763                                     cl::desc("Details for matching functions"));
764 
765   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
766                                       cl::init("-"), cl::desc("Output file"));
767   cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
768                             cl::aliasopt(OutputFilename));
769   cl::opt<ProfileKinds> ProfileKind(
770       cl::desc("Profile kind:"), cl::init(instr),
771       cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
772                  clEnumVal(sample, "Sample profile")));
773   cl::opt<uint32_t> TopNFunctions(
774       "topn", cl::init(0),
775       cl::desc("Show the list of functions with the largest internal counts"));
776 
777   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
778 
779   if (OutputFilename.empty())
780     OutputFilename = "-";
781 
782   std::error_code EC;
783   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
784   if (EC)
785     exitWithErrorCode(EC, OutputFilename);
786 
787   if (ShowAllFunctions && !ShowFunction.empty())
788     errs() << "warning: -function argument ignored: showing all functions\n";
789 
790   std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
791                                 DetailedSummaryCutoffs.end());
792   if (ProfileKind == instr)
793     return showInstrProfile(Filename, ShowCounts, TopNFunctions,
794                             ShowIndirectCallTargets, ShowMemOPSizes,
795                             ShowDetailedSummary, DetailedSummaryCutoffs,
796                             ShowAllFunctions, ShowFunction, TextFormat, OS);
797   else
798     return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
799                              ShowFunction, OS);
800 }
801 
802 int main(int argc, const char *argv[]) {
803   InitLLVM X(argc, argv);
804 
805   StringRef ProgName(sys::path::filename(argv[0]));
806   if (argc > 1) {
807     int (*func)(int, const char *[]) = nullptr;
808 
809     if (strcmp(argv[1], "merge") == 0)
810       func = merge_main;
811     else if (strcmp(argv[1], "show") == 0)
812       func = show_main;
813 
814     if (func) {
815       std::string Invocation(ProgName.str() + " " + argv[1]);
816       argv[1] = Invocation.c_str();
817       return func(argc - 1, argv + 1);
818     }
819 
820     if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
821         strcmp(argv[1], "--help") == 0) {
822 
823       errs() << "OVERVIEW: LLVM profile data tools\n\n"
824              << "USAGE: " << ProgName << " <command> [args...]\n"
825              << "USAGE: " << ProgName << " <command> -help\n\n"
826              << "See each individual command --help for more details.\n"
827              << "Available commands: merge, show\n";
828       return 0;
829     }
830   }
831 
832   if (argc < 2)
833     errs() << ProgName << ": No command specified!\n";
834   else
835     errs() << ProgName << ": Unknown command!\n";
836 
837   errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
838   return 1;
839 }
840