LLVM 19.0.0git
InstrProf.cpp
Go to the documentation of this file.
1//===- InstrProf.cpp - Instrumented profiling format support --------------===//
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// This file contains support for clang's instrumentation based PGO and
10// coverage.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/SetVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Config/config.h"
21#include "llvm/IR/Constant.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Instruction.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/MDBuilder.h"
29#include "llvm/IR/Metadata.h"
30#include "llvm/IR/Module.h"
31#include "llvm/IR/Type.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/Endian.h"
39#include "llvm/Support/Error.h"
41#include "llvm/Support/LEB128.h"
43#include "llvm/Support/Path.h"
47#include <algorithm>
48#include <cassert>
49#include <cstddef>
50#include <cstdint>
51#include <cstring>
52#include <memory>
53#include <string>
54#include <system_error>
55#include <type_traits>
56#include <utility>
57#include <vector>
58
59using namespace llvm;
60
61#define DEBUG_TYPE "instrprof"
62
64 "static-func-full-module-prefix", cl::init(true), cl::Hidden,
65 cl::desc("Use full module build paths in the profile counter names for "
66 "static functions."));
67
68// This option is tailored to users that have different top-level directory in
69// profile-gen and profile-use compilation. Users need to specific the number
70// of levels to strip. A value larger than the number of directories in the
71// source file will strip all the directory names and only leave the basename.
72//
73// Note current ThinLTO module importing for the indirect-calls assumes
74// the source directory name not being stripped. A non-zero option value here
75// can potentially prevent some inter-module indirect-call-promotions.
77 "static-func-strip-dirname-prefix", cl::init(0), cl::Hidden,
78 cl::desc("Strip specified level of directory name from source path in "
79 "the profile counter name for static functions."));
80
82 const std::string &ErrMsg = "") {
83 std::string Msg;
85
86 switch (Err) {
87 case instrprof_error::success:
88 OS << "success";
89 break;
90 case instrprof_error::eof:
91 OS << "end of File";
92 break;
93 case instrprof_error::unrecognized_format:
94 OS << "unrecognized instrumentation profile encoding format";
95 break;
96 case instrprof_error::bad_magic:
97 OS << "invalid instrumentation profile data (bad magic)";
98 break;
99 case instrprof_error::bad_header:
100 OS << "invalid instrumentation profile data (file header is corrupt)";
101 break;
102 case instrprof_error::unsupported_version:
103 OS << "unsupported instrumentation profile format version";
104 break;
105 case instrprof_error::unsupported_hash_type:
106 OS << "unsupported instrumentation profile hash type";
107 break;
108 case instrprof_error::too_large:
109 OS << "too much profile data";
110 break;
111 case instrprof_error::truncated:
112 OS << "truncated profile data";
113 break;
114 case instrprof_error::malformed:
115 OS << "malformed instrumentation profile data";
116 break;
117 case instrprof_error::missing_correlation_info:
118 OS << "debug info/binary for correlation is required";
119 break;
120 case instrprof_error::unexpected_correlation_info:
121 OS << "debug info/binary for correlation is not necessary";
122 break;
123 case instrprof_error::unable_to_correlate_profile:
124 OS << "unable to correlate profile";
125 break;
126 case instrprof_error::invalid_prof:
127 OS << "invalid profile created. Please file a bug "
128 "at: " BUG_REPORT_URL
129 " and include the profraw files that caused this error.";
130 break;
131 case instrprof_error::unknown_function:
132 OS << "no profile data available for function";
133 break;
134 case instrprof_error::hash_mismatch:
135 OS << "function control flow change detected (hash mismatch)";
136 break;
137 case instrprof_error::count_mismatch:
138 OS << "function basic block count change detected (counter mismatch)";
139 break;
140 case instrprof_error::bitmap_mismatch:
141 OS << "function bitmap size change detected (bitmap size mismatch)";
142 break;
143 case instrprof_error::counter_overflow:
144 OS << "counter overflow";
145 break;
146 case instrprof_error::value_site_count_mismatch:
147 OS << "function value site count change detected (counter mismatch)";
148 break;
149 case instrprof_error::compress_failed:
150 OS << "failed to compress data (zlib)";
151 break;
152 case instrprof_error::uncompress_failed:
153 OS << "failed to uncompress data (zlib)";
154 break;
155 case instrprof_error::empty_raw_profile:
156 OS << "empty raw profile file";
157 break;
158 case instrprof_error::zlib_unavailable:
159 OS << "profile uses zlib compression but the profile reader was built "
160 "without zlib support";
161 break;
162 case instrprof_error::raw_profile_version_mismatch:
163 OS << "raw profile version mismatch";
164 break;
165 case instrprof_error::counter_value_too_large:
166 OS << "excessively large counter value suggests corrupted profile data";
167 break;
168 }
169
170 // If optional error message is not empty, append it to the message.
171 if (!ErrMsg.empty())
172 OS << ": " << ErrMsg;
173
174 return OS.str();
175}
176
177namespace {
178
179// FIXME: This class is only here to support the transition to llvm::Error. It
180// will be removed once this transition is complete. Clients should prefer to
181// deal with the Error value directly, rather than converting to error_code.
182class InstrProfErrorCategoryType : public std::error_category {
183 const char *name() const noexcept override { return "llvm.instrprof"; }
184
185 std::string message(int IE) const override {
186 return getInstrProfErrString(static_cast<instrprof_error>(IE));
187 }
188};
189
190} // end anonymous namespace
191
192const std::error_category &llvm::instrprof_category() {
193 static InstrProfErrorCategoryType ErrorCategory;
194 return ErrorCategory;
195}
196
197namespace {
198
199const char *InstrProfSectNameCommon[] = {
200#define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
201 SectNameCommon,
203};
204
205const char *InstrProfSectNameCoff[] = {
206#define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
207 SectNameCoff,
209};
210
211const char *InstrProfSectNamePrefix[] = {
212#define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
213 Prefix,
215};
216
217} // namespace
218
219namespace llvm {
220
222 "enable-name-compression",
223 cl::desc("Enable name/filename string compression"), cl::init(true));
224
226 "enable-vtable-value-profiling", cl::init(false),
227 cl::desc("If true, the virtual table address will be instrumented to know "
228 "the types of a C++ pointer. The information is used in indirect "
229 "call promotion to do selective vtable-based comparison."));
230
232 "enable-vtable-profile-use", cl::init(false),
233 cl::desc("If ThinLTO and WPD is enabled and this option is true, vtable "
234 "profiles will be used by ICP pass for more efficient indirect "
235 "call sequence. If false, type profiles won't be used."));
236
239 bool AddSegmentInfo) {
240 std::string SectName;
241
242 if (OF == Triple::MachO && AddSegmentInfo)
243 SectName = InstrProfSectNamePrefix[IPSK];
244
245 if (OF == Triple::COFF)
246 SectName += InstrProfSectNameCoff[IPSK];
247 else
248 SectName += InstrProfSectNameCommon[IPSK];
249
250 if (OF == Triple::MachO && IPSK == IPSK_data && AddSegmentInfo)
251 SectName += ",regular,live_support";
252
253 return SectName;
254}
255
256std::string InstrProfError::message() const {
257 return getInstrProfErrString(Err, Msg);
258}
259
260char InstrProfError::ID = 0;
261
263 StringRef FileName,
265 // Value names may be prefixed with a binary '1' to indicate
266 // that the backend should not modify the symbols due to any platform
267 // naming convention. Do not include that '1' in the PGO profile name.
268 if (Name[0] == '\1')
269 Name = Name.substr(1);
270
271 std::string NewName = std::string(Name);
273 // For local symbols, prepend the main file name to distinguish them.
274 // Do not include the full path in the file name since there's no guarantee
275 // that it will stay the same, e.g., if the files are checked out from
276 // version control in different locations.
277 if (FileName.empty())
278 NewName = NewName.insert(0, "<unknown>:");
279 else
280 NewName = NewName.insert(0, FileName.str() + ":");
281 }
282 return NewName;
283}
284
285// Strip NumPrefix level of directory name from PathNameStr. If the number of
286// directory separators is less than NumPrefix, strip all the directories and
287// leave base file name only.
288static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
289 uint32_t Count = NumPrefix;
290 uint32_t Pos = 0, LastPos = 0;
291 for (const auto &CI : PathNameStr) {
292 ++Pos;
294 LastPos = Pos;
295 --Count;
296 }
297 if (Count == 0)
298 break;
299 }
300 return PathNameStr.substr(LastPos);
301}
302
304 StringRef FileName(GO.getParent()->getSourceFileName());
305 uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t)-1;
306 if (StripLevel < StaticFuncStripDirNamePrefix)
307 StripLevel = StaticFuncStripDirNamePrefix;
308 if (StripLevel)
309 FileName = stripDirPrefix(FileName, StripLevel);
310 return FileName;
311}
312
313// The PGO name has the format [<filepath>;]<mangled-name> where <filepath>; is
314// provided if linkage is local and is used to discriminate possibly identical
315// mangled names. ";" is used because it is unlikely to be found in either
316// <filepath> or <mangled-name>.
317//
318// Older compilers used getPGOFuncName() which has the format
319// [<filepath>:]<mangled-name>. This caused trouble for Objective-C functions
320// which commonly have :'s in their names. We still need to compute this name to
321// lookup functions from profiles built by older compilers.
322static std::string
325 StringRef FileName) {
326 return GlobalValue::getGlobalIdentifier(GO.getName(), Linkage, FileName);
327}
328
329static std::optional<std::string> lookupPGONameFromMetadata(MDNode *MD) {
330 if (MD != nullptr) {
331 StringRef S = cast<MDString>(MD->getOperand(0))->getString();
332 return S.str();
333 }
334 return {};
335}
336
337// Returns the PGO object name. This function has some special handling
338// when called in LTO optimization. The following only applies when calling in
339// LTO passes (when \c InLTO is true): LTO's internalization privatizes many
340// global linkage symbols. This happens after value profile annotation, but
341// those internal linkage functions should not have a source prefix.
342// Additionally, for ThinLTO mode, exported internal functions are promoted
343// and renamed. We need to ensure that the original internal PGO name is
344// used when computing the GUID that is compared against the profiled GUIDs.
345// To differentiate compiler generated internal symbols from original ones,
346// PGOFuncName meta data are created and attached to the original internal
347// symbols in the value profile annotation step
348// (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
349// data, its original linkage must be non-internal.
350static std::string getIRPGOObjectName(const GlobalObject &GO, bool InLTO,
351 MDNode *PGONameMetadata) {
352 if (!InLTO) {
353 auto FileName = getStrippedSourceFileName(GO);
354 return getIRPGONameForGlobalObject(GO, GO.getLinkage(), FileName);
355 }
356
357 // In LTO mode (when InLTO is true), first check if there is a meta data.
358 if (auto IRPGOFuncName = lookupPGONameFromMetadata(PGONameMetadata))
359 return *IRPGOFuncName;
360
361 // If there is no meta data, the function must be a global before the value
362 // profile annotation pass. Its current linkage may be internal if it is
363 // internalized in LTO mode.
365}
366
367// Returns the IRPGO function name and does special handling when called
368// in LTO optimization. See the comments of `getIRPGOObjectName` for details.
369std::string getIRPGOFuncName(const Function &F, bool InLTO) {
371}
372
373// Please use getIRPGOFuncName for LLVM IR instrumentation. This function is
374// for front-end (Clang, etc) instrumentation.
375// The implementation is kept for profile matching from older profiles.
376// This is similar to `getIRPGOFuncName` except that this function calls
377// 'getPGOFuncName' to get a name and `getIRPGOFuncName` calls
378// 'getIRPGONameForGlobalObject'. See the difference between two callees in the
379// comments of `getIRPGONameForGlobalObject`.
380std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
381 if (!InLTO) {
382 auto FileName = getStrippedSourceFileName(F);
383 return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
384 }
385
386 // In LTO mode (when InLTO is true), first check if there is a meta data.
387 if (auto PGOFuncName = lookupPGONameFromMetadata(getPGOFuncNameMetadata(F)))
388 return *PGOFuncName;
389
390 // If there is no meta data, the function must be a global before the value
391 // profile annotation pass. Its current linkage may be internal if it is
392 // internalized in LTO mode.
393 return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
394}
395
396std::string getPGOName(const GlobalVariable &V, bool InLTO) {
397 // PGONameMetadata should be set by compiler at profile use time
398 // and read by symtab creation to look up symbols corresponding to
399 // a MD5 hash.
400 return getIRPGOObjectName(V, InLTO, V.getMetadata(getPGONameMetadataName()));
401}
402
403// See getIRPGOObjectName() for a discription of the format.
404std::pair<StringRef, StringRef> getParsedIRPGOName(StringRef IRPGOName) {
405 auto [FileName, MangledName] = IRPGOName.split(kGlobalIdentifierDelimiter);
406 if (MangledName.empty())
407 return std::make_pair(StringRef(), IRPGOName);
408 return std::make_pair(FileName, MangledName);
409}
410
412 if (FileName.empty())
413 return PGOFuncName;
414 // Drop the file name including ':' or ';'. See getIRPGONameForGlobalObject as
415 // well.
416 if (PGOFuncName.starts_with(FileName))
417 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
418 return PGOFuncName;
419}
420
421// \p FuncName is the string used as profile lookup key for the function. A
422// symbol is created to hold the name. Return the legalized symbol name.
423std::string getPGOFuncNameVarName(StringRef FuncName,
425 std::string VarName = std::string(getInstrProfNameVarPrefix());
426 VarName += FuncName;
427
428 if (!GlobalValue::isLocalLinkage(Linkage))
429 return VarName;
430
431 // Now fix up illegal chars in local VarName that may upset the assembler.
432 const char InvalidChars[] = "-:;<>/\"'";
433 size_t found = VarName.find_first_of(InvalidChars);
434 while (found != std::string::npos) {
435 VarName[found] = '_';
436 found = VarName.find_first_of(InvalidChars, found + 1);
437 }
438 return VarName;
439}
440
443 StringRef PGOFuncName) {
444 // We generally want to match the function's linkage, but available_externally
445 // and extern_weak both have the wrong semantics, and anything that doesn't
446 // need to link across compilation units doesn't need to be visible at all.
449 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
451 else if (Linkage == GlobalValue::InternalLinkage ||
454
455 auto *Value =
456 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
457 auto FuncNameVar =
458 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
459 getPGOFuncNameVarName(PGOFuncName, Linkage));
460
461 // Hide the symbol so that we correctly get a copy for each executable.
462 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
463 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
464
465 return FuncNameVar;
466}
467
469 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
470}
471
473 for (Function &F : M) {
474 // Function may not have a name: like using asm("") to overwrite the name.
475 // Ignore in this case.
476 if (!F.hasName())
477 continue;
478 if (Error E = addFuncWithName(F, getIRPGOFuncName(F, InLTO)))
479 return E;
480 // Also use getPGOFuncName() so that we can find records from older profiles
481 if (Error E = addFuncWithName(F, getPGOFuncName(F, InLTO)))
482 return E;
483 }
484
486 for (GlobalVariable &G : M.globals()) {
487 if (!G.hasName() || !G.hasMetadata(LLVMContext::MD_type))
488 continue;
489 if (Error E = addVTableWithName(G, getPGOName(G, InLTO)))
490 return E;
491 }
492
493 Sorted = false;
494 finalizeSymtab();
495 return Error::success();
496}
497
498Error InstrProfSymtab::addVTableWithName(GlobalVariable &VTable,
499 StringRef VTablePGOName) {
500 auto mapName = [&](StringRef Name) -> Error {
501 if (Error E = addSymbolName(Name))
502 return E;
503
504 bool Inserted = true;
505 std::tie(std::ignore, Inserted) =
506 MD5VTableMap.try_emplace(GlobalValue::getGUID(Name), &VTable);
507 if (!Inserted)
508 LLVM_DEBUG(dbgs() << "GUID conflict within one module");
509 return Error::success();
510 };
511 if (Error E = mapName(VTablePGOName))
512 return E;
513
514 StringRef CanonicalName = getCanonicalName(VTablePGOName);
515 if (CanonicalName != VTablePGOName)
516 return mapName(CanonicalName);
517
518 return Error::success();
519}
520
521/// \c NameStrings is a string composed of one of more possibly encoded
522/// sub-strings. The substrings are separated by 0 or more zero bytes. This
523/// method decodes the string and calls `NameCallback` for each substring.
524static Error
526 std::function<Error(StringRef)> NameCallback) {
527 const uint8_t *P = NameStrings.bytes_begin();
528 const uint8_t *EndP = NameStrings.bytes_end();
529 while (P < EndP) {
530 uint32_t N;
531 uint64_t UncompressedSize = decodeULEB128(P, &N);
532 P += N;
533 uint64_t CompressedSize = decodeULEB128(P, &N);
534 P += N;
535 bool isCompressed = (CompressedSize != 0);
536 SmallVector<uint8_t, 128> UncompressedNameStrings;
537 StringRef NameStrings;
538 if (isCompressed) {
540 return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
541
542 if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
543 UncompressedNameStrings,
544 UncompressedSize)) {
545 consumeError(std::move(E));
546 return make_error<InstrProfError>(instrprof_error::uncompress_failed);
547 }
548 P += CompressedSize;
549 NameStrings = toStringRef(UncompressedNameStrings);
550 } else {
551 NameStrings =
552 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
553 P += UncompressedSize;
554 }
555 // Now parse the name strings.
557 NameStrings.split(Names, getInstrProfNameSeparator());
558 for (StringRef &Name : Names)
559 if (Error E = NameCallback(Name))
560 return E;
561
562 while (P < EndP && *P == 0)
563 P++;
564 }
565 return Error::success();
566}
567
570 NameStrings,
571 std::bind(&InstrProfSymtab::addFuncName, this, std::placeholders::_1));
572}
573
575 StringRef VTableNameStrings) {
576 if (Error E = readAndDecodeStrings(FuncNameStrings,
578 this, std::placeholders::_1)))
579 return E;
580
582 VTableNameStrings,
583 std::bind(&InstrProfSymtab::addVTableName, this, std::placeholders::_1));
584}
585
587 StringRef CompressedVTableStrings) {
589 CompressedVTableStrings,
590 std::bind(&InstrProfSymtab::addVTableName, this, std::placeholders::_1));
591}
592
593StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName) {
594 // In ThinLTO, local function may have been promoted to global and have
595 // suffix ".llvm." added to the function name. We need to add the
596 // stripped function name to the symbol table so that we can find a match
597 // from profile.
598 //
599 // ".__uniq." suffix is used to differentiate internal linkage functions in
600 // different modules and should be kept. This is the only suffix with the
601 // pattern ".xxx" which is kept before matching, other suffixes similar as
602 // ".llvm." will be stripped.
603 const std::string UniqSuffix = ".__uniq.";
604 size_t pos = PGOName.find(UniqSuffix);
605 if (pos != StringRef::npos)
606 pos += UniqSuffix.length();
607 else
608 pos = 0;
609
610 // Search '.' after ".__uniq." if ".__uniq." exists, otherwise search '.' from
611 // the beginning.
612 pos = PGOName.find('.', pos);
613 if (pos != StringRef::npos && pos != 0)
614 return PGOName.substr(0, pos);
615
616 return PGOName;
617}
618
619Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
620 auto mapName = [&](StringRef Name) -> Error {
621 if (Error E = addFuncName(Name))
622 return E;
623 MD5FuncMap.emplace_back(Function::getGUID(Name), &F);
624 return Error::success();
625 };
626 if (Error E = mapName(PGOFuncName))
627 return E;
628
629 StringRef CanonicalFuncName = getCanonicalName(PGOFuncName);
630 if (CanonicalFuncName != PGOFuncName)
631 return mapName(CanonicalFuncName);
632
633 return Error::success();
634}
635
637 // Given a runtime address, look up the hash value in the interval map, and
638 // fallback to value 0 if a hash value is not found.
639 return VTableAddrMap.lookup(Address, 0);
640}
641
643 finalizeSymtab();
644 auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
645 return A.first < Address;
646 });
647 // Raw function pointer collected by value profiler may be from
648 // external functions that are not instrumented. They won't have
649 // mapping data to be used by the deserializer. Force the value to
650 // be 0 in this case.
651 if (It != AddrToMD5Map.end() && It->first == Address)
652 return (uint64_t)It->second;
653 return 0;
654}
655
657 SmallVector<StringRef, 0> Sorted(NameTab.keys());
658 llvm::sort(Sorted);
659 for (StringRef S : Sorted)
660 OS << S << '\n';
661}
662
664 bool doCompression, std::string &Result) {
665 assert(!NameStrs.empty() && "No name data to emit");
666
667 uint8_t Header[20], *P = Header;
668 std::string UncompressedNameStrings =
669 join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
670
671 assert(StringRef(UncompressedNameStrings)
672 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
673 "PGO name is invalid (contains separator token)");
674
675 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
676 P += EncLen;
677
678 auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
679 EncLen = encodeULEB128(CompressedLen, P);
680 P += EncLen;
681 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
682 unsigned HeaderLen = P - &Header[0];
683 Result.append(HeaderStr, HeaderLen);
684 Result += InputStr;
685 return Error::success();
686 };
687
688 if (!doCompression) {
689 return WriteStringToResult(0, UncompressedNameStrings);
690 }
691
692 SmallVector<uint8_t, 128> CompressedNameStrings;
693 compression::zlib::compress(arrayRefFromStringRef(UncompressedNameStrings),
694 CompressedNameStrings,
696
697 return WriteStringToResult(CompressedNameStrings.size(),
698 toStringRef(CompressedNameStrings));
699}
700
702 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
703 StringRef NameStr =
704 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
705 return NameStr;
706}
707
709 std::string &Result, bool doCompression) {
710 std::vector<std::string> NameStrs;
711 for (auto *NameVar : NameVars) {
712 NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar)));
713 }
715 NameStrs, compression::zlib::isAvailable() && doCompression, Result);
716}
717
719 std::string &Result, bool doCompression) {
720 std::vector<std::string> VTableNameStrs;
721 for (auto *VTable : VTables)
722 VTableNameStrs.push_back(getPGOName(*VTable));
724 VTableNameStrs, compression::zlib::isAvailable() && doCompression,
725 Result);
726}
727
729 uint64_t FuncSum = 0;
730 Sum.NumEntries += Counts.size();
731 for (uint64_t Count : Counts)
732 FuncSum += Count;
733 Sum.CountSum += FuncSum;
734
735 for (uint32_t VK = IPVK_First; VK <= IPVK_Last; ++VK) {
736 uint64_t KindSum = 0;
737 uint32_t NumValueSites = getNumValueSites(VK);
738 for (size_t I = 0; I < NumValueSites; ++I) {
739 for (const auto &V : getValueArrayForSite(VK, I))
740 KindSum += V.Count;
741 }
742 Sum.ValueCounts[VK] += KindSum;
743 }
744}
745
747 uint32_t ValueKind,
748 OverlapStats &Overlap,
749 OverlapStats &FuncLevelOverlap) {
750 this->sortByTargetValues();
751 Input.sortByTargetValues();
752 double Score = 0.0f, FuncLevelScore = 0.0f;
753 auto I = ValueData.begin();
754 auto IE = ValueData.end();
755 auto J = Input.ValueData.begin();
756 auto JE = Input.ValueData.end();
757 while (I != IE && J != JE) {
758 if (I->Value == J->Value) {
759 Score += OverlapStats::score(I->Count, J->Count,
760 Overlap.Base.ValueCounts[ValueKind],
761 Overlap.Test.ValueCounts[ValueKind]);
762 FuncLevelScore += OverlapStats::score(
763 I->Count, J->Count, FuncLevelOverlap.Base.ValueCounts[ValueKind],
764 FuncLevelOverlap.Test.ValueCounts[ValueKind]);
765 ++I;
766 } else if (I->Value < J->Value) {
767 ++I;
768 continue;
769 }
770 ++J;
771 }
772 Overlap.Overlap.ValueCounts[ValueKind] += Score;
773 FuncLevelOverlap.Overlap.ValueCounts[ValueKind] += FuncLevelScore;
774}
775
776// Return false on mismatch.
779 OverlapStats &Overlap,
780 OverlapStats &FuncLevelOverlap) {
781 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
782 assert(ThisNumValueSites == Other.getNumValueSites(ValueKind));
783 if (!ThisNumValueSites)
784 return;
785
786 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
787 getOrCreateValueSitesForKind(ValueKind);
789 Other.getValueSitesForKind(ValueKind);
790 for (uint32_t I = 0; I < ThisNumValueSites; I++)
791 ThisSiteRecords[I].overlap(OtherSiteRecords[I], ValueKind, Overlap,
792 FuncLevelOverlap);
793}
794
796 OverlapStats &FuncLevelOverlap,
797 uint64_t ValueCutoff) {
798 // FuncLevel CountSum for other should already computed and nonzero.
799 assert(FuncLevelOverlap.Test.CountSum >= 1.0f);
800 accumulateCounts(FuncLevelOverlap.Base);
801 bool Mismatch = (Counts.size() != Other.Counts.size());
802
803 // Check if the value profiles mismatch.
804 if (!Mismatch) {
805 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
806 uint32_t ThisNumValueSites = getNumValueSites(Kind);
807 uint32_t OtherNumValueSites = Other.getNumValueSites(Kind);
808 if (ThisNumValueSites != OtherNumValueSites) {
809 Mismatch = true;
810 break;
811 }
812 }
813 }
814 if (Mismatch) {
815 Overlap.addOneMismatch(FuncLevelOverlap.Test);
816 return;
817 }
818
819 // Compute overlap for value counts.
820 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
821 overlapValueProfData(Kind, Other, Overlap, FuncLevelOverlap);
822
823 double Score = 0.0;
824 uint64_t MaxCount = 0;
825 // Compute overlap for edge counts.
826 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
827 Score += OverlapStats::score(Counts[I], Other.Counts[I],
828 Overlap.Base.CountSum, Overlap.Test.CountSum);
829 MaxCount = std::max(Other.Counts[I], MaxCount);
830 }
831 Overlap.Overlap.CountSum += Score;
832 Overlap.Overlap.NumEntries += 1;
833
834 if (MaxCount >= ValueCutoff) {
835 double FuncScore = 0.0;
836 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I)
837 FuncScore += OverlapStats::score(Counts[I], Other.Counts[I],
838 FuncLevelOverlap.Base.CountSum,
839 FuncLevelOverlap.Test.CountSum);
840 FuncLevelOverlap.Overlap.CountSum = FuncScore;
841 FuncLevelOverlap.Overlap.NumEntries = Other.Counts.size();
842 FuncLevelOverlap.Valid = true;
843 }
844}
845
847 uint64_t Weight,
848 function_ref<void(instrprof_error)> Warn) {
849 this->sortByTargetValues();
850 Input.sortByTargetValues();
851 auto I = ValueData.begin();
852 auto IE = ValueData.end();
853 std::vector<InstrProfValueData> Merged;
854 Merged.reserve(std::max(ValueData.size(), Input.ValueData.size()));
855 for (const InstrProfValueData &J : Input.ValueData) {
856 while (I != IE && I->Value < J.Value) {
857 Merged.push_back(*I);
858 ++I;
859 }
860 if (I != IE && I->Value == J.Value) {
861 bool Overflowed;
862 I->Count = SaturatingMultiplyAdd(J.Count, Weight, I->Count, &Overflowed);
863 if (Overflowed)
865 Merged.push_back(*I);
866 ++I;
867 continue;
868 }
869 Merged.push_back(J);
870 }
871 Merged.insert(Merged.end(), I, IE);
872 ValueData = std::move(Merged);
873}
874
876 function_ref<void(instrprof_error)> Warn) {
877 for (InstrProfValueData &I : ValueData) {
878 bool Overflowed;
879 I.Count = SaturatingMultiply(I.Count, N, &Overflowed) / D;
880 if (Overflowed)
882 }
883}
884
885// Merge Value Profile data from Src record to this record for ValueKind.
886// Scale merged value counts by \p Weight.
887void InstrProfRecord::mergeValueProfData(
888 uint32_t ValueKind, InstrProfRecord &Src, uint64_t Weight,
889 function_ref<void(instrprof_error)> Warn) {
890 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
891 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
892 if (ThisNumValueSites != OtherNumValueSites) {
894 return;
895 }
896 if (!ThisNumValueSites)
897 return;
898 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
899 getOrCreateValueSitesForKind(ValueKind);
901 Src.getValueSitesForKind(ValueKind);
902 for (uint32_t I = 0; I < ThisNumValueSites; I++)
903 ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight, Warn);
904}
905
907 function_ref<void(instrprof_error)> Warn) {
908 // If the number of counters doesn't match we either have bad data
909 // or a hash collision.
910 if (Counts.size() != Other.Counts.size()) {
912 return;
913 }
914
915 // Special handling of the first count as the PseudoCount.
916 CountPseudoKind OtherKind = Other.getCountPseudoKind();
918 if (OtherKind != NotPseudo || ThisKind != NotPseudo) {
919 // We don't allow the merge of a profile with pseudo counts and
920 // a normal profile (i.e. without pesudo counts).
921 // Profile supplimenation should be done after the profile merge.
922 if (OtherKind == NotPseudo || ThisKind == NotPseudo) {
924 return;
925 }
926 if (OtherKind == PseudoHot || ThisKind == PseudoHot)
928 else
930 return;
931 }
932
933 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
934 bool Overflowed;
936 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
939 Overflowed = true;
940 }
941 Counts[I] = Value;
942 if (Overflowed)
944 }
945
946 // If the number of bitmap bytes doesn't match we either have bad data
947 // or a hash collision.
948 if (BitmapBytes.size() != Other.BitmapBytes.size()) {
950 return;
951 }
952
953 // Bitmap bytes are merged by simply ORing them together.
954 for (size_t I = 0, E = Other.BitmapBytes.size(); I < E; ++I) {
955 BitmapBytes[I] = Other.BitmapBytes[I] | BitmapBytes[I];
956 }
957
958 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
959 mergeValueProfData(Kind, Other, Weight, Warn);
960}
961
962void InstrProfRecord::scaleValueProfData(
963 uint32_t ValueKind, uint64_t N, uint64_t D,
964 function_ref<void(instrprof_error)> Warn) {
965 for (auto &R : getValueSitesForKind(ValueKind))
966 R.scale(N, D, Warn);
967}
968
970 function_ref<void(instrprof_error)> Warn) {
971 assert(D != 0 && "D cannot be 0");
972 for (auto &Count : this->Counts) {
973 bool Overflowed;
974 Count = SaturatingMultiply(Count, N, &Overflowed) / D;
975 if (Count > getInstrMaxCountValue()) {
976 Count = getInstrMaxCountValue();
977 Overflowed = true;
978 }
979 if (Overflowed)
981 }
982 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
983 scaleValueProfData(Kind, N, D, Warn);
984}
985
986// Map indirect call target name hash to name string.
987uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
988 InstrProfSymtab *SymTab) {
989 if (!SymTab)
990 return Value;
991
992 if (ValueKind == IPVK_IndirectCallTarget)
993 return SymTab->getFunctionHashFromAddress(Value);
994
995 if (ValueKind == IPVK_VTableTarget)
996 return SymTab->getVTableHashFromAddress(Value);
997
998 return Value;
999}
1000
1002 InstrProfValueData *VData, uint32_t N,
1004 for (uint32_t I = 0; I < N; I++) {
1005 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
1006 }
1007 std::vector<InstrProfValueSiteRecord> &ValueSites =
1008 getOrCreateValueSitesForKind(ValueKind);
1009 assert(ValueSites.size() == Site);
1010 if (N == 0)
1011 ValueSites.emplace_back();
1012 else
1013 ValueSites.emplace_back(VData, VData + N);
1014}
1015
1017 ArrayRef<TemporalProfTraceTy> Traces, std::vector<BPFunctionNode> &Nodes,
1018 bool RemoveOutlierUNs) {
1019 using IDT = BPFunctionNode::IDT;
1020 using UtilityNodeT = BPFunctionNode::UtilityNodeT;
1021 UtilityNodeT MaxUN = 0;
1022 DenseMap<IDT, size_t> IdToFirstTimestamp;
1023 DenseMap<IDT, UtilityNodeT> IdToFirstUN;
1025 // TODO: We need to use the Trace.Weight field to give more weight to more
1026 // important utilities
1027 for (auto &Trace : Traces) {
1028 size_t CutoffTimestamp = 1;
1029 for (size_t Timestamp = 0; Timestamp < Trace.FunctionNameRefs.size();
1030 Timestamp++) {
1031 IDT Id = Trace.FunctionNameRefs[Timestamp];
1032 auto [It, WasInserted] = IdToFirstTimestamp.try_emplace(Id, Timestamp);
1033 if (!WasInserted)
1034 It->getSecond() = std::min<size_t>(It->getSecond(), Timestamp);
1035 if (Timestamp >= CutoffTimestamp) {
1036 ++MaxUN;
1037 CutoffTimestamp = 2 * Timestamp;
1038 }
1039 IdToFirstUN.try_emplace(Id, MaxUN);
1040 }
1041 for (auto &[Id, FirstUN] : IdToFirstUN)
1042 for (auto UN = FirstUN; UN <= MaxUN; ++UN)
1043 IdToUNs[Id].push_back(UN);
1044 ++MaxUN;
1045 IdToFirstUN.clear();
1046 }
1047
1048 if (RemoveOutlierUNs) {
1050 for (auto &[Id, UNs] : IdToUNs)
1051 for (auto &UN : UNs)
1052 ++UNFrequency[UN];
1053 // Filter out utility nodes that are too infrequent or too prevalent to make
1054 // BalancedPartitioning more effective.
1055 for (auto &[Id, UNs] : IdToUNs)
1056 llvm::erase_if(UNs, [&](auto &UN) {
1057 return UNFrequency[UN] <= 1 || 2 * UNFrequency[UN] > IdToUNs.size();
1058 });
1059 }
1060
1061 for (auto &[Id, UNs] : IdToUNs)
1062 Nodes.emplace_back(Id, UNs);
1063
1064 // Since BalancedPartitioning is sensitive to the initial order, we explicitly
1065 // order nodes by their earliest timestamp.
1066 llvm::sort(Nodes, [&](auto &L, auto &R) {
1067 return std::make_pair(IdToFirstTimestamp[L.Id], L.Id) <
1068 std::make_pair(IdToFirstTimestamp[R.Id], R.Id);
1069 });
1070}
1071
1072#define INSTR_PROF_COMMON_API_IMPL
1074
1075/*!
1076 * ValueProfRecordClosure Interface implementation for InstrProfRecord
1077 * class. These C wrappers are used as adaptors so that C++ code can be
1078 * invoked as callbacks.
1079 */
1081 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
1082}
1083
1085 return reinterpret_cast<const InstrProfRecord *>(Record)
1086 ->getNumValueSites(VKind);
1087}
1088
1090 return reinterpret_cast<const InstrProfRecord *>(Record)
1091 ->getNumValueData(VKind);
1092}
1093
1095 uint32_t S) {
1096 const auto *IPR = reinterpret_cast<const InstrProfRecord *>(R);
1097 return IPR->getValueArrayForSite(VK, S).size();
1098}
1099
1100void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
1101 uint32_t K, uint32_t S) {
1102 const auto *IPR = reinterpret_cast<const InstrProfRecord *>(R);
1103 llvm::copy(IPR->getValueArrayForSite(K, S), Dst);
1104}
1105
1106ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
1107 ValueProfData *VD =
1108 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
1109 memset(VD, 0, TotalSizeInBytes);
1110 return VD;
1111}
1112
1113static ValueProfRecordClosure InstrProfRecordClosure = {
1114 nullptr,
1119 nullptr,
1122
1123// Wrapper implementation using the closure mechanism.
1124uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
1125 auto Closure = InstrProfRecordClosure;
1126 Closure.Record = &Record;
1127 return getValueProfDataSize(&Closure);
1128}
1129
1130// Wrapper implementation using the closure mechanism.
1131std::unique_ptr<ValueProfData>
1132ValueProfData::serializeFrom(const InstrProfRecord &Record) {
1134
1135 std::unique_ptr<ValueProfData> VPD(
1136 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
1137 return VPD;
1138}
1139
1140void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
1141 InstrProfSymtab *SymTab) {
1142 Record.reserveSites(Kind, NumValueSites);
1143
1144 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
1145 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
1146 uint8_t ValueDataCount = this->SiteCountArray[VSite];
1147 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
1148 ValueData += ValueDataCount;
1149 }
1150}
1151
1152// For writing/serializing, Old is the host endianness, and New is
1153// byte order intended on disk. For Reading/deserialization, Old
1154// is the on-disk source endianness, and New is the host endianness.
1155void ValueProfRecord::swapBytes(llvm::endianness Old, llvm::endianness New) {
1156 using namespace support;
1157
1158 if (Old == New)
1159 return;
1160
1161 if (llvm::endianness::native != Old) {
1162 sys::swapByteOrder<uint32_t>(NumValueSites);
1163 sys::swapByteOrder<uint32_t>(Kind);
1164 }
1165 uint32_t ND = getValueProfRecordNumValueData(this);
1166 InstrProfValueData *VD = getValueProfRecordValueData(this);
1167
1168 // No need to swap byte array: SiteCountArrray.
1169 for (uint32_t I = 0; I < ND; I++) {
1170 sys::swapByteOrder<uint64_t>(VD[I].Value);
1171 sys::swapByteOrder<uint64_t>(VD[I].Count);
1172 }
1173 if (llvm::endianness::native == Old) {
1174 sys::swapByteOrder<uint32_t>(NumValueSites);
1175 sys::swapByteOrder<uint32_t>(Kind);
1176 }
1177}
1178
1179void ValueProfData::deserializeTo(InstrProfRecord &Record,
1180 InstrProfSymtab *SymTab) {
1181 if (NumValueKinds == 0)
1182 return;
1183
1184 ValueProfRecord *VR = getFirstValueProfRecord(this);
1185 for (uint32_t K = 0; K < NumValueKinds; K++) {
1186 VR->deserializeTo(Record, SymTab);
1187 VR = getValueProfRecordNext(VR);
1188 }
1189}
1190
1191static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
1192 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
1193 ValueProfData());
1194}
1195
1196Error ValueProfData::checkIntegrity() {
1197 if (NumValueKinds > IPVK_Last + 1)
1198 return make_error<InstrProfError>(
1199 instrprof_error::malformed, "number of value profile kinds is invalid");
1200 // Total size needs to be multiple of quadword size.
1201 if (TotalSize % sizeof(uint64_t))
1202 return make_error<InstrProfError>(
1203 instrprof_error::malformed, "total size is not multiples of quardword");
1204
1205 ValueProfRecord *VR = getFirstValueProfRecord(this);
1206 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
1207 if (VR->Kind > IPVK_Last)
1208 return make_error<InstrProfError>(instrprof_error::malformed,
1209 "value kind is invalid");
1210 VR = getValueProfRecordNext(VR);
1211 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
1212 return make_error<InstrProfError>(
1214 "value profile address is greater than total size");
1215 }
1216 return Error::success();
1217}
1218
1220ValueProfData::getValueProfData(const unsigned char *D,
1221 const unsigned char *const BufferEnd,
1222 llvm::endianness Endianness) {
1223 using namespace support;
1224
1225 if (D + sizeof(ValueProfData) > BufferEnd)
1226 return make_error<InstrProfError>(instrprof_error::truncated);
1227
1228 const unsigned char *Header = D;
1229 uint32_t TotalSize = endian::readNext<uint32_t>(Header, Endianness);
1230
1231 if (D + TotalSize > BufferEnd)
1232 return make_error<InstrProfError>(instrprof_error::too_large);
1233
1234 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
1235 memcpy(VPD.get(), D, TotalSize);
1236 // Byte swap.
1237 VPD->swapBytesToHost(Endianness);
1238
1239 Error E = VPD->checkIntegrity();
1240 if (E)
1241 return std::move(E);
1242
1243 return std::move(VPD);
1244}
1245
1246void ValueProfData::swapBytesToHost(llvm::endianness Endianness) {
1247 using namespace support;
1248
1249 if (Endianness == llvm::endianness::native)
1250 return;
1251
1252 sys::swapByteOrder<uint32_t>(TotalSize);
1253 sys::swapByteOrder<uint32_t>(NumValueKinds);
1254
1255 ValueProfRecord *VR = getFirstValueProfRecord(this);
1256 for (uint32_t K = 0; K < NumValueKinds; K++) {
1257 VR->swapBytes(Endianness, llvm::endianness::native);
1258 VR = getValueProfRecordNext(VR);
1259 }
1260}
1261
1262void ValueProfData::swapBytesFromHost(llvm::endianness Endianness) {
1263 using namespace support;
1264
1265 if (Endianness == llvm::endianness::native)
1266 return;
1267
1268 ValueProfRecord *VR = getFirstValueProfRecord(this);
1269 for (uint32_t K = 0; K < NumValueKinds; K++) {
1270 ValueProfRecord *NVR = getValueProfRecordNext(VR);
1271 VR->swapBytes(llvm::endianness::native, Endianness);
1272 VR = NVR;
1273 }
1274 sys::swapByteOrder<uint32_t>(TotalSize);
1275 sys::swapByteOrder<uint32_t>(NumValueKinds);
1276}
1277
1279 const InstrProfRecord &InstrProfR,
1280 InstrProfValueKind ValueKind, uint32_t SiteIdx,
1281 uint32_t MaxMDCount) {
1282 auto VDs = InstrProfR.getValueArrayForSite(ValueKind, SiteIdx);
1283 if (VDs.empty())
1284 return;
1285 uint64_t Sum = 0;
1286 for (const InstrProfValueData &V : VDs)
1287 Sum = SaturatingAdd(Sum, V.Count);
1288 annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
1289}
1290
1293 uint64_t Sum, InstrProfValueKind ValueKind,
1294 uint32_t MaxMDCount) {
1295 if (VDs.empty())
1296 return;
1297 LLVMContext &Ctx = M.getContext();
1298 MDBuilder MDHelper(Ctx);
1300 // Tag
1301 Vals.push_back(MDHelper.createString("VP"));
1302 // Value Kind
1303 Vals.push_back(MDHelper.createConstant(
1304 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
1305 // Total Count
1306 Vals.push_back(
1307 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
1308
1309 // Value Profile Data
1310 uint32_t MDCount = MaxMDCount;
1311 for (const auto &VD : VDs) {
1312 Vals.push_back(MDHelper.createConstant(
1313 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
1314 Vals.push_back(MDHelper.createConstant(
1315 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
1316 if (--MDCount == 0)
1317 break;
1318 }
1319 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
1320}
1321
1323 InstrProfValueKind ValueKind) {
1324 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
1325 if (!MD)
1326 return nullptr;
1327
1328 if (MD->getNumOperands() < 5)
1329 return nullptr;
1330
1331 MDString *Tag = cast<MDString>(MD->getOperand(0));
1332 if (!Tag || Tag->getString() != "VP")
1333 return nullptr;
1334
1335 // Now check kind:
1336 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
1337 if (!KindInt)
1338 return nullptr;
1339 if (KindInt->getZExtValue() != ValueKind)
1340 return nullptr;
1341
1342 return MD;
1343}
1344
1345static bool getValueProfDataFromInstImpl(const MDNode *const MD,
1346 const uint32_t MaxNumDataWant,
1347 InstrProfValueData ValueData[],
1348 uint32_t &ActualNumValueData,
1349 uint64_t &TotalC, bool GetNoICPValue) {
1350 const unsigned NOps = MD->getNumOperands();
1351 // Get total count
1352 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
1353 if (!TotalCInt)
1354 return false;
1355 TotalC = TotalCInt->getZExtValue();
1356 ActualNumValueData = 0;
1357
1358 for (unsigned I = 3; I < NOps; I += 2) {
1359 if (ActualNumValueData >= MaxNumDataWant)
1360 break;
1361 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
1362 ConstantInt *Count =
1363 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
1364 if (!Value || !Count)
1365 return false;
1366 uint64_t CntValue = Count->getZExtValue();
1367 if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
1368 continue;
1369 ValueData[ActualNumValueData].Value = Value->getZExtValue();
1370 ValueData[ActualNumValueData].Count = CntValue;
1371 ActualNumValueData++;
1372 }
1373 return true;
1374}
1375
1376std::unique_ptr<InstrProfValueData[]>
1378 uint32_t MaxNumValueData, uint32_t &ActualNumValueData,
1379 uint64_t &TotalC, bool GetNoICPValue) {
1380 MDNode *MD = mayHaveValueProfileOfKind(Inst, ValueKind);
1381 if (!MD)
1382 return nullptr;
1383 auto ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumValueData);
1384 if (!getValueProfDataFromInstImpl(MD, MaxNumValueData, ValueDataArray.get(),
1385 ActualNumValueData, TotalC, GetNoICPValue))
1386 return nullptr;
1387 return ValueDataArray;
1388}
1389
1392 uint32_t MaxNumValueData, uint64_t &TotalC,
1393 bool GetNoICPValue) {
1394 // Four inline elements seem to work well in practice. With MaxNumValueData,
1395 // this array won't grow very big anyway.
1397 MDNode *MD = mayHaveValueProfileOfKind(Inst, ValueKind);
1398 if (!MD)
1399 return ValueData;
1400 const unsigned NOps = MD->getNumOperands();
1401 // Get total count
1402 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
1403 if (!TotalCInt)
1404 return ValueData;
1405 TotalC = TotalCInt->getZExtValue();
1406
1407 ValueData.reserve((NOps - 3) / 2);
1408 for (unsigned I = 3; I < NOps; I += 2) {
1409 if (ValueData.size() >= MaxNumValueData)
1410 break;
1411 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
1412 ConstantInt *Count =
1413 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
1414 if (!Value || !Count) {
1415 ValueData.clear();
1416 return ValueData;
1417 }
1418 uint64_t CntValue = Count->getZExtValue();
1419 if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
1420 continue;
1421 InstrProfValueData V;
1422 V.Value = Value->getZExtValue();
1423 V.Count = CntValue;
1424 ValueData.push_back(V);
1425 }
1426 return ValueData;
1427}
1428
1430 return F.getMetadata(getPGOFuncNameMetadataName());
1431}
1432
1433static void createPGONameMetadata(GlobalObject &GO, StringRef MetadataName,
1434 StringRef PGOName) {
1435 // Only for internal linkage functions or global variables. The name is not
1436 // the same as PGO name for these global objects.
1437 if (GO.getName() == PGOName)
1438 return;
1439
1440 // Don't create duplicated metadata.
1441 if (GO.getMetadata(MetadataName))
1442 return;
1443
1444 LLVMContext &C = GO.getContext();
1445 MDNode *N = MDNode::get(C, MDString::get(C, PGOName));
1446 GO.setMetadata(MetadataName, N);
1447}
1448
1450 return createPGONameMetadata(F, getPGOFuncNameMetadataName(), PGOFuncName);
1451}
1452
1454 return createPGONameMetadata(GO, getPGONameMetadataName(), PGOName);
1455}
1456
1457bool needsComdatForCounter(const GlobalObject &GO, const Module &M) {
1458 if (GO.hasComdat())
1459 return true;
1460
1461 if (!Triple(M.getTargetTriple()).supportsCOMDAT())
1462 return false;
1463
1464 // See createPGOFuncNameVar for more details. To avoid link errors, profile
1465 // counters for function with available_externally linkage needs to be changed
1466 // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
1467 // created. Without using comdat, duplicate entries won't be removed by the
1468 // linker leading to increased data segement size and raw profile size. Even
1469 // worse, since the referenced counter from profile per-function data object
1470 // will be resolved to the common strong definition, the profile counts for
1471 // available_externally functions will end up being duplicated in raw profile
1472 // data. This can result in distorted profile as the counts of those dups
1473 // will be accumulated by the profile merger.
1475 if (Linkage != GlobalValue::ExternalWeakLinkage &&
1477 return false;
1478
1479 return true;
1480}
1481
1482// Check if INSTR_PROF_RAW_VERSION_VAR is defined.
1483bool isIRPGOFlagSet(const Module *M) {
1484 auto IRInstrVar =
1485 M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1486 if (!IRInstrVar || IRInstrVar->hasLocalLinkage())
1487 return false;
1488
1489 // For CSPGO+LTO, this variable might be marked as non-prevailing and we only
1490 // have the decl.
1491 if (IRInstrVar->isDeclaration())
1492 return true;
1493
1494 // Check if the flag is set.
1495 if (!IRInstrVar->hasInitializer())
1496 return false;
1497
1498 auto *InitVal = dyn_cast_or_null<ConstantInt>(IRInstrVar->getInitializer());
1499 if (!InitVal)
1500 return false;
1501 return (InitVal->getZExtValue() & VARIANT_MASK_IR_PROF) != 0;
1502}
1503
1504// Check if we can safely rename this Comdat function.
1505bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken) {
1506 if (F.getName().empty())
1507 return false;
1508 if (!needsComdatForCounter(F, *(F.getParent())))
1509 return false;
1510 // Unsafe to rename the address-taken function (which can be used in
1511 // function comparison).
1512 if (CheckAddressTaken && F.hasAddressTaken())
1513 return false;
1514 // Only safe to do if this function may be discarded if it is not used
1515 // in the compilation unit.
1516 if (!GlobalValue::isDiscardableIfUnused(F.getLinkage()))
1517 return false;
1518
1519 // For AvailableExternallyLinkage functions.
1520 if (!F.hasComdat()) {
1522 return true;
1523 }
1524 return true;
1525}
1526
1527// Create the variable for the profile file name.
1528void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput) {
1529 if (InstrProfileOutput.empty())
1530 return;
1531 Constant *ProfileNameConst =
1532 ConstantDataArray::getString(M.getContext(), InstrProfileOutput, true);
1533 GlobalVariable *ProfileNameVar = new GlobalVariable(
1534 M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
1535 ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
1537 Triple TT(M.getTargetTriple());
1538 if (TT.supportsCOMDAT()) {
1540 ProfileNameVar->setComdat(M.getOrInsertComdat(
1541 StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
1542 }
1543}
1544
1545Error OverlapStats::accumulateCounts(const std::string &BaseFilename,
1546 const std::string &TestFilename,
1547 bool IsCS) {
1548 auto getProfileSum = [IsCS](const std::string &Filename,
1549 CountSumOrPercent &Sum) -> Error {
1550 // This function is only used from llvm-profdata that doesn't use any kind
1551 // of VFS. Just create a default RealFileSystem to read profiles.
1552 auto FS = vfs::getRealFileSystem();
1553 auto ReaderOrErr = InstrProfReader::create(Filename, *FS);
1554 if (Error E = ReaderOrErr.takeError()) {
1555 return E;
1556 }
1557 auto Reader = std::move(ReaderOrErr.get());
1558 Reader->accumulateCounts(Sum, IsCS);
1559 return Error::success();
1560 };
1561 auto Ret = getProfileSum(BaseFilename, Base);
1562 if (Ret)
1563 return Ret;
1564 Ret = getProfileSum(TestFilename, Test);
1565 if (Ret)
1566 return Ret;
1567 this->BaseFilename = &BaseFilename;
1568 this->TestFilename = &TestFilename;
1569 Valid = true;
1570 return Error::success();
1571}
1572
1574 Mismatch.NumEntries += 1;
1575 Mismatch.CountSum += MismatchFunc.CountSum / Test.CountSum;
1576 for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1577 if (Test.ValueCounts[I] >= 1.0f)
1579 MismatchFunc.ValueCounts[I] / Test.ValueCounts[I];
1580 }
1581}
1582
1584 Unique.NumEntries += 1;
1585 Unique.CountSum += UniqueFunc.CountSum / Test.CountSum;
1586 for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1587 if (Test.ValueCounts[I] >= 1.0f)
1588 Unique.ValueCounts[I] += UniqueFunc.ValueCounts[I] / Test.ValueCounts[I];
1589 }
1590}
1591
1593 if (!Valid)
1594 return;
1595
1596 const char *EntryName =
1597 (Level == ProgramLevel ? "functions" : "edge counters");
1598 if (Level == ProgramLevel) {
1599 OS << "Profile overlap infomation for base_profile: " << *BaseFilename
1600 << " and test_profile: " << *TestFilename << "\nProgram level:\n";
1601 } else {
1602 OS << "Function level:\n"
1603 << " Function: " << FuncName << " (Hash=" << FuncHash << ")\n";
1604 }
1605
1606 OS << " # of " << EntryName << " overlap: " << Overlap.NumEntries << "\n";
1607 if (Mismatch.NumEntries)
1608 OS << " # of " << EntryName << " mismatch: " << Mismatch.NumEntries
1609 << "\n";
1610 if (Unique.NumEntries)
1611 OS << " # of " << EntryName
1612 << " only in test_profile: " << Unique.NumEntries << "\n";
1613
1614 OS << " Edge profile overlap: " << format("%.3f%%", Overlap.CountSum * 100)
1615 << "\n";
1616 if (Mismatch.NumEntries)
1617 OS << " Mismatched count percentage (Edge): "
1618 << format("%.3f%%", Mismatch.CountSum * 100) << "\n";
1619 if (Unique.NumEntries)
1620 OS << " Percentage of Edge profile only in test_profile: "
1621 << format("%.3f%%", Unique.CountSum * 100) << "\n";
1622 OS << " Edge profile base count sum: " << format("%.0f", Base.CountSum)
1623 << "\n"
1624 << " Edge profile test count sum: " << format("%.0f", Test.CountSum)
1625 << "\n";
1626
1627 for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1628 if (Base.ValueCounts[I] < 1.0f && Test.ValueCounts[I] < 1.0f)
1629 continue;
1630 char ProfileKindName[20] = {0};
1631 switch (I) {
1632 case IPVK_IndirectCallTarget:
1633 strncpy(ProfileKindName, "IndirectCall", 19);
1634 break;
1635 case IPVK_MemOPSize:
1636 strncpy(ProfileKindName, "MemOP", 19);
1637 break;
1638 case IPVK_VTableTarget:
1639 strncpy(ProfileKindName, "VTable", 19);
1640 break;
1641 default:
1642 snprintf(ProfileKindName, 19, "VP[%d]", I);
1643 break;
1644 }
1645 OS << " " << ProfileKindName
1646 << " profile overlap: " << format("%.3f%%", Overlap.ValueCounts[I] * 100)
1647 << "\n";
1648 if (Mismatch.NumEntries)
1649 OS << " Mismatched count percentage (" << ProfileKindName
1650 << "): " << format("%.3f%%", Mismatch.ValueCounts[I] * 100) << "\n";
1651 if (Unique.NumEntries)
1652 OS << " Percentage of " << ProfileKindName
1653 << " profile only in test_profile: "
1654 << format("%.3f%%", Unique.ValueCounts[I] * 100) << "\n";
1655 OS << " " << ProfileKindName
1656 << " profile base count sum: " << format("%.0f", Base.ValueCounts[I])
1657 << "\n"
1658 << " " << ProfileKindName
1659 << " profile test count sum: " << format("%.0f", Test.ValueCounts[I])
1660 << "\n";
1661 }
1662}
1663
1664namespace IndexedInstrProf {
1665Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
1666 using namespace support;
1667 static_assert(std::is_standard_layout_v<Header>,
1668 "Use standard layout for Header for simplicity");
1669 Header H;
1670
1671 H.Magic = endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1672 // Check the magic number.
1673 if (H.Magic != IndexedInstrProf::Magic)
1674 return make_error<InstrProfError>(instrprof_error::bad_magic);
1675
1676 // Read the version.
1677 H.Version = endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1678 if (H.getIndexedProfileVersion() >
1680 return make_error<InstrProfError>(instrprof_error::unsupported_version);
1681
1683 "Please update the reader as needed when a new field is added "
1684 "or when indexed profile version gets bumped.");
1685
1686 Buffer += sizeof(uint64_t); // Skip Header.Unused field.
1687 H.HashType = endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1688 H.HashOffset = endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1689 if (H.getIndexedProfileVersion() >= 8)
1690 H.MemProfOffset =
1691 endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1692 if (H.getIndexedProfileVersion() >= 9)
1693 H.BinaryIdOffset =
1694 endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1695 // Version 11 is handled by this condition.
1696 if (H.getIndexedProfileVersion() >= 10)
1697 H.TemporalProfTracesOffset =
1698 endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1699 if (H.getIndexedProfileVersion() >= 12)
1700 H.VTableNamesOffset =
1701 endian::readNext<uint64_t, llvm::endianness::little>(Buffer);
1702 return H;
1703}
1704
1706 return GET_VERSION(Version);
1707}
1708
1709size_t Header::size() const {
1710 switch (getIndexedProfileVersion()) {
1711 // To retain backward compatibility, new fields must be appended to the end
1712 // of the header, and byte offset of existing fields shouldn't change when
1713 // indexed profile version gets incremented.
1714 static_assert(
1716 "Please update the size computation below if a new field has "
1717 "been added to the header; for a version bump without new "
1718 "fields, add a case statement to fall through to the latest version.");
1719 case 12ull:
1720 return 72;
1721 case 11ull:
1722 [[fallthrough]];
1723 case 10ull:
1724 return 64;
1725 case 9ull:
1726 return 56;
1727 case 8ull:
1728 return 48;
1729 default: // Version7 (when the backwards compatible header was introduced).
1730 return 40;
1731 }
1732}
1733
1734} // namespace IndexedInstrProf
1735
1736} // end namespace llvm
aarch64 promote const
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:203
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
static cl::opt< bool > StaticFuncFullModulePrefix("static-func-full-module-prefix", cl::init(true), cl::Hidden, cl::desc("Use full module build paths in the profile counter names for " "static functions."))
static cl::opt< unsigned > StaticFuncStripDirNamePrefix("static-func-strip-dirname-prefix", cl::init(0), cl::Hidden, cl::desc("Strip specified level of directory name from source path in " "the profile counter name for static functions."))
static std::string getInstrProfErrString(instrprof_error Err, const std::string &ErrMsg="")
Definition: InstrProf.cpp:81
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define H(x, y, z)
Definition: MD5.cpp:57
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
#define P(N)
uint64_t Timestamp
Definition: Profile.cpp:320
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:50
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Defines the virtual file system interface vfs::FileSystem.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2900
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:155
This is an important base class in LLVM.
Definition: Constant.h:41
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:235
unsigned size() const
Definition: DenseMap.h:99
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1487
void setComdat(Comdat *C)
Definition: Globals.cpp:206
bool hasComdat() const
Definition: GlobalObject.h:128
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:409
LinkageTypes getLinkage() const
Definition: GlobalValue.h:546
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:537
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
bool isDiscardableIfUnused() const
Definition: GlobalValue.h:548
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:254
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:178
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:54
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:61
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
static char ID
Definition: InstrProf.h:436
std::string message() const override
Return the error message as a string.
Definition: InstrProf.cpp:256
static Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const InstrProfCorrelator *Correlator=nullptr, std::function< void(Error)> Warn=nullptr)
Factory method to create an appropriately typed reader for the given instrprof file.
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
Definition: InstrProf.h:460
uint64_t getFunctionHashFromAddress(uint64_t Address)
Return a function's hash, or 0, if the function isn't in this SymTab.
Definition: InstrProf.cpp:642
Error addSymbolName(StringRef SymbolName)
Definition: InstrProf.h:580
uint64_t getVTableHashFromAddress(uint64_t Address)
Return a vtable's hash, or 0 if the vtable doesn't exist in this SymTab.
Definition: InstrProf.cpp:636
Error addVTableName(StringRef VTableName)
Adds VTableName as a known symbol, and inserts it to a map that tracks all vtable names.
Definition: InstrProf.h:602
void dumpNames(raw_ostream &OS) const
Dump the symbols in this table.
Definition: InstrProf.cpp:656
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
Error addFuncName(StringRef FuncName)
The method name is kept since there are many callers.
Definition: InstrProf.h:598
Error initVTableNamesFromCompressedStrings(StringRef CompressedVTableNames)
Initialize 'this' with the set of vtable names encoded in CompressedVTableNames.
Definition: InstrProf.cpp:586
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:381
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1635
ValT lookup(KeyT x, ValT NotFound=ValT()) const
lookup - Return the mapped value at x or NotFound.
Definition: IntervalMap.h:1119
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
Metadata node.
Definition: Metadata.h:1067
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1434
A single uniqued string.
Definition: Metadata.h:720
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:600
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:278
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
size_t size() const
Definition: SmallVector.h:91
void reserve(size_type N)
Definition: SmallVector.h:676
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
iterator_range< StringMapKeyIterator< ValueTy > > keys() const
Definition: StringMap.h:228
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:693
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:223
const unsigned char * bytes_end() const
Definition: StringRef.h:118
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:564
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:258
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:602
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:290
static constexpr size_t npos
Definition: StringRef.h:52
const unsigned char * bytes_begin() const
Definition: StringRef.h:115
unsigned size() const
Definition: Trace.h:95
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool supportsCOMDAT() const
Tests whether the target supports comdat.
Definition: Triple.h:1048
ObjectFormatType
Definition: Triple.h:297
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
See the file comment.
Definition: ValueMap.h:84
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:471
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint64_t Magic
Definition: InstrProf.h:1079
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
constexpr int BestSizeCompression
Definition: Compression.h:39
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition: Path.cpp:602
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef getInstrProfNameVarPrefix()
Return the name prefix of variables containing instrumented function names.
Definition: InstrProf.h:92
std::string getPGOFuncName(const Function &F, bool InLTO=false, uint64_t Version=INSTR_PROF_INDEX_VERSION)
Please use getIRPGOFuncName for LLVM IR instrumentation.
Definition: InstrProf.cpp:380
void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName)
Create the PGOFuncName meta data if PGOFuncName is different from function's raw name.
Definition: InstrProf.cpp:1449
std::string getIRPGOFuncName(const Function &F, bool InLTO=false)
Definition: InstrProf.cpp:369
StringRef getPGOFuncNameMetadataName()
Definition: InstrProf.h:306
void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, uint32_t K, uint32_t S)
Definition: InstrProf.cpp:1100
cl::opt< bool > DoInstrProfNameCompression
StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName="<unknown>")
Given a PGO function name, remove the filename prefix and return the original (static) function name.
Definition: InstrProf.cpp:411
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:2008
std::unique_ptr< InstrProfValueData[]> getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, uint32_t &ActualNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst and returns them if Inst is annotated with value profile dat...
Definition: InstrProf.cpp:1377
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:131
void createPGONameMetadata(GlobalObject &GO, StringRef PGOName)
Create the PGOName metadata if a global object's PGO name is different from its mangled name.
Definition: InstrProf.cpp:1453
std::pair< StringRef, StringRef > getParsedIRPGOName(StringRef IRPGOName)
Definition: InstrProf.cpp:404
MDNode * getPGOFuncNameMetadata(const Function &F)
Return the PGOFuncName meta data associated with a function.
Definition: InstrProf.cpp:1429
static std::unique_ptr< ValueProfData > allocValueProfData(uint32_t TotalSize)
Definition: InstrProf.cpp:1191
MDNode * mayHaveValueProfileOfKind(const Instruction &Inst, InstrProfValueKind ValueKind)
Definition: InstrProf.cpp:1322
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:237
cl::opt< bool > EnableVTableProfileUse("enable-vtable-profile-use", cl::init(false), cl::desc("If ThinLTO and WPD is enabled and this option is true, vtable " "profiles will be used by ICP pass for more efficient indirect " "call sequence. If false, type profiles won't be used."))
uint64_t getInstrMaxCountValue()
Return the max count value. We reserver a few large values for special use.
Definition: InstrProf.h:66
bool needsComdatForCounter(const GlobalObject &GV, const Module &M)
Check if we can use Comdat for profile variables.
Definition: InstrProf.cpp:1457
constexpr char kGlobalIdentifierDelimiter
Definition: GlobalValue.h:46
std::string getPGOName(const GlobalVariable &V, bool InLTO=false)
Definition: InstrProf.cpp:396
GlobalVariable * createPGOFuncNameVar(Function &F, StringRef PGOFuncName)
Create and return the global variable for function name used in PGO instrumentation.
Definition: InstrProf.cpp:468
void annotateValueSite(Module &M, Instruction &Inst, const InstrProfRecord &InstrProfR, InstrProfValueKind ValueKind, uint32_t SiteIndx, uint32_t MaxMDCount=3)
Get the value profile data for value site SiteIdx from InstrProfR and annotate the instruction Inst w...
Definition: InstrProf.cpp:1278
Error collectPGOFuncNameStrings(ArrayRef< GlobalVariable * > NameVars, std::string &Result, bool doCompression=true)
Produce Result string with the same format described above.
Definition: InstrProf.cpp:708
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
InstrProfSectKind
Definition: InstrProf.h:60
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar)
Return the initializer in string of the PGO name var NameVar.
Definition: InstrProf.cpp:701
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
Definition: MathExtras.h:667
StringRef getInstrProfNameSeparator()
Return the marker used to separate PGO names during serialization.
Definition: InstrProf.h:178
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
static std::string getIRPGOObjectName(const GlobalObject &GO, bool InLTO, MDNode *PGONameMetadata)
Definition: InstrProf.cpp:350
@ Other
Any other memory.
instrprof_error
Definition: InstrProf.h:354
InstrProfValueKind
Definition: InstrProf.h:267
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:621
const std::error_category & instrprof_category()
Definition: InstrProf.cpp:192
Error collectVTableStrings(ArrayRef< GlobalVariable * > VTables, std::string &Result, bool doCompression)
Definition: InstrProf.cpp:718
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1914
static StringRef getStrippedSourceFileName(const GlobalObject &GO)
Definition: InstrProf.cpp:303
uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind)
Definition: InstrProf.cpp:1084
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken=false)
Check if we can safely rename this Comdat function.
Definition: InstrProf.cpp:1505
void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput)
Definition: InstrProf.cpp:1528
Error collectGlobalObjectNameStrings(ArrayRef< std::string > NameStrs, bool doCompression, std::string &Result)
Given a vector of strings (names of global objects like functions or, virtual tables) NameStrs,...
Definition: InstrProf.cpp:663
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:2051
static bool getValueProfDataFromInstImpl(const MDNode *const MD, const uint32_t MaxNumDataWant, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC, bool GetNoICPValue)
Definition: InstrProf.cpp:1345
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, uint32_t S)
Definition: InstrProf.cpp:1094
static ValueProfRecordClosure InstrProfRecordClosure
Definition: InstrProf.cpp:1113
static Error readAndDecodeStrings(StringRef NameStrings, std::function< Error(StringRef)> NameCallback)
NameStrings is a string composed of one of more possibly encoded sub-strings.
Definition: InstrProf.cpp:525
std::string getPGOFuncNameVarName(StringRef FuncName, GlobalValue::LinkageTypes Linkage)
Return the name of the global variable used to store a function name in PGO instrumentation.
Definition: InstrProf.cpp:423
static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix)
Definition: InstrProf.cpp:288
endianness
Definition: bit.h:70
static std::optional< std::string > lookupPGONameFromMetadata(MDNode *MD)
Definition: InstrProf.cpp:329
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:592
bool isIRPGOFlagSet(const Module *M)
Check if INSTR_PROF_RAW_VERSION_VAR is defined.
Definition: InstrProf.cpp:1483
StringRef getPGONameMetadataName()
Definition: InstrProf.h:308
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
const uint64_t NOMORE_ICP_MAGICNUM
Magic number in the value profile metadata showing a target has been promoted for the instruction and...
Definition: Metadata.h:57
uint32_t getNumValueKindsInstrProf(const void *Record)
ValueProfRecordClosure Interface implementation for InstrProfRecord class.
Definition: InstrProf.cpp:1080
ValueProfData * allocValueProfDataInstrProf(size_t TotalSizeInBytes)
Definition: InstrProf.cpp:1106
uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind)
Definition: InstrProf.cpp:1089
static std::string getIRPGONameForGlobalObject(const GlobalObject &GO, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Definition: InstrProf.cpp:323
cl::opt< bool > EnableVTableValueProfiling("enable-vtable-value-profiling", cl::init(false), cl::desc("If true, the virtual table address will be instrumented to know " "the types of a C++ pointer. The information is used in indirect " "call promotion to do selective vtable-based comparison."))
#define N
std::array< double, IPVK_Last - IPVK_First+1 > ValueCounts
Definition: InstrProf.h:746
uint64_t getIndexedProfileVersion() const
Definition: InstrProf.cpp:1705
static Expected< Header > readFromBuffer(const unsigned char *Buffer)
Definition: InstrProf.cpp:1665
Profiling information for a single function.
Definition: InstrProf.h:834
void overlapValueProfData(uint32_t ValueKind, InstrProfRecord &Src, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap)
Compute the overlap of value profile counts.
Definition: InstrProf.cpp:777
std::vector< uint64_t > Counts
Definition: InstrProf.h:835
ArrayRef< InstrProfValueData > getValueArrayForSite(uint32_t ValueKind, uint32_t Site) const
Return the array of profiled values at Site.
Definition: InstrProf.h:1039
CountPseudoKind getCountPseudoKind() const
Definition: InstrProf.h:932
void accumulateCounts(CountSumOrPercent &Sum) const
Compute the sums of all counts and store in Sum.
Definition: InstrProf.cpp:728
uint32_t getNumValueSites(uint32_t ValueKind) const
Return the number of instrumented sites for ValueKind.
Definition: InstrProf.h:1034
void setPseudoCount(CountPseudoKind Kind)
Definition: InstrProf.h:940
void merge(InstrProfRecord &Other, uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Merge the counts in Other into this one.
Definition: InstrProf.cpp:906
void addValueData(uint32_t ValueKind, uint32_t Site, InstrProfValueData *VData, uint32_t N, InstrProfSymtab *SymTab)
Add ValueData for ValueKind at value Site.
Definition: InstrProf.cpp:1001
void overlap(InstrProfRecord &Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff)
Compute the overlap b/w this IntrprofRecord and Other.
Definition: InstrProf.cpp:795
std::vector< uint8_t > BitmapBytes
Definition: InstrProf.h:836
void scale(uint64_t N, uint64_t D, function_ref< void(instrprof_error)> Warn)
Scale up profile counts (including value profile data) by a factor of (N / D).
Definition: InstrProf.cpp:969
void sortByTargetValues()
Sort ValueData ascending by Value.
Definition: InstrProf.h:812
std::vector< InstrProfValueData > ValueData
Value profiling data pairs at a given value site.
Definition: InstrProf.h:804
void merge(InstrProfValueSiteRecord &Input, uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Merge data from another InstrProfValueSiteRecord Optionally scale merged counts by Weight.
Definition: InstrProf.cpp:846
void overlap(InstrProfValueSiteRecord &Input, uint32_t ValueKind, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap)
Compute the overlap b/w this record and Input record.
Definition: InstrProf.cpp:746
void scale(uint64_t N, uint64_t D, function_ref< void(instrprof_error)> Warn)
Scale up value profile data counts by N (Numerator) / D (Denominator).
Definition: InstrProf.cpp:875
void addOneMismatch(const CountSumOrPercent &MismatchFunc)
Definition: InstrProf.cpp:1573
static double score(uint64_t Val1, uint64_t Val2, double Sum1, double Sum2)
Definition: InstrProf.h:787
Error accumulateCounts(const std::string &BaseFilename, const std::string &TestFilename, bool IsCS)
Definition: InstrProf.cpp:1545
void dump(raw_fd_ostream &OS) const
Definition: InstrProf.cpp:1592
CountSumOrPercent Overlap
Definition: InstrProf.h:763
CountSumOrPercent Base
Definition: InstrProf.h:759
uint64_t FuncHash
Definition: InstrProf.h:770
void addOneUnique(const CountSumOrPercent &UniqueFunc)
Definition: InstrProf.cpp:1583
const std::string * BaseFilename
Definition: InstrProf.h:767
const std::string * TestFilename
Definition: InstrProf.h:768
CountSumOrPercent Unique
Definition: InstrProf.h:765
CountSumOrPercent Mismatch
Definition: InstrProf.h:764
StringRef FuncName
Definition: InstrProf.h:769
CountSumOrPercent Test
Definition: InstrProf.h:761
static void createBPFunctionNodes(ArrayRef< TemporalProfTraceTy > Traces, std::vector< BPFunctionNode > &Nodes, bool RemoveOutlierUNs=true)
Use a set of temporal profile traces to create a list of balanced partitioning function nodes used by...
Definition: InstrProf.cpp:1016