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