[lib/ObjectYAML] - Change interface to return `bool` instead of `int`. NFCI
[llvm-complete.git] / tools / llvm-profdata / llvm-profdata.cpp
blob4f825dfb5b87fd0a51ff8e27e6bccd25877a86a2
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.
11 //===----------------------------------------------------------------------===//
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/ProfileData/InstrProfReader.h"
18 #include "llvm/ProfileData/InstrProfWriter.h"
19 #include "llvm/ProfileData/ProfileCommon.h"
20 #include "llvm/ProfileData/SampleProfReader.h"
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Errc.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/ThreadPool.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
34 using namespace llvm;
36 enum ProfileFormat {
37 PF_None = 0,
38 PF_Text,
39 PF_Compact_Binary,
40 PF_Ext_Binary,
41 PF_GCC,
42 PF_Binary
45 static void warn(Twine Message, std::string Whence = "",
46 std::string Hint = "") {
47 WithColor::warning();
48 if (!Whence.empty())
49 errs() << Whence << ": ";
50 errs() << Message << "\n";
51 if (!Hint.empty())
52 WithColor::note() << Hint << "\n";
55 static void exitWithError(Twine Message, std::string Whence = "",
56 std::string Hint = "") {
57 WithColor::error();
58 if (!Whence.empty())
59 errs() << Whence << ": ";
60 errs() << Message << "\n";
61 if (!Hint.empty())
62 WithColor::note() << Hint << "\n";
63 ::exit(1);
66 static void exitWithError(Error E, StringRef Whence = "") {
67 if (E.isA<InstrProfError>()) {
68 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
69 instrprof_error instrError = IPE.get();
70 StringRef Hint = "";
71 if (instrError == instrprof_error::unrecognized_format) {
72 // Hint for common error of forgetting -sample for sample profiles.
73 Hint = "Perhaps you forgot to use the -sample option?";
75 exitWithError(IPE.message(), Whence, Hint);
76 });
79 exitWithError(toString(std::move(E)), Whence);
82 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
83 exitWithError(EC.message(), Whence);
86 namespace {
87 enum ProfileKinds { instr, sample };
88 enum FailureMode { failIfAnyAreInvalid, failIfAllAreInvalid };
91 static void warnOrExitGivenError(FailureMode FailMode, std::error_code EC,
92 StringRef Whence = "") {
93 if (FailMode == failIfAnyAreInvalid)
94 exitWithErrorCode(EC, Whence);
95 else
96 warn(EC.message(), Whence);
99 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
100 StringRef WhenceFunction = "",
101 bool ShowHint = true) {
102 if (!WhenceFile.empty())
103 errs() << WhenceFile << ": ";
104 if (!WhenceFunction.empty())
105 errs() << WhenceFunction << ": ";
107 auto IPE = instrprof_error::success;
108 E = handleErrors(std::move(E),
109 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
110 IPE = E->get();
111 return Error(std::move(E));
113 errs() << toString(std::move(E)) << "\n";
115 if (ShowHint) {
116 StringRef Hint = "";
117 if (IPE != instrprof_error::success) {
118 switch (IPE) {
119 case instrprof_error::hash_mismatch:
120 case instrprof_error::count_mismatch:
121 case instrprof_error::value_site_count_mismatch:
122 Hint = "Make sure that all profile data to be merged is generated "
123 "from the same binary.";
124 break;
125 default:
126 break;
130 if (!Hint.empty())
131 errs() << Hint << "\n";
135 namespace {
136 /// A remapper from original symbol names to new symbol names based on a file
137 /// containing a list of mappings from old name to new name.
138 class SymbolRemapper {
139 std::unique_ptr<MemoryBuffer> File;
140 DenseMap<StringRef, StringRef> RemappingTable;
142 public:
143 /// Build a SymbolRemapper from a file containing a list of old/new symbols.
144 static std::unique_ptr<SymbolRemapper> create(StringRef InputFile) {
145 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile);
146 if (!BufOrError)
147 exitWithErrorCode(BufOrError.getError(), InputFile);
149 auto Remapper = std::make_unique<SymbolRemapper>();
150 Remapper->File = std::move(BufOrError.get());
152 for (line_iterator LineIt(*Remapper->File, /*SkipBlanks=*/true, '#');
153 !LineIt.is_at_eof(); ++LineIt) {
154 std::pair<StringRef, StringRef> Parts = LineIt->split(' ');
155 if (Parts.first.empty() || Parts.second.empty() ||
156 Parts.second.count(' ')) {
157 exitWithError("unexpected line in remapping file",
158 (InputFile + ":" + Twine(LineIt.line_number())).str(),
159 "expected 'old_symbol new_symbol'");
161 Remapper->RemappingTable.insert(Parts);
163 return Remapper;
166 /// Attempt to map the given old symbol into a new symbol.
168 /// \return The new symbol, or \p Name if no such symbol was found.
169 StringRef operator()(StringRef Name) {
170 StringRef New = RemappingTable.lookup(Name);
171 return New.empty() ? Name : New;
176 struct WeightedFile {
177 std::string Filename;
178 uint64_t Weight;
180 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
182 /// Keep track of merged data and reported errors.
183 struct WriterContext {
184 std::mutex Lock;
185 InstrProfWriter Writer;
186 std::vector<std::pair<Error, std::string>> Errors;
187 std::mutex &ErrLock;
188 SmallSet<instrprof_error, 4> &WriterErrorCodes;
190 WriterContext(bool IsSparse, std::mutex &ErrLock,
191 SmallSet<instrprof_error, 4> &WriterErrorCodes)
192 : Lock(), Writer(IsSparse), Errors(), ErrLock(ErrLock),
193 WriterErrorCodes(WriterErrorCodes) {}
196 /// Computer the overlap b/w profile BaseFilename and TestFileName,
197 /// and store the program level result to Overlap.
198 static void overlapInput(const std::string &BaseFilename,
199 const std::string &TestFilename, WriterContext *WC,
200 OverlapStats &Overlap,
201 const OverlapFuncFilters &FuncFilter,
202 raw_fd_ostream &OS, bool IsCS) {
203 auto ReaderOrErr = InstrProfReader::create(TestFilename);
204 if (Error E = ReaderOrErr.takeError()) {
205 // Skip the empty profiles by returning sliently.
206 instrprof_error IPE = InstrProfError::take(std::move(E));
207 if (IPE != instrprof_error::empty_raw_profile)
208 WC->Errors.emplace_back(make_error<InstrProfError>(IPE), TestFilename);
209 return;
212 auto Reader = std::move(ReaderOrErr.get());
213 for (auto &I : *Reader) {
214 OverlapStats FuncOverlap(OverlapStats::FunctionLevel);
215 FuncOverlap.setFuncInfo(I.Name, I.Hash);
217 WC->Writer.overlapRecord(std::move(I), Overlap, FuncOverlap, FuncFilter);
218 FuncOverlap.dump(OS);
222 /// Load an input into a writer context.
223 static void loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
224 WriterContext *WC) {
225 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
227 // Copy the filename, because llvm::ThreadPool copied the input "const
228 // WeightedFile &" by value, making a reference to the filename within it
229 // invalid outside of this packaged task.
230 std::string Filename = Input.Filename;
232 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
233 if (Error E = ReaderOrErr.takeError()) {
234 // Skip the empty profiles by returning sliently.
235 instrprof_error IPE = InstrProfError::take(std::move(E));
236 if (IPE != instrprof_error::empty_raw_profile)
237 WC->Errors.emplace_back(make_error<InstrProfError>(IPE), Filename);
238 return;
241 auto Reader = std::move(ReaderOrErr.get());
242 bool IsIRProfile = Reader->isIRLevelProfile();
243 bool HasCSIRProfile = Reader->hasCSIRLevelProfile();
244 if (WC->Writer.setIsIRLevelProfile(IsIRProfile, HasCSIRProfile)) {
245 WC->Errors.emplace_back(
246 make_error<StringError>(
247 "Merge IR generated profile with Clang generated profile.",
248 std::error_code()),
249 Filename);
250 return;
253 for (auto &I : *Reader) {
254 if (Remapper)
255 I.Name = (*Remapper)(I.Name);
256 const StringRef FuncName = I.Name;
257 bool Reported = false;
258 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
259 if (Reported) {
260 consumeError(std::move(E));
261 return;
263 Reported = true;
264 // Only show hint the first time an error occurs.
265 instrprof_error IPE = InstrProfError::take(std::move(E));
266 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
267 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
268 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
269 FuncName, firstTime);
272 if (Reader->hasError())
273 if (Error E = Reader->getError())
274 WC->Errors.emplace_back(std::move(E), Filename);
277 /// Merge the \p Src writer context into \p Dst.
278 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
279 for (auto &ErrorPair : Src->Errors)
280 Dst->Errors.push_back(std::move(ErrorPair));
281 Src->Errors.clear();
283 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
284 instrprof_error IPE = InstrProfError::take(std::move(E));
285 std::unique_lock<std::mutex> ErrGuard{Dst->ErrLock};
286 bool firstTime = Dst->WriterErrorCodes.insert(IPE).second;
287 if (firstTime)
288 warn(toString(make_error<InstrProfError>(IPE)));
292 static void mergeInstrProfile(const WeightedFileVector &Inputs,
293 SymbolRemapper *Remapper,
294 StringRef OutputFilename,
295 ProfileFormat OutputFormat, bool OutputSparse,
296 unsigned NumThreads, FailureMode FailMode) {
297 if (OutputFilename.compare("-") == 0)
298 exitWithError("Cannot write indexed profdata format to stdout.");
300 if (OutputFormat != PF_Binary && OutputFormat != PF_Compact_Binary &&
301 OutputFormat != PF_Ext_Binary && OutputFormat != PF_Text)
302 exitWithError("Unknown format is specified.");
304 std::mutex ErrorLock;
305 SmallSet<instrprof_error, 4> WriterErrorCodes;
307 // If NumThreads is not specified, auto-detect a good default.
308 if (NumThreads == 0)
309 NumThreads =
310 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
312 // Initialize the writer contexts.
313 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
314 for (unsigned I = 0; I < NumThreads; ++I)
315 Contexts.emplace_back(std::make_unique<WriterContext>(
316 OutputSparse, ErrorLock, WriterErrorCodes));
318 if (NumThreads == 1) {
319 for (const auto &Input : Inputs)
320 loadInput(Input, Remapper, Contexts[0].get());
321 } else {
322 ThreadPool Pool(NumThreads);
324 // Load the inputs in parallel (N/NumThreads serial steps).
325 unsigned Ctx = 0;
326 for (const auto &Input : Inputs) {
327 Pool.async(loadInput, Input, Remapper, Contexts[Ctx].get());
328 Ctx = (Ctx + 1) % NumThreads;
330 Pool.wait();
332 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
333 unsigned Mid = Contexts.size() / 2;
334 unsigned End = Contexts.size();
335 assert(Mid > 0 && "Expected more than one context");
336 do {
337 for (unsigned I = 0; I < Mid; ++I)
338 Pool.async(mergeWriterContexts, Contexts[I].get(),
339 Contexts[I + Mid].get());
340 Pool.wait();
341 if (End & 1) {
342 Pool.async(mergeWriterContexts, Contexts[0].get(),
343 Contexts[End - 1].get());
344 Pool.wait();
346 End = Mid;
347 Mid /= 2;
348 } while (Mid > 0);
351 // Handle deferred errors encountered during merging. If the number of errors
352 // is equal to the number of inputs the merge failed.
353 unsigned NumErrors = 0;
354 for (std::unique_ptr<WriterContext> &WC : Contexts) {
355 for (auto &ErrorPair : WC->Errors) {
356 ++NumErrors;
357 warn(toString(std::move(ErrorPair.first)), ErrorPair.second);
360 if (NumErrors == Inputs.size() ||
361 (NumErrors > 0 && FailMode == failIfAnyAreInvalid))
362 exitWithError("No profiles could be merged.");
364 std::error_code EC;
365 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::OF_None);
366 if (EC)
367 exitWithErrorCode(EC, OutputFilename);
369 InstrProfWriter &Writer = Contexts[0]->Writer;
370 if (OutputFormat == PF_Text) {
371 if (Error E = Writer.writeText(Output))
372 exitWithError(std::move(E));
373 } else {
374 Writer.write(Output);
378 /// Make a copy of the given function samples with all symbol names remapped
379 /// by the provided symbol remapper.
380 static sampleprof::FunctionSamples
381 remapSamples(const sampleprof::FunctionSamples &Samples,
382 SymbolRemapper &Remapper, sampleprof_error &Error) {
383 sampleprof::FunctionSamples Result;
384 Result.setName(Remapper(Samples.getName()));
385 Result.addTotalSamples(Samples.getTotalSamples());
386 Result.addHeadSamples(Samples.getHeadSamples());
387 for (const auto &BodySample : Samples.getBodySamples()) {
388 Result.addBodySamples(BodySample.first.LineOffset,
389 BodySample.first.Discriminator,
390 BodySample.second.getSamples());
391 for (const auto &Target : BodySample.second.getCallTargets()) {
392 Result.addCalledTargetSamples(BodySample.first.LineOffset,
393 BodySample.first.Discriminator,
394 Remapper(Target.first()), Target.second);
397 for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) {
398 sampleprof::FunctionSamplesMap &Target =
399 Result.functionSamplesAt(CallsiteSamples.first);
400 for (const auto &Callsite : CallsiteSamples.second) {
401 sampleprof::FunctionSamples Remapped =
402 remapSamples(Callsite.second, Remapper, Error);
403 MergeResult(Error, Target[Remapped.getName()].merge(Remapped));
406 return Result;
409 static sampleprof::SampleProfileFormat FormatMap[] = {
410 sampleprof::SPF_None,
411 sampleprof::SPF_Text,
412 sampleprof::SPF_Compact_Binary,
413 sampleprof::SPF_Ext_Binary,
414 sampleprof::SPF_GCC,
415 sampleprof::SPF_Binary};
417 static std::unique_ptr<MemoryBuffer>
418 getInputFileBuf(const StringRef &InputFile) {
419 if (InputFile == "")
420 return {};
422 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile);
423 if (!BufOrError)
424 exitWithErrorCode(BufOrError.getError(), InputFile);
426 return std::move(*BufOrError);
429 static void populateProfileSymbolList(MemoryBuffer *Buffer,
430 sampleprof::ProfileSymbolList &PSL) {
431 if (!Buffer)
432 return;
434 SmallVector<StringRef, 32> SymbolVec;
435 StringRef Data = Buffer->getBuffer();
436 Data.split(SymbolVec, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
438 for (StringRef symbol : SymbolVec)
439 PSL.add(symbol);
442 static void mergeSampleProfile(const WeightedFileVector &Inputs,
443 SymbolRemapper *Remapper,
444 StringRef OutputFilename,
445 ProfileFormat OutputFormat,
446 StringRef ProfileSymbolListFile,
447 bool CompressProfSymList, FailureMode FailMode) {
448 using namespace sampleprof;
449 StringMap<FunctionSamples> ProfileMap;
450 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
451 LLVMContext Context;
452 sampleprof::ProfileSymbolList WriterList;
453 for (const auto &Input : Inputs) {
454 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
455 if (std::error_code EC = ReaderOrErr.getError()) {
456 warnOrExitGivenError(FailMode, EC, Input.Filename);
457 continue;
460 // We need to keep the readers around until after all the files are
461 // read so that we do not lose the function names stored in each
462 // reader's memory. The function names are needed to write out the
463 // merged profile map.
464 Readers.push_back(std::move(ReaderOrErr.get()));
465 const auto Reader = Readers.back().get();
466 if (std::error_code EC = Reader->read()) {
467 warnOrExitGivenError(FailMode, EC, Input.Filename);
468 Readers.pop_back();
469 continue;
472 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
473 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
474 E = Profiles.end();
475 I != E; ++I) {
476 sampleprof_error Result = sampleprof_error::success;
477 FunctionSamples Remapped =
478 Remapper ? remapSamples(I->second, *Remapper, Result)
479 : FunctionSamples();
480 FunctionSamples &Samples = Remapper ? Remapped : I->second;
481 StringRef FName = Samples.getName();
482 MergeResult(Result, ProfileMap[FName].merge(Samples, Input.Weight));
483 if (Result != sampleprof_error::success) {
484 std::error_code EC = make_error_code(Result);
485 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
489 std::unique_ptr<sampleprof::ProfileSymbolList> ReaderList =
490 Reader->getProfileSymbolList();
491 if (ReaderList)
492 WriterList.merge(*ReaderList);
494 auto WriterOrErr =
495 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
496 if (std::error_code EC = WriterOrErr.getError())
497 exitWithErrorCode(EC, OutputFilename);
499 // WriterList will have StringRef refering to string in Buffer.
500 // Make sure Buffer lives as long as WriterList.
501 auto Buffer = getInputFileBuf(ProfileSymbolListFile);
502 populateProfileSymbolList(Buffer.get(), WriterList);
503 WriterList.setToCompress(CompressProfSymList);
504 if (WriterList.size() > 0 && OutputFormat != PF_Ext_Binary)
505 warn("Profile Symbol list is not empty but the output format is not "
506 "ExtBinary format. The list will be lost in the output. ");
508 auto Writer = std::move(WriterOrErr.get());
509 Writer->setProfileSymbolList(&WriterList);
510 Writer->write(ProfileMap);
513 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
514 StringRef WeightStr, FileName;
515 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
517 uint64_t Weight;
518 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
519 exitWithError("Input weight must be a positive integer.");
521 return {FileName, Weight};
524 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
525 StringRef Filename = WF.Filename;
526 uint64_t Weight = WF.Weight;
528 // If it's STDIN just pass it on.
529 if (Filename == "-") {
530 WNI.push_back({Filename, Weight});
531 return;
534 llvm::sys::fs::file_status Status;
535 llvm::sys::fs::status(Filename, Status);
536 if (!llvm::sys::fs::exists(Status))
537 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
538 Filename);
539 // If it's a source file, collect it.
540 if (llvm::sys::fs::is_regular_file(Status)) {
541 WNI.push_back({Filename, Weight});
542 return;
545 if (llvm::sys::fs::is_directory(Status)) {
546 std::error_code EC;
547 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
548 F != E && !EC; F.increment(EC)) {
549 if (llvm::sys::fs::is_regular_file(F->path())) {
550 addWeightedInput(WNI, {F->path(), Weight});
553 if (EC)
554 exitWithErrorCode(EC, Filename);
558 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
559 WeightedFileVector &WFV) {
560 if (!Buffer)
561 return;
563 SmallVector<StringRef, 8> Entries;
564 StringRef Data = Buffer->getBuffer();
565 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
566 for (const StringRef &FileWeightEntry : Entries) {
567 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
568 // Skip comments.
569 if (SanitizedEntry.startswith("#"))
570 continue;
571 // If there's no comma, it's an unweighted profile.
572 else if (SanitizedEntry.find(',') == StringRef::npos)
573 addWeightedInput(WFV, {SanitizedEntry, 1});
574 else
575 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
579 static int merge_main(int argc, const char *argv[]) {
580 cl::list<std::string> InputFilenames(cl::Positional,
581 cl::desc("<filename...>"));
582 cl::list<std::string> WeightedInputFilenames("weighted-input",
583 cl::desc("<weight>,<filename>"));
584 cl::opt<std::string> InputFilenamesFile(
585 "input-files", cl::init(""),
586 cl::desc("Path to file containing newline-separated "
587 "[<weight>,]<filename> entries"));
588 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
589 cl::aliasopt(InputFilenamesFile));
590 cl::opt<bool> DumpInputFileList(
591 "dump-input-file-list", cl::init(false), cl::Hidden,
592 cl::desc("Dump the list of input files and their weights, then exit"));
593 cl::opt<std::string> RemappingFile("remapping-file", cl::value_desc("file"),
594 cl::desc("Symbol remapping file"));
595 cl::alias RemappingFileA("r", cl::desc("Alias for --remapping-file"),
596 cl::aliasopt(RemappingFile));
597 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
598 cl::init("-"), cl::Required,
599 cl::desc("Output file"));
600 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
601 cl::aliasopt(OutputFilename));
602 cl::opt<ProfileKinds> ProfileKind(
603 cl::desc("Profile kind:"), cl::init(instr),
604 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
605 clEnumVal(sample, "Sample profile")));
606 cl::opt<ProfileFormat> OutputFormat(
607 cl::desc("Format of output profile"), cl::init(PF_Binary),
608 cl::values(
609 clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
610 clEnumValN(PF_Compact_Binary, "compbinary",
611 "Compact binary encoding"),
612 clEnumValN(PF_Ext_Binary, "extbinary", "Extensible binary encoding"),
613 clEnumValN(PF_Text, "text", "Text encoding"),
614 clEnumValN(PF_GCC, "gcc",
615 "GCC encoding (only meaningful for -sample)")));
616 cl::opt<FailureMode> FailureMode(
617 "failure-mode", cl::init(failIfAnyAreInvalid), cl::desc("Failure mode:"),
618 cl::values(clEnumValN(failIfAnyAreInvalid, "any",
619 "Fail if any profile is invalid."),
620 clEnumValN(failIfAllAreInvalid, "all",
621 "Fail only if all profiles are invalid.")));
622 cl::opt<bool> OutputSparse("sparse", cl::init(false),
623 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
624 cl::opt<unsigned> NumThreads(
625 "num-threads", cl::init(0),
626 cl::desc("Number of merge threads to use (default: autodetect)"));
627 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
628 cl::aliasopt(NumThreads));
629 cl::opt<std::string> ProfileSymbolListFile(
630 "prof-sym-list", cl::init(""),
631 cl::desc("Path to file containing the list of function symbols "
632 "used to populate profile symbol list"));
633 cl::opt<bool> CompressProfSymList(
634 "compress-prof-sym-list", cl::init(false), cl::Hidden,
635 cl::desc("Compress profile symbol list before write it into profile. "));
637 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
639 WeightedFileVector WeightedInputs;
640 for (StringRef Filename : InputFilenames)
641 addWeightedInput(WeightedInputs, {Filename, 1});
642 for (StringRef WeightedFilename : WeightedInputFilenames)
643 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
645 // Make sure that the file buffer stays alive for the duration of the
646 // weighted input vector's lifetime.
647 auto Buffer = getInputFileBuf(InputFilenamesFile);
648 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
650 if (WeightedInputs.empty())
651 exitWithError("No input files specified. See " +
652 sys::path::filename(argv[0]) + " -help");
654 if (DumpInputFileList) {
655 for (auto &WF : WeightedInputs)
656 outs() << WF.Weight << "," << WF.Filename << "\n";
657 return 0;
660 std::unique_ptr<SymbolRemapper> Remapper;
661 if (!RemappingFile.empty())
662 Remapper = SymbolRemapper::create(RemappingFile);
664 if (ProfileKind == instr)
665 mergeInstrProfile(WeightedInputs, Remapper.get(), OutputFilename,
666 OutputFormat, OutputSparse, NumThreads, FailureMode);
667 else
668 mergeSampleProfile(WeightedInputs, Remapper.get(), OutputFilename,
669 OutputFormat, ProfileSymbolListFile,
670 CompressProfSymList, FailureMode);
672 return 0;
675 /// Computer the overlap b/w profile BaseFilename and profile TestFilename.
676 static void overlapInstrProfile(const std::string &BaseFilename,
677 const std::string &TestFilename,
678 const OverlapFuncFilters &FuncFilter,
679 raw_fd_ostream &OS, bool IsCS) {
680 std::mutex ErrorLock;
681 SmallSet<instrprof_error, 4> WriterErrorCodes;
682 WriterContext Context(false, ErrorLock, WriterErrorCodes);
683 WeightedFile WeightedInput{BaseFilename, 1};
684 OverlapStats Overlap;
685 Error E = Overlap.accumuateCounts(BaseFilename, TestFilename, IsCS);
686 if (E)
687 exitWithError(std::move(E), "Error in getting profile count sums");
688 if (Overlap.Base.CountSum < 1.0f) {
689 OS << "Sum of edge counts for profile " << BaseFilename << " is 0.\n";
690 exit(0);
692 if (Overlap.Test.CountSum < 1.0f) {
693 OS << "Sum of edge counts for profile " << TestFilename << " is 0.\n";
694 exit(0);
696 loadInput(WeightedInput, nullptr, &Context);
697 overlapInput(BaseFilename, TestFilename, &Context, Overlap, FuncFilter, OS,
698 IsCS);
699 Overlap.dump(OS);
702 static int overlap_main(int argc, const char *argv[]) {
703 cl::opt<std::string> BaseFilename(cl::Positional, cl::Required,
704 cl::desc("<base profile file>"));
705 cl::opt<std::string> TestFilename(cl::Positional, cl::Required,
706 cl::desc("<test profile file>"));
707 cl::opt<std::string> Output("output", cl::value_desc("output"), cl::init("-"),
708 cl::desc("Output file"));
709 cl::alias OutputA("o", cl::desc("Alias for --output"), cl::aliasopt(Output));
710 cl::opt<bool> IsCS("cs", cl::init(false),
711 cl::desc("For context sensitive counts"));
712 cl::opt<unsigned long long> ValueCutoff(
713 "value-cutoff", cl::init(-1),
714 cl::desc(
715 "Function level overlap information for every function in test "
716 "profile with max count value greater then the parameter value"));
717 cl::opt<std::string> FuncNameFilter(
718 "function",
719 cl::desc("Function level overlap information for matching functions"));
720 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data overlap tool\n");
722 std::error_code EC;
723 raw_fd_ostream OS(Output.data(), EC, sys::fs::OF_Text);
724 if (EC)
725 exitWithErrorCode(EC, Output);
727 overlapInstrProfile(BaseFilename, TestFilename,
728 OverlapFuncFilters{ValueCutoff, FuncNameFilter}, OS,
729 IsCS);
731 return 0;
734 typedef struct ValueSitesStats {
735 ValueSitesStats()
736 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
737 TotalNumValues(0) {}
738 uint64_t TotalNumValueSites;
739 uint64_t TotalNumValueSitesWithValueProfile;
740 uint64_t TotalNumValues;
741 std::vector<unsigned> ValueSitesHistogram;
742 } ValueSitesStats;
744 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
745 ValueSitesStats &Stats, raw_fd_ostream &OS,
746 InstrProfSymtab *Symtab) {
747 uint32_t NS = Func.getNumValueSites(VK);
748 Stats.TotalNumValueSites += NS;
749 for (size_t I = 0; I < NS; ++I) {
750 uint32_t NV = Func.getNumValueDataForSite(VK, I);
751 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
752 Stats.TotalNumValues += NV;
753 if (NV) {
754 Stats.TotalNumValueSitesWithValueProfile++;
755 if (NV > Stats.ValueSitesHistogram.size())
756 Stats.ValueSitesHistogram.resize(NV, 0);
757 Stats.ValueSitesHistogram[NV - 1]++;
760 uint64_t SiteSum = 0;
761 for (uint32_t V = 0; V < NV; V++)
762 SiteSum += VD[V].Count;
763 if (SiteSum == 0)
764 SiteSum = 1;
766 for (uint32_t V = 0; V < NV; V++) {
767 OS << "\t[ " << format("%2u", I) << ", ";
768 if (Symtab == nullptr)
769 OS << format("%4" PRIu64, VD[V].Value);
770 else
771 OS << Symtab->getFuncName(VD[V].Value);
772 OS << ", " << format("%10" PRId64, VD[V].Count) << " ] ("
773 << format("%.2f%%", (VD[V].Count * 100.0 / SiteSum)) << ")\n";
778 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
779 ValueSitesStats &Stats) {
780 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n";
781 OS << " Total number of sites with values: "
782 << Stats.TotalNumValueSitesWithValueProfile << "\n";
783 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n";
785 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n";
786 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
787 if (Stats.ValueSitesHistogram[I] > 0)
788 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
792 static int showInstrProfile(const std::string &Filename, bool ShowCounts,
793 uint32_t TopN, bool ShowIndirectCallTargets,
794 bool ShowMemOPSizes, bool ShowDetailedSummary,
795 std::vector<uint32_t> DetailedSummaryCutoffs,
796 bool ShowAllFunctions, bool ShowCS,
797 uint64_t ValueCutoff, bool OnlyListBelow,
798 const std::string &ShowFunction, bool TextFormat,
799 raw_fd_ostream &OS) {
800 auto ReaderOrErr = InstrProfReader::create(Filename);
801 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
802 if (ShowDetailedSummary && Cutoffs.empty()) {
803 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
805 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
806 if (Error E = ReaderOrErr.takeError())
807 exitWithError(std::move(E), Filename);
809 auto Reader = std::move(ReaderOrErr.get());
810 bool IsIRInstr = Reader->isIRLevelProfile();
811 size_t ShownFunctions = 0;
812 size_t BelowCutoffFunctions = 0;
813 int NumVPKind = IPVK_Last - IPVK_First + 1;
814 std::vector<ValueSitesStats> VPStats(NumVPKind);
816 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
817 const std::pair<std::string, uint64_t> &v2) {
818 return v1.second > v2.second;
821 std::priority_queue<std::pair<std::string, uint64_t>,
822 std::vector<std::pair<std::string, uint64_t>>,
823 decltype(MinCmp)>
824 HottestFuncs(MinCmp);
826 if (!TextFormat && OnlyListBelow) {
827 OS << "The list of functions with the maximum counter less than "
828 << ValueCutoff << ":\n";
831 // Add marker so that IR-level instrumentation round-trips properly.
832 if (TextFormat && IsIRInstr)
833 OS << ":ir\n";
835 for (const auto &Func : *Reader) {
836 if (Reader->isIRLevelProfile()) {
837 bool FuncIsCS = NamedInstrProfRecord::hasCSFlagInHash(Func.Hash);
838 if (FuncIsCS != ShowCS)
839 continue;
841 bool Show =
842 ShowAllFunctions || (!ShowFunction.empty() &&
843 Func.Name.find(ShowFunction) != Func.Name.npos);
845 bool doTextFormatDump = (Show && TextFormat);
847 if (doTextFormatDump) {
848 InstrProfSymtab &Symtab = Reader->getSymtab();
849 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
850 OS);
851 continue;
854 assert(Func.Counts.size() > 0 && "function missing entry counter");
855 Builder.addRecord(Func);
857 uint64_t FuncMax = 0;
858 uint64_t FuncSum = 0;
859 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I) {
860 FuncMax = std::max(FuncMax, Func.Counts[I]);
861 FuncSum += Func.Counts[I];
864 if (FuncMax < ValueCutoff) {
865 ++BelowCutoffFunctions;
866 if (OnlyListBelow) {
867 OS << " " << Func.Name << ": (Max = " << FuncMax
868 << " Sum = " << FuncSum << ")\n";
870 continue;
871 } else if (OnlyListBelow)
872 continue;
874 if (TopN) {
875 if (HottestFuncs.size() == TopN) {
876 if (HottestFuncs.top().second < FuncMax) {
877 HottestFuncs.pop();
878 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
880 } else
881 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
884 if (Show) {
885 if (!ShownFunctions)
886 OS << "Counters:\n";
888 ++ShownFunctions;
890 OS << " " << Func.Name << ":\n"
891 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
892 << " Counters: " << Func.Counts.size() << "\n";
893 if (!IsIRInstr)
894 OS << " Function count: " << Func.Counts[0] << "\n";
896 if (ShowIndirectCallTargets)
897 OS << " Indirect Call Site Count: "
898 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
900 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
901 if (ShowMemOPSizes && NumMemOPCalls > 0)
902 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls
903 << "\n";
905 if (ShowCounts) {
906 OS << " Block counts: [";
907 size_t Start = (IsIRInstr ? 0 : 1);
908 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
909 OS << (I == Start ? "" : ", ") << Func.Counts[I];
911 OS << "]\n";
914 if (ShowIndirectCallTargets) {
915 OS << " Indirect Target Results:\n";
916 traverseAllValueSites(Func, IPVK_IndirectCallTarget,
917 VPStats[IPVK_IndirectCallTarget], OS,
918 &(Reader->getSymtab()));
921 if (ShowMemOPSizes && NumMemOPCalls > 0) {
922 OS << " Memory Intrinsic Size Results:\n";
923 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
924 nullptr);
928 if (Reader->hasError())
929 exitWithError(Reader->getError(), Filename);
931 if (TextFormat)
932 return 0;
933 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
934 OS << "Instrumentation level: "
935 << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
936 if (ShowAllFunctions || !ShowFunction.empty())
937 OS << "Functions shown: " << ShownFunctions << "\n";
938 OS << "Total functions: " << PS->getNumFunctions() << "\n";
939 if (ValueCutoff > 0) {
940 OS << "Number of functions with maximum count (< " << ValueCutoff
941 << "): " << BelowCutoffFunctions << "\n";
942 OS << "Number of functions with maximum count (>= " << ValueCutoff
943 << "): " << PS->getNumFunctions() - BelowCutoffFunctions << "\n";
945 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
946 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
948 if (TopN) {
949 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
950 while (!HottestFuncs.empty()) {
951 SortedHottestFuncs.emplace_back(HottestFuncs.top());
952 HottestFuncs.pop();
954 OS << "Top " << TopN
955 << " functions with the largest internal block counts: \n";
956 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
957 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
960 if (ShownFunctions && ShowIndirectCallTargets) {
961 OS << "Statistics for indirect call sites profile:\n";
962 showValueSitesStats(OS, IPVK_IndirectCallTarget,
963 VPStats[IPVK_IndirectCallTarget]);
966 if (ShownFunctions && ShowMemOPSizes) {
967 OS << "Statistics for memory intrinsic calls sizes profile:\n";
968 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
971 if (ShowDetailedSummary) {
972 OS << "Detailed summary:\n";
973 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
974 OS << "Total count: " << PS->getTotalCount() << "\n";
975 for (auto Entry : PS->getDetailedSummary()) {
976 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
977 << " account for "
978 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
979 << " percentage of the total counts.\n";
982 return 0;
985 static int showSampleProfile(const std::string &Filename, bool ShowCounts,
986 bool ShowAllFunctions,
987 const std::string &ShowFunction,
988 bool ShowProfileSymbolList, raw_fd_ostream &OS) {
989 using namespace sampleprof;
990 LLVMContext Context;
991 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
992 if (std::error_code EC = ReaderOrErr.getError())
993 exitWithErrorCode(EC, Filename);
995 auto Reader = std::move(ReaderOrErr.get());
996 if (std::error_code EC = Reader->read())
997 exitWithErrorCode(EC, Filename);
999 if (ShowAllFunctions || ShowFunction.empty())
1000 Reader->dump(OS);
1001 else
1002 Reader->dumpFunctionProfile(ShowFunction, OS);
1004 if (ShowProfileSymbolList) {
1005 std::unique_ptr<sampleprof::ProfileSymbolList> ReaderList =
1006 Reader->getProfileSymbolList();
1007 ReaderList->dump(OS);
1010 return 0;
1013 static int show_main(int argc, const char *argv[]) {
1014 cl::opt<std::string> Filename(cl::Positional, cl::Required,
1015 cl::desc("<profdata-file>"));
1017 cl::opt<bool> ShowCounts("counts", cl::init(false),
1018 cl::desc("Show counter values for shown functions"));
1019 cl::opt<bool> TextFormat(
1020 "text", cl::init(false),
1021 cl::desc("Show instr profile data in text dump format"));
1022 cl::opt<bool> ShowIndirectCallTargets(
1023 "ic-targets", cl::init(false),
1024 cl::desc("Show indirect call site target values for shown functions"));
1025 cl::opt<bool> ShowMemOPSizes(
1026 "memop-sizes", cl::init(false),
1027 cl::desc("Show the profiled sizes of the memory intrinsic calls "
1028 "for shown functions"));
1029 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
1030 cl::desc("Show detailed profile summary"));
1031 cl::list<uint32_t> DetailedSummaryCutoffs(
1032 cl::CommaSeparated, "detailed-summary-cutoffs",
1033 cl::desc(
1034 "Cutoff percentages (times 10000) for generating detailed summary"),
1035 cl::value_desc("800000,901000,999999"));
1036 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
1037 cl::desc("Details for every function"));
1038 cl::opt<bool> ShowCS("showcs", cl::init(false),
1039 cl::desc("Show context sensitive counts"));
1040 cl::opt<std::string> ShowFunction("function",
1041 cl::desc("Details for matching functions"));
1043 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
1044 cl::init("-"), cl::desc("Output file"));
1045 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
1046 cl::aliasopt(OutputFilename));
1047 cl::opt<ProfileKinds> ProfileKind(
1048 cl::desc("Profile kind:"), cl::init(instr),
1049 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
1050 clEnumVal(sample, "Sample profile")));
1051 cl::opt<uint32_t> TopNFunctions(
1052 "topn", cl::init(0),
1053 cl::desc("Show the list of functions with the largest internal counts"));
1054 cl::opt<uint32_t> ValueCutoff(
1055 "value-cutoff", cl::init(0),
1056 cl::desc("Set the count value cutoff. Functions with the maximum count "
1057 "less than this value will not be printed out. (Default is 0)"));
1058 cl::opt<bool> OnlyListBelow(
1059 "list-below-cutoff", cl::init(false),
1060 cl::desc("Only output names of functions whose max count values are "
1061 "below the cutoff value"));
1062 cl::opt<bool> ShowProfileSymbolList(
1063 "show-prof-sym-list", cl::init(false),
1064 cl::desc("Show profile symbol list if it exists in the profile. "));
1066 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
1068 if (OutputFilename.empty())
1069 OutputFilename = "-";
1071 if (!Filename.compare(OutputFilename)) {
1072 errs() << sys::path::filename(argv[0])
1073 << ": Input file name cannot be the same as the output file name!\n";
1074 return 1;
1077 std::error_code EC;
1078 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_Text);
1079 if (EC)
1080 exitWithErrorCode(EC, OutputFilename);
1082 if (ShowAllFunctions && !ShowFunction.empty())
1083 WithColor::warning() << "-function argument ignored: showing all functions\n";
1085 if (ProfileKind == instr)
1086 return showInstrProfile(Filename, ShowCounts, TopNFunctions,
1087 ShowIndirectCallTargets, ShowMemOPSizes,
1088 ShowDetailedSummary, DetailedSummaryCutoffs,
1089 ShowAllFunctions, ShowCS, ValueCutoff,
1090 OnlyListBelow, ShowFunction, TextFormat, OS);
1091 else
1092 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
1093 ShowFunction, ShowProfileSymbolList, OS);
1096 int main(int argc, const char *argv[]) {
1097 InitLLVM X(argc, argv);
1099 StringRef ProgName(sys::path::filename(argv[0]));
1100 if (argc > 1) {
1101 int (*func)(int, const char *[]) = nullptr;
1103 if (strcmp(argv[1], "merge") == 0)
1104 func = merge_main;
1105 else if (strcmp(argv[1], "show") == 0)
1106 func = show_main;
1107 else if (strcmp(argv[1], "overlap") == 0)
1108 func = overlap_main;
1110 if (func) {
1111 std::string Invocation(ProgName.str() + " " + argv[1]);
1112 argv[1] = Invocation.c_str();
1113 return func(argc - 1, argv + 1);
1116 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
1117 strcmp(argv[1], "--help") == 0) {
1119 errs() << "OVERVIEW: LLVM profile data tools\n\n"
1120 << "USAGE: " << ProgName << " <command> [args...]\n"
1121 << "USAGE: " << ProgName << " <command> -help\n\n"
1122 << "See each individual command --help for more details.\n"
1123 << "Available commands: merge, show, overlap\n";
1124 return 0;
1128 if (argc < 2)
1129 errs() << ProgName << ": No command specified!\n";
1130 else
1131 errs() << ProgName << ": Unknown command!\n";
1133 errs() << "USAGE: " << ProgName << " <merge|show|overlap> [args...]\n";
1134 return 1;