blob: f5aa7062e62d941b1cd9124e5ba981a7d5967e2a [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
Vedant Kumarfaaa42a2017-11-17 02:58:23 +000040static void exitWithError(Twine Message, std::string Whence = "",
41 std::string 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;
Vedant Kumarfaaa42a2017-11-17 02:58:23 +0000122 std::string ErrWhence;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000123 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
Vedant Kumarfaaa42a2017-11-17 02:58:23 +0000140 // Copy the filename, because llvm::ThreadPool copied the input "const
141 // WeightedFile &" by value, making a reference to the filename within it
142 // invalid outside of this packaged task.
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000143 WC->ErrWhence = Input.Filename;
144
145 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Rong Xu2c684cf2016-10-19 22:51:17 +0000146 if (Error E = ReaderOrErr.takeError()) {
147 // Skip the empty profiles by returning sliently.
148 instrprof_error IPE = InstrProfError::take(std::move(E));
149 if (IPE != instrprof_error::empty_raw_profile)
150 WC->Err = make_error<InstrProfError>(IPE);
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000151 return;
Rong Xu2c684cf2016-10-19 22:51:17 +0000152 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000153
154 auto Reader = std::move(ReaderOrErr.get());
155 bool IsIRProfile = Reader->isIRLevelProfile();
156 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
157 WC->Err = make_error<StringError>(
158 "Merge IR generated profile with Clang generated profile.",
159 std::error_code());
160 return;
161 }
162
163 for (auto &I : *Reader) {
Rong Xufe90d862016-10-19 23:31:59 +0000164 const StringRef FuncName = I.Name;
David Blaikie98cce002017-07-10 03:04:59 +0000165 bool Reported = false;
166 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
167 if (Reported) {
168 consumeError(std::move(E));
169 return;
170 }
171 Reported = true;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000172 // Only show hint the first time an error occurs.
173 instrprof_error IPE = InstrProfError::take(std::move(E));
174 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
175 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
176 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
Rong Xufe90d862016-10-19 23:31:59 +0000177 FuncName, firstTime);
David Blaikie98cce002017-07-10 03:04:59 +0000178 });
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000179 }
180 if (Reader->hasError())
181 WC->Err = Reader->getError();
182}
183
184/// Merge the \p Src writer context into \p Dst.
185static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
Vedant Kumarfaaa42a2017-11-17 02:58:23 +0000186 // If we've already seen a hard error, continuing with the merge would
187 // clobber it.
188 if (Dst->Err || Src->Err)
189 return;
190
David Blaikie98cce002017-07-10 03:04:59 +0000191 bool Reported = false;
192 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
193 if (Reported) {
194 consumeError(std::move(E));
195 return;
196 }
197 Reported = true;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000198 Dst->Err = std::move(E);
David Blaikie98cce002017-07-10 03:04:59 +0000199 });
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000200}
201
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000202static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000203 StringRef OutputFilename,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000204 ProfileFormat OutputFormat, bool OutputSparse,
205 unsigned NumThreads) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000206 if (OutputFilename.compare("-") == 0)
207 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000208
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000209 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
210 exitWithError("Unknown format is specified.");
211
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000212 std::error_code EC;
213 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
214 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000215 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000216
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000217 std::mutex ErrorLock;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000218 SmallSet<instrprof_error, 4> WriterErrorCodes;
Justin Bognerf8d79192014-03-21 17:24:48 +0000219
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000220 // If NumThreads is not specified, auto-detect a good default.
221 if (NumThreads == 0)
Rafael Espindola8c0ff952017-10-04 20:27:01 +0000222 NumThreads =
223 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
Rong Xu33c76c02016-02-10 17:18:30 +0000224
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000225 // Initialize the writer contexts.
226 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
227 for (unsigned I = 0; I < NumThreads; ++I)
228 Contexts.emplace_back(llvm::make_unique<WriterContext>(
229 OutputSparse, ErrorLock, WriterErrorCodes));
230
231 if (NumThreads == 1) {
232 for (const auto &Input : Inputs)
233 loadInput(Input, Contexts[0].get());
234 } else {
235 ThreadPool Pool(NumThreads);
236
237 // Load the inputs in parallel (N/NumThreads serial steps).
238 unsigned Ctx = 0;
239 for (const auto &Input : Inputs) {
240 Pool.async(loadInput, Input, Contexts[Ctx].get());
241 Ctx = (Ctx + 1) % NumThreads;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000242 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000243 Pool.wait();
244
245 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
246 unsigned Mid = Contexts.size() / 2;
247 unsigned End = Contexts.size();
248 assert(Mid > 0 && "Expected more than one context");
249 do {
250 for (unsigned I = 0; I < Mid; ++I)
251 Pool.async(mergeWriterContexts, Contexts[I].get(),
252 Contexts[I + Mid].get());
253 Pool.wait();
254 if (End & 1) {
255 Pool.async(mergeWriterContexts, Contexts[0].get(),
256 Contexts[End - 1].get());
257 Pool.wait();
258 }
259 End = Mid;
260 Mid /= 2;
261 } while (Mid > 0);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000262 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000263
264 // Handle deferred hard errors encountered during merging.
265 for (std::unique_ptr<WriterContext> &WC : Contexts)
266 if (WC->Err)
267 exitWithError(std::move(WC->Err), WC->ErrWhence);
268
269 InstrProfWriter &Writer = Contexts[0]->Writer;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000270 if (OutputFormat == PF_Text) {
271 if (Error E = Writer.writeText(Output))
272 exitWithError(std::move(E));
273 } else {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000274 Writer.write(Output);
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000275 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000276}
277
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000278static sampleprof::SampleProfileFormat FormatMap[] = {
279 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
280 sampleprof::SPF_GCC};
281
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000282static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000283 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000284 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000285 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000286 auto WriterOrErr =
287 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000288 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000289 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000290
Diego Novillofcd55602014-11-03 00:51:45 +0000291 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000292 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000293 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000294 LLVMContext Context;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000295 for (const auto &Input : Inputs) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000296 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000297 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000298 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000299
Diego Novilloaae1ed82015-10-08 19:40:37 +0000300 // We need to keep the readers around until after all the files are
301 // read so that we do not lose the function names stored in each
302 // reader's memory. The function names are needed to write out the
303 // merged profile map.
304 Readers.push_back(std::move(ReaderOrErr.get()));
305 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000306 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000307 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000308
309 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
310 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
311 E = Profiles.end();
312 I != E; ++I) {
313 StringRef FName = I->first();
314 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000315 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
316 if (Result != sampleprof_error::success) {
317 std::error_code EC = make_error_code(Result);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000318 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000319 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000320 }
321 }
322 Writer->write(ProfileMap);
323}
324
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000325static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000326 StringRef WeightStr, FileName;
327 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000328
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000329 uint64_t Weight;
330 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
331 exitWithError("Input weight must be a positive integer.");
332
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000333 return {FileName, Weight};
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000334}
335
Vedant Kumarcef43602016-06-07 22:47:31 +0000336static std::unique_ptr<MemoryBuffer>
337getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
338 if (InputFilenamesFile == "")
339 return {};
340
341 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
342 if (!BufOrError)
343 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
344
345 return std::move(*BufOrError);
346}
347
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000348static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
349 StringRef Filename = WF.Filename;
350 uint64_t Weight = WF.Weight;
Benjamin Kramera81f4722016-07-22 12:39:55 +0000351
352 // If it's STDIN just pass it on.
353 if (Filename == "-") {
354 WNI.push_back({Filename, Weight});
355 return;
356 }
357
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000358 llvm::sys::fs::file_status Status;
359 llvm::sys::fs::status(Filename, Status);
360 if (!llvm::sys::fs::exists(Status))
361 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
362 Filename);
363 // If it's a source file, collect it.
364 if (llvm::sys::fs::is_regular_file(Status)) {
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000365 WNI.push_back({Filename, Weight});
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000366 return;
367 }
368
369 if (llvm::sys::fs::is_directory(Status)) {
370 std::error_code EC;
371 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
372 F != E && !EC; F.increment(EC)) {
373 if (llvm::sys::fs::is_regular_file(F->path())) {
374 addWeightedInput(WNI, {F->path(), Weight});
375 }
376 }
377 if (EC)
378 exitWithErrorCode(EC, Filename);
379 }
380}
381
Vedant Kumarcef43602016-06-07 22:47:31 +0000382static void parseInputFilenamesFile(MemoryBuffer *Buffer,
383 WeightedFileVector &WFV) {
384 if (!Buffer)
385 return;
386
387 SmallVector<StringRef, 8> Entries;
388 StringRef Data = Buffer->getBuffer();
389 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
390 for (const StringRef &FileWeightEntry : Entries) {
391 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
392 // Skip comments.
393 if (SanitizedEntry.startswith("#"))
394 continue;
395 // If there's no comma, it's an unweighted profile.
396 else if (SanitizedEntry.find(',') == StringRef::npos)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000397 addWeightedInput(WFV, {SanitizedEntry, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000398 else
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000399 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
Vedant Kumarcef43602016-06-07 22:47:31 +0000400 }
401}
402
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000403static int merge_main(int argc, const char *argv[]) {
404 cl::list<std::string> InputFilenames(cl::Positional,
405 cl::desc("<filename...>"));
406 cl::list<std::string> WeightedInputFilenames("weighted-input",
407 cl::desc("<weight>,<filename>"));
Vedant Kumarcef43602016-06-07 22:47:31 +0000408 cl::opt<std::string> InputFilenamesFile(
409 "input-files", cl::init(""),
410 cl::desc("Path to file containing newline-separated "
411 "[<weight>,]<filename> entries"));
412 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
413 cl::aliasopt(InputFilenamesFile));
414 cl::opt<bool> DumpInputFileList(
415 "dump-input-file-list", cl::init(false), cl::Hidden,
416 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000417 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
418 cl::init("-"), cl::Required,
419 cl::desc("Output file"));
420 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
421 cl::aliasopt(OutputFilename));
422 cl::opt<ProfileKinds> ProfileKind(
423 cl::desc("Profile kind:"), cl::init(instr),
424 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000425 clEnumVal(sample, "Sample profile")));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000426 cl::opt<ProfileFormat> OutputFormat(
427 cl::desc("Format of output profile"), cl::init(PF_Binary),
428 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
429 clEnumValN(PF_Text, "text", "Text encoding"),
430 clEnumValN(PF_GCC, "gcc",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000431 "GCC encoding (only meaningful for -sample)")));
Vedant Kumar00dab222016-01-29 22:54:45 +0000432 cl::opt<bool> OutputSparse("sparse", cl::init(false),
433 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000434 cl::opt<unsigned> NumThreads(
435 "num-threads", cl::init(0),
436 cl::desc("Number of merge threads to use (default: autodetect)"));
437 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
438 cl::aliasopt(NumThreads));
Vedant Kumar00dab222016-01-29 22:54:45 +0000439
Diego Novillod5336ae2014-11-01 00:56:55 +0000440 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
441
Vedant Kumarcef43602016-06-07 22:47:31 +0000442 WeightedFileVector WeightedInputs;
443 for (StringRef Filename : InputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000444 addWeightedInput(WeightedInputs, {Filename, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000445 for (StringRef WeightedFilename : WeightedInputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000446 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
Vedant Kumarcef43602016-06-07 22:47:31 +0000447
448 // Make sure that the file buffer stays alive for the duration of the
449 // weighted input vector's lifetime.
450 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
451 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
452
453 if (WeightedInputs.empty())
Chandler Carruth0c30f892016-06-04 03:08:01 +0000454 exitWithError("No input files specified. See " +
455 sys::path::filename(argv[0]) + " -help");
456
Vedant Kumarcef43602016-06-07 22:47:31 +0000457 if (DumpInputFileList) {
458 for (auto &WF : WeightedInputs)
459 outs() << WF.Weight << "," << WF.Filename << "\n";
460 return 0;
461 }
Vedant Kumarf771a052016-06-04 00:36:28 +0000462
Diego Novillod5336ae2014-11-01 00:56:55 +0000463 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000464 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000465 OutputSparse, NumThreads);
Diego Novillod5336ae2014-11-01 00:56:55 +0000466 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000467 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000468
Justin Bognerec49f982014-03-12 22:00:57 +0000469 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000470}
Justin Bogner618bcea2014-03-19 02:20:46 +0000471
Rong Xu0cf1f562017-03-09 19:03:57 +0000472typedef struct ValueSitesStats {
473 ValueSitesStats()
474 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
475 TotalNumValues(0) {}
476 uint64_t TotalNumValueSites;
477 uint64_t TotalNumValueSitesWithValueProfile;
478 uint64_t TotalNumValues;
479 std::vector<unsigned> ValueSitesHistogram;
480} ValueSitesStats;
481
482static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
483 ValueSitesStats &Stats, raw_fd_ostream &OS,
Rong Xu60faea12017-03-16 21:15:48 +0000484 InstrProfSymtab *Symtab) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000485 uint32_t NS = Func.getNumValueSites(VK);
486 Stats.TotalNumValueSites += NS;
487 for (size_t I = 0; I < NS; ++I) {
488 uint32_t NV = Func.getNumValueDataForSite(VK, I);
489 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
490 Stats.TotalNumValues += NV;
491 if (NV) {
492 Stats.TotalNumValueSitesWithValueProfile++;
493 if (NV > Stats.ValueSitesHistogram.size())
494 Stats.ValueSitesHistogram.resize(NV, 0);
495 Stats.ValueSitesHistogram[NV - 1]++;
496 }
497 for (uint32_t V = 0; V < NV; V++) {
498 OS << "\t[ " << I << ", ";
Rong Xu60faea12017-03-16 21:15:48 +0000499 if (Symtab == nullptr)
500 OS << VD[V].Value;
501 else
502 OS << Symtab->getFuncName(VD[V].Value);
503 OS << ", " << VD[V].Count << " ]\n";
Rong Xu0cf1f562017-03-09 19:03:57 +0000504 }
505 }
506}
507
508static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
509 ValueSitesStats &Stats) {
510 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n";
511 OS << " Total number of sites with values: "
512 << Stats.TotalNumValueSitesWithValueProfile << "\n";
513 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n";
514
515 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n";
516 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
517 if (Stats.ValueSitesHistogram[I] > 0)
518 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
519 }
520}
521
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000522static int showInstrProfile(const std::string &Filename, bool ShowCounts,
Xinliang David Li801b5312017-07-11 20:30:43 +0000523 uint32_t TopN, bool ShowIndirectCallTargets,
524 bool ShowMemOPSizes, bool ShowDetailedSummary,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000525 std::vector<uint32_t> DetailedSummaryCutoffs,
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000526 bool ShowAllFunctions,
527 const std::string &ShowFunction, bool TextFormat,
528 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000529 auto ReaderOrErr = InstrProfReader::create(Filename);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000530 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
531 if (ShowDetailedSummary && Cutoffs.empty()) {
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000532 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
533 }
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000534 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000535 if (Error E = ReaderOrErr.takeError())
536 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000537
Diego Novillofcd55602014-11-03 00:51:45 +0000538 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000539 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000540 size_t ShownFunctions = 0;
Rong Xu0cf1f562017-03-09 19:03:57 +0000541 int NumVPKind = IPVK_Last - IPVK_First + 1;
542 std::vector<ValueSitesStats> VPStats(NumVPKind);
Xinliang David Li801b5312017-07-11 20:30:43 +0000543
544 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
545 const std::pair<std::string, uint64_t> &v2) {
546 return v1.second > v2.second;
547 };
548
549 std::priority_queue<std::pair<std::string, uint64_t>,
550 std::vector<std::pair<std::string, uint64_t>>,
551 decltype(MinCmp)>
552 HottestFuncs(MinCmp);
553
Justin Bogner9af28ef2014-03-21 17:29:44 +0000554 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000555 bool Show =
556 ShowAllFunctions || (!ShowFunction.empty() &&
557 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000558
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000559 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
560
561 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000562 InstrProfSymtab &Symtab = Reader->getSymtab();
David Blaikiecf9d52c2017-07-06 19:00:12 +0000563 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
564 OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000565 continue;
566 }
567
Justin Bognerb59d7c72014-04-25 02:45:33 +0000568 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000569 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000570
Xinliang David Li801b5312017-07-11 20:30:43 +0000571 if (TopN) {
572 uint64_t FuncMax = 0;
573 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I)
574 FuncMax = std::max(FuncMax, Func.Counts[I]);
575
576 if (HottestFuncs.size() == TopN) {
577 if (HottestFuncs.top().second < FuncMax) {
578 HottestFuncs.pop();
579 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
580 }
581 } else
582 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
583 }
584
Justin Bogner9af28ef2014-03-21 17:29:44 +0000585 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000586
Justin Bogner9af28ef2014-03-21 17:29:44 +0000587 if (!ShownFunctions)
588 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000589
Justin Bogner9af28ef2014-03-21 17:29:44 +0000590 ++ShownFunctions;
591
592 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000593 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000594 << " Counters: " << Func.Counts.size() << "\n";
595 if (!IsIRInstr)
596 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000597
Justin Bogner9e9a0572015-09-29 22:13:58 +0000598 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000599 OS << " Indirect Call Site Count: "
600 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000601
Rong Xu60faea12017-03-16 21:15:48 +0000602 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
603 if (ShowMemOPSizes && NumMemOPCalls > 0)
604 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls
605 << "\n";
606
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000607 if (ShowCounts) {
608 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000609 size_t Start = (IsIRInstr ? 0 : 1);
610 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
611 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000612 }
613 OS << "]\n";
614 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000615
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000616 if (ShowIndirectCallTargets) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000617 OS << " Indirect Target Results:\n";
618 traverseAllValueSites(Func, IPVK_IndirectCallTarget,
619 VPStats[IPVK_IndirectCallTarget], OS,
Rong Xu60faea12017-03-16 21:15:48 +0000620 &(Reader->getSymtab()));
621 }
622
623 if (ShowMemOPSizes && NumMemOPCalls > 0) {
Teresa Johnsoncd2aa0d2017-05-24 17:55:25 +0000624 OS << " Memory Intrinsic Size Results:\n";
Rong Xu60faea12017-03-16 21:15:48 +0000625 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
626 nullptr);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000627 }
628 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000629 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000630 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000631 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000632
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000633 if (ShowCounts && TextFormat)
634 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000635 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Adam Nemet1142b2d2017-11-14 16:59:18 +0000636 OS << "Instrumentation level: "
637 << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000638 if (ShowAllFunctions || !ShowFunction.empty())
639 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000640 OS << "Total functions: " << PS->getNumFunctions() << "\n";
641 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000642 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Rong Xu60faea12017-03-16 21:15:48 +0000643
Xinliang David Li801b5312017-07-11 20:30:43 +0000644 if (TopN) {
645 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
646 while (!HottestFuncs.empty()) {
647 SortedHottestFuncs.emplace_back(HottestFuncs.top());
648 HottestFuncs.pop();
649 }
650 OS << "Top " << TopN
651 << " functions with the largest internal block counts: \n";
652 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
653 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
654 }
655
Xinliang David Li872362c2016-05-23 16:36:11 +0000656 if (ShownFunctions && ShowIndirectCallTargets) {
Rong Xu0cf1f562017-03-09 19:03:57 +0000657 OS << "Statistics for indirect call sites profile:\n";
658 showValueSitesStats(OS, IPVK_IndirectCallTarget,
659 VPStats[IPVK_IndirectCallTarget]);
Xinliang David Li872362c2016-05-23 16:36:11 +0000660 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000661
Rong Xu60faea12017-03-16 21:15:48 +0000662 if (ShownFunctions && ShowMemOPSizes) {
663 OS << "Statistics for memory intrinsic calls sizes profile:\n";
664 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
665 }
666
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000667 if (ShowDetailedSummary) {
668 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000669 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000670 OS << "Total count: " << PS->getTotalCount() << "\n";
671 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000672 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000673 << " account for "
674 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
675 << " percentage of the total counts.\n";
676 }
677 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000678 return 0;
679}
680
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000681static int showSampleProfile(const std::string &Filename, bool ShowCounts,
682 bool ShowAllFunctions,
683 const std::string &ShowFunction,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000684 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000685 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000686 LLVMContext Context;
687 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000688 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000689 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000690
Diego Novillofcd55602014-11-03 00:51:45 +0000691 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000692 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000693 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000694
Diego Novillod5336ae2014-11-01 00:56:55 +0000695 if (ShowAllFunctions || ShowFunction.empty())
696 Reader->dump(OS);
697 else
698 Reader->dumpFunctionProfile(ShowFunction, OS);
699
700 return 0;
701}
702
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000703static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000704 cl::opt<std::string> Filename(cl::Positional, cl::Required,
705 cl::desc("<profdata-file>"));
706
707 cl::opt<bool> ShowCounts("counts", cl::init(false),
708 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000709 cl::opt<bool> TextFormat(
710 "text", cl::init(false),
711 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000712 cl::opt<bool> ShowIndirectCallTargets(
713 "ic-targets", cl::init(false),
714 cl::desc("Show indirect call site target values for shown functions"));
Rong Xu60faea12017-03-16 21:15:48 +0000715 cl::opt<bool> ShowMemOPSizes(
716 "memop-sizes", cl::init(false),
717 cl::desc("Show the profiled sizes of the memory intrinsic calls "
718 "for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000719 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
720 cl::desc("Show detailed profile summary"));
721 cl::list<uint32_t> DetailedSummaryCutoffs(
722 cl::CommaSeparated, "detailed-summary-cutoffs",
723 cl::desc(
724 "Cutoff percentages (times 10000) for generating detailed summary"),
725 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000726 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
727 cl::desc("Details for every function"));
728 cl::opt<std::string> ShowFunction("function",
729 cl::desc("Details for matching functions"));
730
731 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
732 cl::init("-"), cl::desc("Output file"));
733 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
734 cl::aliasopt(OutputFilename));
735 cl::opt<ProfileKinds> ProfileKind(
736 cl::desc("Profile kind:"), cl::init(instr),
737 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000738 clEnumVal(sample, "Sample profile")));
Xinliang David Li801b5312017-07-11 20:30:43 +0000739 cl::opt<uint32_t> TopNFunctions(
740 "topn", cl::init(0),
741 cl::desc("Show the list of functions with the largest internal counts"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000742
743 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
744
745 if (OutputFilename.empty())
746 OutputFilename = "-";
747
748 std::error_code EC;
749 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
750 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000751 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000752
753 if (ShowAllFunctions && !ShowFunction.empty())
754 errs() << "warning: -function argument ignored: showing all functions\n";
755
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000756 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
757 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000758 if (ProfileKind == instr)
Xinliang David Li801b5312017-07-11 20:30:43 +0000759 return showInstrProfile(Filename, ShowCounts, TopNFunctions,
760 ShowIndirectCallTargets, ShowMemOPSizes,
761 ShowDetailedSummary, DetailedSummaryCutoffs,
762 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000763 else
764 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
765 ShowFunction, OS);
766}
767
Justin Bogner618bcea2014-03-19 02:20:46 +0000768int main(int argc, const char *argv[]) {
769 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000770 sys::PrintStackTraceOnErrorSignal(argv[0]);
Justin Bogner618bcea2014-03-19 02:20:46 +0000771 PrettyStackTraceProgram X(argc, argv);
772 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
773
774 StringRef ProgName(sys::path::filename(argv[0]));
775 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000776 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000777
778 if (strcmp(argv[1], "merge") == 0)
779 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000780 else if (strcmp(argv[1], "show") == 0)
781 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000782
783 if (func) {
784 std::string Invocation(ProgName.str() + " " + argv[1]);
785 argv[1] = Invocation.c_str();
786 return func(argc - 1, argv + 1);
787 }
788
Diego Novillod3babdb2015-12-14 20:37:15 +0000789 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000790 strcmp(argv[1], "--help") == 0) {
791
792 errs() << "OVERVIEW: LLVM profile data tools\n\n"
793 << "USAGE: " << ProgName << " <command> [args...]\n"
794 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner253eb172016-08-03 23:10:51 +0000795 << "See each individual command --help for more details.\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000796 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000797 return 0;
798 }
799 }
800
801 if (argc < 2)
802 errs() << ProgName << ": No command specified!\n";
803 else
804 errs() << ProgName << ": Unknown command!\n";
805
Justin Bogner9af28ef2014-03-21 17:29:44 +0000806 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000807 return 1;
808}