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