blob: 8e21a7a9b4fc93116c3f1ef563a7776570ea71a1 [file] [log] [blame]
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +00001//===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// llvm-profdata merges .profdata files.
11//
12//===----------------------------------------------------------------------===//
13
Nathan Slingerlandc21a44d2015-11-18 17:10:24 +000014#include "llvm/ADT/SmallSet.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000015#include "llvm/ADT/SmallVector.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000016#include "llvm/ADT/StringRef.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/LLVMContext.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000018#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000019#include "llvm/ProfileData/InstrProfWriter.h"
Easwaran Ramand68aae22016-02-04 23:34:31 +000020#include "llvm/ProfileData/ProfileCommon.h"
Diego Novillod5336ae2014-11-01 00:56:55 +000021#include "llvm/ProfileData/SampleProfReader.h"
22#include "llvm/ProfileData/SampleProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000023#include "llvm/Support/CommandLine.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000024#include "llvm/Support/Errc.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000025#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000026#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000027#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000029#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000030#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/Signals.h"
Vedant Kumare3a0bf52016-07-19 01:17:20 +000032#include "llvm/Support/ThreadPool.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000033#include "llvm/Support/raw_ostream.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000034#include <algorithm>
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000035
36using namespace llvm;
37
Xinliang David Li6f7c19a2015-11-23 20:47:38 +000038enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
39
Diego Novillod3babdb2015-12-14 20:37:15 +000040static void exitWithError(const Twine &Message, StringRef Whence = "",
Nathan Slingerland4f823662015-11-13 03:47:58 +000041 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000042 errs() << "error: ";
43 if (!Whence.empty())
44 errs() << Whence << ": ";
45 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000046 if (!Hint.empty())
47 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000048 ::exit(1);
49}
50
Vedant Kumar9152fd12016-05-19 03:54:45 +000051static void exitWithError(Error E, StringRef Whence = "") {
52 if (E.isA<InstrProfError>()) {
53 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
54 instrprof_error instrError = IPE.get();
55 StringRef Hint = "";
56 if (instrError == instrprof_error::unrecognized_format) {
57 // Hint for common error of forgetting -sample for sample profiles.
58 Hint = "Perhaps you forgot to use the -sample option?";
59 }
60 exitWithError(IPE.message(), Whence, Hint);
61 });
Nathan Slingerland4f823662015-11-13 03:47:58 +000062 }
Vedant Kumar9152fd12016-05-19 03:54:45 +000063
64 exitWithError(toString(std::move(E)), Whence);
65}
66
67static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
68 exitWithError(EC.message(), Whence);
Nathan Slingerland4f823662015-11-13 03:47:58 +000069}
70
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000071namespace {
Diego Novillod3babdb2015-12-14 20:37:15 +000072enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000073}
Justin Bogner618bcea2014-03-19 02:20:46 +000074
Vedant Kumar9152fd12016-05-19 03:54:45 +000075static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000076 StringRef WhenceFunction = "",
Diego Novillod3babdb2015-12-14 20:37:15 +000077 bool ShowHint = true) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000078 if (!WhenceFile.empty())
79 errs() << WhenceFile << ": ";
80 if (!WhenceFunction.empty())
81 errs() << WhenceFunction << ": ";
Vedant Kumar9152fd12016-05-19 03:54:45 +000082
83 auto IPE = instrprof_error::success;
84 E = handleErrors(std::move(E),
85 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
86 IPE = E->get();
87 return Error(std::move(E));
88 });
89 errs() << toString(std::move(E)) << "\n";
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000090
91 if (ShowHint) {
92 StringRef Hint = "";
Vedant Kumar9152fd12016-05-19 03:54:45 +000093 if (IPE != instrprof_error::success) {
94 switch (IPE) {
Nathan Slingerland11c938d12015-11-17 23:37:09 +000095 case instrprof_error::hash_mismatch:
96 case instrprof_error::count_mismatch:
97 case instrprof_error::value_site_count_mismatch:
Diego Novillod3babdb2015-12-14 20:37:15 +000098 Hint = "Make sure that all profile data to be merged is generated "
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000099 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +0000100 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +0000101 default:
102 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000103 }
104 }
105
106 if (!Hint.empty())
107 errs() << Hint << "\n";
108 }
109}
110
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000111struct WeightedFile {
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000112 std::string Filename;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000113 uint64_t Weight;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000114};
115typedef SmallVector<WeightedFile, 5> WeightedFileVector;
116
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000117/// Keep track of merged data and reported errors.
118struct WriterContext {
119 std::mutex Lock;
120 InstrProfWriter Writer;
121 Error Err;
122 StringRef ErrWhence;
123 std::mutex &ErrLock;
124 SmallSet<instrprof_error, 4> &WriterErrorCodes;
125
126 WriterContext(bool IsSparse, std::mutex &ErrLock,
127 SmallSet<instrprof_error, 4> &WriterErrorCodes)
128 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
129 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
130};
131
132/// Load an input into a writer context.
133static void loadInput(const WeightedFile &Input, WriterContext *WC) {
134 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
135
136 // If there's a pending hard error, don't do more work.
137 if (WC->Err)
138 return;
139
140 WC->ErrWhence = Input.Filename;
141
142 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Rong Xu2c684cf2016-10-19 22:51:17 +0000143 if (Error E = ReaderOrErr.takeError()) {
144 // Skip the empty profiles by returning sliently.
145 instrprof_error IPE = InstrProfError::take(std::move(E));
146 if (IPE != instrprof_error::empty_raw_profile)
147 WC->Err = make_error<InstrProfError>(IPE);
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000148 return;
Rong Xu2c684cf2016-10-19 22:51:17 +0000149 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000150
151 auto Reader = std::move(ReaderOrErr.get());
152 bool IsIRProfile = Reader->isIRLevelProfile();
153 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
154 WC->Err = make_error<StringError>(
155 "Merge IR generated profile with Clang generated profile.",
156 std::error_code());
157 return;
158 }
159
160 for (auto &I : *Reader) {
Rong Xufe90d862016-10-19 23:31:59 +0000161 const StringRef FuncName = I.Name;
David Blaikie98cce002017-07-10 03:04:59 +0000162 bool Reported = false;
163 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
164 if (Reported) {
165 consumeError(std::move(E));
166 return;
167 }
168 Reported = true;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000169 // Only show hint the first time an error occurs.
170 instrprof_error IPE = InstrProfError::take(std::move(E));
171 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
172 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
173 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
Rong Xufe90d862016-10-19 23:31:59 +0000174 FuncName, firstTime);
David Blaikie98cce002017-07-10 03:04:59 +0000175 });
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000176 }
177 if (Reader->hasError())
178 WC->Err = Reader->getError();
179}
180
181/// Merge the \p Src writer context into \p Dst.
182static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
David Blaikie98cce002017-07-10 03:04:59 +0000183 bool Reported = false;
184 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
185 if (Reported) {
186 consumeError(std::move(E));
187 return;
188 }
189 Reported = true;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000190 Dst->Err = std::move(E);
David Blaikie98cce002017-07-10 03:04:59 +0000191 });
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000192}
193
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000194static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000195 StringRef OutputFilename,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000196 ProfileFormat OutputFormat, bool OutputSparse,
197 unsigned NumThreads) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000198 if (OutputFilename.compare("-") == 0)
199 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000200
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000201 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
202 exitWithError("Unknown format is specified.");
203
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000204 std::error_code EC;
205 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
206 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000207 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000208
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000209 std::mutex ErrorLock;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000210 SmallSet<instrprof_error, 4> WriterErrorCodes;
Justin Bognerf8d79192014-03-21 17:24:48 +0000211
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000212 // If NumThreads is not specified, auto-detect a good default.
213 if (NumThreads == 0)
Rafael Espindola8c0ff952017-10-04 20:27:01 +0000214 NumThreads =
215 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
Rong Xu33c76c02016-02-10 17:18:30 +0000216
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000217 // Initialize the writer contexts.
218 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
219 for (unsigned I = 0; I < NumThreads; ++I)
220 Contexts.emplace_back(llvm::make_unique<WriterContext>(
221 OutputSparse, ErrorLock, WriterErrorCodes));
222
223 if (NumThreads == 1) {
224 for (const auto &Input : Inputs)
225 loadInput(Input, Contexts[0].get());
226 } else {
227 ThreadPool Pool(NumThreads);
228
229 // Load the inputs in parallel (N/NumThreads serial steps).
230 unsigned Ctx = 0;
231 for (const auto &Input : Inputs) {
232 Pool.async(loadInput, Input, Contexts[Ctx].get());
233 Ctx = (Ctx + 1) % NumThreads;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000234 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000235 Pool.wait();
236
237 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
238 unsigned Mid = Contexts.size() / 2;
239 unsigned End = Contexts.size();
240 assert(Mid > 0 && "Expected more than one context");
241 do {
242 for (unsigned I = 0; I < Mid; ++I)
243 Pool.async(mergeWriterContexts, Contexts[I].get(),
244 Contexts[I + Mid].get());
245 Pool.wait();
246 if (End & 1) {
247 Pool.async(mergeWriterContexts, Contexts[0].get(),
248 Contexts[End - 1].get());
249 Pool.wait();
250 }
251 End = Mid;
252 Mid /= 2;
253 } while (Mid > 0);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000254 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000255
256 // Handle deferred hard errors encountered during merging.
257 for (std::unique_ptr<WriterContext> &WC : Contexts)
258 if (WC->Err)
259 exitWithError(std::move(WC->Err), WC->ErrWhence);
260
261 InstrProfWriter &Writer = Contexts[0]->Writer;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000262 if (OutputFormat == PF_Text) {
263 if (Error E = Writer.writeText(Output))
264 exitWithError(std::move(E));
265 } else {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000266 Writer.write(Output);
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000267 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000268}
269
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000270static sampleprof::SampleProfileFormat FormatMap[] = {
271 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
272 sampleprof::SPF_GCC};
273
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000274static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000275 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000276 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000277 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000278 auto WriterOrErr =
279 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000280 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000281 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000282
Diego Novillofcd55602014-11-03 00:51:45 +0000283 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000284 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000285 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000286 LLVMContext Context;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000287 for (const auto &Input : Inputs) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000288 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000289 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000290 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000291
Diego Novilloaae1ed82015-10-08 19:40:37 +0000292 // We need to keep the readers around until after all the files are
293 // read so that we do not lose the function names stored in each
294 // reader's memory. The function names are needed to write out the
295 // merged profile map.
296 Readers.push_back(std::move(ReaderOrErr.get()));
297 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000298 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000299 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000300
301 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
302 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
303 E = Profiles.end();
304 I != E; ++I) {
305 StringRef FName = I->first();
306 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000307 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
308 if (Result != sampleprof_error::success) {
309 std::error_code EC = make_error_code(Result);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000310 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000311 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000312 }
313 }
314 Writer->write(ProfileMap);
315}
316
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000317static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000318 StringRef WeightStr, FileName;
319 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000320
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000321 uint64_t Weight;
322 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
323 exitWithError("Input weight must be a positive integer.");
324
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000325 return {FileName, Weight};
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000326}
327
Vedant Kumarcef43602016-06-07 22:47:31 +0000328static std::unique_ptr<MemoryBuffer>
329getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
330 if (InputFilenamesFile == "")
331 return {};
332
333 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
334 if (!BufOrError)
335 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
336
337 return std::move(*BufOrError);
338}
339
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000340static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
341 StringRef Filename = WF.Filename;
342 uint64_t Weight = WF.Weight;
Benjamin Kramera81f4722016-07-22 12:39:55 +0000343
344 // If it's STDIN just pass it on.
345 if (Filename == "-") {
346 WNI.push_back({Filename, Weight});
347 return;
348 }
349
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000350 llvm::sys::fs::file_status Status;
351 llvm::sys::fs::status(Filename, Status);
352 if (!llvm::sys::fs::exists(Status))
353 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
354 Filename);
355 // If it's a source file, collect it.
356 if (llvm::sys::fs::is_regular_file(Status)) {
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000357 WNI.push_back({Filename, Weight});
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000358 return;
359 }
360
361 if (llvm::sys::fs::is_directory(Status)) {
362 std::error_code EC;
363 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
364 F != E && !EC; F.increment(EC)) {
365 if (llvm::sys::fs::is_regular_file(F->path())) {
366 addWeightedInput(WNI, {F->path(), Weight});
367 }
368 }
369 if (EC)
370 exitWithErrorCode(EC, Filename);
371 }
372}
373
Vedant Kumarcef43602016-06-07 22:47:31 +0000374static void parseInputFilenamesFile(MemoryBuffer *Buffer,
375 WeightedFileVector &WFV) {
376 if (!Buffer)
377 return;
378
379 SmallVector<StringRef, 8> Entries;
380 StringRef Data = Buffer->getBuffer();
381 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
382 for (const StringRef &FileWeightEntry : Entries) {
383 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
384 // Skip comments.
385 if (SanitizedEntry.startswith("#"))
386 continue;
387 // If there's no comma, it's an unweighted profile.
388 else if (SanitizedEntry.find(',') == StringRef::npos)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000389 addWeightedInput(WFV, {SanitizedEntry, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000390 else
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000391 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
Vedant Kumarcef43602016-06-07 22:47:31 +0000392 }
393}
394
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000395static int merge_main(int argc, const char *argv[]) {
396 cl::list<std::string> InputFilenames(cl::Positional,
397 cl::desc("<filename...>"));
398 cl::list<std::string> WeightedInputFilenames("weighted-input",
399 cl::desc("<weight>,<filename>"));
Vedant Kumarcef43602016-06-07 22:47:31 +0000400 cl::opt<std::string> InputFilenamesFile(
401 "input-files", cl::init(""),
402 cl::desc("Path to file containing newline-separated "
403 "[<weight>,]<filename> entries"));
404 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
405 cl::aliasopt(InputFilenamesFile));
406 cl::opt<bool> DumpInputFileList(
407 "dump-input-file-list", cl::init(false), cl::Hidden,
408 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000409 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
410 cl::init("-"), cl::Required,
411 cl::desc("Output file"));
412 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
413 cl::aliasopt(OutputFilename));
414 cl::opt<ProfileKinds> ProfileKind(
415 cl::desc("Profile kind:"), cl::init(instr),
416 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000417 clEnumVal(sample, "Sample profile")));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000418 cl::opt<ProfileFormat> OutputFormat(
419 cl::desc("Format of output profile"), cl::init(PF_Binary),
420 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
421 clEnumValN(PF_Text, "text", "Text encoding"),
422 clEnumValN(PF_GCC, "gcc",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000423 "GCC encoding (only meaningful for -sample)")));
Vedant Kumar00dab222016-01-29 22:54:45 +0000424 cl::opt<bool> OutputSparse("sparse", cl::init(false),
425 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000426 cl::opt<unsigned> NumThreads(
427 "num-threads", cl::init(0),
428 cl::desc("Number of merge threads to use (default: autodetect)"));
429 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
430 cl::aliasopt(NumThreads));
Vedant Kumar00dab222016-01-29 22:54:45 +0000431
Diego Novillod5336ae2014-11-01 00:56:55 +0000432 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
433
Vedant Kumarcef43602016-06-07 22:47:31 +0000434 WeightedFileVector WeightedInputs;
435 for (StringRef Filename : InputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000436 addWeightedInput(WeightedInputs, {Filename, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000437 for (StringRef WeightedFilename : WeightedInputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000438 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
Vedant Kumarcef43602016-06-07 22:47:31 +0000439
440 // Make sure that the file buffer stays alive for the duration of the
441 // weighted input vector's lifetime.
442 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
443 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
444
445 if (WeightedInputs.empty())
Chandler Carruth0c30f892016-06-04 03:08:01 +0000446 exitWithError("No input files specified. See " +
447 sys::path::filename(argv[0]) + " -help");
448
Vedant Kumarcef43602016-06-07 22:47:31 +0000449 if (DumpInputFileList) {
450 for (auto &WF : WeightedInputs)
451 outs() << WF.Weight << "," << WF.Filename << "\n";
452 return 0;
453 }
Vedant Kumarf771a052016-06-04 00:36:28 +0000454
Diego Novillod5336ae2014-11-01 00:56:55 +0000455 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000456 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000457 OutputSparse, NumThreads);
Diego Novillod5336ae2014-11-01 00:56:55 +0000458 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000459 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000460
Justin Bognerec49f982014-03-12 22:00:57 +0000461 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000462}
Justin Bogner618bcea2014-03-19 02:20:46 +0000463
Rong Xu0cf1f562017-03-09 19:03:57 +0000464typedef struct ValueSitesStats {
465 ValueSitesStats()
466 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
467 TotalNumValues(0) {}
468 uint64_t TotalNumValueSites;
469 uint64_t TotalNumValueSitesWithValueProfile;
470 uint64_t TotalNumValues;
471 std::vector<unsigned> ValueSitesHistogram;
472} ValueSitesStats;
473
474static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
475 ValueSitesStats &Stats, raw_fd_ostream &OS,
Rong Xu60faea12017-03-16 21:15:48 +0000476 InstrProfSymtab *Symtab) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000477 uint32_t NS = Func.getNumValueSites(VK);
478 Stats.TotalNumValueSites += NS;
479 for (size_t I = 0; I < NS; ++I) {
480 uint32_t NV = Func.getNumValueDataForSite(VK, I);
481 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
482 Stats.TotalNumValues += NV;
483 if (NV) {
484 Stats.TotalNumValueSitesWithValueProfile++;
485 if (NV > Stats.ValueSitesHistogram.size())
486 Stats.ValueSitesHistogram.resize(NV, 0);
487 Stats.ValueSitesHistogram[NV - 1]++;
488 }
489 for (uint32_t V = 0; V < NV; V++) {
490 OS << "\t[ " << I << ", ";
Rong Xu60faea12017-03-16 21:15:48 +0000491 if (Symtab == nullptr)
492 OS << VD[V].Value;
493 else
494 OS << Symtab->getFuncName(VD[V].Value);
495 OS << ", " << VD[V].Count << " ]\n";
Rong Xu0cf1f562017-03-09 19:03:57 +0000496 }
497 }
498}
499
500static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
501 ValueSitesStats &Stats) {
502 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n";
503 OS << " Total number of sites with values: "
504 << Stats.TotalNumValueSitesWithValueProfile << "\n";
505 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n";
506
507 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n";
508 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
509 if (Stats.ValueSitesHistogram[I] > 0)
510 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
511 }
512}
513
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000514static int showInstrProfile(const std::string &Filename, bool ShowCounts,
Xinliang David Li801b5312017-07-11 20:30:43 +0000515 uint32_t TopN, bool ShowIndirectCallTargets,
516 bool ShowMemOPSizes, bool ShowDetailedSummary,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000517 std::vector<uint32_t> DetailedSummaryCutoffs,
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000518 bool ShowAllFunctions,
519 const std::string &ShowFunction, bool TextFormat,
520 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000521 auto ReaderOrErr = InstrProfReader::create(Filename);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000522 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
523 if (ShowDetailedSummary && Cutoffs.empty()) {
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000524 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
525 }
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000526 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000527 if (Error E = ReaderOrErr.takeError())
528 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000529
Diego Novillofcd55602014-11-03 00:51:45 +0000530 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000531 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000532 size_t ShownFunctions = 0;
Rong Xu0cf1f562017-03-09 19:03:57 +0000533 int NumVPKind = IPVK_Last - IPVK_First + 1;
534 std::vector<ValueSitesStats> VPStats(NumVPKind);
Xinliang David Li801b5312017-07-11 20:30:43 +0000535
536 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
537 const std::pair<std::string, uint64_t> &v2) {
538 return v1.second > v2.second;
539 };
540
541 std::priority_queue<std::pair<std::string, uint64_t>,
542 std::vector<std::pair<std::string, uint64_t>>,
543 decltype(MinCmp)>
544 HottestFuncs(MinCmp);
545
Justin Bogner9af28ef2014-03-21 17:29:44 +0000546 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000547 bool Show =
548 ShowAllFunctions || (!ShowFunction.empty() &&
549 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000550
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000551 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
552
553 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000554 InstrProfSymtab &Symtab = Reader->getSymtab();
David Blaikiecf9d52c2017-07-06 19:00:12 +0000555 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
556 OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000557 continue;
558 }
559
Justin Bognerb59d7c72014-04-25 02:45:33 +0000560 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000561 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000562
Xinliang David Li801b5312017-07-11 20:30:43 +0000563 if (TopN) {
564 uint64_t FuncMax = 0;
565 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I)
566 FuncMax = std::max(FuncMax, Func.Counts[I]);
567
568 if (HottestFuncs.size() == TopN) {
569 if (HottestFuncs.top().second < FuncMax) {
570 HottestFuncs.pop();
571 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
572 }
573 } else
574 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
575 }
576
Justin Bogner9af28ef2014-03-21 17:29:44 +0000577 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000578
Justin Bogner9af28ef2014-03-21 17:29:44 +0000579 if (!ShownFunctions)
580 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000581
Justin Bogner9af28ef2014-03-21 17:29:44 +0000582 ++ShownFunctions;
583
584 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000585 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000586 << " Counters: " << Func.Counts.size() << "\n";
587 if (!IsIRInstr)
588 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000589
Justin Bogner9e9a0572015-09-29 22:13:58 +0000590 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000591 OS << " Indirect Call Site Count: "
592 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000593
Rong Xu60faea12017-03-16 21:15:48 +0000594 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
595 if (ShowMemOPSizes && NumMemOPCalls > 0)
596 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls
597 << "\n";
598
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000599 if (ShowCounts) {
600 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000601 size_t Start = (IsIRInstr ? 0 : 1);
602 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
603 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000604 }
605 OS << "]\n";
606 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000607
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000608 if (ShowIndirectCallTargets) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000609 OS << " Indirect Target Results:\n";
610 traverseAllValueSites(Func, IPVK_IndirectCallTarget,
611 VPStats[IPVK_IndirectCallTarget], OS,
Rong Xu60faea12017-03-16 21:15:48 +0000612 &(Reader->getSymtab()));
613 }
614
615 if (ShowMemOPSizes && NumMemOPCalls > 0) {
Teresa Johnsoncd2aa0d2017-05-24 17:55:25 +0000616 OS << " Memory Intrinsic Size Results:\n";
Rong Xu60faea12017-03-16 21:15:48 +0000617 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
618 nullptr);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000619 }
620 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000621 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000622 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000623 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000624
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000625 if (ShowCounts && TextFormat)
626 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000627 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000628 if (ShowAllFunctions || !ShowFunction.empty())
629 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000630 OS << "Total functions: " << PS->getNumFunctions() << "\n";
631 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000632 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Rong Xu60faea12017-03-16 21:15:48 +0000633
Xinliang David Li801b5312017-07-11 20:30:43 +0000634 if (TopN) {
635 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
636 while (!HottestFuncs.empty()) {
637 SortedHottestFuncs.emplace_back(HottestFuncs.top());
638 HottestFuncs.pop();
639 }
640 OS << "Top " << TopN
641 << " functions with the largest internal block counts: \n";
642 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
643 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
644 }
645
Xinliang David Li872362c2016-05-23 16:36:11 +0000646 if (ShownFunctions && ShowIndirectCallTargets) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000647 OS << "Statistics for indirect call sites profile:\n";
648 showValueSitesStats(OS, IPVK_IndirectCallTarget,
649 VPStats[IPVK_IndirectCallTarget]);
Xinliang David Li872362c2016-05-23 16:36:11 +0000650 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000651
Rong Xu60faea12017-03-16 21:15:48 +0000652 if (ShownFunctions && ShowMemOPSizes) {
653 OS << "Statistics for memory intrinsic calls sizes profile:\n";
654 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
655 }
656
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000657 if (ShowDetailedSummary) {
658 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000659 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000660 OS << "Total count: " << PS->getTotalCount() << "\n";
661 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000662 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000663 << " account for "
664 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
665 << " percentage of the total counts.\n";
666 }
667 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000668 return 0;
669}
670
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000671static int showSampleProfile(const std::string &Filename, bool ShowCounts,
672 bool ShowAllFunctions,
673 const std::string &ShowFunction,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000674 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000675 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000676 LLVMContext Context;
677 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000678 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000679 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000680
Diego Novillofcd55602014-11-03 00:51:45 +0000681 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000682 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000683 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000684
Diego Novillod5336ae2014-11-01 00:56:55 +0000685 if (ShowAllFunctions || ShowFunction.empty())
686 Reader->dump(OS);
687 else
688 Reader->dumpFunctionProfile(ShowFunction, OS);
689
690 return 0;
691}
692
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000693static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000694 cl::opt<std::string> Filename(cl::Positional, cl::Required,
695 cl::desc("<profdata-file>"));
696
697 cl::opt<bool> ShowCounts("counts", cl::init(false),
698 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000699 cl::opt<bool> TextFormat(
700 "text", cl::init(false),
701 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000702 cl::opt<bool> ShowIndirectCallTargets(
703 "ic-targets", cl::init(false),
704 cl::desc("Show indirect call site target values for shown functions"));
Rong Xu60faea12017-03-16 21:15:48 +0000705 cl::opt<bool> ShowMemOPSizes(
706 "memop-sizes", cl::init(false),
707 cl::desc("Show the profiled sizes of the memory intrinsic calls "
708 "for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000709 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
710 cl::desc("Show detailed profile summary"));
711 cl::list<uint32_t> DetailedSummaryCutoffs(
712 cl::CommaSeparated, "detailed-summary-cutoffs",
713 cl::desc(
714 "Cutoff percentages (times 10000) for generating detailed summary"),
715 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000716 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
717 cl::desc("Details for every function"));
718 cl::opt<std::string> ShowFunction("function",
719 cl::desc("Details for matching functions"));
720
721 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
722 cl::init("-"), cl::desc("Output file"));
723 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
724 cl::aliasopt(OutputFilename));
725 cl::opt<ProfileKinds> ProfileKind(
726 cl::desc("Profile kind:"), cl::init(instr),
727 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000728 clEnumVal(sample, "Sample profile")));
Xinliang David Li801b5312017-07-11 20:30:43 +0000729 cl::opt<uint32_t> TopNFunctions(
730 "topn", cl::init(0),
731 cl::desc("Show the list of functions with the largest internal counts"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000732
733 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
734
735 if (OutputFilename.empty())
736 OutputFilename = "-";
737
738 std::error_code EC;
739 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
740 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000741 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000742
743 if (ShowAllFunctions && !ShowFunction.empty())
744 errs() << "warning: -function argument ignored: showing all functions\n";
745
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000746 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
747 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000748 if (ProfileKind == instr)
Xinliang David Li801b5312017-07-11 20:30:43 +0000749 return showInstrProfile(Filename, ShowCounts, TopNFunctions,
750 ShowIndirectCallTargets, ShowMemOPSizes,
751 ShowDetailedSummary, DetailedSummaryCutoffs,
752 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000753 else
754 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
755 ShowFunction, OS);
756}
757
Justin Bogner618bcea2014-03-19 02:20:46 +0000758int main(int argc, const char *argv[]) {
759 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000760 sys::PrintStackTraceOnErrorSignal(argv[0]);
Justin Bogner618bcea2014-03-19 02:20:46 +0000761 PrettyStackTraceProgram X(argc, argv);
762 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
763
764 StringRef ProgName(sys::path::filename(argv[0]));
765 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000766 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000767
768 if (strcmp(argv[1], "merge") == 0)
769 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000770 else if (strcmp(argv[1], "show") == 0)
771 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000772
773 if (func) {
774 std::string Invocation(ProgName.str() + " " + argv[1]);
775 argv[1] = Invocation.c_str();
776 return func(argc - 1, argv + 1);
777 }
778
Diego Novillod3babdb2015-12-14 20:37:15 +0000779 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000780 strcmp(argv[1], "--help") == 0) {
781
782 errs() << "OVERVIEW: LLVM profile data tools\n\n"
783 << "USAGE: " << ProgName << " <command> [args...]\n"
784 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner253eb172016-08-03 23:10:51 +0000785 << "See each individual command --help for more details.\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000786 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000787 return 0;
788 }
789 }
790
791 if (argc < 2)
792 errs() << ProgName << ": No command specified!\n";
793 else
794 errs() << ProgName << ": Unknown command!\n";
795
Justin Bogner9af28ef2014-03-21 17:29:44 +0000796 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000797 return 1;
798}