1 //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // llvm-profdata merges .profdata files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/SmallSet.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/ProfileData/InstrProfCorrelator.h"
19 #include "llvm/ProfileData/InstrProfReader.h"
20 #include "llvm/ProfileData/InstrProfWriter.h"
21 #include "llvm/ProfileData/MemProf.h"
22 #include "llvm/ProfileData/ProfileCommon.h"
23 #include "llvm/ProfileData/RawMemProfReader.h"
24 #include "llvm/ProfileData/SampleProfReader.h"
25 #include "llvm/ProfileData/SampleProfWriter.h"
26 #include "llvm/Support/BalancedPartitioning.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Discriminator.h"
29 #include "llvm/Support/Errc.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Support/InitLLVM.h"
34 #include "llvm/Support/LLVMDriver.h"
35 #include "llvm/Support/MD5.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/ThreadPool.h"
39 #include "llvm/Support/Threading.h"
40 #include "llvm/Support/VirtualFileSystem.h"
41 #include "llvm/Support/WithColor.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <algorithm>
44 #include <cmath>
45 #include <optional>
46 #include <queue>
47 
48 using namespace llvm;
49 using ProfCorrelatorKind = InstrProfCorrelator::ProfCorrelatorKind;
50 
51 // https://llvm.org/docs/CommandGuide/llvm-profdata.html has documentations
52 // on each subcommand.
53 cl::SubCommand ShowSubcommand(
54     "show",
55     "Takes a profile data file and displays the profiles. See detailed "
56     "documentation in "
57     "https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-show");
58 cl::SubCommand OrderSubcommand(
59     "order",
60     "Reads temporal profiling traces from a profile and outputs a function "
61     "order that reduces the number of page faults for those traces. See "
62     "detailed documentation in "
63     "https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-order");
64 cl::SubCommand OverlapSubcommand(
65     "overlap",
66     "Computes and displays the overlap between two profiles. See detailed "
67     "documentation in "
68     "https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-overlap");
69 cl::SubCommand MergeSubcommand(
70     "merge",
71     "Takes several profiles and merge them together. See detailed "
72     "documentation in "
73     "https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-merge");
74 
75 namespace {
76 enum ProfileKinds { instr, sample, memory };
77 enum FailureMode { warnOnly, failIfAnyAreInvalid, failIfAllAreInvalid };
78 } // namespace
79 
80 enum ProfileFormat {
81   PF_None = 0,
82   PF_Text,
83   PF_Compact_Binary, // Deprecated
84   PF_Ext_Binary,
85   PF_GCC,
86   PF_Binary
87 };
88 
89 enum class ShowFormat { Text, Json, Yaml };
90 
91 // Common options.
92 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
93                                     cl::init("-"), cl::desc("Output file"),
94                                     cl::sub(ShowSubcommand),
95                                     cl::sub(OrderSubcommand),
96                                     cl::sub(OverlapSubcommand),
97                                     cl::sub(MergeSubcommand));
98 // NOTE: cl::alias must not have cl::sub(), since aliased option's cl::sub()
99 // will be used. llvm::cl::alias::done() method asserts this condition.
100 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
101                           cl::aliasopt(OutputFilename));
102 
103 // Options common to at least two commands.
104 cl::opt<ProfileKinds> ProfileKind(
105     cl::desc("Profile kind:"), cl::sub(MergeSubcommand),
106     cl::sub(OverlapSubcommand), cl::init(instr),
107     cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
108                clEnumVal(sample, "Sample profile")));
109 cl::opt<std::string> Filename(cl::Positional, cl::desc("<profdata-file>"),
110                               cl::sub(ShowSubcommand),
111                               cl::sub(OrderSubcommand));
112 cl::opt<unsigned> MaxDbgCorrelationWarnings(
113     "max-debug-info-correlation-warnings",
114     cl::desc("The maximum number of warnings to emit when correlating "
115              "profile from debug info (0 = no limit)"),
116     cl::sub(MergeSubcommand), cl::sub(ShowSubcommand), cl::init(5));
117 cl::opt<std::string> ProfiledBinary(
118     "profiled-binary", cl::init(""),
119     cl::desc("Path to binary from which the profile was collected."),
120     cl::sub(ShowSubcommand), cl::sub(MergeSubcommand));
121 cl::opt<std::string> DebugInfoFilename(
122     "debug-info", cl::init(""),
123     cl::desc(
124         "For show, read and extract profile metadata from debug info and show "
125         "the functions it found. For merge, use the provided debug info to "
126         "correlate the raw profile."),
127     cl::sub(ShowSubcommand), cl::sub(MergeSubcommand));
128 cl::opt<std::string>
129     BinaryFilename("binary-file", cl::init(""),
130                    cl::desc("For merge, use the provided unstripped bianry to "
131                             "correlate the raw profile."),
132                    cl::sub(MergeSubcommand));
133 cl::opt<std::string> FuncNameFilter(
134     "function",
135     cl::desc("Details for matching functions. For overlapping CSSPGO, this "
136              "takes a function name with calling context."),
137     cl::sub(ShowSubcommand), cl::sub(OverlapSubcommand));
138 
139 // TODO: Consider creating a template class (e.g., MergeOption, ShowOption) to
140 // factor out the common cl::sub in cl::opt constructor for subcommand-specific
141 // options.
142 
143 // Options specific to merge subcommand.
144 cl::list<std::string> InputFilenames(cl::Positional, cl::sub(MergeSubcommand),
145                                      cl::desc("<filename...>"));
146 cl::list<std::string> WeightedInputFilenames("weighted-input",
147                                              cl::sub(MergeSubcommand),
148                                              cl::desc("<weight>,<filename>"));
149 cl::opt<ProfileFormat> OutputFormat(
150     cl::desc("Format of output profile"), cl::sub(MergeSubcommand),
151     cl::init(PF_Ext_Binary),
152     cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding"),
153                clEnumValN(PF_Ext_Binary, "extbinary",
154                           "Extensible binary encoding "
155                           "(default)"),
156                clEnumValN(PF_Text, "text", "Text encoding"),
157                clEnumValN(PF_GCC, "gcc",
158                           "GCC encoding (only meaningful for -sample)")));
159 cl::opt<std::string>
160     InputFilenamesFile("input-files", cl::init(""), cl::sub(MergeSubcommand),
161                        cl::desc("Path to file containing newline-separated "
162                                 "[<weight>,]<filename> entries"));
163 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
164                               cl::aliasopt(InputFilenamesFile));
165 cl::opt<bool> DumpInputFileList(
166     "dump-input-file-list", cl::init(false), cl::Hidden,
167     cl::sub(MergeSubcommand),
168     cl::desc("Dump the list of input files and their weights, then exit"));
169 cl::opt<std::string> RemappingFile("remapping-file", cl::value_desc("file"),
170                                    cl::sub(MergeSubcommand),
171                                    cl::desc("Symbol remapping file"));
172 cl::alias RemappingFileA("r", cl::desc("Alias for --remapping-file"),
173                          cl::aliasopt(RemappingFile));
174 cl::opt<bool>
175     UseMD5("use-md5", cl::init(false), cl::Hidden,
176            cl::desc("Choose to use MD5 to represent string in name table (only "
177                     "meaningful for -extbinary)"),
178            cl::sub(MergeSubcommand));
179 cl::opt<bool> CompressAllSections(
180     "compress-all-sections", cl::init(false), cl::Hidden,
181     cl::sub(MergeSubcommand),
182     cl::desc("Compress all sections when writing the profile (only "
183              "meaningful for -extbinary)"));
184 cl::opt<bool> SampleMergeColdContext(
185     "sample-merge-cold-context", cl::init(false), cl::Hidden,
186     cl::sub(MergeSubcommand),
187     cl::desc(
188         "Merge context sample profiles whose count is below cold threshold"));
189 cl::opt<bool> SampleTrimColdContext(
190     "sample-trim-cold-context", cl::init(false), cl::Hidden,
191     cl::sub(MergeSubcommand),
192     cl::desc(
193         "Trim context sample profiles whose count is below cold threshold"));
194 cl::opt<uint32_t> SampleColdContextFrameDepth(
195     "sample-frame-depth-for-cold-context", cl::init(1),
196     cl::sub(MergeSubcommand),
197     cl::desc("Keep the last K frames while merging cold profile. 1 means the "
198              "context-less base profile"));
199 cl::opt<size_t> OutputSizeLimit(
200     "output-size-limit", cl::init(0), cl::Hidden, cl::sub(MergeSubcommand),
201     cl::desc("Trim cold functions until profile size is below specified "
202              "limit in bytes. This uses a heursitic and functions may be "
203              "excessively trimmed"));
204 cl::opt<bool> GenPartialProfile(
205     "gen-partial-profile", cl::init(false), cl::Hidden,
206     cl::sub(MergeSubcommand),
207     cl::desc("Generate a partial profile (only meaningful for -extbinary)"));
208 cl::opt<std::string> SupplInstrWithSample(
209     "supplement-instr-with-sample", cl::init(""), cl::Hidden,
210     cl::sub(MergeSubcommand),
211     cl::desc("Supplement an instr profile with sample profile, to correct "
212              "the profile unrepresentativeness issue. The sample "
213              "profile is the input of the flag. Output will be in instr "
214              "format (The flag only works with -instr)"));
215 cl::opt<float> ZeroCounterThreshold(
216     "zero-counter-threshold", cl::init(0.7), cl::Hidden,
217     cl::sub(MergeSubcommand),
218     cl::desc("For the function which is cold in instr profile but hot in "
219              "sample profile, if the ratio of the number of zero counters "
220              "divided by the total number of counters is above the "
221              "threshold, the profile of the function will be regarded as "
222              "being harmful for performance and will be dropped."));
223 cl::opt<unsigned> SupplMinSizeThreshold(
224     "suppl-min-size-threshold", cl::init(10), cl::Hidden,
225     cl::sub(MergeSubcommand),
226     cl::desc("If the size of a function is smaller than the threshold, "
227              "assume it can be inlined by PGO early inliner and it won't "
228              "be adjusted based on sample profile."));
229 cl::opt<unsigned> InstrProfColdThreshold(
230     "instr-prof-cold-threshold", cl::init(0), cl::Hidden,
231     cl::sub(MergeSubcommand),
232     cl::desc("User specified cold threshold for instr profile which will "
233              "override the cold threshold got from profile summary. "));
234 // WARNING: This reservoir size value is propagated to any input indexed
235 // profiles for simplicity. Changing this value between invocations could
236 // result in sample bias.
237 cl::opt<uint64_t> TemporalProfTraceReservoirSize(
238     "temporal-profile-trace-reservoir-size", cl::init(100),
239     cl::sub(MergeSubcommand),
240     cl::desc("The maximum number of stored temporal profile traces (default: "
241              "100)"));
242 cl::opt<uint64_t> TemporalProfMaxTraceLength(
243     "temporal-profile-max-trace-length", cl::init(10000),
244     cl::sub(MergeSubcommand),
245     cl::desc("The maximum length of a single temporal profile trace "
246              "(default: 10000)"));
247 
248 cl::opt<FailureMode>
249     FailMode("failure-mode", cl::init(failIfAnyAreInvalid),
250              cl::desc("Failure mode:"), cl::sub(MergeSubcommand),
251              cl::values(clEnumValN(warnOnly, "warn",
252                                    "Do not fail and just print warnings."),
253                         clEnumValN(failIfAnyAreInvalid, "any",
254                                    "Fail if any profile is invalid."),
255                         clEnumValN(failIfAllAreInvalid, "all",
256                                    "Fail only if all profiles are invalid.")));
257 
258 cl::opt<bool> OutputSparse(
259     "sparse", cl::init(false), cl::sub(MergeSubcommand),
260     cl::desc("Generate a sparse profile (only meaningful for -instr)"));
261 cl::opt<unsigned> NumThreads(
262     "num-threads", cl::init(0), cl::sub(MergeSubcommand),
263     cl::desc("Number of merge threads to use (default: autodetect)"));
264 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
265                       cl::aliasopt(NumThreads));
266 
267 cl::opt<std::string> ProfileSymbolListFile(
268     "prof-sym-list", cl::init(""), cl::sub(MergeSubcommand),
269     cl::desc("Path to file containing the list of function symbols "
270              "used to populate profile symbol list"));
271 
272 cl::opt<SampleProfileLayout> ProfileLayout(
273     "convert-sample-profile-layout",
274     cl::desc("Convert the generated profile to a profile with a new layout"),
275     cl::sub(MergeSubcommand), cl::init(SPL_None),
276     cl::values(
277         clEnumValN(SPL_Nest, "nest",
278                    "Nested profile, the input should be CS flat profile"),
279         clEnumValN(SPL_Flat, "flat",
280                    "Profile with nested inlinee flatten out")));
281 
282 cl::opt<bool> DropProfileSymbolList(
283     "drop-profile-symbol-list", cl::init(false), cl::Hidden,
284     cl::sub(MergeSubcommand),
285     cl::desc("Drop the profile symbol list when merging AutoFDO profiles "
286              "(only meaningful for -sample)"));
287 
288 // Options specific to overlap subcommand.
289 cl::opt<std::string> BaseFilename(cl::Positional, cl::Required,
290                                   cl::desc("<base profile file>"),
291                                   cl::sub(OverlapSubcommand));
292 cl::opt<std::string> TestFilename(cl::Positional, cl::Required,
293                                   cl::desc("<test profile file>"),
294                                   cl::sub(OverlapSubcommand));
295 
296 cl::opt<unsigned long long> SimilarityCutoff(
297     "similarity-cutoff", cl::init(0),
298     cl::desc("For sample profiles, list function names (with calling context "
299              "for csspgo) for overlapped functions "
300              "with similarities below the cutoff (percentage times 10000)."),
301     cl::sub(OverlapSubcommand));
302 
303 cl::opt<bool> IsCS(
304     "cs", cl::init(false),
305     cl::desc("For context sensitive PGO counts. Does not work with CSSPGO."),
306     cl::sub(OverlapSubcommand));
307 
308 cl::opt<unsigned long long> OverlapValueCutoff(
309     "value-cutoff", cl::init(-1),
310     cl::desc(
311         "Function level overlap information for every function (with calling "
312         "context for csspgo) in test "
313         "profile with max count value greater then the parameter value"),
314     cl::sub(OverlapSubcommand));
315 
316 // Options unique to show subcommand.
317 cl::opt<bool> ShowCounts("counts", cl::init(false),
318                          cl::desc("Show counter values for shown functions"),
319                          cl::sub(ShowSubcommand));
320 cl::opt<ShowFormat>
321     SFormat("show-format", cl::init(ShowFormat::Text),
322             cl::desc("Emit output in the selected format if supported"),
323             cl::sub(ShowSubcommand),
324             cl::values(clEnumValN(ShowFormat::Text, "text",
325                                   "emit normal text output (default)"),
326                        clEnumValN(ShowFormat::Json, "json", "emit JSON"),
327                        clEnumValN(ShowFormat::Yaml, "yaml", "emit YAML")));
328 // TODO: Consider replacing this with `--show-format=text-encoding`.
329 cl::opt<bool>
330     TextFormat("text", cl::init(false),
331                cl::desc("Show instr profile data in text dump format"),
332                cl::sub(ShowSubcommand));
333 cl::opt<bool>
334     JsonFormat("json",
335                cl::desc("Show sample profile data in the JSON format "
336                         "(deprecated, please use --show-format=json)"),
337                cl::sub(ShowSubcommand));
338 cl::opt<bool> ShowIndirectCallTargets(
339     "ic-targets", cl::init(false),
340     cl::desc("Show indirect call site target values for shown functions"),
341     cl::sub(ShowSubcommand));
342 cl::opt<bool> ShowMemOPSizes(
343     "memop-sizes", cl::init(false),
344     cl::desc("Show the profiled sizes of the memory intrinsic calls "
345              "for shown functions"),
346     cl::sub(ShowSubcommand));
347 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
348                                   cl::desc("Show detailed profile summary"),
349                                   cl::sub(ShowSubcommand));
350 cl::list<uint32_t> DetailedSummaryCutoffs(
351     cl::CommaSeparated, "detailed-summary-cutoffs",
352     cl::desc(
353         "Cutoff percentages (times 10000) for generating detailed summary"),
354     cl::value_desc("800000,901000,999999"), cl::sub(ShowSubcommand));
355 cl::opt<bool>
356     ShowHotFuncList("hot-func-list", cl::init(false),
357                     cl::desc("Show profile summary of a list of hot functions"),
358                     cl::sub(ShowSubcommand));
359 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
360                                cl::desc("Details for each and every function"),
361                                cl::sub(ShowSubcommand));
362 cl::opt<bool> ShowCS("showcs", cl::init(false),
363                      cl::desc("Show context sensitive counts"),
364                      cl::sub(ShowSubcommand));
365 cl::opt<ProfileKinds> ShowProfileKind(
366     cl::desc("Profile kind supported by show:"), cl::sub(ShowSubcommand),
367     cl::init(instr),
368     cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
369                clEnumVal(sample, "Sample profile"),
370                clEnumVal(memory, "MemProf memory access profile")));
371 cl::opt<uint32_t> TopNFunctions(
372     "topn", cl::init(0),
373     cl::desc("Show the list of functions with the largest internal counts"),
374     cl::sub(ShowSubcommand));
375 cl::opt<uint32_t> ShowValueCutoff(
376     "value-cutoff", cl::init(0),
377     cl::desc("Set the count value cutoff. Functions with the maximum count "
378              "less than this value will not be printed out. (Default is 0)"),
379     cl::sub(ShowSubcommand));
380 cl::opt<bool> OnlyListBelow(
381     "list-below-cutoff", cl::init(false),
382     cl::desc("Only output names of functions whose max count values are "
383              "below the cutoff value"),
384     cl::sub(ShowSubcommand));
385 cl::opt<bool> ShowProfileSymbolList(
386     "show-prof-sym-list", cl::init(false),
387     cl::desc("Show profile symbol list if it exists in the profile. "),
388     cl::sub(ShowSubcommand));
389 cl::opt<bool> ShowSectionInfoOnly(
390     "show-sec-info-only", cl::init(false),
391     cl::desc("Show the information of each section in the sample profile. "
392              "The flag is only usable when the sample profile is in "
393              "extbinary format"),
394     cl::sub(ShowSubcommand));
395 cl::opt<bool> ShowBinaryIds("binary-ids", cl::init(false),
396                             cl::desc("Show binary ids in the profile. "),
397                             cl::sub(ShowSubcommand));
398 cl::opt<bool> ShowTemporalProfTraces(
399     "temporal-profile-traces",
400     cl::desc("Show temporal profile traces in the profile."),
401     cl::sub(ShowSubcommand));
402 
403 cl::opt<bool>
404     ShowCovered("covered", cl::init(false),
405                 cl::desc("Show only the functions that have been executed."),
406                 cl::sub(ShowSubcommand));
407 
408 cl::opt<bool> ShowProfileVersion("profile-version", cl::init(false),
409                                  cl::desc("Show profile version. "),
410                                  cl::sub(ShowSubcommand));
411 
412 // We use this string to indicate that there are
413 // multiple static functions map to the same name.
414 const std::string DuplicateNameStr = "----";
415 
416 static void warn(Twine Message, std::string Whence = "",
417                  std::string Hint = "") {
418   WithColor::warning();
419   if (!Whence.empty())
420     errs() << Whence << ": ";
421   errs() << Message << "\n";
422   if (!Hint.empty())
423     WithColor::note() << Hint << "\n";
424 }
425 
426 static void warn(Error E, StringRef Whence = "") {
427   if (E.isA<InstrProfError>()) {
428     handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
429       warn(IPE.message(), std::string(Whence), std::string(""));
430     });
431   }
432 }
433 
434 static void exitWithError(Twine Message, std::string Whence = "",
435                           std::string Hint = "") {
436   WithColor::error();
437   if (!Whence.empty())
438     errs() << Whence << ": ";
439   errs() << Message << "\n";
440   if (!Hint.empty())
441     WithColor::note() << Hint << "\n";
442   ::exit(1);
443 }
444 
445 static void exitWithError(Error E, StringRef Whence = "") {
446   if (E.isA<InstrProfError>()) {
447     handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
448       instrprof_error instrError = IPE.get();
449       StringRef Hint = "";
450       if (instrError == instrprof_error::unrecognized_format) {
451         // Hint in case user missed specifying the profile type.
452         Hint = "Perhaps you forgot to use the --sample or --memory option?";
453       }
454       exitWithError(IPE.message(), std::string(Whence), std::string(Hint));
455     });
456     return;
457   }
458 
459   exitWithError(toString(std::move(E)), std::string(Whence));
460 }
461 
462 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
463   exitWithError(EC.message(), std::string(Whence));
464 }
465 
466 static void warnOrExitGivenError(FailureMode FailMode, std::error_code EC,
467                                  StringRef Whence = "") {
468   if (FailMode == failIfAnyAreInvalid)
469     exitWithErrorCode(EC, Whence);
470   else
471     warn(EC.message(), std::string(Whence));
472 }
473 
474 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
475                                    StringRef WhenceFunction = "",
476                                    bool ShowHint = true) {
477   if (!WhenceFile.empty())
478     errs() << WhenceFile << ": ";
479   if (!WhenceFunction.empty())
480     errs() << WhenceFunction << ": ";
481 
482   auto IPE = instrprof_error::success;
483   E = handleErrors(std::move(E),
484                    [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
485                      IPE = E->get();
486                      return Error(std::move(E));
487                    });
488   errs() << toString(std::move(E)) << "\n";
489 
490   if (ShowHint) {
491     StringRef Hint = "";
492     if (IPE != instrprof_error::success) {
493       switch (IPE) {
494       case instrprof_error::hash_mismatch:
495       case instrprof_error::count_mismatch:
496       case instrprof_error::value_site_count_mismatch:
497         Hint = "Make sure that all profile data to be merged is generated "
498                "from the same binary.";
499         break;
500       default:
501         break;
502       }
503     }
504 
505     if (!Hint.empty())
506       errs() << Hint << "\n";
507   }
508 }
509 
510 namespace {
511 /// A remapper from original symbol names to new symbol names based on a file
512 /// containing a list of mappings from old name to new name.
513 class SymbolRemapper {
514   std::unique_ptr<MemoryBuffer> File;
515   DenseMap<StringRef, StringRef> RemappingTable;
516 
517 public:
518   /// Build a SymbolRemapper from a file containing a list of old/new symbols.
519   static std::unique_ptr<SymbolRemapper> create(StringRef InputFile) {
520     auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile);
521     if (!BufOrError)
522       exitWithErrorCode(BufOrError.getError(), InputFile);
523 
524     auto Remapper = std::make_unique<SymbolRemapper>();
525     Remapper->File = std::move(BufOrError.get());
526 
527     for (line_iterator LineIt(*Remapper->File, /*SkipBlanks=*/true, '#');
528          !LineIt.is_at_eof(); ++LineIt) {
529       std::pair<StringRef, StringRef> Parts = LineIt->split(' ');
530       if (Parts.first.empty() || Parts.second.empty() ||
531           Parts.second.count(' ')) {
532         exitWithError("unexpected line in remapping file",
533                       (InputFile + ":" + Twine(LineIt.line_number())).str(),
534                       "expected 'old_symbol new_symbol'");
535       }
536       Remapper->RemappingTable.insert(Parts);
537     }
538     return Remapper;
539   }
540 
541   /// Attempt to map the given old symbol into a new symbol.
542   ///
543   /// \return The new symbol, or \p Name if no such symbol was found.
544   StringRef operator()(StringRef Name) {
545     StringRef New = RemappingTable.lookup(Name);
546     return New.empty() ? Name : New;
547   }
548 
549   FunctionId operator()(FunctionId Name) {
550     // MD5 name cannot be remapped.
551     if (!Name.isStringRef())
552       return Name;
553     StringRef New = RemappingTable.lookup(Name.stringRef());
554     return New.empty() ? Name : FunctionId(New);
555   }
556 };
557 }
558 
559 struct WeightedFile {
560   std::string Filename;
561   uint64_t Weight;
562 };
563 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
564 
565 /// Keep track of merged data and reported errors.
566 struct WriterContext {
567   std::mutex Lock;
568   InstrProfWriter Writer;
569   std::vector<std::pair<Error, std::string>> Errors;
570   std::mutex &ErrLock;
571   SmallSet<instrprof_error, 4> &WriterErrorCodes;
572 
573   WriterContext(bool IsSparse, std::mutex &ErrLock,
574                 SmallSet<instrprof_error, 4> &WriterErrorCodes,
575                 uint64_t ReservoirSize = 0, uint64_t MaxTraceLength = 0)
576       : Writer(IsSparse, ReservoirSize, MaxTraceLength), ErrLock(ErrLock),
577         WriterErrorCodes(WriterErrorCodes) {}
578 };
579 
580 /// Computer the overlap b/w profile BaseFilename and TestFileName,
581 /// and store the program level result to Overlap.
582 static void overlapInput(const std::string &BaseFilename,
583                          const std::string &TestFilename, WriterContext *WC,
584                          OverlapStats &Overlap,
585                          const OverlapFuncFilters &FuncFilter,
586                          raw_fd_ostream &OS, bool IsCS) {
587   auto FS = vfs::getRealFileSystem();
588   auto ReaderOrErr = InstrProfReader::create(TestFilename, *FS);
589   if (Error E = ReaderOrErr.takeError()) {
590     // Skip the empty profiles by returning sliently.
591     auto [ErrorCode, Msg] = InstrProfError::take(std::move(E));
592     if (ErrorCode != instrprof_error::empty_raw_profile)
593       WC->Errors.emplace_back(make_error<InstrProfError>(ErrorCode, Msg),
594                               TestFilename);
595     return;
596   }
597 
598   auto Reader = std::move(ReaderOrErr.get());
599   for (auto &I : *Reader) {
600     OverlapStats FuncOverlap(OverlapStats::FunctionLevel);
601     FuncOverlap.setFuncInfo(I.Name, I.Hash);
602 
603     WC->Writer.overlapRecord(std::move(I), Overlap, FuncOverlap, FuncFilter);
604     FuncOverlap.dump(OS);
605   }
606 }
607 
608 /// Load an input into a writer context.
609 static void loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
610                       const InstrProfCorrelator *Correlator,
611                       const StringRef ProfiledBinary, WriterContext *WC) {
612   std::unique_lock<std::mutex> CtxGuard{WC->Lock};
613 
614   // Copy the filename, because llvm::ThreadPool copied the input "const
615   // WeightedFile &" by value, making a reference to the filename within it
616   // invalid outside of this packaged task.
617   std::string Filename = Input.Filename;
618 
619   using ::llvm::memprof::RawMemProfReader;
620   if (RawMemProfReader::hasFormat(Input.Filename)) {
621     auto ReaderOrErr = RawMemProfReader::create(Input.Filename, ProfiledBinary);
622     if (!ReaderOrErr) {
623       exitWithError(ReaderOrErr.takeError(), Input.Filename);
624     }
625     std::unique_ptr<RawMemProfReader> Reader = std::move(ReaderOrErr.get());
626     // Check if the profile types can be merged, e.g. clang frontend profiles
627     // should not be merged with memprof profiles.
628     if (Error E = WC->Writer.mergeProfileKind(Reader->getProfileKind())) {
629       consumeError(std::move(E));
630       WC->Errors.emplace_back(
631           make_error<StringError>(
632               "Cannot merge MemProf profile with Clang generated profile.",
633               std::error_code()),
634           Filename);
635       return;
636     }
637 
638     auto MemProfError = [&](Error E) {
639       auto [ErrorCode, Msg] = InstrProfError::take(std::move(E));
640       WC->Errors.emplace_back(make_error<InstrProfError>(ErrorCode, Msg),
641                               Filename);
642     };
643 
644     // Add the frame mappings into the writer context.
645     const auto &IdToFrame = Reader->getFrameMapping();
646     for (const auto &I : IdToFrame) {
647       bool Succeeded = WC->Writer.addMemProfFrame(
648           /*Id=*/I.first, /*Frame=*/I.getSecond(), MemProfError);
649       // If we weren't able to add the frame mappings then it doesn't make sense
650       // to try to add the records from this profile.
651       if (!Succeeded)
652         return;
653     }
654     const auto &FunctionProfileData = Reader->getProfileData();
655     // Add the memprof records into the writer context.
656     for (const auto &I : FunctionProfileData) {
657       WC->Writer.addMemProfRecord(/*Id=*/I.first, /*Record=*/I.second);
658     }
659     return;
660   }
661 
662   auto FS = vfs::getRealFileSystem();
663   // TODO: This only saves the first non-fatal error from InstrProfReader, and
664   // then added to WriterContext::Errors. However, this is not extensible, if
665   // we have more non-fatal errors from InstrProfReader in the future. How
666   // should this interact with different -failure-mode?
667   std::optional<std::pair<Error, std::string>> ReaderWarning;
668   auto Warn = [&](Error E) {
669     if (ReaderWarning) {
670       consumeError(std::move(E));
671       return;
672     }
673     // Only show the first time an error occurs in this file.
674     auto [ErrCode, Msg] = InstrProfError::take(std::move(E));
675     ReaderWarning = {make_error<InstrProfError>(ErrCode, Msg), Filename};
676   };
677   auto ReaderOrErr =
678       InstrProfReader::create(Input.Filename, *FS, Correlator, Warn);
679   if (Error E = ReaderOrErr.takeError()) {
680     // Skip the empty profiles by returning silently.
681     auto [ErrCode, Msg] = InstrProfError::take(std::move(E));
682     if (ErrCode != instrprof_error::empty_raw_profile)
683       WC->Errors.emplace_back(make_error<InstrProfError>(ErrCode, Msg),
684                               Filename);
685     return;
686   }
687 
688   auto Reader = std::move(ReaderOrErr.get());
689   if (Error E = WC->Writer.mergeProfileKind(Reader->getProfileKind())) {
690     consumeError(std::move(E));
691     WC->Errors.emplace_back(
692         make_error<StringError>(
693             "Merge IR generated profile with Clang generated profile.",
694             std::error_code()),
695         Filename);
696     return;
697   }
698 
699   for (auto &I : *Reader) {
700     if (Remapper)
701       I.Name = (*Remapper)(I.Name);
702     const StringRef FuncName = I.Name;
703     bool Reported = false;
704     WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
705       if (Reported) {
706         consumeError(std::move(E));
707         return;
708       }
709       Reported = true;
710       // Only show hint the first time an error occurs.
711       auto [ErrCode, Msg] = InstrProfError::take(std::move(E));
712       std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
713       bool firstTime = WC->WriterErrorCodes.insert(ErrCode).second;
714       handleMergeWriterError(make_error<InstrProfError>(ErrCode, Msg),
715                              Input.Filename, FuncName, firstTime);
716     });
717   }
718 
719   if (Reader->hasTemporalProfile()) {
720     auto &Traces = Reader->getTemporalProfTraces(Input.Weight);
721     if (!Traces.empty())
722       WC->Writer.addTemporalProfileTraces(
723           Traces, Reader->getTemporalProfTraceStreamSize());
724   }
725   if (Reader->hasError()) {
726     if (Error E = Reader->getError()) {
727       WC->Errors.emplace_back(std::move(E), Filename);
728       return;
729     }
730   }
731 
732   std::vector<llvm::object::BuildID> BinaryIds;
733   if (Error E = Reader->readBinaryIds(BinaryIds)) {
734     WC->Errors.emplace_back(std::move(E), Filename);
735     return;
736   }
737   WC->Writer.addBinaryIds(BinaryIds);
738 
739   if (ReaderWarning) {
740     WC->Errors.emplace_back(std::move(ReaderWarning->first),
741                             ReaderWarning->second);
742   }
743 }
744 
745 /// Merge the \p Src writer context into \p Dst.
746 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
747   for (auto &ErrorPair : Src->Errors)
748     Dst->Errors.push_back(std::move(ErrorPair));
749   Src->Errors.clear();
750 
751   if (Error E = Dst->Writer.mergeProfileKind(Src->Writer.getProfileKind()))
752     exitWithError(std::move(E));
753 
754   Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
755     auto [ErrorCode, Msg] = InstrProfError::take(std::move(E));
756     std::unique_lock<std::mutex> ErrGuard{Dst->ErrLock};
757     bool firstTime = Dst->WriterErrorCodes.insert(ErrorCode).second;
758     if (firstTime)
759       warn(toString(make_error<InstrProfError>(ErrorCode, Msg)));
760   });
761 }
762 
763 static void writeInstrProfile(StringRef OutputFilename,
764                               ProfileFormat OutputFormat,
765                               InstrProfWriter &Writer) {
766   std::error_code EC;
767   raw_fd_ostream Output(OutputFilename.data(), EC,
768                         OutputFormat == PF_Text ? sys::fs::OF_TextWithCRLF
769                                                 : sys::fs::OF_None);
770   if (EC)
771     exitWithErrorCode(EC, OutputFilename);
772 
773   if (OutputFormat == PF_Text) {
774     if (Error E = Writer.writeText(Output))
775       warn(std::move(E));
776   } else {
777     if (Output.is_displayed())
778       exitWithError("cannot write a non-text format profile to the terminal");
779     if (Error E = Writer.write(Output))
780       warn(std::move(E));
781   }
782 }
783 
784 static void mergeInstrProfile(const WeightedFileVector &Inputs,
785                               SymbolRemapper *Remapper,
786                               int MaxDbgCorrelationWarnings,
787                               const StringRef ProfiledBinary) {
788   const uint64_t TraceReservoirSize = TemporalProfTraceReservoirSize.getValue();
789   const uint64_t MaxTraceLength = TemporalProfMaxTraceLength.getValue();
790   if (OutputFormat == PF_Compact_Binary)
791     exitWithError("Compact Binary is deprecated");
792   if (OutputFormat != PF_Binary && OutputFormat != PF_Ext_Binary &&
793       OutputFormat != PF_Text)
794     exitWithError("unknown format is specified");
795 
796   // TODO: Maybe we should support correlation with mixture of different
797   // correlation modes(w/wo debug-info/object correlation).
798   if (!DebugInfoFilename.empty() && !BinaryFilename.empty())
799     exitWithError("Expected only one of -debug-info, -binary-file");
800   std::string CorrelateFilename;
801   ProfCorrelatorKind CorrelateKind = ProfCorrelatorKind::NONE;
802   if (!DebugInfoFilename.empty()) {
803     CorrelateFilename = DebugInfoFilename;
804     CorrelateKind = ProfCorrelatorKind::DEBUG_INFO;
805   } else if (!BinaryFilename.empty()) {
806     CorrelateFilename = BinaryFilename;
807     CorrelateKind = ProfCorrelatorKind::BINARY;
808   }
809 
810   std::unique_ptr<InstrProfCorrelator> Correlator;
811   if (CorrelateKind != InstrProfCorrelator::NONE) {
812     if (auto Err = InstrProfCorrelator::get(CorrelateFilename, CorrelateKind)
813                        .moveInto(Correlator))
814       exitWithError(std::move(Err), CorrelateFilename);
815     if (auto Err = Correlator->correlateProfileData(MaxDbgCorrelationWarnings))
816       exitWithError(std::move(Err), CorrelateFilename);
817   }
818 
819   std::mutex ErrorLock;
820   SmallSet<instrprof_error, 4> WriterErrorCodes;
821 
822   // If NumThreads is not specified, auto-detect a good default.
823   if (NumThreads == 0)
824     NumThreads = std::min(hardware_concurrency().compute_thread_count(),
825                           unsigned((Inputs.size() + 1) / 2));
826 
827   // Initialize the writer contexts.
828   SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
829   for (unsigned I = 0; I < NumThreads; ++I)
830     Contexts.emplace_back(std::make_unique<WriterContext>(
831         OutputSparse, ErrorLock, WriterErrorCodes, TraceReservoirSize,
832         MaxTraceLength));
833 
834   if (NumThreads == 1) {
835     for (const auto &Input : Inputs)
836       loadInput(Input, Remapper, Correlator.get(), ProfiledBinary,
837                 Contexts[0].get());
838   } else {
839     ThreadPool Pool(hardware_concurrency(NumThreads));
840 
841     // Load the inputs in parallel (N/NumThreads serial steps).
842     unsigned Ctx = 0;
843     for (const auto &Input : Inputs) {
844       Pool.async(loadInput, Input, Remapper, Correlator.get(), ProfiledBinary,
845                  Contexts[Ctx].get());
846       Ctx = (Ctx + 1) % NumThreads;
847     }
848     Pool.wait();
849 
850     // Merge the writer contexts together (~ lg(NumThreads) serial steps).
851     unsigned Mid = Contexts.size() / 2;
852     unsigned End = Contexts.size();
853     assert(Mid > 0 && "Expected more than one context");
854     do {
855       for (unsigned I = 0; I < Mid; ++I)
856         Pool.async(mergeWriterContexts, Contexts[I].get(),
857                    Contexts[I + Mid].get());
858       Pool.wait();
859       if (End & 1) {
860         Pool.async(mergeWriterContexts, Contexts[0].get(),
861                    Contexts[End - 1].get());
862         Pool.wait();
863       }
864       End = Mid;
865       Mid /= 2;
866     } while (Mid > 0);
867   }
868 
869   // Handle deferred errors encountered during merging. If the number of errors
870   // is equal to the number of inputs the merge failed.
871   unsigned NumErrors = 0;
872   for (std::unique_ptr<WriterContext> &WC : Contexts) {
873     for (auto &ErrorPair : WC->Errors) {
874       ++NumErrors;
875       warn(toString(std::move(ErrorPair.first)), ErrorPair.second);
876     }
877   }
878   if ((NumErrors == Inputs.size() && FailMode == failIfAllAreInvalid) ||
879       (NumErrors > 0 && FailMode == failIfAnyAreInvalid))
880     exitWithError("no profile can be merged");
881 
882   writeInstrProfile(OutputFilename, OutputFormat, Contexts[0]->Writer);
883 }
884 
885 /// The profile entry for a function in instrumentation profile.
886 struct InstrProfileEntry {
887   uint64_t MaxCount = 0;
888   uint64_t NumEdgeCounters = 0;
889   float ZeroCounterRatio = 0.0;
890   InstrProfRecord *ProfRecord;
891   InstrProfileEntry(InstrProfRecord *Record);
892   InstrProfileEntry() = default;
893 };
894 
895 InstrProfileEntry::InstrProfileEntry(InstrProfRecord *Record) {
896   ProfRecord = Record;
897   uint64_t CntNum = Record->Counts.size();
898   uint64_t ZeroCntNum = 0;
899   for (size_t I = 0; I < CntNum; ++I) {
900     MaxCount = std::max(MaxCount, Record->Counts[I]);
901     ZeroCntNum += !Record->Counts[I];
902   }
903   ZeroCounterRatio = (float)ZeroCntNum / CntNum;
904   NumEdgeCounters = CntNum;
905 }
906 
907 /// Either set all the counters in the instr profile entry \p IFE to
908 /// -1 / -2 /in order to drop the profile or scale up the
909 /// counters in \p IFP to be above hot / cold threshold. We use
910 /// the ratio of zero counters in the profile of a function to
911 /// decide the profile is helpful or harmful for performance,
912 /// and to choose whether to scale up or drop it.
913 static void updateInstrProfileEntry(InstrProfileEntry &IFE, bool SetToHot,
914                                     uint64_t HotInstrThreshold,
915                                     uint64_t ColdInstrThreshold,
916                                     float ZeroCounterThreshold) {
917   InstrProfRecord *ProfRecord = IFE.ProfRecord;
918   if (!IFE.MaxCount || IFE.ZeroCounterRatio > ZeroCounterThreshold) {
919     // If all or most of the counters of the function are zero, the
920     // profile is unaccountable and should be dropped. Reset all the
921     // counters to be -1 / -2 and PGO profile-use will drop the profile.
922     // All counters being -1 also implies that the function is hot so
923     // PGO profile-use will also set the entry count metadata to be
924     // above hot threshold.
925     // All counters being -2 implies that the function is warm so
926     // PGO profile-use will also set the entry count metadata to be
927     // above cold threshold.
928     auto Kind =
929         (SetToHot ? InstrProfRecord::PseudoHot : InstrProfRecord::PseudoWarm);
930     ProfRecord->setPseudoCount(Kind);
931     return;
932   }
933 
934   // Scale up the MaxCount to be multiple times above hot / cold threshold.
935   const unsigned MultiplyFactor = 3;
936   uint64_t Threshold = (SetToHot ? HotInstrThreshold : ColdInstrThreshold);
937   uint64_t Numerator = Threshold * MultiplyFactor;
938 
939   // Make sure Threshold for warm counters is below the HotInstrThreshold.
940   if (!SetToHot && Threshold >= HotInstrThreshold) {
941     Threshold = (HotInstrThreshold + ColdInstrThreshold) / 2;
942   }
943 
944   uint64_t Denominator = IFE.MaxCount;
945   if (Numerator <= Denominator)
946     return;
947   ProfRecord->scale(Numerator, Denominator, [&](instrprof_error E) {
948     warn(toString(make_error<InstrProfError>(E)));
949   });
950 }
951 
952 const uint64_t ColdPercentileIdx = 15;
953 const uint64_t HotPercentileIdx = 11;
954 
955 using sampleprof::FSDiscriminatorPass;
956 
957 // Internal options to set FSDiscriminatorPass. Used in merge and show
958 // commands.
959 static cl::opt<FSDiscriminatorPass> FSDiscriminatorPassOption(
960     "fs-discriminator-pass", cl::init(PassLast), cl::Hidden,
961     cl::desc("Zero out the discriminator bits for the FS discrimiantor "
962              "pass beyond this value. The enum values are defined in "
963              "Support/Discriminator.h"),
964     cl::values(clEnumVal(Base, "Use base discriminators only"),
965                clEnumVal(Pass1, "Use base and pass 1 discriminators"),
966                clEnumVal(Pass2, "Use base and pass 1-2 discriminators"),
967                clEnumVal(Pass3, "Use base and pass 1-3 discriminators"),
968                clEnumVal(PassLast, "Use all discriminator bits (default)")));
969 
970 static unsigned getDiscriminatorMask() {
971   return getN1Bits(getFSPassBitEnd(FSDiscriminatorPassOption.getValue()));
972 }
973 
974 /// Adjust the instr profile in \p WC based on the sample profile in
975 /// \p Reader.
976 static void
977 adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
978                    std::unique_ptr<sampleprof::SampleProfileReader> &Reader,
979                    unsigned SupplMinSizeThreshold, float ZeroCounterThreshold,
980                    unsigned InstrProfColdThreshold) {
981   // Function to its entry in instr profile.
982   StringMap<InstrProfileEntry> InstrProfileMap;
983   StringMap<StringRef> StaticFuncMap;
984   InstrProfSummaryBuilder IPBuilder(ProfileSummaryBuilder::DefaultCutoffs);
985 
986   auto checkSampleProfileHasFUnique = [&Reader]() {
987     for (const auto &PD : Reader->getProfiles()) {
988       auto &FContext = PD.second.getContext();
989       if (FContext.toString().find(FunctionSamples::UniqSuffix) !=
990           std::string::npos) {
991         return true;
992       }
993     }
994     return false;
995   };
996 
997   bool SampleProfileHasFUnique = checkSampleProfileHasFUnique();
998 
999   auto buildStaticFuncMap = [&StaticFuncMap,
1000                              SampleProfileHasFUnique](const StringRef Name) {
1001     std::string Prefixes[] = {".cpp:", "cc:", ".c:", ".hpp:", ".h:"};
1002     size_t PrefixPos = StringRef::npos;
1003     for (auto &Prefix : Prefixes) {
1004       PrefixPos = Name.find_insensitive(Prefix);
1005       if (PrefixPos == StringRef::npos)
1006         continue;
1007       PrefixPos += Prefix.size();
1008       break;
1009     }
1010 
1011     if (PrefixPos == StringRef::npos) {
1012       return;
1013     }
1014 
1015     StringRef NewName = Name.drop_front(PrefixPos);
1016     StringRef FName = Name.substr(0, PrefixPos - 1);
1017     if (NewName.size() == 0) {
1018       return;
1019     }
1020 
1021     // This name should have a static linkage.
1022     size_t PostfixPos = NewName.find(FunctionSamples::UniqSuffix);
1023     bool ProfileHasFUnique = (PostfixPos != StringRef::npos);
1024 
1025     // If sample profile and instrumented profile do not agree on symbol
1026     // uniqification.
1027     if (SampleProfileHasFUnique != ProfileHasFUnique) {
1028       // If instrumented profile uses -funique-internal-linkage-symbols,
1029       // we need to trim the name.
1030       if (ProfileHasFUnique) {
1031         NewName = NewName.substr(0, PostfixPos);
1032       } else {
1033         // If sample profile uses -funique-internal-linkage-symbols,
1034         // we build the map.
1035         std::string NStr =
1036             NewName.str() + getUniqueInternalLinkagePostfix(FName);
1037         NewName = StringRef(NStr);
1038         StaticFuncMap[NewName] = Name;
1039         return;
1040       }
1041     }
1042 
1043     if (!StaticFuncMap.contains(NewName)) {
1044       StaticFuncMap[NewName] = Name;
1045     } else {
1046       StaticFuncMap[NewName] = DuplicateNameStr;
1047     }
1048   };
1049 
1050   // We need to flatten the SampleFDO profile as the InstrFDO
1051   // profile does not have inlined callsite profiles.
1052   // One caveat is the pre-inlined function -- their samples
1053   // should be collapsed into the caller function.
1054   // Here we do a DFS traversal to get the flatten profile
1055   // info: the sum of entrycount and the max of maxcount.
1056   // Here is the algorithm:
1057   //   recursive (FS, root_name) {
1058   //      name = FS->getName();
1059   //      get samples for FS;
1060   //      if (InstrProf.find(name) {
1061   //        root_name = name;
1062   //      } else {
1063   //        if (name is in static_func map) {
1064   //          root_name = static_name;
1065   //        }
1066   //      }
1067   //      update the Map entry for root_name;
1068   //      for (subfs: FS) {
1069   //        recursive(subfs, root_name);
1070   //      }
1071   //   }
1072   //
1073   // Here is an example.
1074   //
1075   // SampleProfile:
1076   // foo:12345:1000
1077   // 1: 1000
1078   // 2.1: 1000
1079   // 15: 5000
1080   // 4: bar:1000
1081   //  1: 1000
1082   //  2: goo:3000
1083   //   1: 3000
1084   // 8: bar:40000
1085   //  1: 10000
1086   //  2: goo:30000
1087   //   1: 30000
1088   //
1089   // InstrProfile has two entries:
1090   //  foo
1091   //  bar.cc:bar
1092   //
1093   // After BuildMaxSampleMap, we should have the following in FlattenSampleMap:
1094   // {"foo", {1000, 5000}}
1095   // {"bar.cc:bar", {11000, 30000}}
1096   //
1097   // foo's has an entry count of 1000, and max body count of 5000.
1098   // bar.cc:bar has an entry count of 11000 (sum two callsites of 1000 and
1099   // 10000), and max count of 30000 (from the callsite in line 8).
1100   //
1101   // Note that goo's count will remain in bar.cc:bar() as it does not have an
1102   // entry in InstrProfile.
1103   llvm::StringMap<std::pair<uint64_t, uint64_t>> FlattenSampleMap;
1104   auto BuildMaxSampleMap = [&FlattenSampleMap, &StaticFuncMap,
1105                             &InstrProfileMap](const FunctionSamples &FS,
1106                                               const StringRef &RootName) {
1107     auto BuildMaxSampleMapImpl = [&](const FunctionSamples &FS,
1108                                      const StringRef &RootName,
1109                                      auto &BuildImpl) -> void {
1110       std::string NameStr = FS.getFunction().str();
1111       const StringRef Name = NameStr;
1112       const StringRef *NewRootName = &RootName;
1113       uint64_t EntrySample = FS.getHeadSamplesEstimate();
1114       uint64_t MaxBodySample = FS.getMaxCountInside(/* SkipCallSite*/ true);
1115 
1116       auto It = InstrProfileMap.find(Name);
1117       if (It != InstrProfileMap.end()) {
1118         NewRootName = &Name;
1119       } else {
1120         auto NewName = StaticFuncMap.find(Name);
1121         if (NewName != StaticFuncMap.end()) {
1122           It = InstrProfileMap.find(NewName->second.str());
1123           if (NewName->second != DuplicateNameStr) {
1124             NewRootName = &NewName->second;
1125           }
1126         } else {
1127           // Here the EntrySample is of an inlined function, so we should not
1128           // update the EntrySample in the map.
1129           EntrySample = 0;
1130         }
1131       }
1132       EntrySample += FlattenSampleMap[*NewRootName].first;
1133       MaxBodySample =
1134           std::max(FlattenSampleMap[*NewRootName].second, MaxBodySample);
1135       FlattenSampleMap[*NewRootName] =
1136           std::make_pair(EntrySample, MaxBodySample);
1137 
1138       for (const auto &C : FS.getCallsiteSamples())
1139         for (const auto &F : C.second)
1140           BuildImpl(F.second, *NewRootName, BuildImpl);
1141     };
1142     BuildMaxSampleMapImpl(FS, RootName, BuildMaxSampleMapImpl);
1143   };
1144 
1145   for (auto &PD : WC->Writer.getProfileData()) {
1146     // Populate IPBuilder.
1147     for (const auto &PDV : PD.getValue()) {
1148       InstrProfRecord Record = PDV.second;
1149       IPBuilder.addRecord(Record);
1150     }
1151 
1152     // If a function has multiple entries in instr profile, skip it.
1153     if (PD.getValue().size() != 1)
1154       continue;
1155 
1156     // Initialize InstrProfileMap.
1157     InstrProfRecord *R = &PD.getValue().begin()->second;
1158     StringRef FullName = PD.getKey();
1159     InstrProfileMap[FullName] = InstrProfileEntry(R);
1160     buildStaticFuncMap(FullName);
1161   }
1162 
1163   for (auto &PD : Reader->getProfiles()) {
1164     sampleprof::FunctionSamples &FS = PD.second;
1165     std::string Name = FS.getFunction().str();
1166     BuildMaxSampleMap(FS, Name);
1167   }
1168 
1169   ProfileSummary InstrPS = *IPBuilder.getSummary();
1170   ProfileSummary SamplePS = Reader->getSummary();
1171 
1172   // Compute cold thresholds for instr profile and sample profile.
1173   uint64_t HotSampleThreshold =
1174       ProfileSummaryBuilder::getEntryForPercentile(
1175           SamplePS.getDetailedSummary(),
1176           ProfileSummaryBuilder::DefaultCutoffs[HotPercentileIdx])
1177           .MinCount;
1178   uint64_t ColdSampleThreshold =
1179       ProfileSummaryBuilder::getEntryForPercentile(
1180           SamplePS.getDetailedSummary(),
1181           ProfileSummaryBuilder::DefaultCutoffs[ColdPercentileIdx])
1182           .MinCount;
1183   uint64_t HotInstrThreshold =
1184       ProfileSummaryBuilder::getEntryForPercentile(
1185           InstrPS.getDetailedSummary(),
1186           ProfileSummaryBuilder::DefaultCutoffs[HotPercentileIdx])
1187           .MinCount;
1188   uint64_t ColdInstrThreshold =
1189       InstrProfColdThreshold
1190           ? InstrProfColdThreshold
1191           : ProfileSummaryBuilder::getEntryForPercentile(
1192                 InstrPS.getDetailedSummary(),
1193                 ProfileSummaryBuilder::DefaultCutoffs[ColdPercentileIdx])
1194                 .MinCount;
1195 
1196   // Find hot/warm functions in sample profile which is cold in instr profile
1197   // and adjust the profiles of those functions in the instr profile.
1198   for (const auto &E : FlattenSampleMap) {
1199     uint64_t SampleMaxCount = std::max(E.second.first, E.second.second);
1200     if (SampleMaxCount < ColdSampleThreshold)
1201       continue;
1202     StringRef Name = E.first();
1203     auto It = InstrProfileMap.find(Name);
1204     if (It == InstrProfileMap.end()) {
1205       auto NewName = StaticFuncMap.find(Name);
1206       if (NewName != StaticFuncMap.end()) {
1207         It = InstrProfileMap.find(NewName->second.str());
1208         if (NewName->second == DuplicateNameStr) {
1209           WithColor::warning()
1210               << "Static function " << Name
1211               << " has multiple promoted names, cannot adjust profile.\n";
1212         }
1213       }
1214     }
1215     if (It == InstrProfileMap.end() ||
1216         It->second.MaxCount > ColdInstrThreshold ||
1217         It->second.NumEdgeCounters < SupplMinSizeThreshold)
1218       continue;
1219     bool SetToHot = SampleMaxCount >= HotSampleThreshold;
1220     updateInstrProfileEntry(It->second, SetToHot, HotInstrThreshold,
1221                             ColdInstrThreshold, ZeroCounterThreshold);
1222   }
1223 }
1224 
1225 /// The main function to supplement instr profile with sample profile.
1226 /// \Inputs contains the instr profile. \p SampleFilename specifies the
1227 /// sample profile. \p OutputFilename specifies the output profile name.
1228 /// \p OutputFormat specifies the output profile format. \p OutputSparse
1229 /// specifies whether to generate sparse profile. \p SupplMinSizeThreshold
1230 /// specifies the minimal size for the functions whose profile will be
1231 /// adjusted. \p ZeroCounterThreshold is the threshold to check whether
1232 /// a function contains too many zero counters and whether its profile
1233 /// should be dropped. \p InstrProfColdThreshold is the user specified
1234 /// cold threshold which will override the cold threshold got from the
1235 /// instr profile summary.
1236 static void supplementInstrProfile(const WeightedFileVector &Inputs,
1237                                    StringRef SampleFilename, bool OutputSparse,
1238                                    unsigned SupplMinSizeThreshold,
1239                                    float ZeroCounterThreshold,
1240                                    unsigned InstrProfColdThreshold) {
1241   if (OutputFilename.compare("-") == 0)
1242     exitWithError("cannot write indexed profdata format to stdout");
1243   if (Inputs.size() != 1)
1244     exitWithError("expect one input to be an instr profile");
1245   if (Inputs[0].Weight != 1)
1246     exitWithError("expect instr profile doesn't have weight");
1247 
1248   StringRef InstrFilename = Inputs[0].Filename;
1249 
1250   // Read sample profile.
1251   LLVMContext Context;
1252   auto FS = vfs::getRealFileSystem();
1253   auto ReaderOrErr = sampleprof::SampleProfileReader::create(
1254       SampleFilename.str(), Context, *FS, FSDiscriminatorPassOption);
1255   if (std::error_code EC = ReaderOrErr.getError())
1256     exitWithErrorCode(EC, SampleFilename);
1257   auto Reader = std::move(ReaderOrErr.get());
1258   if (std::error_code EC = Reader->read())
1259     exitWithErrorCode(EC, SampleFilename);
1260 
1261   // Read instr profile.
1262   std::mutex ErrorLock;
1263   SmallSet<instrprof_error, 4> WriterErrorCodes;
1264   auto WC = std::make_unique<WriterContext>(OutputSparse, ErrorLock,
1265                                             WriterErrorCodes);
1266   loadInput(Inputs[0], nullptr, nullptr, /*ProfiledBinary=*/"", WC.get());
1267   if (WC->Errors.size() > 0)
1268     exitWithError(std::move(WC->Errors[0].first), InstrFilename);
1269 
1270   adjustInstrProfile(WC, Reader, SupplMinSizeThreshold, ZeroCounterThreshold,
1271                      InstrProfColdThreshold);
1272   writeInstrProfile(OutputFilename, OutputFormat, WC->Writer);
1273 }
1274 
1275 /// Make a copy of the given function samples with all symbol names remapped
1276 /// by the provided symbol remapper.
1277 static sampleprof::FunctionSamples
1278 remapSamples(const sampleprof::FunctionSamples &Samples,
1279              SymbolRemapper &Remapper, sampleprof_error &Error) {
1280   sampleprof::FunctionSamples Result;
1281   Result.setFunction(Remapper(Samples.getFunction()));
1282   Result.addTotalSamples(Samples.getTotalSamples());
1283   Result.addHeadSamples(Samples.getHeadSamples());
1284   for (const auto &BodySample : Samples.getBodySamples()) {
1285     uint32_t MaskedDiscriminator =
1286         BodySample.first.Discriminator & getDiscriminatorMask();
1287     Result.addBodySamples(BodySample.first.LineOffset, MaskedDiscriminator,
1288                           BodySample.second.getSamples());
1289     for (const auto &Target : BodySample.second.getCallTargets()) {
1290       Result.addCalledTargetSamples(BodySample.first.LineOffset,
1291                                     MaskedDiscriminator,
1292                                     Remapper(Target.first), Target.second);
1293     }
1294   }
1295   for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) {
1296     sampleprof::FunctionSamplesMap &Target =
1297         Result.functionSamplesAt(CallsiteSamples.first);
1298     for (const auto &Callsite : CallsiteSamples.second) {
1299       sampleprof::FunctionSamples Remapped =
1300           remapSamples(Callsite.second, Remapper, Error);
1301       MergeResult(Error, Target[Remapped.getFunction()].merge(Remapped));
1302     }
1303   }
1304   return Result;
1305 }
1306 
1307 static sampleprof::SampleProfileFormat FormatMap[] = {
1308     sampleprof::SPF_None,
1309     sampleprof::SPF_Text,
1310     sampleprof::SPF_None,
1311     sampleprof::SPF_Ext_Binary,
1312     sampleprof::SPF_GCC,
1313     sampleprof::SPF_Binary};
1314 
1315 static std::unique_ptr<MemoryBuffer>
1316 getInputFileBuf(const StringRef &InputFile) {
1317   if (InputFile == "")
1318     return {};
1319 
1320   auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile);
1321   if (!BufOrError)
1322     exitWithErrorCode(BufOrError.getError(), InputFile);
1323 
1324   return std::move(*BufOrError);
1325 }
1326 
1327 static void populateProfileSymbolList(MemoryBuffer *Buffer,
1328                                       sampleprof::ProfileSymbolList &PSL) {
1329   if (!Buffer)
1330     return;
1331 
1332   SmallVector<StringRef, 32> SymbolVec;
1333   StringRef Data = Buffer->getBuffer();
1334   Data.split(SymbolVec, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
1335 
1336   for (StringRef SymbolStr : SymbolVec)
1337     PSL.add(SymbolStr.trim());
1338 }
1339 
1340 static void handleExtBinaryWriter(sampleprof::SampleProfileWriter &Writer,
1341                                   ProfileFormat OutputFormat,
1342                                   MemoryBuffer *Buffer,
1343                                   sampleprof::ProfileSymbolList &WriterList,
1344                                   bool CompressAllSections, bool UseMD5,
1345                                   bool GenPartialProfile) {
1346   populateProfileSymbolList(Buffer, WriterList);
1347   if (WriterList.size() > 0 && OutputFormat != PF_Ext_Binary)
1348     warn("Profile Symbol list is not empty but the output format is not "
1349          "ExtBinary format. The list will be lost in the output. ");
1350 
1351   Writer.setProfileSymbolList(&WriterList);
1352 
1353   if (CompressAllSections) {
1354     if (OutputFormat != PF_Ext_Binary)
1355       warn("-compress-all-section is ignored. Specify -extbinary to enable it");
1356     else
1357       Writer.setToCompressAllSections();
1358   }
1359   if (UseMD5) {
1360     if (OutputFormat != PF_Ext_Binary)
1361       warn("-use-md5 is ignored. Specify -extbinary to enable it");
1362     else
1363       Writer.setUseMD5();
1364   }
1365   if (GenPartialProfile) {
1366     if (OutputFormat != PF_Ext_Binary)
1367       warn("-gen-partial-profile is ignored. Specify -extbinary to enable it");
1368     else
1369       Writer.setPartialProfile();
1370   }
1371 }
1372 
1373 static void mergeSampleProfile(const WeightedFileVector &Inputs,
1374                                SymbolRemapper *Remapper,
1375                                StringRef ProfileSymbolListFile,
1376                                size_t OutputSizeLimit) {
1377   using namespace sampleprof;
1378   SampleProfileMap ProfileMap;
1379   SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
1380   LLVMContext Context;
1381   sampleprof::ProfileSymbolList WriterList;
1382   std::optional<bool> ProfileIsProbeBased;
1383   std::optional<bool> ProfileIsCS;
1384   for (const auto &Input : Inputs) {
1385     auto FS = vfs::getRealFileSystem();
1386     auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context, *FS,
1387                                                    FSDiscriminatorPassOption);
1388     if (std::error_code EC = ReaderOrErr.getError()) {
1389       warnOrExitGivenError(FailMode, EC, Input.Filename);
1390       continue;
1391     }
1392 
1393     // We need to keep the readers around until after all the files are
1394     // read so that we do not lose the function names stored in each
1395     // reader's memory. The function names are needed to write out the
1396     // merged profile map.
1397     Readers.push_back(std::move(ReaderOrErr.get()));
1398     const auto Reader = Readers.back().get();
1399     if (std::error_code EC = Reader->read()) {
1400       warnOrExitGivenError(FailMode, EC, Input.Filename);
1401       Readers.pop_back();
1402       continue;
1403     }
1404 
1405     SampleProfileMap &Profiles = Reader->getProfiles();
1406     if (ProfileIsProbeBased &&
1407         ProfileIsProbeBased != FunctionSamples::ProfileIsProbeBased)
1408       exitWithError(
1409           "cannot merge probe-based profile with non-probe-based profile");
1410     ProfileIsProbeBased = FunctionSamples::ProfileIsProbeBased;
1411     if (ProfileIsCS && ProfileIsCS != FunctionSamples::ProfileIsCS)
1412       exitWithError("cannot merge CS profile with non-CS profile");
1413     ProfileIsCS = FunctionSamples::ProfileIsCS;
1414     for (SampleProfileMap::iterator I = Profiles.begin(), E = Profiles.end();
1415          I != E; ++I) {
1416       sampleprof_error Result = sampleprof_error::success;
1417       FunctionSamples Remapped =
1418           Remapper ? remapSamples(I->second, *Remapper, Result)
1419                    : FunctionSamples();
1420       FunctionSamples &Samples = Remapper ? Remapped : I->second;
1421       SampleContext FContext = Samples.getContext();
1422       MergeResult(Result, ProfileMap[FContext].merge(Samples, Input.Weight));
1423       if (Result != sampleprof_error::success) {
1424         std::error_code EC = make_error_code(Result);
1425         handleMergeWriterError(errorCodeToError(EC), Input.Filename,
1426                                FContext.toString());
1427       }
1428     }
1429 
1430     if (!DropProfileSymbolList) {
1431       std::unique_ptr<sampleprof::ProfileSymbolList> ReaderList =
1432           Reader->getProfileSymbolList();
1433       if (ReaderList)
1434         WriterList.merge(*ReaderList);
1435     }
1436   }
1437 
1438   if (ProfileIsCS && (SampleMergeColdContext || SampleTrimColdContext)) {
1439     // Use threshold calculated from profile summary unless specified.
1440     SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
1441     auto Summary = Builder.computeSummaryForProfiles(ProfileMap);
1442     uint64_t SampleProfColdThreshold =
1443         ProfileSummaryBuilder::getColdCountThreshold(
1444             (Summary->getDetailedSummary()));
1445 
1446     // Trim and merge cold context profile using cold threshold above;
1447     SampleContextTrimmer(ProfileMap)
1448         .trimAndMergeColdContextProfiles(
1449             SampleProfColdThreshold, SampleTrimColdContext,
1450             SampleMergeColdContext, SampleColdContextFrameDepth, false);
1451   }
1452 
1453   if (ProfileLayout == llvm::sampleprof::SPL_Flat) {
1454     ProfileConverter::flattenProfile(ProfileMap, FunctionSamples::ProfileIsCS);
1455     ProfileIsCS = FunctionSamples::ProfileIsCS = false;
1456   } else if (ProfileIsCS && ProfileLayout == llvm::sampleprof::SPL_Nest) {
1457     ProfileConverter CSConverter(ProfileMap);
1458     CSConverter.convertCSProfiles();
1459     ProfileIsCS = FunctionSamples::ProfileIsCS = false;
1460   }
1461 
1462   auto WriterOrErr =
1463       SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
1464   if (std::error_code EC = WriterOrErr.getError())
1465     exitWithErrorCode(EC, OutputFilename);
1466 
1467   auto Writer = std::move(WriterOrErr.get());
1468   // WriterList will have StringRef refering to string in Buffer.
1469   // Make sure Buffer lives as long as WriterList.
1470   auto Buffer = getInputFileBuf(ProfileSymbolListFile);
1471   handleExtBinaryWriter(*Writer, OutputFormat, Buffer.get(), WriterList,
1472                         CompressAllSections, UseMD5, GenPartialProfile);
1473 
1474   // If OutputSizeLimit is 0 (default), it is the same as write().
1475   if (std::error_code EC =
1476           Writer->writeWithSizeLimit(ProfileMap, OutputSizeLimit))
1477     exitWithErrorCode(std::move(EC));
1478 }
1479 
1480 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
1481   StringRef WeightStr, FileName;
1482   std::tie(WeightStr, FileName) = WeightedFilename.split(',');
1483 
1484   uint64_t Weight;
1485   if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
1486     exitWithError("input weight must be a positive integer");
1487 
1488   return {std::string(FileName), Weight};
1489 }
1490 
1491 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
1492   StringRef Filename = WF.Filename;
1493   uint64_t Weight = WF.Weight;
1494 
1495   // If it's STDIN just pass it on.
1496   if (Filename == "-") {
1497     WNI.push_back({std::string(Filename), Weight});
1498     return;
1499   }
1500 
1501   llvm::sys::fs::file_status Status;
1502   llvm::sys::fs::status(Filename, Status);
1503   if (!llvm::sys::fs::exists(Status))
1504     exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
1505                       Filename);
1506   // If it's a source file, collect it.
1507   if (llvm::sys::fs::is_regular_file(Status)) {
1508     WNI.push_back({std::string(Filename), Weight});
1509     return;
1510   }
1511 
1512   if (llvm::sys::fs::is_directory(Status)) {
1513     std::error_code EC;
1514     for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
1515          F != E && !EC; F.increment(EC)) {
1516       if (llvm::sys::fs::is_regular_file(F->path())) {
1517         addWeightedInput(WNI, {F->path(), Weight});
1518       }
1519     }
1520     if (EC)
1521       exitWithErrorCode(EC, Filename);
1522   }
1523 }
1524 
1525 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
1526                                     WeightedFileVector &WFV) {
1527   if (!Buffer)
1528     return;
1529 
1530   SmallVector<StringRef, 8> Entries;
1531   StringRef Data = Buffer->getBuffer();
1532   Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
1533   for (const StringRef &FileWeightEntry : Entries) {
1534     StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
1535     // Skip comments.
1536     if (SanitizedEntry.starts_with("#"))
1537       continue;
1538     // If there's no comma, it's an unweighted profile.
1539     else if (!SanitizedEntry.contains(','))
1540       addWeightedInput(WFV, {std::string(SanitizedEntry), 1});
1541     else
1542       addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
1543   }
1544 }
1545 
1546 static int merge_main(int argc, const char *argv[]) {
1547   WeightedFileVector WeightedInputs;
1548   for (StringRef Filename : InputFilenames)
1549     addWeightedInput(WeightedInputs, {std::string(Filename), 1});
1550   for (StringRef WeightedFilename : WeightedInputFilenames)
1551     addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
1552 
1553   // Make sure that the file buffer stays alive for the duration of the
1554   // weighted input vector's lifetime.
1555   auto Buffer = getInputFileBuf(InputFilenamesFile);
1556   parseInputFilenamesFile(Buffer.get(), WeightedInputs);
1557 
1558   if (WeightedInputs.empty())
1559     exitWithError("no input files specified. See " +
1560                   sys::path::filename(argv[0]) + " " + argv[1] + " -help");
1561 
1562   if (DumpInputFileList) {
1563     for (auto &WF : WeightedInputs)
1564       outs() << WF.Weight << "," << WF.Filename << "\n";
1565     return 0;
1566   }
1567 
1568   std::unique_ptr<SymbolRemapper> Remapper;
1569   if (!RemappingFile.empty())
1570     Remapper = SymbolRemapper::create(RemappingFile);
1571 
1572   if (!SupplInstrWithSample.empty()) {
1573     if (ProfileKind != instr)
1574       exitWithError(
1575           "-supplement-instr-with-sample can only work with -instr. ");
1576 
1577     supplementInstrProfile(WeightedInputs, SupplInstrWithSample, OutputSparse,
1578                            SupplMinSizeThreshold, ZeroCounterThreshold,
1579                            InstrProfColdThreshold);
1580     return 0;
1581   }
1582 
1583   if (ProfileKind == instr)
1584     mergeInstrProfile(WeightedInputs, Remapper.get(), MaxDbgCorrelationWarnings,
1585                       ProfiledBinary);
1586   else
1587     mergeSampleProfile(WeightedInputs, Remapper.get(), ProfileSymbolListFile,
1588                        OutputSizeLimit);
1589   return 0;
1590 }
1591 
1592 /// Computer the overlap b/w profile BaseFilename and profile TestFilename.
1593 static void overlapInstrProfile(const std::string &BaseFilename,
1594                                 const std::string &TestFilename,
1595                                 const OverlapFuncFilters &FuncFilter,
1596                                 raw_fd_ostream &OS, bool IsCS) {
1597   std::mutex ErrorLock;
1598   SmallSet<instrprof_error, 4> WriterErrorCodes;
1599   WriterContext Context(false, ErrorLock, WriterErrorCodes);
1600   WeightedFile WeightedInput{BaseFilename, 1};
1601   OverlapStats Overlap;
1602   Error E = Overlap.accumulateCounts(BaseFilename, TestFilename, IsCS);
1603   if (E)
1604     exitWithError(std::move(E), "error in getting profile count sums");
1605   if (Overlap.Base.CountSum < 1.0f) {
1606     OS << "Sum of edge counts for profile " << BaseFilename << " is 0.\n";
1607     exit(0);
1608   }
1609   if (Overlap.Test.CountSum < 1.0f) {
1610     OS << "Sum of edge counts for profile " << TestFilename << " is 0.\n";
1611     exit(0);
1612   }
1613   loadInput(WeightedInput, nullptr, nullptr, /*ProfiledBinary=*/"", &Context);
1614   overlapInput(BaseFilename, TestFilename, &Context, Overlap, FuncFilter, OS,
1615                IsCS);
1616   Overlap.dump(OS);
1617 }
1618 
1619 namespace {
1620 struct SampleOverlapStats {
1621   SampleContext BaseName;
1622   SampleContext TestName;
1623   // Number of overlap units
1624   uint64_t OverlapCount = 0;
1625   // Total samples of overlap units
1626   uint64_t OverlapSample = 0;
1627   // Number of and total samples of units that only present in base or test
1628   // profile
1629   uint64_t BaseUniqueCount = 0;
1630   uint64_t BaseUniqueSample = 0;
1631   uint64_t TestUniqueCount = 0;
1632   uint64_t TestUniqueSample = 0;
1633   // Number of units and total samples in base or test profile
1634   uint64_t BaseCount = 0;
1635   uint64_t BaseSample = 0;
1636   uint64_t TestCount = 0;
1637   uint64_t TestSample = 0;
1638   // Number of and total samples of units that present in at least one profile
1639   uint64_t UnionCount = 0;
1640   uint64_t UnionSample = 0;
1641   // Weighted similarity
1642   double Similarity = 0.0;
1643   // For SampleOverlapStats instances representing functions, weights of the
1644   // function in base and test profiles
1645   double BaseWeight = 0.0;
1646   double TestWeight = 0.0;
1647 
1648   SampleOverlapStats() = default;
1649 };
1650 } // end anonymous namespace
1651 
1652 namespace {
1653 struct FuncSampleStats {
1654   uint64_t SampleSum;
1655   uint64_t MaxSample;
1656   uint64_t HotBlockCount;
1657   FuncSampleStats() : SampleSum(0), MaxSample(0), HotBlockCount(0) {}
1658   FuncSampleStats(uint64_t SampleSum, uint64_t MaxSample,
1659                   uint64_t HotBlockCount)
1660       : SampleSum(SampleSum), MaxSample(MaxSample),
1661         HotBlockCount(HotBlockCount) {}
1662 };
1663 } // end anonymous namespace
1664 
1665 namespace {
1666 enum MatchStatus { MS_Match, MS_FirstUnique, MS_SecondUnique, MS_None };
1667 
1668 // Class for updating merging steps for two sorted maps. The class should be
1669 // instantiated with a map iterator type.
1670 template <class T> class MatchStep {
1671 public:
1672   MatchStep() = delete;
1673 
1674   MatchStep(T FirstIter, T FirstEnd, T SecondIter, T SecondEnd)
1675       : FirstIter(FirstIter), FirstEnd(FirstEnd), SecondIter(SecondIter),
1676         SecondEnd(SecondEnd), Status(MS_None) {}
1677 
1678   bool areBothFinished() const {
1679     return (FirstIter == FirstEnd && SecondIter == SecondEnd);
1680   }
1681 
1682   bool isFirstFinished() const { return FirstIter == FirstEnd; }
1683 
1684   bool isSecondFinished() const { return SecondIter == SecondEnd; }
1685 
1686   /// Advance one step based on the previous match status unless the previous
1687   /// status is MS_None. Then update Status based on the comparison between two
1688   /// container iterators at the current step. If the previous status is
1689   /// MS_None, it means two iterators are at the beginning and no comparison has
1690   /// been made, so we simply update Status without advancing the iterators.
1691   void updateOneStep();
1692 
1693   T getFirstIter() const { return FirstIter; }
1694 
1695   T getSecondIter() const { return SecondIter; }
1696 
1697   MatchStatus getMatchStatus() const { return Status; }
1698 
1699 private:
1700   // Current iterator and end iterator of the first container.
1701   T FirstIter;
1702   T FirstEnd;
1703   // Current iterator and end iterator of the second container.
1704   T SecondIter;
1705   T SecondEnd;
1706   // Match status of the current step.
1707   MatchStatus Status;
1708 };
1709 } // end anonymous namespace
1710 
1711 template <class T> void MatchStep<T>::updateOneStep() {
1712   switch (Status) {
1713   case MS_Match:
1714     ++FirstIter;
1715     ++SecondIter;
1716     break;
1717   case MS_FirstUnique:
1718     ++FirstIter;
1719     break;
1720   case MS_SecondUnique:
1721     ++SecondIter;
1722     break;
1723   case MS_None:
1724     break;
1725   }
1726 
1727   // Update Status according to iterators at the current step.
1728   if (areBothFinished())
1729     return;
1730   if (FirstIter != FirstEnd &&
1731       (SecondIter == SecondEnd || FirstIter->first < SecondIter->first))
1732     Status = MS_FirstUnique;
1733   else if (SecondIter != SecondEnd &&
1734            (FirstIter == FirstEnd || SecondIter->first < FirstIter->first))
1735     Status = MS_SecondUnique;
1736   else
1737     Status = MS_Match;
1738 }
1739 
1740 // Return the sum of line/block samples, the max line/block sample, and the
1741 // number of line/block samples above the given threshold in a function
1742 // including its inlinees.
1743 static void getFuncSampleStats(const sampleprof::FunctionSamples &Func,
1744                                FuncSampleStats &FuncStats,
1745                                uint64_t HotThreshold) {
1746   for (const auto &L : Func.getBodySamples()) {
1747     uint64_t Sample = L.second.getSamples();
1748     FuncStats.SampleSum += Sample;
1749     FuncStats.MaxSample = std::max(FuncStats.MaxSample, Sample);
1750     if (Sample >= HotThreshold)
1751       ++FuncStats.HotBlockCount;
1752   }
1753 
1754   for (const auto &C : Func.getCallsiteSamples()) {
1755     for (const auto &F : C.second)
1756       getFuncSampleStats(F.second, FuncStats, HotThreshold);
1757   }
1758 }
1759 
1760 /// Predicate that determines if a function is hot with a given threshold. We
1761 /// keep it separate from its callsites for possible extension in the future.
1762 static bool isFunctionHot(const FuncSampleStats &FuncStats,
1763                           uint64_t HotThreshold) {
1764   // We intentionally compare the maximum sample count in a function with the
1765   // HotThreshold to get an approximate determination on hot functions.
1766   return (FuncStats.MaxSample >= HotThreshold);
1767 }
1768 
1769 namespace {
1770 class SampleOverlapAggregator {
1771 public:
1772   SampleOverlapAggregator(const std::string &BaseFilename,
1773                           const std::string &TestFilename,
1774                           double LowSimilarityThreshold, double Epsilon,
1775                           const OverlapFuncFilters &FuncFilter)
1776       : BaseFilename(BaseFilename), TestFilename(TestFilename),
1777         LowSimilarityThreshold(LowSimilarityThreshold), Epsilon(Epsilon),
1778         FuncFilter(FuncFilter) {}
1779 
1780   /// Detect 0-sample input profile and report to output stream. This interface
1781   /// should be called after loadProfiles().
1782   bool detectZeroSampleProfile(raw_fd_ostream &OS) const;
1783 
1784   /// Write out function-level similarity statistics for functions specified by
1785   /// options --function, --value-cutoff, and --similarity-cutoff.
1786   void dumpFuncSimilarity(raw_fd_ostream &OS) const;
1787 
1788   /// Write out program-level similarity and overlap statistics.
1789   void dumpProgramSummary(raw_fd_ostream &OS) const;
1790 
1791   /// Write out hot-function and hot-block statistics for base_profile,
1792   /// test_profile, and their overlap. For both cases, the overlap HO is
1793   /// calculated as follows:
1794   ///    Given the number of functions (or blocks) that are hot in both profiles
1795   ///    HCommon and the number of functions (or blocks) that are hot in at
1796   ///    least one profile HUnion, HO = HCommon / HUnion.
1797   void dumpHotFuncAndBlockOverlap(raw_fd_ostream &OS) const;
1798 
1799   /// This function tries matching functions in base and test profiles. For each
1800   /// pair of matched functions, it aggregates the function-level
1801   /// similarity into a profile-level similarity. It also dump function-level
1802   /// similarity information of functions specified by --function,
1803   /// --value-cutoff, and --similarity-cutoff options. The program-level
1804   /// similarity PS is computed as follows:
1805   ///     Given function-level similarity FS(A) for all function A, the
1806   ///     weight of function A in base profile WB(A), and the weight of function
1807   ///     A in test profile WT(A), compute PS(base_profile, test_profile) =
1808   ///     sum_A(FS(A) * avg(WB(A), WT(A))) ranging in [0.0f to 1.0f] with 0.0
1809   ///     meaning no-overlap.
1810   void computeSampleProfileOverlap(raw_fd_ostream &OS);
1811 
1812   /// Initialize ProfOverlap with the sum of samples in base and test
1813   /// profiles. This function also computes and keeps the sum of samples and
1814   /// max sample counts of each function in BaseStats and TestStats for later
1815   /// use to avoid re-computations.
1816   void initializeSampleProfileOverlap();
1817 
1818   /// Load profiles specified by BaseFilename and TestFilename.
1819   std::error_code loadProfiles();
1820 
1821   using FuncSampleStatsMap =
1822       std::unordered_map<SampleContext, FuncSampleStats, SampleContext::Hash>;
1823 
1824 private:
1825   SampleOverlapStats ProfOverlap;
1826   SampleOverlapStats HotFuncOverlap;
1827   SampleOverlapStats HotBlockOverlap;
1828   std::string BaseFilename;
1829   std::string TestFilename;
1830   std::unique_ptr<sampleprof::SampleProfileReader> BaseReader;
1831   std::unique_ptr<sampleprof::SampleProfileReader> TestReader;
1832   // BaseStats and TestStats hold FuncSampleStats for each function, with
1833   // function name as the key.
1834   FuncSampleStatsMap BaseStats;
1835   FuncSampleStatsMap TestStats;
1836   // Low similarity threshold in floating point number
1837   double LowSimilarityThreshold;
1838   // Block samples above BaseHotThreshold or TestHotThreshold are considered hot
1839   // for tracking hot blocks.
1840   uint64_t BaseHotThreshold;
1841   uint64_t TestHotThreshold;
1842   // A small threshold used to round the results of floating point accumulations
1843   // to resolve imprecision.
1844   const double Epsilon;
1845   std::multimap<double, SampleOverlapStats, std::greater<double>>
1846       FuncSimilarityDump;
1847   // FuncFilter carries specifications in options --value-cutoff and
1848   // --function.
1849   OverlapFuncFilters FuncFilter;
1850   // Column offsets for printing the function-level details table.
1851   static const unsigned int TestWeightCol = 15;
1852   static const unsigned int SimilarityCol = 30;
1853   static const unsigned int OverlapCol = 43;
1854   static const unsigned int BaseUniqueCol = 53;
1855   static const unsigned int TestUniqueCol = 67;
1856   static const unsigned int BaseSampleCol = 81;
1857   static const unsigned int TestSampleCol = 96;
1858   static const unsigned int FuncNameCol = 111;
1859 
1860   /// Return a similarity of two line/block sample counters in the same
1861   /// function in base and test profiles. The line/block-similarity BS(i) is
1862   /// computed as follows:
1863   ///    For an offsets i, given the sample count at i in base profile BB(i),
1864   ///    the sample count at i in test profile BT(i), the sum of sample counts
1865   ///    in this function in base profile SB, and the sum of sample counts in
1866   ///    this function in test profile ST, compute BS(i) = 1.0 - fabs(BB(i)/SB -
1867   ///    BT(i)/ST), ranging in [0.0f to 1.0f] with 0.0 meaning no-overlap.
1868   double computeBlockSimilarity(uint64_t BaseSample, uint64_t TestSample,
1869                                 const SampleOverlapStats &FuncOverlap) const;
1870 
1871   void updateHotBlockOverlap(uint64_t BaseSample, uint64_t TestSample,
1872                              uint64_t HotBlockCount);
1873 
1874   void getHotFunctions(const FuncSampleStatsMap &ProfStats,
1875                        FuncSampleStatsMap &HotFunc,
1876                        uint64_t HotThreshold) const;
1877 
1878   void computeHotFuncOverlap();
1879 
1880   /// This function updates statistics in FuncOverlap, HotBlockOverlap, and
1881   /// Difference for two sample units in a matched function according to the
1882   /// given match status.
1883   void updateOverlapStatsForFunction(uint64_t BaseSample, uint64_t TestSample,
1884                                      uint64_t HotBlockCount,
1885                                      SampleOverlapStats &FuncOverlap,
1886                                      double &Difference, MatchStatus Status);
1887 
1888   /// This function updates statistics in FuncOverlap, HotBlockOverlap, and
1889   /// Difference for unmatched callees that only present in one profile in a
1890   /// matched caller function.
1891   void updateForUnmatchedCallee(const sampleprof::FunctionSamples &Func,
1892                                 SampleOverlapStats &FuncOverlap,
1893                                 double &Difference, MatchStatus Status);
1894 
1895   /// This function updates sample overlap statistics of an overlap function in
1896   /// base and test profile. It also calculates a function-internal similarity
1897   /// FIS as follows:
1898   ///    For offsets i that have samples in at least one profile in this
1899   ///    function A, given BS(i) returned by computeBlockSimilarity(), compute
1900   ///    FIS(A) = (2.0 - sum_i(1.0 - BS(i))) / 2, ranging in [0.0f to 1.0f] with
1901   ///    0.0 meaning no overlap.
1902   double computeSampleFunctionInternalOverlap(
1903       const sampleprof::FunctionSamples &BaseFunc,
1904       const sampleprof::FunctionSamples &TestFunc,
1905       SampleOverlapStats &FuncOverlap);
1906 
1907   /// Function-level similarity (FS) is a weighted value over function internal
1908   /// similarity (FIS). This function computes a function's FS from its FIS by
1909   /// applying the weight.
1910   double weightForFuncSimilarity(double FuncSimilarity, uint64_t BaseFuncSample,
1911                                  uint64_t TestFuncSample) const;
1912 
1913   /// The function-level similarity FS(A) for a function A is computed as
1914   /// follows:
1915   ///     Compute a function-internal similarity FIS(A) by
1916   ///     computeSampleFunctionInternalOverlap(). Then, with the weight of
1917   ///     function A in base profile WB(A), and the weight of function A in test
1918   ///     profile WT(A), compute FS(A) = FIS(A) * (1.0 - fabs(WB(A) - WT(A)))
1919   ///     ranging in [0.0f to 1.0f] with 0.0 meaning no overlap.
1920   double
1921   computeSampleFunctionOverlap(const sampleprof::FunctionSamples *BaseFunc,
1922                                const sampleprof::FunctionSamples *TestFunc,
1923                                SampleOverlapStats *FuncOverlap,
1924                                uint64_t BaseFuncSample,
1925                                uint64_t TestFuncSample);
1926 
1927   /// Profile-level similarity (PS) is a weighted aggregate over function-level
1928   /// similarities (FS). This method weights the FS value by the function
1929   /// weights in the base and test profiles for the aggregation.
1930   double weightByImportance(double FuncSimilarity, uint64_t BaseFuncSample,
1931                             uint64_t TestFuncSample) const;
1932 };
1933 } // end anonymous namespace
1934 
1935 bool SampleOverlapAggregator::detectZeroSampleProfile(
1936     raw_fd_ostream &OS) const {
1937   bool HaveZeroSample = false;
1938   if (ProfOverlap.BaseSample == 0) {
1939     OS << "Sum of sample counts for profile " << BaseFilename << " is 0.\n";
1940     HaveZeroSample = true;
1941   }
1942   if (ProfOverlap.TestSample == 0) {
1943     OS << "Sum of sample counts for profile " << TestFilename << " is 0.\n";
1944     HaveZeroSample = true;
1945   }
1946   return HaveZeroSample;
1947 }
1948 
1949 double SampleOverlapAggregator::computeBlockSimilarity(
1950     uint64_t BaseSample, uint64_t TestSample,
1951     const SampleOverlapStats &FuncOverlap) const {
1952   double BaseFrac = 0.0;
1953   double TestFrac = 0.0;
1954   if (FuncOverlap.BaseSample > 0)
1955     BaseFrac = static_cast<double>(BaseSample) / FuncOverlap.BaseSample;
1956   if (FuncOverlap.TestSample > 0)
1957     TestFrac = static_cast<double>(TestSample) / FuncOverlap.TestSample;
1958   return 1.0 - std::fabs(BaseFrac - TestFrac);
1959 }
1960 
1961 void SampleOverlapAggregator::updateHotBlockOverlap(uint64_t BaseSample,
1962                                                     uint64_t TestSample,
1963                                                     uint64_t HotBlockCount) {
1964   bool IsBaseHot = (BaseSample >= BaseHotThreshold);
1965   bool IsTestHot = (TestSample >= TestHotThreshold);
1966   if (!IsBaseHot && !IsTestHot)
1967     return;
1968 
1969   HotBlockOverlap.UnionCount += HotBlockCount;
1970   if (IsBaseHot)
1971     HotBlockOverlap.BaseCount += HotBlockCount;
1972   if (IsTestHot)
1973     HotBlockOverlap.TestCount += HotBlockCount;
1974   if (IsBaseHot && IsTestHot)
1975     HotBlockOverlap.OverlapCount += HotBlockCount;
1976 }
1977 
1978 void SampleOverlapAggregator::getHotFunctions(
1979     const FuncSampleStatsMap &ProfStats, FuncSampleStatsMap &HotFunc,
1980     uint64_t HotThreshold) const {
1981   for (const auto &F : ProfStats) {
1982     if (isFunctionHot(F.second, HotThreshold))
1983       HotFunc.emplace(F.first, F.second);
1984   }
1985 }
1986 
1987 void SampleOverlapAggregator::computeHotFuncOverlap() {
1988   FuncSampleStatsMap BaseHotFunc;
1989   getHotFunctions(BaseStats, BaseHotFunc, BaseHotThreshold);
1990   HotFuncOverlap.BaseCount = BaseHotFunc.size();
1991 
1992   FuncSampleStatsMap TestHotFunc;
1993   getHotFunctions(TestStats, TestHotFunc, TestHotThreshold);
1994   HotFuncOverlap.TestCount = TestHotFunc.size();
1995   HotFuncOverlap.UnionCount = HotFuncOverlap.TestCount;
1996 
1997   for (const auto &F : BaseHotFunc) {
1998     if (TestHotFunc.count(F.first))
1999       ++HotFuncOverlap.OverlapCount;
2000     else
2001       ++HotFuncOverlap.UnionCount;
2002   }
2003 }
2004 
2005 void SampleOverlapAggregator::updateOverlapStatsForFunction(
2006     uint64_t BaseSample, uint64_t TestSample, uint64_t HotBlockCount,
2007     SampleOverlapStats &FuncOverlap, double &Difference, MatchStatus Status) {
2008   assert(Status != MS_None &&
2009          "Match status should be updated before updating overlap statistics");
2010   if (Status == MS_FirstUnique) {
2011     TestSample = 0;
2012     FuncOverlap.BaseUniqueSample += BaseSample;
2013   } else if (Status == MS_SecondUnique) {
2014     BaseSample = 0;
2015     FuncOverlap.TestUniqueSample += TestSample;
2016   } else {
2017     ++FuncOverlap.OverlapCount;
2018   }
2019 
2020   FuncOverlap.UnionSample += std::max(BaseSample, TestSample);
2021   FuncOverlap.OverlapSample += std::min(BaseSample, TestSample);
2022   Difference +=
2023       1.0 - computeBlockSimilarity(BaseSample, TestSample, FuncOverlap);
2024   updateHotBlockOverlap(BaseSample, TestSample, HotBlockCount);
2025 }
2026 
2027 void SampleOverlapAggregator::updateForUnmatchedCallee(
2028     const sampleprof::FunctionSamples &Func, SampleOverlapStats &FuncOverlap,
2029     double &Difference, MatchStatus Status) {
2030   assert((Status == MS_FirstUnique || Status == MS_SecondUnique) &&
2031          "Status must be either of the two unmatched cases");
2032   FuncSampleStats FuncStats;
2033   if (Status == MS_FirstUnique) {
2034     getFuncSampleStats(Func, FuncStats, BaseHotThreshold);
2035     updateOverlapStatsForFunction(FuncStats.SampleSum, 0,
2036                                   FuncStats.HotBlockCount, FuncOverlap,
2037                                   Difference, Status);
2038   } else {
2039     getFuncSampleStats(Func, FuncStats, TestHotThreshold);
2040     updateOverlapStatsForFunction(0, FuncStats.SampleSum,
2041                                   FuncStats.HotBlockCount, FuncOverlap,
2042                                   Difference, Status);
2043   }
2044 }
2045 
2046 double SampleOverlapAggregator::computeSampleFunctionInternalOverlap(
2047     const sampleprof::FunctionSamples &BaseFunc,
2048     const sampleprof::FunctionSamples &TestFunc,
2049     SampleOverlapStats &FuncOverlap) {
2050 
2051   using namespace sampleprof;
2052 
2053   double Difference = 0;
2054 
2055   // Accumulate Difference for regular line/block samples in the function.
2056   // We match them through sort-merge join algorithm because
2057   // FunctionSamples::getBodySamples() returns a map of sample counters ordered
2058   // by their offsets.
2059   MatchStep<BodySampleMap::const_iterator> BlockIterStep(
2060       BaseFunc.getBodySamples().cbegin(), BaseFunc.getBodySamples().cend(),
2061       TestFunc.getBodySamples().cbegin(), TestFunc.getBodySamples().cend());
2062   BlockIterStep.updateOneStep();
2063   while (!BlockIterStep.areBothFinished()) {
2064     uint64_t BaseSample =
2065         BlockIterStep.isFirstFinished()
2066             ? 0
2067             : BlockIterStep.getFirstIter()->second.getSamples();
2068     uint64_t TestSample =
2069         BlockIterStep.isSecondFinished()
2070             ? 0
2071             : BlockIterStep.getSecondIter()->second.getSamples();
2072     updateOverlapStatsForFunction(BaseSample, TestSample, 1, FuncOverlap,
2073                                   Difference, BlockIterStep.getMatchStatus());
2074 
2075     BlockIterStep.updateOneStep();
2076   }
2077 
2078   // Accumulate Difference for callsite lines in the function. We match
2079   // them through sort-merge algorithm because
2080   // FunctionSamples::getCallsiteSamples() returns a map of callsite records
2081   // ordered by their offsets.
2082   MatchStep<CallsiteSampleMap::const_iterator> CallsiteIterStep(
2083       BaseFunc.getCallsiteSamples().cbegin(),
2084       BaseFunc.getCallsiteSamples().cend(),
2085       TestFunc.getCallsiteSamples().cbegin(),
2086       TestFunc.getCallsiteSamples().cend());
2087   CallsiteIterStep.updateOneStep();
2088   while (!CallsiteIterStep.areBothFinished()) {
2089     MatchStatus CallsiteStepStatus = CallsiteIterStep.getMatchStatus();
2090     assert(CallsiteStepStatus != MS_None &&
2091            "Match status should be updated before entering loop body");
2092 
2093     if (CallsiteStepStatus != MS_Match) {
2094       auto Callsite = (CallsiteStepStatus == MS_FirstUnique)
2095                           ? CallsiteIterStep.getFirstIter()
2096                           : CallsiteIterStep.getSecondIter();
2097       for (const auto &F : Callsite->second)
2098         updateForUnmatchedCallee(F.second, FuncOverlap, Difference,
2099                                  CallsiteStepStatus);
2100     } else {
2101       // There may be multiple inlinees at the same offset, so we need to try
2102       // matching all of them. This match is implemented through sort-merge
2103       // algorithm because callsite records at the same offset are ordered by
2104       // function names.
2105       MatchStep<FunctionSamplesMap::const_iterator> CalleeIterStep(
2106           CallsiteIterStep.getFirstIter()->second.cbegin(),
2107           CallsiteIterStep.getFirstIter()->second.cend(),
2108           CallsiteIterStep.getSecondIter()->second.cbegin(),
2109           CallsiteIterStep.getSecondIter()->second.cend());
2110       CalleeIterStep.updateOneStep();
2111       while (!CalleeIterStep.areBothFinished()) {
2112         MatchStatus CalleeStepStatus = CalleeIterStep.getMatchStatus();
2113         if (CalleeStepStatus != MS_Match) {
2114           auto Callee = (CalleeStepStatus == MS_FirstUnique)
2115                             ? CalleeIterStep.getFirstIter()
2116                             : CalleeIterStep.getSecondIter();
2117           updateForUnmatchedCallee(Callee->second, FuncOverlap, Difference,
2118                                    CalleeStepStatus);
2119         } else {
2120           // An inlined function can contain other inlinees inside, so compute
2121           // the Difference recursively.
2122           Difference += 2.0 - 2 * computeSampleFunctionInternalOverlap(
2123                                       CalleeIterStep.getFirstIter()->second,
2124                                       CalleeIterStep.getSecondIter()->second,
2125                                       FuncOverlap);
2126         }
2127         CalleeIterStep.updateOneStep();
2128       }
2129     }
2130     CallsiteIterStep.updateOneStep();
2131   }
2132 
2133   // Difference reflects the total differences of line/block samples in this
2134   // function and ranges in [0.0f to 2.0f]. Take (2.0 - Difference) / 2 to
2135   // reflect the similarity between function profiles in [0.0f to 1.0f].
2136   return (2.0 - Difference) / 2;
2137 }
2138 
2139 double SampleOverlapAggregator::weightForFuncSimilarity(
2140     double FuncInternalSimilarity, uint64_t BaseFuncSample,
2141     uint64_t TestFuncSample) const {
2142   // Compute the weight as the distance between the function weights in two
2143   // profiles.
2144   double BaseFrac = 0.0;
2145   double TestFrac = 0.0;
2146   assert(ProfOverlap.BaseSample > 0 &&
2147          "Total samples in base profile should be greater than 0");
2148   BaseFrac = static_cast<double>(BaseFuncSample) / ProfOverlap.BaseSample;
2149   assert(ProfOverlap.TestSample > 0 &&
2150          "Total samples in test profile should be greater than 0");
2151   TestFrac = static_cast<double>(TestFuncSample) / ProfOverlap.TestSample;
2152   double WeightDistance = std::fabs(BaseFrac - TestFrac);
2153 
2154   // Take WeightDistance into the similarity.
2155   return FuncInternalSimilarity * (1 - WeightDistance);
2156 }
2157 
2158 double
2159 SampleOverlapAggregator::weightByImportance(double FuncSimilarity,
2160                                             uint64_t BaseFuncSample,
2161                                             uint64_t TestFuncSample) const {
2162 
2163   double BaseFrac = 0.0;
2164   double TestFrac = 0.0;
2165   assert(ProfOverlap.BaseSample > 0 &&
2166          "Total samples in base profile should be greater than 0");
2167   BaseFrac = static_cast<double>(BaseFuncSample) / ProfOverlap.BaseSample / 2.0;
2168   assert(ProfOverlap.TestSample > 0 &&
2169          "Total samples in test profile should be greater than 0");
2170   TestFrac = static_cast<double>(TestFuncSample) / ProfOverlap.TestSample / 2.0;
2171   return FuncSimilarity * (BaseFrac + TestFrac);
2172 }
2173 
2174 double SampleOverlapAggregator::computeSampleFunctionOverlap(
2175     const sampleprof::FunctionSamples *BaseFunc,
2176     const sampleprof::FunctionSamples *TestFunc,
2177     SampleOverlapStats *FuncOverlap, uint64_t BaseFuncSample,
2178     uint64_t TestFuncSample) {
2179   // Default function internal similarity before weighted, meaning two functions
2180   // has no overlap.
2181   const double DefaultFuncInternalSimilarity = 0;
2182   double FuncSimilarity;
2183   double FuncInternalSimilarity;
2184 
2185   // If BaseFunc or TestFunc is nullptr, it means the functions do not overlap.
2186   // In this case, we use DefaultFuncInternalSimilarity as the function internal
2187   // similarity.
2188   if (!BaseFunc || !TestFunc) {
2189     FuncInternalSimilarity = DefaultFuncInternalSimilarity;
2190   } else {
2191     assert(FuncOverlap != nullptr &&
2192            "FuncOverlap should be provided in this case");
2193     FuncInternalSimilarity = computeSampleFunctionInternalOverlap(
2194         *BaseFunc, *TestFunc, *FuncOverlap);
2195     // Now, FuncInternalSimilarity may be a little less than 0 due to
2196     // imprecision of floating point accumulations. Make it zero if the
2197     // difference is below Epsilon.
2198     FuncInternalSimilarity = (std::fabs(FuncInternalSimilarity - 0) < Epsilon)
2199                                  ? 0
2200                                  : FuncInternalSimilarity;
2201   }
2202   FuncSimilarity = weightForFuncSimilarity(FuncInternalSimilarity,
2203                                            BaseFuncSample, TestFuncSample);
2204   return FuncSimilarity;
2205 }
2206 
2207 void SampleOverlapAggregator::computeSampleProfileOverlap(raw_fd_ostream &OS) {
2208   using namespace sampleprof;
2209 
2210   std::unordered_map<SampleContext, const FunctionSamples *,
2211                      SampleContext::Hash>
2212       BaseFuncProf;
2213   const auto &BaseProfiles = BaseReader->getProfiles();
2214   for (const auto &BaseFunc : BaseProfiles) {
2215     BaseFuncProf.emplace(BaseFunc.second.getContext(), &(BaseFunc.second));
2216   }
2217   ProfOverlap.UnionCount = BaseFuncProf.size();
2218 
2219   const auto &TestProfiles = TestReader->getProfiles();
2220   for (const auto &TestFunc : TestProfiles) {
2221     SampleOverlapStats FuncOverlap;
2222     FuncOverlap.TestName = TestFunc.second.getContext();
2223     assert(TestStats.count(FuncOverlap.TestName) &&
2224            "TestStats should have records for all functions in test profile "
2225            "except inlinees");
2226     FuncOverlap.TestSample = TestStats[FuncOverlap.TestName].SampleSum;
2227 
2228     bool Matched = false;
2229     const auto Match = BaseFuncProf.find(FuncOverlap.TestName);
2230     if (Match == BaseFuncProf.end()) {
2231       const FuncSampleStats &FuncStats = TestStats[FuncOverlap.TestName];
2232       ++ProfOverlap.TestUniqueCount;
2233       ProfOverlap.TestUniqueSample += FuncStats.SampleSum;
2234       FuncOverlap.TestUniqueSample = FuncStats.SampleSum;
2235 
2236       updateHotBlockOverlap(0, FuncStats.SampleSum, FuncStats.HotBlockCount);
2237 
2238       double FuncSimilarity = computeSampleFunctionOverlap(
2239           nullptr, nullptr, nullptr, 0, FuncStats.SampleSum);
2240       ProfOverlap.Similarity +=
2241           weightByImportance(FuncSimilarity, 0, FuncStats.SampleSum);
2242 
2243       ++ProfOverlap.UnionCount;
2244       ProfOverlap.UnionSample += FuncStats.SampleSum;
2245     } else {
2246       ++ProfOverlap.OverlapCount;
2247 
2248       // Two functions match with each other. Compute function-level overlap and
2249       // aggregate them into profile-level overlap.
2250       FuncOverlap.BaseName = Match->second->getContext();
2251       assert(BaseStats.count(FuncOverlap.BaseName) &&
2252              "BaseStats should have records for all functions in base profile "
2253              "except inlinees");
2254       FuncOverlap.BaseSample = BaseStats[FuncOverlap.BaseName].SampleSum;
2255 
2256       FuncOverlap.Similarity = computeSampleFunctionOverlap(
2257           Match->second, &TestFunc.second, &FuncOverlap, FuncOverlap.BaseSample,
2258           FuncOverlap.TestSample);
2259       ProfOverlap.Similarity +=
2260           weightByImportance(FuncOverlap.Similarity, FuncOverlap.BaseSample,
2261                              FuncOverlap.TestSample);
2262       ProfOverlap.OverlapSample += FuncOverlap.OverlapSample;
2263       ProfOverlap.UnionSample += FuncOverlap.UnionSample;
2264 
2265       // Accumulate the percentage of base unique and test unique samples into
2266       // ProfOverlap.
2267       ProfOverlap.BaseUniqueSample += FuncOverlap.BaseUniqueSample;
2268       ProfOverlap.TestUniqueSample += FuncOverlap.TestUniqueSample;
2269 
2270       // Remove matched base functions for later reporting functions not found
2271       // in test profile.
2272       BaseFuncProf.erase(Match);
2273       Matched = true;
2274     }
2275 
2276     // Print function-level similarity information if specified by options.
2277     assert(TestStats.count(FuncOverlap.TestName) &&
2278            "TestStats should have records for all functions in test profile "
2279            "except inlinees");
2280     if (TestStats[FuncOverlap.TestName].MaxSample >= FuncFilter.ValueCutoff ||
2281         (Matched && FuncOverlap.Similarity < LowSimilarityThreshold) ||
2282         (Matched && !FuncFilter.NameFilter.empty() &&
2283          FuncOverlap.BaseName.toString().find(FuncFilter.NameFilter) !=
2284              std::string::npos)) {
2285       assert(ProfOverlap.BaseSample > 0 &&
2286              "Total samples in base profile should be greater than 0");
2287       FuncOverlap.BaseWeight =
2288           static_cast<double>(FuncOverlap.BaseSample) / ProfOverlap.BaseSample;
2289       assert(ProfOverlap.TestSample > 0 &&
2290              "Total samples in test profile should be greater than 0");
2291       FuncOverlap.TestWeight =
2292           static_cast<double>(FuncOverlap.TestSample) / ProfOverlap.TestSample;
2293       FuncSimilarityDump.emplace(FuncOverlap.BaseWeight, FuncOverlap);
2294     }
2295   }
2296 
2297   // Traverse through functions in base profile but not in test profile.
2298   for (const auto &F : BaseFuncProf) {
2299     assert(BaseStats.count(F.second->getContext()) &&
2300            "BaseStats should have records for all functions in base profile "
2301            "except inlinees");
2302     const FuncSampleStats &FuncStats = BaseStats[F.second->getContext()];
2303     ++ProfOverlap.BaseUniqueCount;
2304     ProfOverlap.BaseUniqueSample += FuncStats.SampleSum;
2305 
2306     updateHotBlockOverlap(FuncStats.SampleSum, 0, FuncStats.HotBlockCount);
2307 
2308     double FuncSimilarity = computeSampleFunctionOverlap(
2309         nullptr, nullptr, nullptr, FuncStats.SampleSum, 0);
2310     ProfOverlap.Similarity +=
2311         weightByImportance(FuncSimilarity, FuncStats.SampleSum, 0);
2312 
2313     ProfOverlap.UnionSample += FuncStats.SampleSum;
2314   }
2315 
2316   // Now, ProfSimilarity may be a little greater than 1 due to imprecision
2317   // of floating point accumulations. Make it 1.0 if the difference is below
2318   // Epsilon.
2319   ProfOverlap.Similarity = (std::fabs(ProfOverlap.Similarity - 1) < Epsilon)
2320                                ? 1
2321                                : ProfOverlap.Similarity;
2322 
2323   computeHotFuncOverlap();
2324 }
2325 
2326 void SampleOverlapAggregator::initializeSampleProfileOverlap() {
2327   const auto &BaseProf = BaseReader->getProfiles();
2328   for (const auto &I : BaseProf) {
2329     ++ProfOverlap.BaseCount;
2330     FuncSampleStats FuncStats;
2331     getFuncSampleStats(I.second, FuncStats, BaseHotThreshold);
2332     ProfOverlap.BaseSample += FuncStats.SampleSum;
2333     BaseStats.emplace(I.second.getContext(), FuncStats);
2334   }
2335 
2336   const auto &TestProf = TestReader->getProfiles();
2337   for (const auto &I : TestProf) {
2338     ++ProfOverlap.TestCount;
2339     FuncSampleStats FuncStats;
2340     getFuncSampleStats(I.second, FuncStats, TestHotThreshold);
2341     ProfOverlap.TestSample += FuncStats.SampleSum;
2342     TestStats.emplace(I.second.getContext(), FuncStats);
2343   }
2344 
2345   ProfOverlap.BaseName = StringRef(BaseFilename);
2346   ProfOverlap.TestName = StringRef(TestFilename);
2347 }
2348 
2349 void SampleOverlapAggregator::dumpFuncSimilarity(raw_fd_ostream &OS) const {
2350   using namespace sampleprof;
2351 
2352   if (FuncSimilarityDump.empty())
2353     return;
2354 
2355   formatted_raw_ostream FOS(OS);
2356   FOS << "Function-level details:\n";
2357   FOS << "Base weight";
2358   FOS.PadToColumn(TestWeightCol);
2359   FOS << "Test weight";
2360   FOS.PadToColumn(SimilarityCol);
2361   FOS << "Similarity";
2362   FOS.PadToColumn(OverlapCol);
2363   FOS << "Overlap";
2364   FOS.PadToColumn(BaseUniqueCol);
2365   FOS << "Base unique";
2366   FOS.PadToColumn(TestUniqueCol);
2367   FOS << "Test unique";
2368   FOS.PadToColumn(BaseSampleCol);
2369   FOS << "Base samples";
2370   FOS.PadToColumn(TestSampleCol);
2371   FOS << "Test samples";
2372   FOS.PadToColumn(FuncNameCol);
2373   FOS << "Function name\n";
2374   for (const auto &F : FuncSimilarityDump) {
2375     double OverlapPercent =
2376         F.second.UnionSample > 0
2377             ? static_cast<double>(F.second.OverlapSample) / F.second.UnionSample
2378             : 0;
2379     double BaseUniquePercent =
2380         F.second.BaseSample > 0
2381             ? static_cast<double>(F.second.BaseUniqueSample) /
2382                   F.second.BaseSample
2383             : 0;
2384     double TestUniquePercent =
2385         F.second.TestSample > 0
2386             ? static_cast<double>(F.second.TestUniqueSample) /
2387                   F.second.TestSample
2388             : 0;
2389 
2390     FOS << format("%.2f%%", F.second.BaseWeight * 100);
2391     FOS.PadToColumn(TestWeightCol);
2392     FOS << format("%.2f%%", F.second.TestWeight * 100);
2393     FOS.PadToColumn(SimilarityCol);
2394     FOS << format("%.2f%%", F.second.Similarity * 100);
2395     FOS.PadToColumn(OverlapCol);
2396     FOS << format("%.2f%%", OverlapPercent * 100);
2397     FOS.PadToColumn(BaseUniqueCol);
2398     FOS << format("%.2f%%", BaseUniquePercent * 100);
2399     FOS.PadToColumn(TestUniqueCol);
2400     FOS << format("%.2f%%", TestUniquePercent * 100);
2401     FOS.PadToColumn(BaseSampleCol);
2402     FOS << F.second.BaseSample;
2403     FOS.PadToColumn(TestSampleCol);
2404     FOS << F.second.TestSample;
2405     FOS.PadToColumn(FuncNameCol);
2406     FOS << F.second.TestName.toString() << "\n";
2407   }
2408 }
2409 
2410 void SampleOverlapAggregator::dumpProgramSummary(raw_fd_ostream &OS) const {
2411   OS << "Profile overlap infomation for base_profile: "
2412      << ProfOverlap.BaseName.toString()
2413      << " and test_profile: " << ProfOverlap.TestName.toString()
2414      << "\nProgram level:\n";
2415 
2416   OS << "  Whole program profile similarity: "
2417      << format("%.3f%%", ProfOverlap.Similarity * 100) << "\n";
2418 
2419   assert(ProfOverlap.UnionSample > 0 &&
2420          "Total samples in two profile should be greater than 0");
2421   double OverlapPercent =
2422       static_cast<double>(ProfOverlap.OverlapSample) / ProfOverlap.UnionSample;
2423   assert(ProfOverlap.BaseSample > 0 &&
2424          "Total samples in base profile should be greater than 0");
2425   double BaseUniquePercent = static_cast<double>(ProfOverlap.BaseUniqueSample) /
2426                              ProfOverlap.BaseSample;
2427   assert(ProfOverlap.TestSample > 0 &&
2428          "Total samples in test profile should be greater than 0");
2429   double TestUniquePercent = static_cast<double>(ProfOverlap.TestUniqueSample) /
2430                              ProfOverlap.TestSample;
2431 
2432   OS << "  Whole program sample overlap: "
2433      << format("%.3f%%", OverlapPercent * 100) << "\n";
2434   OS << "    percentage of samples unique in base profile: "
2435      << format("%.3f%%", BaseUniquePercent * 100) << "\n";
2436   OS << "    percentage of samples unique in test profile: "
2437      << format("%.3f%%", TestUniquePercent * 100) << "\n";
2438   OS << "    total samples in base profile: " << ProfOverlap.BaseSample << "\n"
2439      << "    total samples in test profile: " << ProfOverlap.TestSample << "\n";
2440 
2441   assert(ProfOverlap.UnionCount > 0 &&
2442          "There should be at least one function in two input profiles");
2443   double FuncOverlapPercent =
2444       static_cast<double>(ProfOverlap.OverlapCount) / ProfOverlap.UnionCount;
2445   OS << "  Function overlap: " << format("%.3f%%", FuncOverlapPercent * 100)
2446      << "\n";
2447   OS << "    overlap functions: " << ProfOverlap.OverlapCount << "\n";
2448   OS << "    functions unique in base profile: " << ProfOverlap.BaseUniqueCount
2449      << "\n";
2450   OS << "    functions unique in test profile: " << ProfOverlap.TestUniqueCount
2451      << "\n";
2452 }
2453 
2454 void SampleOverlapAggregator::dumpHotFuncAndBlockOverlap(
2455     raw_fd_ostream &OS) const {
2456   assert(HotFuncOverlap.UnionCount > 0 &&
2457          "There should be at least one hot function in two input profiles");
2458   OS << "  Hot-function overlap: "
2459      << format("%.3f%%", static_cast<double>(HotFuncOverlap.OverlapCount) /
2460                              HotFuncOverlap.UnionCount * 100)
2461      << "\n";
2462   OS << "    overlap hot functions: " << HotFuncOverlap.OverlapCount << "\n";
2463   OS << "    hot functions unique in base profile: "
2464      << HotFuncOverlap.BaseCount - HotFuncOverlap.OverlapCount << "\n";
2465   OS << "    hot functions unique in test profile: "
2466      << HotFuncOverlap.TestCount - HotFuncOverlap.OverlapCount << "\n";
2467 
2468   assert(HotBlockOverlap.UnionCount > 0 &&
2469          "There should be at least one hot block in two input profiles");
2470   OS << "  Hot-block overlap: "
2471      << format("%.3f%%", static_cast<double>(HotBlockOverlap.OverlapCount) /
2472                              HotBlockOverlap.UnionCount * 100)
2473      << "\n";
2474   OS << "    overlap hot blocks: " << HotBlockOverlap.OverlapCount << "\n";
2475   OS << "    hot blocks unique in base profile: "
2476      << HotBlockOverlap.BaseCount - HotBlockOverlap.OverlapCount << "\n";
2477   OS << "    hot blocks unique in test profile: "
2478      << HotBlockOverlap.TestCount - HotBlockOverlap.OverlapCount << "\n";
2479 }
2480 
2481 std::error_code SampleOverlapAggregator::loadProfiles() {
2482   using namespace sampleprof;
2483 
2484   LLVMContext Context;
2485   auto FS = vfs::getRealFileSystem();
2486   auto BaseReaderOrErr = SampleProfileReader::create(BaseFilename, Context, *FS,
2487                                                      FSDiscriminatorPassOption);
2488   if (std::error_code EC = BaseReaderOrErr.getError())
2489     exitWithErrorCode(EC, BaseFilename);
2490 
2491   auto TestReaderOrErr = SampleProfileReader::create(TestFilename, Context, *FS,
2492                                                      FSDiscriminatorPassOption);
2493   if (std::error_code EC = TestReaderOrErr.getError())
2494     exitWithErrorCode(EC, TestFilename);
2495 
2496   BaseReader = std::move(BaseReaderOrErr.get());
2497   TestReader = std::move(TestReaderOrErr.get());
2498 
2499   if (std::error_code EC = BaseReader->read())
2500     exitWithErrorCode(EC, BaseFilename);
2501   if (std::error_code EC = TestReader->read())
2502     exitWithErrorCode(EC, TestFilename);
2503   if (BaseReader->profileIsProbeBased() != TestReader->profileIsProbeBased())
2504     exitWithError(
2505         "cannot compare probe-based profile with non-probe-based profile");
2506   if (BaseReader->profileIsCS() != TestReader->profileIsCS())
2507     exitWithError("cannot compare CS profile with non-CS profile");
2508 
2509   // Load BaseHotThreshold and TestHotThreshold as 99-percentile threshold in
2510   // profile summary.
2511   ProfileSummary &BasePS = BaseReader->getSummary();
2512   ProfileSummary &TestPS = TestReader->getSummary();
2513   BaseHotThreshold =
2514       ProfileSummaryBuilder::getHotCountThreshold(BasePS.getDetailedSummary());
2515   TestHotThreshold =
2516       ProfileSummaryBuilder::getHotCountThreshold(TestPS.getDetailedSummary());
2517 
2518   return std::error_code();
2519 }
2520 
2521 void overlapSampleProfile(const std::string &BaseFilename,
2522                           const std::string &TestFilename,
2523                           const OverlapFuncFilters &FuncFilter,
2524                           uint64_t SimilarityCutoff, raw_fd_ostream &OS) {
2525   using namespace sampleprof;
2526 
2527   // We use 0.000005 to initialize OverlapAggr.Epsilon because the final metrics
2528   // report 2--3 places after decimal point in percentage numbers.
2529   SampleOverlapAggregator OverlapAggr(
2530       BaseFilename, TestFilename,
2531       static_cast<double>(SimilarityCutoff) / 1000000, 0.000005, FuncFilter);
2532   if (std::error_code EC = OverlapAggr.loadProfiles())
2533     exitWithErrorCode(EC);
2534 
2535   OverlapAggr.initializeSampleProfileOverlap();
2536   if (OverlapAggr.detectZeroSampleProfile(OS))
2537     return;
2538 
2539   OverlapAggr.computeSampleProfileOverlap(OS);
2540 
2541   OverlapAggr.dumpProgramSummary(OS);
2542   OverlapAggr.dumpHotFuncAndBlockOverlap(OS);
2543   OverlapAggr.dumpFuncSimilarity(OS);
2544 }
2545 
2546 static int overlap_main(int argc, const char *argv[]) {
2547   std::error_code EC;
2548   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
2549   if (EC)
2550     exitWithErrorCode(EC, OutputFilename);
2551 
2552   if (ProfileKind == instr)
2553     overlapInstrProfile(BaseFilename, TestFilename,
2554                         OverlapFuncFilters{OverlapValueCutoff, FuncNameFilter},
2555                         OS, IsCS);
2556   else
2557     overlapSampleProfile(BaseFilename, TestFilename,
2558                          OverlapFuncFilters{OverlapValueCutoff, FuncNameFilter},
2559                          SimilarityCutoff, OS);
2560 
2561   return 0;
2562 }
2563 
2564 namespace {
2565 struct ValueSitesStats {
2566   ValueSitesStats()
2567       : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
2568         TotalNumValues(0) {}
2569   uint64_t TotalNumValueSites;
2570   uint64_t TotalNumValueSitesWithValueProfile;
2571   uint64_t TotalNumValues;
2572   std::vector<unsigned> ValueSitesHistogram;
2573 };
2574 } // namespace
2575 
2576 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
2577                                   ValueSitesStats &Stats, raw_fd_ostream &OS,
2578                                   InstrProfSymtab *Symtab) {
2579   uint32_t NS = Func.getNumValueSites(VK);
2580   Stats.TotalNumValueSites += NS;
2581   for (size_t I = 0; I < NS; ++I) {
2582     uint32_t NV = Func.getNumValueDataForSite(VK, I);
2583     std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
2584     Stats.TotalNumValues += NV;
2585     if (NV) {
2586       Stats.TotalNumValueSitesWithValueProfile++;
2587       if (NV > Stats.ValueSitesHistogram.size())
2588         Stats.ValueSitesHistogram.resize(NV, 0);
2589       Stats.ValueSitesHistogram[NV - 1]++;
2590     }
2591 
2592     uint64_t SiteSum = 0;
2593     for (uint32_t V = 0; V < NV; V++)
2594       SiteSum += VD[V].Count;
2595     if (SiteSum == 0)
2596       SiteSum = 1;
2597 
2598     for (uint32_t V = 0; V < NV; V++) {
2599       OS << "\t[ " << format("%2u", I) << ", ";
2600       if (Symtab == nullptr)
2601         OS << format("%4" PRIu64, VD[V].Value);
2602       else
2603         OS << Symtab->getFuncOrVarName(VD[V].Value);
2604       OS << ", " << format("%10" PRId64, VD[V].Count) << " ] ("
2605          << format("%.2f%%", (VD[V].Count * 100.0 / SiteSum)) << ")\n";
2606     }
2607   }
2608 }
2609 
2610 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
2611                                 ValueSitesStats &Stats) {
2612   OS << "  Total number of sites: " << Stats.TotalNumValueSites << "\n";
2613   OS << "  Total number of sites with values: "
2614      << Stats.TotalNumValueSitesWithValueProfile << "\n";
2615   OS << "  Total number of profiled values: " << Stats.TotalNumValues << "\n";
2616 
2617   OS << "  Value sites histogram:\n\tNumTargets, SiteCount\n";
2618   for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
2619     if (Stats.ValueSitesHistogram[I] > 0)
2620       OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
2621   }
2622 }
2623 
2624 static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
2625   if (SFormat == ShowFormat::Json)
2626     exitWithError("JSON output is not supported for instr profiles");
2627   if (SFormat == ShowFormat::Yaml)
2628     exitWithError("YAML output is not supported for instr profiles");
2629   auto FS = vfs::getRealFileSystem();
2630   auto ReaderOrErr = InstrProfReader::create(Filename, *FS);
2631   std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
2632   if (ShowDetailedSummary && Cutoffs.empty()) {
2633     Cutoffs = ProfileSummaryBuilder::DefaultCutoffs;
2634   }
2635   InstrProfSummaryBuilder Builder(std::move(Cutoffs));
2636   if (Error E = ReaderOrErr.takeError())
2637     exitWithError(std::move(E), Filename);
2638 
2639   auto Reader = std::move(ReaderOrErr.get());
2640   bool IsIRInstr = Reader->isIRLevelProfile();
2641   size_t ShownFunctions = 0;
2642   size_t BelowCutoffFunctions = 0;
2643   int NumVPKind = IPVK_Last - IPVK_First + 1;
2644   std::vector<ValueSitesStats> VPStats(NumVPKind);
2645 
2646   auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
2647                    const std::pair<std::string, uint64_t> &v2) {
2648     return v1.second > v2.second;
2649   };
2650 
2651   std::priority_queue<std::pair<std::string, uint64_t>,
2652                       std::vector<std::pair<std::string, uint64_t>>,
2653                       decltype(MinCmp)>
2654       HottestFuncs(MinCmp);
2655 
2656   if (!TextFormat && OnlyListBelow) {
2657     OS << "The list of functions with the maximum counter less than "
2658        << ShowValueCutoff << ":\n";
2659   }
2660 
2661   // Add marker so that IR-level instrumentation round-trips properly.
2662   if (TextFormat && IsIRInstr)
2663     OS << ":ir\n";
2664 
2665   for (const auto &Func : *Reader) {
2666     if (Reader->isIRLevelProfile()) {
2667       bool FuncIsCS = NamedInstrProfRecord::hasCSFlagInHash(Func.Hash);
2668       if (FuncIsCS != ShowCS)
2669         continue;
2670     }
2671     bool Show = ShowAllFunctions ||
2672                 (!FuncNameFilter.empty() && Func.Name.contains(FuncNameFilter));
2673 
2674     bool doTextFormatDump = (Show && TextFormat);
2675 
2676     if (doTextFormatDump) {
2677       InstrProfSymtab &Symtab = Reader->getSymtab();
2678       InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
2679                                          OS);
2680       continue;
2681     }
2682 
2683     assert(Func.Counts.size() > 0 && "function missing entry counter");
2684     Builder.addRecord(Func);
2685 
2686     if (ShowCovered) {
2687       if (llvm::any_of(Func.Counts, [](uint64_t C) { return C; }))
2688         OS << Func.Name << "\n";
2689       continue;
2690     }
2691 
2692     uint64_t FuncMax = 0;
2693     uint64_t FuncSum = 0;
2694 
2695     auto PseudoKind = Func.getCountPseudoKind();
2696     if (PseudoKind != InstrProfRecord::NotPseudo) {
2697       if (Show) {
2698         if (!ShownFunctions)
2699           OS << "Counters:\n";
2700         ++ShownFunctions;
2701         OS << "  " << Func.Name << ":\n"
2702            << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
2703            << "    Counters: " << Func.Counts.size();
2704         if (PseudoKind == InstrProfRecord::PseudoHot)
2705           OS << "    <PseudoHot>\n";
2706         else if (PseudoKind == InstrProfRecord::PseudoWarm)
2707           OS << "    <PseudoWarm>\n";
2708         else
2709           llvm_unreachable("Unknown PseudoKind");
2710       }
2711       continue;
2712     }
2713 
2714     for (size_t I = 0, E = Func.Counts.size(); I < E; ++I) {
2715       FuncMax = std::max(FuncMax, Func.Counts[I]);
2716       FuncSum += Func.Counts[I];
2717     }
2718 
2719     if (FuncMax < ShowValueCutoff) {
2720       ++BelowCutoffFunctions;
2721       if (OnlyListBelow) {
2722         OS << "  " << Func.Name << ": (Max = " << FuncMax
2723            << " Sum = " << FuncSum << ")\n";
2724       }
2725       continue;
2726     } else if (OnlyListBelow)
2727       continue;
2728 
2729     if (TopNFunctions) {
2730       if (HottestFuncs.size() == TopNFunctions) {
2731         if (HottestFuncs.top().second < FuncMax) {
2732           HottestFuncs.pop();
2733           HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
2734         }
2735       } else
2736         HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
2737     }
2738 
2739     if (Show) {
2740       if (!ShownFunctions)
2741         OS << "Counters:\n";
2742 
2743       ++ShownFunctions;
2744 
2745       OS << "  " << Func.Name << ":\n"
2746          << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
2747          << "    Counters: " << Func.Counts.size() << "\n";
2748       if (!IsIRInstr)
2749         OS << "    Function count: " << Func.Counts[0] << "\n";
2750 
2751       if (ShowIndirectCallTargets)
2752         OS << "    Indirect Call Site Count: "
2753            << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
2754 
2755       uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
2756       if (ShowMemOPSizes && NumMemOPCalls > 0)
2757         OS << "    Number of Memory Intrinsics Calls: " << NumMemOPCalls
2758            << "\n";
2759 
2760       if (ShowCounts) {
2761         OS << "    Block counts: [";
2762         size_t Start = (IsIRInstr ? 0 : 1);
2763         for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
2764           OS << (I == Start ? "" : ", ") << Func.Counts[I];
2765         }
2766         OS << "]\n";
2767       }
2768 
2769       if (ShowIndirectCallTargets) {
2770         OS << "    Indirect Target Results:\n";
2771         traverseAllValueSites(Func, IPVK_IndirectCallTarget,
2772                               VPStats[IPVK_IndirectCallTarget], OS,
2773                               &(Reader->getSymtab()));
2774       }
2775 
2776       if (ShowMemOPSizes && NumMemOPCalls > 0) {
2777         OS << "    Memory Intrinsic Size Results:\n";
2778         traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
2779                               nullptr);
2780       }
2781     }
2782   }
2783   if (Reader->hasError())
2784     exitWithError(Reader->getError(), Filename);
2785 
2786   if (TextFormat || ShowCovered)
2787     return 0;
2788   std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
2789   bool IsIR = Reader->isIRLevelProfile();
2790   OS << "Instrumentation level: " << (IsIR ? "IR" : "Front-end");
2791   if (IsIR)
2792     OS << "  entry_first = " << Reader->instrEntryBBEnabled();
2793   OS << "\n";
2794   if (ShowAllFunctions || !FuncNameFilter.empty())
2795     OS << "Functions shown: " << ShownFunctions << "\n";
2796   OS << "Total functions: " << PS->getNumFunctions() << "\n";
2797   if (ShowValueCutoff > 0) {
2798     OS << "Number of functions with maximum count (< " << ShowValueCutoff
2799        << "): " << BelowCutoffFunctions << "\n";
2800     OS << "Number of functions with maximum count (>= " << ShowValueCutoff
2801        << "): " << PS->getNumFunctions() - BelowCutoffFunctions << "\n";
2802   }
2803   OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
2804   OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
2805 
2806   if (TopNFunctions) {
2807     std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
2808     while (!HottestFuncs.empty()) {
2809       SortedHottestFuncs.emplace_back(HottestFuncs.top());
2810       HottestFuncs.pop();
2811     }
2812     OS << "Top " << TopNFunctions
2813        << " functions with the largest internal block counts: \n";
2814     for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
2815       OS << "  " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
2816   }
2817 
2818   if (ShownFunctions && ShowIndirectCallTargets) {
2819     OS << "Statistics for indirect call sites profile:\n";
2820     showValueSitesStats(OS, IPVK_IndirectCallTarget,
2821                         VPStats[IPVK_IndirectCallTarget]);
2822   }
2823 
2824   if (ShownFunctions && ShowMemOPSizes) {
2825     OS << "Statistics for memory intrinsic calls sizes profile:\n";
2826     showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
2827   }
2828 
2829   if (ShowDetailedSummary) {
2830     OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
2831     OS << "Total count: " << PS->getTotalCount() << "\n";
2832     PS->printDetailedSummary(OS);
2833   }
2834 
2835   if (ShowBinaryIds)
2836     if (Error E = Reader->printBinaryIds(OS))
2837       exitWithError(std::move(E), Filename);
2838 
2839   if (ShowProfileVersion)
2840     OS << "Profile version: " << Reader->getVersion() << "\n";
2841 
2842   if (ShowTemporalProfTraces) {
2843     auto &Traces = Reader->getTemporalProfTraces();
2844     OS << "Temporal Profile Traces (samples=" << Traces.size()
2845        << " seen=" << Reader->getTemporalProfTraceStreamSize() << "):\n";
2846     for (unsigned i = 0; i < Traces.size(); i++) {
2847       OS << "  Temporal Profile Trace " << i << " (weight=" << Traces[i].Weight
2848          << " count=" << Traces[i].FunctionNameRefs.size() << "):\n";
2849       for (auto &NameRef : Traces[i].FunctionNameRefs)
2850         OS << "    " << Reader->getSymtab().getFuncOrVarName(NameRef) << "\n";
2851     }
2852   }
2853 
2854   return 0;
2855 }
2856 
2857 static void showSectionInfo(sampleprof::SampleProfileReader *Reader,
2858                             raw_fd_ostream &OS) {
2859   if (!Reader->dumpSectionInfo(OS)) {
2860     WithColor::warning() << "-show-sec-info-only is only supported for "
2861                          << "sample profile in extbinary format and is "
2862                          << "ignored for other formats.\n";
2863     return;
2864   }
2865 }
2866 
2867 namespace {
2868 struct HotFuncInfo {
2869   std::string FuncName;
2870   uint64_t TotalCount;
2871   double TotalCountPercent;
2872   uint64_t MaxCount;
2873   uint64_t EntryCount;
2874 
2875   HotFuncInfo()
2876       : TotalCount(0), TotalCountPercent(0.0f), MaxCount(0), EntryCount(0) {}
2877 
2878   HotFuncInfo(StringRef FN, uint64_t TS, double TSP, uint64_t MS, uint64_t ES)
2879       : FuncName(FN.begin(), FN.end()), TotalCount(TS), TotalCountPercent(TSP),
2880         MaxCount(MS), EntryCount(ES) {}
2881 };
2882 } // namespace
2883 
2884 // Print out detailed information about hot functions in PrintValues vector.
2885 // Users specify titles and offset of every columns through ColumnTitle and
2886 // ColumnOffset. The size of ColumnTitle and ColumnOffset need to be the same
2887 // and at least 4. Besides, users can optionally give a HotFuncMetric string to
2888 // print out or let it be an empty string.
2889 static void dumpHotFunctionList(const std::vector<std::string> &ColumnTitle,
2890                                 const std::vector<int> &ColumnOffset,
2891                                 const std::vector<HotFuncInfo> &PrintValues,
2892                                 uint64_t HotFuncCount, uint64_t TotalFuncCount,
2893                                 uint64_t HotProfCount, uint64_t TotalProfCount,
2894                                 const std::string &HotFuncMetric,
2895                                 uint32_t TopNFunctions, raw_fd_ostream &OS) {
2896   assert(ColumnOffset.size() == ColumnTitle.size() &&
2897          "ColumnOffset and ColumnTitle should have the same size");
2898   assert(ColumnTitle.size() >= 4 &&
2899          "ColumnTitle should have at least 4 elements");
2900   assert(TotalFuncCount > 0 &&
2901          "There should be at least one function in the profile");
2902   double TotalProfPercent = 0;
2903   if (TotalProfCount > 0)
2904     TotalProfPercent = static_cast<double>(HotProfCount) / TotalProfCount * 100;
2905 
2906   formatted_raw_ostream FOS(OS);
2907   FOS << HotFuncCount << " out of " << TotalFuncCount
2908       << " functions with profile ("
2909       << format("%.2f%%",
2910                 (static_cast<double>(HotFuncCount) / TotalFuncCount * 100))
2911       << ") are considered hot functions";
2912   if (!HotFuncMetric.empty())
2913     FOS << " (" << HotFuncMetric << ")";
2914   FOS << ".\n";
2915   FOS << HotProfCount << " out of " << TotalProfCount << " profile counts ("
2916       << format("%.2f%%", TotalProfPercent) << ") are from hot functions.\n";
2917 
2918   for (size_t I = 0; I < ColumnTitle.size(); ++I) {
2919     FOS.PadToColumn(ColumnOffset[I]);
2920     FOS << ColumnTitle[I];
2921   }
2922   FOS << "\n";
2923 
2924   uint32_t Count = 0;
2925   for (const auto &R : PrintValues) {
2926     if (TopNFunctions && (Count++ == TopNFunctions))
2927       break;
2928     FOS.PadToColumn(ColumnOffset[0]);
2929     FOS << R.TotalCount << " (" << format("%.2f%%", R.TotalCountPercent) << ")";
2930     FOS.PadToColumn(ColumnOffset[1]);
2931     FOS << R.MaxCount;
2932     FOS.PadToColumn(ColumnOffset[2]);
2933     FOS << R.EntryCount;
2934     FOS.PadToColumn(ColumnOffset[3]);
2935     FOS << R.FuncName << "\n";
2936   }
2937 }
2938 
2939 static int showHotFunctionList(const sampleprof::SampleProfileMap &Profiles,
2940                                ProfileSummary &PS, uint32_t TopN,
2941                                raw_fd_ostream &OS) {
2942   using namespace sampleprof;
2943 
2944   const uint32_t HotFuncCutoff = 990000;
2945   auto &SummaryVector = PS.getDetailedSummary();
2946   uint64_t MinCountThreshold = 0;
2947   for (const ProfileSummaryEntry &SummaryEntry : SummaryVector) {
2948     if (SummaryEntry.Cutoff == HotFuncCutoff) {
2949       MinCountThreshold = SummaryEntry.MinCount;
2950       break;
2951     }
2952   }
2953 
2954   // Traverse all functions in the profile and keep only hot functions.
2955   // The following loop also calculates the sum of total samples of all
2956   // functions.
2957   std::multimap<uint64_t, std::pair<const FunctionSamples *, const uint64_t>,
2958                 std::greater<uint64_t>>
2959       HotFunc;
2960   uint64_t ProfileTotalSample = 0;
2961   uint64_t HotFuncSample = 0;
2962   uint64_t HotFuncCount = 0;
2963 
2964   for (const auto &I : Profiles) {
2965     FuncSampleStats FuncStats;
2966     const FunctionSamples &FuncProf = I.second;
2967     ProfileTotalSample += FuncProf.getTotalSamples();
2968     getFuncSampleStats(FuncProf, FuncStats, MinCountThreshold);
2969 
2970     if (isFunctionHot(FuncStats, MinCountThreshold)) {
2971       HotFunc.emplace(FuncProf.getTotalSamples(),
2972                       std::make_pair(&(I.second), FuncStats.MaxSample));
2973       HotFuncSample += FuncProf.getTotalSamples();
2974       ++HotFuncCount;
2975     }
2976   }
2977 
2978   std::vector<std::string> ColumnTitle{"Total sample (%)", "Max sample",
2979                                        "Entry sample", "Function name"};
2980   std::vector<int> ColumnOffset{0, 24, 42, 58};
2981   std::string Metric =
2982       std::string("max sample >= ") + std::to_string(MinCountThreshold);
2983   std::vector<HotFuncInfo> PrintValues;
2984   for (const auto &FuncPair : HotFunc) {
2985     const FunctionSamples &Func = *FuncPair.second.first;
2986     double TotalSamplePercent =
2987         (ProfileTotalSample > 0)
2988             ? (Func.getTotalSamples() * 100.0) / ProfileTotalSample
2989             : 0;
2990     PrintValues.emplace_back(
2991         HotFuncInfo(Func.getContext().toString(), Func.getTotalSamples(),
2992                     TotalSamplePercent, FuncPair.second.second,
2993                     Func.getHeadSamplesEstimate()));
2994   }
2995   dumpHotFunctionList(ColumnTitle, ColumnOffset, PrintValues, HotFuncCount,
2996                       Profiles.size(), HotFuncSample, ProfileTotalSample,
2997                       Metric, TopN, OS);
2998 
2999   return 0;
3000 }
3001 
3002 static int showSampleProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
3003   if (SFormat == ShowFormat::Yaml)
3004     exitWithError("YAML output is not supported for sample profiles");
3005   using namespace sampleprof;
3006   LLVMContext Context;
3007   auto FS = vfs::getRealFileSystem();
3008   auto ReaderOrErr = SampleProfileReader::create(Filename, Context, *FS,
3009                                                  FSDiscriminatorPassOption);
3010   if (std::error_code EC = ReaderOrErr.getError())
3011     exitWithErrorCode(EC, Filename);
3012 
3013   auto Reader = std::move(ReaderOrErr.get());
3014   if (ShowSectionInfoOnly) {
3015     showSectionInfo(Reader.get(), OS);
3016     return 0;
3017   }
3018 
3019   if (std::error_code EC = Reader->read())
3020     exitWithErrorCode(EC, Filename);
3021 
3022   if (ShowAllFunctions || FuncNameFilter.empty()) {
3023     if (SFormat == ShowFormat::Json)
3024       Reader->dumpJson(OS);
3025     else
3026       Reader->dump(OS);
3027   } else {
3028     if (SFormat == ShowFormat::Json)
3029       exitWithError(
3030           "the JSON format is supported only when all functions are to "
3031           "be printed");
3032 
3033     // TODO: parse context string to support filtering by contexts.
3034     FunctionSamples *FS = Reader->getSamplesFor(StringRef(FuncNameFilter));
3035     Reader->dumpFunctionProfile(FS ? *FS : FunctionSamples(), OS);
3036   }
3037 
3038   if (ShowProfileSymbolList) {
3039     std::unique_ptr<sampleprof::ProfileSymbolList> ReaderList =
3040         Reader->getProfileSymbolList();
3041     ReaderList->dump(OS);
3042   }
3043 
3044   if (ShowDetailedSummary) {
3045     auto &PS = Reader->getSummary();
3046     PS.printSummary(OS);
3047     PS.printDetailedSummary(OS);
3048   }
3049 
3050   if (ShowHotFuncList || TopNFunctions)
3051     showHotFunctionList(Reader->getProfiles(), Reader->getSummary(),
3052                         TopNFunctions, OS);
3053 
3054   return 0;
3055 }
3056 
3057 static int showMemProfProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
3058   if (SFormat == ShowFormat::Json)
3059     exitWithError("JSON output is not supported for MemProf");
3060   auto ReaderOr = llvm::memprof::RawMemProfReader::create(
3061       Filename, ProfiledBinary, /*KeepNames=*/true);
3062   if (Error E = ReaderOr.takeError())
3063     // Since the error can be related to the profile or the binary we do not
3064     // pass whence. Instead additional context is provided where necessary in
3065     // the error message.
3066     exitWithError(std::move(E), /*Whence*/ "");
3067 
3068   std::unique_ptr<llvm::memprof::RawMemProfReader> Reader(
3069       ReaderOr.get().release());
3070 
3071   Reader->printYAML(OS);
3072   return 0;
3073 }
3074 
3075 static int showDebugInfoCorrelation(const std::string &Filename,
3076                                     ShowFormat SFormat, raw_fd_ostream &OS) {
3077   if (SFormat == ShowFormat::Json)
3078     exitWithError("JSON output is not supported for debug info correlation");
3079   std::unique_ptr<InstrProfCorrelator> Correlator;
3080   if (auto Err =
3081           InstrProfCorrelator::get(Filename, InstrProfCorrelator::DEBUG_INFO)
3082               .moveInto(Correlator))
3083     exitWithError(std::move(Err), Filename);
3084   if (SFormat == ShowFormat::Yaml) {
3085     if (auto Err = Correlator->dumpYaml(MaxDbgCorrelationWarnings, OS))
3086       exitWithError(std::move(Err), Filename);
3087     return 0;
3088   }
3089 
3090   if (auto Err = Correlator->correlateProfileData(MaxDbgCorrelationWarnings))
3091     exitWithError(std::move(Err), Filename);
3092 
3093   InstrProfSymtab Symtab;
3094   if (auto Err = Symtab.create(
3095           StringRef(Correlator->getNamesPointer(), Correlator->getNamesSize())))
3096     exitWithError(std::move(Err), Filename);
3097 
3098   if (ShowProfileSymbolList)
3099     Symtab.dumpNames(OS);
3100   // TODO: Read "Profile Data Type" from debug info to compute and show how many
3101   // counters the section holds.
3102   if (ShowDetailedSummary)
3103     OS << "Counters section size: 0x"
3104        << Twine::utohexstr(Correlator->getCountersSectionSize()) << " bytes\n";
3105   OS << "Found " << Correlator->getDataSize() << " functions\n";
3106 
3107   return 0;
3108 }
3109 
3110 static int show_main(int argc, const char *argv[]) {
3111   if (Filename.empty() && DebugInfoFilename.empty())
3112     exitWithError(
3113         "the positional argument '<profdata-file>' is required unless '--" +
3114         DebugInfoFilename.ArgStr + "' is provided");
3115 
3116   if (Filename == OutputFilename) {
3117     errs() << sys::path::filename(argv[0]) << " " << argv[1]
3118            << ": Input file name cannot be the same as the output file name!\n";
3119     return 1;
3120   }
3121   if (JsonFormat)
3122     SFormat = ShowFormat::Json;
3123 
3124   std::error_code EC;
3125   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
3126   if (EC)
3127     exitWithErrorCode(EC, OutputFilename);
3128 
3129   if (ShowAllFunctions && !FuncNameFilter.empty())
3130     WithColor::warning() << "-function argument ignored: showing all functions\n";
3131 
3132   if (!DebugInfoFilename.empty())
3133     return showDebugInfoCorrelation(DebugInfoFilename, SFormat, OS);
3134 
3135   if (ShowProfileKind == instr)
3136     return showInstrProfile(SFormat, OS);
3137   if (ShowProfileKind == sample)
3138     return showSampleProfile(SFormat, OS);
3139   return showMemProfProfile(SFormat, OS);
3140 }
3141 
3142 static int order_main(int argc, const char *argv[]) {
3143   std::error_code EC;
3144   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
3145   if (EC)
3146     exitWithErrorCode(EC, OutputFilename);
3147   auto FS = vfs::getRealFileSystem();
3148   auto ReaderOrErr = InstrProfReader::create(Filename, *FS);
3149   if (Error E = ReaderOrErr.takeError())
3150     exitWithError(std::move(E), Filename);
3151 
3152   auto Reader = std::move(ReaderOrErr.get());
3153   for (auto &I : *Reader) {
3154     // Read all entries
3155     (void)I;
3156   }
3157   auto &Traces = Reader->getTemporalProfTraces();
3158   auto Nodes = TemporalProfTraceTy::createBPFunctionNodes(Traces);
3159   BalancedPartitioningConfig Config;
3160   BalancedPartitioning BP(Config);
3161   BP.run(Nodes);
3162 
3163   WithColor::note() << "# Ordered " << Nodes.size() << " functions\n";
3164   for (auto &N : Nodes) {
3165     auto [Filename, ParsedFuncName] =
3166         getParsedIRPGOFuncName(Reader->getSymtab().getFuncOrVarName(N.Id));
3167     if (!Filename.empty())
3168       OS << "# " << Filename << "\n";
3169     OS << ParsedFuncName << "\n";
3170   }
3171   return 0;
3172 }
3173 
3174 int llvm_profdata_main(int argc, char **argvNonConst,
3175                        const llvm::ToolContext &) {
3176   const char **argv = const_cast<const char **>(argvNonConst);
3177   InitLLVM X(argc, argv);
3178 
3179   StringRef ProgName(sys::path::filename(argv[0]));
3180 
3181   if (argc < 2) {
3182     errs() << ProgName
3183            << ": No subcommand specified! Run llvm-profata --help for usage.\n";
3184     return 1;
3185   }
3186 
3187   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data\n");
3188 
3189   if (ShowSubcommand)
3190     return show_main(argc, argv);
3191 
3192   if (OrderSubcommand)
3193     return order_main(argc, argv);
3194 
3195   if (OverlapSubcommand)
3196     return overlap_main(argc, argv);
3197 
3198   if (MergeSubcommand)
3199     return merge_main(argc, argv);
3200 
3201   errs() << ProgName
3202          << ": Unknown command. Run llvm-profdata --help for usage.\n";
3203   return 1;
3204 }
3205