blob: 45f348747dbe6060dc9c87b4a80e45e1c95d0164 [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"
32#include "llvm/Support/raw_ostream.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000033#include <algorithm>
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000034
35using namespace llvm;
36
Xinliang David Li6f7c19a2015-11-23 20:47:38 +000037enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
38
Diego Novillod3babdb2015-12-14 20:37:15 +000039static void exitWithError(const Twine &Message, StringRef Whence = "",
Nathan Slingerland4f823662015-11-13 03:47:58 +000040 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000041 errs() << "error: ";
42 if (!Whence.empty())
43 errs() << Whence << ": ";
44 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000045 if (!Hint.empty())
46 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000047 ::exit(1);
48}
49
Vedant Kumar9152fd12016-05-19 03:54:45 +000050static void exitWithError(Error E, StringRef Whence = "") {
51 if (E.isA<InstrProfError>()) {
52 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
53 instrprof_error instrError = IPE.get();
54 StringRef Hint = "";
55 if (instrError == instrprof_error::unrecognized_format) {
56 // Hint for common error of forgetting -sample for sample profiles.
57 Hint = "Perhaps you forgot to use the -sample option?";
58 }
59 exitWithError(IPE.message(), Whence, Hint);
60 });
Nathan Slingerland4f823662015-11-13 03:47:58 +000061 }
Vedant Kumar9152fd12016-05-19 03:54:45 +000062
63 exitWithError(toString(std::move(E)), Whence);
64}
65
66static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
67 exitWithError(EC.message(), Whence);
Nathan Slingerland4f823662015-11-13 03:47:58 +000068}
69
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000070namespace {
Diego Novillod3babdb2015-12-14 20:37:15 +000071enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000072}
Justin Bogner618bcea2014-03-19 02:20:46 +000073
Vedant Kumar9152fd12016-05-19 03:54:45 +000074static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000075 StringRef WhenceFunction = "",
Diego Novillod3babdb2015-12-14 20:37:15 +000076 bool ShowHint = true) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000077 if (!WhenceFile.empty())
78 errs() << WhenceFile << ": ";
79 if (!WhenceFunction.empty())
80 errs() << WhenceFunction << ": ";
Vedant Kumar9152fd12016-05-19 03:54:45 +000081
82 auto IPE = instrprof_error::success;
83 E = handleErrors(std::move(E),
84 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
85 IPE = E->get();
86 return Error(std::move(E));
87 });
88 errs() << toString(std::move(E)) << "\n";
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000089
90 if (ShowHint) {
91 StringRef Hint = "";
Vedant Kumar9152fd12016-05-19 03:54:45 +000092 if (IPE != instrprof_error::success) {
93 switch (IPE) {
Nathan Slingerland11c938d12015-11-17 23:37:09 +000094 case instrprof_error::hash_mismatch:
95 case instrprof_error::count_mismatch:
96 case instrprof_error::value_site_count_mismatch:
Diego Novillod3babdb2015-12-14 20:37:15 +000097 Hint = "Make sure that all profile data to be merged is generated "
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000098 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +000099 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +0000100 default:
101 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000102 }
103 }
104
105 if (!Hint.empty())
106 errs() << Hint << "\n";
107 }
108}
109
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000110struct WeightedFile {
111 StringRef Filename;
112 uint64_t Weight;
113
114 WeightedFile() {}
115
116 WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {}
117};
118typedef SmallVector<WeightedFile, 5> WeightedFileVector;
119
120static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000121 StringRef OutputFilename,
Vedant Kumar00dab222016-01-29 22:54:45 +0000122 ProfileFormat OutputFormat, bool OutputSparse) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000123 if (OutputFilename.compare("-") == 0)
124 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000125
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000126 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
127 exitWithError("Unknown format is specified.");
128
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000129 std::error_code EC;
130 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
131 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000132 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000133
Vedant Kumar00dab222016-01-29 22:54:45 +0000134 InstrProfWriter Writer(OutputSparse);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000135 SmallSet<instrprof_error, 4> WriterErrorCodes;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000136 for (const auto &Input : Inputs) {
137 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000138 if (Error E = ReaderOrErr.takeError())
139 exitWithError(std::move(E), Input.Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +0000140
Diego Novillofcd55602014-11-03 00:51:45 +0000141 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000142 bool IsIRProfile = Reader->isIRLevelProfile();
143 if (Writer.setIsIRLevelProfile(IsIRProfile))
144 exitWithError("Merge IR generated profile with Clang generated profile.");
145
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000146 for (auto &I : *Reader) {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000147 if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000148 // Only show hint the first time an error occurs.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000149 instrprof_error IPE = InstrProfError::take(std::move(E));
150 bool firstTime = WriterErrorCodes.insert(IPE).second;
151 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
152 I.Name, firstTime);
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000153 }
154 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000155 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000156 exitWithError(Reader->getError(), Input.Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000157 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000158 if (OutputFormat == PF_Text)
159 Writer.writeText(Output);
160 else
161 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000162}
163
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000164static sampleprof::SampleProfileFormat FormatMap[] = {
165 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
166 sampleprof::SPF_GCC};
167
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000168static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000169 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000170 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000171 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000172 auto WriterOrErr =
173 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000174 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000175 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000176
Diego Novillofcd55602014-11-03 00:51:45 +0000177 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000178 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000179 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000180 LLVMContext Context;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000181 for (const auto &Input : Inputs) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000182 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000183 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000184 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000185
Diego Novilloaae1ed82015-10-08 19:40:37 +0000186 // We need to keep the readers around until after all the files are
187 // read so that we do not lose the function names stored in each
188 // reader's memory. The function names are needed to write out the
189 // merged profile map.
190 Readers.push_back(std::move(ReaderOrErr.get()));
191 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000192 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000193 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000194
195 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
196 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
197 E = Profiles.end();
198 I != E; ++I) {
199 StringRef FName = I->first();
200 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000201 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
202 if (Result != sampleprof_error::success) {
203 std::error_code EC = make_error_code(Result);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000204 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000205 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000206 }
207 }
208 Writer->write(ProfileMap);
209}
210
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000211static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
212 StringRef WeightStr, FileName;
213 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000214
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000215 uint64_t Weight;
216 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
217 exitWithError("Input weight must be a positive integer.");
218
219 if (!sys::fs::exists(FileName))
220 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
221 FileName);
222
223 return WeightedFile(FileName, Weight);
224}
225
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000226static std::unique_ptr<MemoryBuffer>
227parseInputFilenamesFile(const StringRef &InputFilenamesFile,
228 WeightedFileVector &WFV) {
Vedant Kumar5c276d02016-06-03 19:05:20 +0000229 if (InputFilenamesFile == "")
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000230 return {};
Vedant Kumar5c276d02016-06-03 19:05:20 +0000231
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000232 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
233 if (!BufOrError)
234 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
Vedant Kumar5c276d02016-06-03 19:05:20 +0000235
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000236 std::unique_ptr<MemoryBuffer> Buffer = std::move(*BufOrError);
237 StringRef Data = Buffer->getBuffer();
238
Vedant Kumar5c276d02016-06-03 19:05:20 +0000239 SmallVector<StringRef, 8> Entries;
240 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
241 for (const StringRef &FileWeightEntry : Entries) {
242 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
243 // Skip comments.
244 if (SanitizedEntry.startswith("#"))
245 continue;
246 // If there's no comma, it's an unweighted profile.
247 else if (SanitizedEntry.rfind(',') == StringRef::npos)
248 WFV.emplace_back(SanitizedEntry, 1);
249 else
250 WFV.emplace_back(parseWeightedFile(SanitizedEntry));
251 }
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000252
253 return Buffer;
Vedant Kumar5c276d02016-06-03 19:05:20 +0000254}
255
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000256static int merge_main(int argc, const char *argv[]) {
257 cl::list<std::string> InputFilenames(cl::Positional,
258 cl::desc("<filename...>"));
259 cl::list<std::string> WeightedInputFilenames("weighted-input",
260 cl::desc("<weight>,<filename>"));
Vedant Kumar5c276d02016-06-03 19:05:20 +0000261 cl::opt<std::string> InputFilenamesFile(
262 "input-files", cl::init(""),
263 cl::desc("Path to file containing newline-separated "
Vedant Kumard45a2772016-06-03 19:10:25 +0000264 "[<weight>,]<filename> entries"));
Vedant Kumar5c276d02016-06-03 19:05:20 +0000265 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
266 cl::aliasopt(InputFilenamesFile));
267 cl::opt<bool> DumpInputFileList(
268 "dump-input-file-list", cl::init(false), cl::Hidden,
269 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000270 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
271 cl::init("-"), cl::Required,
272 cl::desc("Output file"));
273 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
274 cl::aliasopt(OutputFilename));
275 cl::opt<ProfileKinds> ProfileKind(
276 cl::desc("Profile kind:"), cl::init(instr),
277 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
278 clEnumVal(sample, "Sample profile"), clEnumValEnd));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000279 cl::opt<ProfileFormat> OutputFormat(
280 cl::desc("Format of output profile"), cl::init(PF_Binary),
281 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
282 clEnumValN(PF_Text, "text", "Text encoding"),
283 clEnumValN(PF_GCC, "gcc",
284 "GCC encoding (only meaningful for -sample)"),
Diego Novillod5336ae2014-11-01 00:56:55 +0000285 clEnumValEnd));
Vedant Kumar00dab222016-01-29 22:54:45 +0000286 cl::opt<bool> OutputSparse("sparse", cl::init(false),
287 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
288
Diego Novillod5336ae2014-11-01 00:56:55 +0000289 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
290
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000291 WeightedFileVector WeightedInputs;
292 for (StringRef Filename : InputFilenames)
293 WeightedInputs.push_back(WeightedFile(Filename, 1));
294 for (StringRef WeightedFilename : WeightedInputFilenames)
295 WeightedInputs.push_back(parseWeightedFile(WeightedFilename));
Vedant Kumar8a3d7172016-06-03 23:12:38 +0000296 auto Buf = parseInputFilenamesFile(InputFilenamesFile, WeightedInputs);
Vedant Kumar5c276d02016-06-03 19:05:20 +0000297
298 if (WeightedInputs.empty())
299 exitWithError("No input files specified. See " +
300 sys::path::filename(argv[0]) + " -help");
301
302 if (DumpInputFileList) {
303 for (auto &WF : WeightedInputs)
304 outs() << WF.Weight << "," << WF.Filename << "\n";
305 return 0;
306 }
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000307
Diego Novillod5336ae2014-11-01 00:56:55 +0000308 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000309 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
310 OutputSparse);
Diego Novillod5336ae2014-11-01 00:56:55 +0000311 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000312 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000313
Justin Bognerec49f982014-03-12 22:00:57 +0000314 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000315}
Justin Bogner618bcea2014-03-19 02:20:46 +0000316
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000317static int showInstrProfile(std::string Filename, bool ShowCounts,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000318 bool ShowIndirectCallTargets,
319 bool ShowDetailedSummary,
320 std::vector<uint32_t> DetailedSummaryCutoffs,
321 bool ShowAllFunctions, std::string ShowFunction,
322 bool TextFormat, raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000323 auto ReaderOrErr = InstrProfReader::create(Filename);
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000324 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs);
325 if (ShowDetailedSummary && DetailedSummaryCutoffs.empty()) {
326 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
327 }
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000328 InstrProfSummaryBuilder Builder(Cutoffs);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000329 if (Error E = ReaderOrErr.takeError())
330 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000331
Diego Novillofcd55602014-11-03 00:51:45 +0000332 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000333 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000334 size_t ShownFunctions = 0;
Xinliang David Li872362c2016-05-23 16:36:11 +0000335 uint64_t TotalNumValueSites = 0;
336 uint64_t TotalNumValueSitesWithValueProfile = 0;
337 uint64_t TotalNumValues = 0;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000338 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000339 bool Show =
340 ShowAllFunctions || (!ShowFunction.empty() &&
341 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000342
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000343 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
344
345 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000346 InstrProfSymtab &Symtab = Reader->getSymtab();
347 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000348 continue;
349 }
350
Justin Bognerb59d7c72014-04-25 02:45:33 +0000351 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000352 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000353
Justin Bogner9af28ef2014-03-21 17:29:44 +0000354 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000355
Justin Bogner9af28ef2014-03-21 17:29:44 +0000356 if (!ShownFunctions)
357 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000358
Justin Bogner9af28ef2014-03-21 17:29:44 +0000359 ++ShownFunctions;
360
361 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000362 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000363 << " Counters: " << Func.Counts.size() << "\n";
364 if (!IsIRInstr)
365 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000366
Justin Bogner9e9a0572015-09-29 22:13:58 +0000367 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000368 OS << " Indirect Call Site Count: "
369 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000370
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000371 if (ShowCounts) {
372 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000373 size_t Start = (IsIRInstr ? 0 : 1);
374 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
375 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000376 }
377 OS << "]\n";
378 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000379
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000380 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000381 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000382 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
383 OS << " Indirect Target Results: \n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000384 TotalNumValueSites += NS;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000385 for (size_t I = 0; I < NS; ++I) {
386 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
387 std::unique_ptr<InstrProfValueData[]> VD =
388 Func.getValueForSite(IPVK_IndirectCallTarget, I);
Xinliang David Li872362c2016-05-23 16:36:11 +0000389 TotalNumValues += NV;
390 if (NV)
391 TotalNumValueSitesWithValueProfile++;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000392 for (uint32_t V = 0; V < NV; V++) {
393 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000394 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
395 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000396 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000397 }
398 }
399 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000400 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000401 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000402 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000403
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000404 if (ShowCounts && TextFormat)
405 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000406 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000407 if (ShowAllFunctions || !ShowFunction.empty())
408 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000409 OS << "Total functions: " << PS->getNumFunctions() << "\n";
410 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000411 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000412 if (ShownFunctions && ShowIndirectCallTargets) {
413 OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites
414 << "\n";
415 OS << "Total Number of Sites With Values : "
416 << TotalNumValueSitesWithValueProfile << "\n";
417 OS << "Total Number of Profiled Values : " << TotalNumValues << "\n";
418 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000419
420 if (ShowDetailedSummary) {
421 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000422 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000423 OS << "Total count: " << PS->getTotalCount() << "\n";
424 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000425 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000426 << " account for "
427 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
428 << " percentage of the total counts.\n";
429 }
430 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000431 return 0;
432}
433
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000434static int showSampleProfile(std::string Filename, bool ShowCounts,
435 bool ShowAllFunctions, std::string ShowFunction,
436 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000437 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000438 LLVMContext Context;
439 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000440 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000441 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000442
Diego Novillofcd55602014-11-03 00:51:45 +0000443 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000444 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000445 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000446
Diego Novillod5336ae2014-11-01 00:56:55 +0000447 if (ShowAllFunctions || ShowFunction.empty())
448 Reader->dump(OS);
449 else
450 Reader->dumpFunctionProfile(ShowFunction, OS);
451
452 return 0;
453}
454
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000455static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000456 cl::opt<std::string> Filename(cl::Positional, cl::Required,
457 cl::desc("<profdata-file>"));
458
459 cl::opt<bool> ShowCounts("counts", cl::init(false),
460 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000461 cl::opt<bool> TextFormat(
462 "text", cl::init(false),
463 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000464 cl::opt<bool> ShowIndirectCallTargets(
465 "ic-targets", cl::init(false),
466 cl::desc("Show indirect call site target values for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000467 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
468 cl::desc("Show detailed profile summary"));
469 cl::list<uint32_t> DetailedSummaryCutoffs(
470 cl::CommaSeparated, "detailed-summary-cutoffs",
471 cl::desc(
472 "Cutoff percentages (times 10000) for generating detailed summary"),
473 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000474 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
475 cl::desc("Details for every function"));
476 cl::opt<std::string> ShowFunction("function",
477 cl::desc("Details for matching functions"));
478
479 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
480 cl::init("-"), cl::desc("Output file"));
481 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
482 cl::aliasopt(OutputFilename));
483 cl::opt<ProfileKinds> ProfileKind(
484 cl::desc("Profile kind:"), cl::init(instr),
485 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
486 clEnumVal(sample, "Sample profile"), clEnumValEnd));
487
488 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
489
490 if (OutputFilename.empty())
491 OutputFilename = "-";
492
493 std::error_code EC;
494 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
495 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000496 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000497
498 if (ShowAllFunctions && !ShowFunction.empty())
499 errs() << "warning: -function argument ignored: showing all functions\n";
500
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000501 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
502 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000503 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000504 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000505 ShowDetailedSummary, DetailedSummaryCutoffs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000506 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000507 else
508 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
509 ShowFunction, OS);
510}
511
Justin Bogner618bcea2014-03-19 02:20:46 +0000512int main(int argc, const char *argv[]) {
513 // Print a stack trace if we signal out.
514 sys::PrintStackTraceOnErrorSignal();
515 PrettyStackTraceProgram X(argc, argv);
516 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
517
518 StringRef ProgName(sys::path::filename(argv[0]));
519 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000520 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000521
522 if (strcmp(argv[1], "merge") == 0)
523 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000524 else if (strcmp(argv[1], "show") == 0)
525 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000526
527 if (func) {
528 std::string Invocation(ProgName.str() + " " + argv[1]);
529 argv[1] = Invocation.c_str();
530 return func(argc - 1, argv + 1);
531 }
532
Diego Novillod3babdb2015-12-14 20:37:15 +0000533 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000534 strcmp(argv[1], "--help") == 0) {
535
536 errs() << "OVERVIEW: LLVM profile data tools\n\n"
537 << "USAGE: " << ProgName << " <command> [args...]\n"
538 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000539 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000540 return 0;
541 }
542 }
543
544 if (argc < 2)
545 errs() << ProgName << ": No command specified!\n";
546 else
547 errs() << ProgName << ": Unknown command!\n";
548
Justin Bogner9af28ef2014-03-21 17:29:44 +0000549 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000550 return 1;
551}