Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/tools/llvm-profdata/llvm-profdata.cpp
Warning:line 522, column 5
Value stored to 'Threshold' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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