LLVM  15.0.0git
SampleProf.h
Go to the documentation of this file.
1 //===- SampleProf.h - Sampling profiling format support ---------*- C++ -*-===//
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 common definitions used in the reading and writing of
10 // sample profile data.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H
15 #define LLVM_PROFILEDATA_SAMPLEPROF_H
16 
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorOr.h"
27 #include <algorithm>
28 #include <cstdint>
29 #include <list>
30 #include <map>
31 #include <set>
32 #include <sstream>
33 #include <string>
34 #include <system_error>
35 #include <unordered_map>
36 #include <utility>
37 
38 namespace llvm {
39 
40 class DILocation;
41 class raw_ostream;
42 
44 
45 enum class sampleprof_error {
46  success = 0,
47  bad_magic,
49  too_large,
50  truncated,
51  malformed,
61 };
62 
63 inline std::error_code make_error_code(sampleprof_error E) {
64  return std::error_code(static_cast<int>(E), sampleprof_category());
65 }
66 
68  sampleprof_error Result) {
69  // Prefer first error encountered as later errors may be secondary effects of
70  // the initial problem.
72  Result != sampleprof_error::success)
73  Accumulator = Result;
74  return Accumulator;
75 }
76 
77 } // end namespace llvm
78 
79 namespace std {
80 
81 template <>
82 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
83 
84 } // end namespace std
85 
86 namespace llvm {
87 namespace sampleprof {
88 
90  SPF_None = 0,
91  SPF_Text = 0x1,
93  SPF_GCC = 0x3,
95  SPF_Binary = 0xff
96 };
97 
99  return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
100  uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
101  uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
102  uint64_t('2') << (64 - 56) | uint64_t(Format);
103 }
104 
105 /// Get the proper representation of a string according to whether the
106 /// current Format uses MD5 to represent the string.
107 static inline StringRef getRepInFormat(StringRef Name, bool UseMD5,
108  std::string &GUIDBuf) {
109  if (Name.empty() || !UseMD5)
110  return Name;
112  return GUIDBuf;
113 }
114 
115 static inline uint64_t SPVersion() { return 103; }
116 
117 // Section Type used by SampleProfileExtBinaryBaseReader and
118 // SampleProfileExtBinaryBaseWriter. Never change the existing
119 // value of enum. Only append new ones.
120 enum SecType {
128  // marker for the first type of profile.
131 };
132 
133 static inline std::string getSecName(SecType Type) {
134  switch ((int)Type) { // Avoid -Wcovered-switch-default
135  case SecInValid:
136  return "InvalidSection";
137  case SecProfSummary:
138  return "ProfileSummarySection";
139  case SecNameTable:
140  return "NameTableSection";
142  return "ProfileSymbolListSection";
143  case SecFuncOffsetTable:
144  return "FuncOffsetTableSection";
145  case SecFuncMetadata:
146  return "FunctionMetadata";
147  case SecCSNameTable:
148  return "CSNameTableSection";
149  case SecLBRProfile:
150  return "LBRProfileSection";
151  default:
152  return "UnknownSection";
153  }
154 }
155 
156 // Entry type of section header table used by SampleProfileExtBinaryBaseReader
157 // and SampleProfileExtBinaryBaseWriter.
163  // The index indicating the location of the current entry in
164  // SectionHdrLayout table.
166 };
167 
168 // Flags common for all sections are defined here. In SecHdrTableEntry::Flags,
169 // common flags will be saved in the lower 32bits and section specific flags
170 // will be saved in the higher 32 bits.
171 enum class SecCommonFlags : uint32_t {
172  SecFlagInValid = 0,
173  SecFlagCompress = (1 << 0),
174  // Indicate the section contains only profile without context.
175  SecFlagFlat = (1 << 1)
176 };
177 
178 // Section specific flags are defined here.
179 // !!!Note: Everytime a new enum class is created here, please add
180 // a new check in verifySecFlag.
182  SecFlagInValid = 0,
183  SecFlagMD5Name = (1 << 0),
184  // Store MD5 in fixed length instead of ULEB128 so NameTable can be
185  // accessed like an array.
186  SecFlagFixedLengthMD5 = (1 << 1),
187  // Profile contains ".__uniq." suffix name. Compiler shouldn't strip
188  // the suffix when doing profile matching when seeing the flag.
189  SecFlagUniqSuffix = (1 << 2)
190 };
192  SecFlagInValid = 0,
193  /// SecFlagPartial means the profile is for common/shared code.
194  /// The common profile is usually merged from profiles collected
195  /// from running other targets.
196  SecFlagPartial = (1 << 0),
197  /// SecFlagContext means this is context-sensitive flat profile for
198  /// CSSPGO
199  SecFlagFullContext = (1 << 1),
200  /// SecFlagFSDiscriminator means this profile uses flow-sensitive
201  /// discriminators.
202  SecFlagFSDiscriminator = (1 << 2),
203  /// SecFlagIsPreInlined means this profile contains ShouldBeInlined
204  /// contexts thus this is CS preinliner computed.
205  SecFlagIsPreInlined = (1 << 4),
206 };
207 
209  SecFlagInvalid = 0,
210  SecFlagIsProbeBased = (1 << 0),
211  SecFlagHasAttribute = (1 << 1),
212 };
213 
215  SecFlagInvalid = 0,
216  // Store function offsets in an order of contexts. The order ensures that
217  // callee contexts of a given context laid out next to it.
218  SecFlagOrdered = (1 << 0),
219 };
220 
221 // Verify section specific flag is used for the correct section.
222 template <class SecFlagType>
223 static inline void verifySecFlag(SecType Type, SecFlagType Flag) {
224  // No verification is needed for common flags.
225  if (std::is_same<SecCommonFlags, SecFlagType>())
226  return;
227 
228  // Verification starts here for section specific flag.
229  bool IsFlagLegal = false;
230  switch (Type) {
231  case SecNameTable:
232  IsFlagLegal = std::is_same<SecNameTableFlags, SecFlagType>();
233  break;
234  case SecProfSummary:
235  IsFlagLegal = std::is_same<SecProfSummaryFlags, SecFlagType>();
236  break;
237  case SecFuncMetadata:
238  IsFlagLegal = std::is_same<SecFuncMetadataFlags, SecFlagType>();
239  break;
240  default:
241  case SecFuncOffsetTable:
242  IsFlagLegal = std::is_same<SecFuncOffsetFlags, SecFlagType>();
243  break;
244  }
245  if (!IsFlagLegal)
246  llvm_unreachable("Misuse of a flag in an incompatible section");
247 }
248 
249 template <class SecFlagType>
250 static inline void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) {
251  verifySecFlag(Entry.Type, Flag);
252  auto FVal = static_cast<uint64_t>(Flag);
253  bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
254  Entry.Flags |= IsCommon ? FVal : (FVal << 32);
255 }
256 
257 template <class SecFlagType>
258 static inline void removeSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) {
259  verifySecFlag(Entry.Type, Flag);
260  auto FVal = static_cast<uint64_t>(Flag);
261  bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
262  Entry.Flags &= ~(IsCommon ? FVal : (FVal << 32));
263 }
264 
265 template <class SecFlagType>
266 static inline bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag) {
267  verifySecFlag(Entry.Type, Flag);
268  auto FVal = static_cast<uint64_t>(Flag);
269  bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
270  return Entry.Flags & (IsCommon ? FVal : (FVal << 32));
271 }
272 
273 /// Represents the relative location of an instruction.
274 ///
275 /// Instruction locations are specified by the line offset from the
276 /// beginning of the function (marked by the line where the function
277 /// header is) and the discriminator value within that line.
278 ///
279 /// The discriminator value is useful to distinguish instructions
280 /// that are on the same line but belong to different basic blocks
281 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
282 struct LineLocation {
284 
285  void print(raw_ostream &OS) const;
286  void dump() const;
287 
288  bool operator<(const LineLocation &O) const {
289  return LineOffset < O.LineOffset ||
290  (LineOffset == O.LineOffset && Discriminator < O.Discriminator);
291  }
292 
293  bool operator==(const LineLocation &O) const {
294  return LineOffset == O.LineOffset && Discriminator == O.Discriminator;
295  }
296 
297  bool operator!=(const LineLocation &O) const {
298  return LineOffset != O.LineOffset || Discriminator != O.Discriminator;
299  }
300 
303 };
304 
306 
307 /// Representation of a single sample record.
308 ///
309 /// A sample record is represented by a positive integer value, which
310 /// indicates how frequently was the associated line location executed.
311 ///
312 /// Additionally, if the associated location contains a function call,
313 /// the record will hold a list of all the possible called targets. For
314 /// direct calls, this will be the exact function being invoked. For
315 /// indirect calls (function pointers, virtual table dispatch), this
316 /// will be a list of one or more functions.
318 public:
319  using CallTarget = std::pair<StringRef, uint64_t>;
321  bool operator()(const CallTarget &LHS, const CallTarget &RHS) const {
322  if (LHS.second != RHS.second)
323  return LHS.second > RHS.second;
324 
325  return LHS.first < RHS.first;
326  }
327  };
328 
329  using SortedCallTargetSet = std::set<CallTarget, CallTargetComparator>;
331  SampleRecord() = default;
332 
333  /// Increment the number of samples for this record by \p S.
334  /// Optionally scale sample count \p S by \p Weight.
335  ///
336  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
337  /// around unsigned integers.
339  bool Overflowed;
340  NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed);
341  return Overflowed ? sampleprof_error::counter_overflow
343  }
344 
345  /// Decrease the number of samples for this record by \p S. Return the amout
346  /// of samples actually decreased.
348  if (S > NumSamples)
349  S = NumSamples;
350  NumSamples -= S;
351  return S;
352  }
353 
354  /// Add called function \p F with samples \p S.
355  /// Optionally scale sample count \p S by \p Weight.
356  ///
357  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
358  /// around unsigned integers.
360  uint64_t Weight = 1) {
361  uint64_t &TargetSamples = CallTargets[F];
362  bool Overflowed;
363  TargetSamples =
364  SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed);
365  return Overflowed ? sampleprof_error::counter_overflow
367  }
368 
369  /// Remove called function from the call target map. Return the target sample
370  /// count of the called function.
372  uint64_t Count = 0;
373  auto I = CallTargets.find(F);
374  if (I != CallTargets.end()) {
375  Count = I->second;
376  CallTargets.erase(I);
377  }
378  return Count;
379  }
380 
381  /// Return true if this sample record contains function calls.
382  bool hasCalls() const { return !CallTargets.empty(); }
383 
384  uint64_t getSamples() const { return NumSamples; }
385  const CallTargetMap &getCallTargets() const { return CallTargets; }
387  return SortCallTargets(CallTargets);
388  }
389 
391  uint64_t Sum = 0;
392  for (const auto &I : CallTargets)
393  Sum += I.second;
394  return Sum;
395  }
396 
397  /// Sort call targets in descending order of call frequency.
398  static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets) {
399  SortedCallTargetSet SortedTargets;
400  for (const auto &I : Targets) {
401  SortedTargets.emplace(I.first(), I.second);
402  }
403  return SortedTargets;
404  }
405 
406  /// Prorate call targets by a distribution factor.
407  static const CallTargetMap adjustCallTargets(const CallTargetMap &Targets,
408  float DistributionFactor) {
409  CallTargetMap AdjustedTargets;
410  for (const auto &I : Targets) {
411  AdjustedTargets[I.first()] = I.second * DistributionFactor;
412  }
413  return AdjustedTargets;
414  }
415 
416  /// Merge the samples in \p Other into this record.
417  /// Optionally scale sample counts by \p Weight.
418  sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1);
419  void print(raw_ostream &OS, unsigned Indent) const;
420  void dump() const;
421 
422 private:
423  uint64_t NumSamples = 0;
424  CallTargetMap CallTargets;
425 };
426 
427 raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
428 
429 // State of context associated with FunctionSamples
431  UnknownContext = 0x0, // Profile without context
432  RawContext = 0x1, // Full context profile from input profile
433  SyntheticContext = 0x2, // Synthetic context created for context promotion
434  InlinedContext = 0x4, // Profile for context that is inlined into caller
435  MergedContext = 0x8 // Profile for context merged into base profile
436 };
437 
438 // Attribute of context associated with FunctionSamples
440  ContextNone = 0x0,
441  ContextWasInlined = 0x1, // Leaf of context was inlined in previous build
442  ContextShouldBeInlined = 0x2, // Leaf of context should be inlined
444  0x4, // Leaf of context is duplicated into the base profile
445 };
446 
447 // Represents a context frame with function name and line location
451 
453 
456 
457  bool operator==(const SampleContextFrame &That) const {
458  return Location == That.Location && FuncName == That.FuncName;
459  }
460 
461  bool operator!=(const SampleContextFrame &That) const {
462  return !(*this == That);
463  }
464 
465  std::string toString(bool OutputLineLocation) const {
466  std::ostringstream OContextStr;
467  OContextStr << FuncName.str();
468  if (OutputLineLocation) {
469  OContextStr << ":" << Location.LineOffset;
471  OContextStr << "." << Location.Discriminator;
472  }
473  return OContextStr.str();
474  }
475 };
476 
477 static inline hash_code hash_value(const SampleContextFrame &arg) {
478  return hash_combine(arg.FuncName, arg.Location.LineOffset,
479  arg.Location.Discriminator);
480 }
481 
484 
487  return hash_combine_range(S.begin(), S.end());
488  }
489 };
490 
491 // Sample context for FunctionSamples. It consists of the calling context,
492 // the function name and context state. Internally sample context is represented
493 // using ArrayRef, which is also the input for constructing a `SampleContext`.
494 // It can accept and represent both full context string as well as context-less
495 // function name.
496 // For a CS profile, a full context vector can look like:
497 // `main:3 _Z5funcAi:1 _Z8funcLeafi`
498 // For a base CS profile without calling context, the context vector should only
499 // contain the leaf frame name.
500 // For a non-CS profile, the context vector should be empty.
502 public:
503  SampleContext() : State(UnknownContext), Attributes(ContextNone) {}
504 
506  : Name(Name), State(UnknownContext), Attributes(ContextNone) {}
507 
509  ContextStateMask CState = RawContext)
510  : Attributes(ContextNone) {
511  assert(!Context.empty() && "Context is empty");
512  setContext(Context, CState);
513  }
514 
515  // Give a context string, decode and populate internal states like
516  // Function name, Calling context and context state. Example of input
517  // `ContextStr`: `[main:3 @ _Z5funcAi:1 @ _Z8funcLeafi]`
519  std::list<SampleContextFrameVector> &CSNameTable,
520  ContextStateMask CState = RawContext)
521  : Attributes(ContextNone) {
522  assert(!ContextStr.empty());
523  // Note that `[]` wrapped input indicates a full context string, otherwise
524  // it's treated as context-less function name only.
525  bool HasContext = ContextStr.startswith("[");
526  if (!HasContext) {
527  State = UnknownContext;
528  Name = ContextStr;
529  } else {
530  CSNameTable.emplace_back();
531  SampleContextFrameVector &Context = CSNameTable.back();
532  createCtxVectorFromStr(ContextStr, Context);
533  setContext(Context, CState);
534  }
535  }
536 
537  /// Create a context vector from a given context string and save it in
538  /// `Context`.
539  static void createCtxVectorFromStr(StringRef ContextStr,
541  // Remove encapsulating '[' and ']' if any
542  ContextStr = ContextStr.substr(1, ContextStr.size() - 2);
543  StringRef ContextRemain = ContextStr;
544  StringRef ChildContext;
545  StringRef CalleeName;
546  while (!ContextRemain.empty()) {
547  auto ContextSplit = ContextRemain.split(" @ ");
548  ChildContext = ContextSplit.first;
549  ContextRemain = ContextSplit.second;
550  LineLocation CallSiteLoc(0, 0);
551  decodeContextString(ChildContext, CalleeName, CallSiteLoc);
552  Context.emplace_back(CalleeName, CallSiteLoc);
553  }
554  }
555 
556  // Decode context string for a frame to get function name and location.
557  // `ContextStr` is in the form of `FuncName:StartLine.Discriminator`.
558  static void decodeContextString(StringRef ContextStr, StringRef &FName,
559  LineLocation &LineLoc) {
560  // Get function name
561  auto EntrySplit = ContextStr.split(':');
562  FName = EntrySplit.first;
563 
564  LineLoc = {0, 0};
565  if (!EntrySplit.second.empty()) {
566  // Get line offset, use signed int for getAsInteger so string will
567  // be parsed as signed.
568  int LineOffset = 0;
569  auto LocSplit = EntrySplit.second.split('.');
570  LocSplit.first.getAsInteger(10, LineOffset);
571  LineLoc.LineOffset = LineOffset;
572 
573  // Get discriminator
574  if (!LocSplit.second.empty())
575  LocSplit.second.getAsInteger(10, LineLoc.Discriminator);
576  }
577  }
578 
579  operator SampleContextFrames() const { return FullContext; }
580  bool hasAttribute(ContextAttributeMask A) { return Attributes & (uint32_t)A; }
581  void setAttribute(ContextAttributeMask A) { Attributes |= (uint32_t)A; }
582  uint32_t getAllAttributes() { return Attributes; }
583  void setAllAttributes(uint32_t A) { Attributes = A; }
584  bool hasState(ContextStateMask S) { return State & (uint32_t)S; }
585  void setState(ContextStateMask S) { State |= (uint32_t)S; }
586  void clearState(ContextStateMask S) { State &= (uint32_t)~S; }
587  bool hasContext() const { return State != UnknownContext; }
588  bool isBaseContext() const { return FullContext.size() == 1; }
589  StringRef getName() const { return Name; }
590  SampleContextFrames getContextFrames() const { return FullContext; }
591 
593  bool IncludeLeafLineLocation = false) {
594  std::ostringstream OContextStr;
595  for (uint32_t I = 0; I < Context.size(); I++) {
596  if (OContextStr.str().size()) {
597  OContextStr << " @ ";
598  }
599  OContextStr << Context[I].toString(I != Context.size() - 1 ||
600  IncludeLeafLineLocation);
601  }
602  return OContextStr.str();
603  }
604 
605  std::string toString() const {
606  if (!hasContext())
607  return Name.str();
608  return getContextString(FullContext, false);
609  }
610 
613  : hash_value(getName());
614  }
615 
616  /// Set the name of the function and clear the current context.
617  void setName(StringRef FunctionName) {
618  Name = FunctionName;
619  FullContext = SampleContextFrames();
620  State = UnknownContext;
621  }
622 
624  ContextStateMask CState = RawContext) {
625  assert(CState != UnknownContext);
626  FullContext = Context;
627  Name = Context.back().FuncName;
628  State = CState;
629  }
630 
631  bool operator==(const SampleContext &That) const {
632  return State == That.State && Name == That.Name &&
633  FullContext == That.FullContext;
634  }
635 
636  bool operator!=(const SampleContext &That) const { return !(*this == That); }
637 
638  bool operator<(const SampleContext &That) const {
639  if (State != That.State)
640  return State < That.State;
641 
642  if (!hasContext()) {
643  return (Name.compare(That.Name)) == -1;
644  }
645 
646  uint64_t I = 0;
647  while (I < std::min(FullContext.size(), That.FullContext.size())) {
648  auto &Context1 = FullContext[I];
649  auto &Context2 = That.FullContext[I];
650  auto V = Context1.FuncName.compare(Context2.FuncName);
651  if (V)
652  return V == -1;
653  if (Context1.Location != Context2.Location)
654  return Context1.Location < Context2.Location;
655  I++;
656  }
657 
658  return FullContext.size() < That.FullContext.size();
659  }
660 
661  struct Hash {
663  return Context.getHashCode();
664  }
665  };
666 
667  bool IsPrefixOf(const SampleContext &That) const {
668  auto ThisContext = FullContext;
669  auto ThatContext = That.FullContext;
670  if (ThatContext.size() < ThisContext.size())
671  return false;
672  ThatContext = ThatContext.take_front(ThisContext.size());
673  // Compare Leaf frame first
674  if (ThisContext.back().FuncName != ThatContext.back().FuncName)
675  return false;
676  // Compare leading context
677  return ThisContext.drop_back() == ThatContext.drop_back();
678  }
679 
680 private:
681  /// Mangled name of the function.
682  StringRef Name;
683  // Full context including calling context and leaf function name
684  SampleContextFrames FullContext;
685  // State of the associated sample profile
686  uint32_t State;
687  // Attribute of the associated sample profile
688  uint32_t Attributes;
689 };
690 
691 static inline hash_code hash_value(const SampleContext &arg) {
692  return arg.hasContext() ? hash_value(arg.getContextFrames())
693  : hash_value(arg.getName());
694 }
695 
696 class FunctionSamples;
698 
699 using BodySampleMap = std::map<LineLocation, SampleRecord>;
700 // NOTE: Using a StringMap here makes parsed profiles consume around 17% more
701 // memory, which is *very* significant for large profiles.
702 using FunctionSamplesMap = std::map<std::string, FunctionSamples, std::less<>>;
703 using CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>;
704 
705 /// Representation of the samples collected for a function.
706 ///
707 /// This data structure contains all the collected samples for the body
708 /// of a function. Each sample corresponds to a LineLocation instance
709 /// within the body of the function.
711 public:
712  FunctionSamples() = default;
713 
714  void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
715  void dump() const;
716 
718  bool Overflowed;
719  TotalSamples =
720  SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed);
721  return Overflowed ? sampleprof_error::counter_overflow
723  }
724 
726  if (TotalSamples < Num)
727  TotalSamples = 0;
728  else
729  TotalSamples -= Num;
730  }
731 
732  void setTotalSamples(uint64_t Num) { TotalSamples = Num; }
733 
735  bool Overflowed;
736  TotalHeadSamples =
737  SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed);
738  return Overflowed ? sampleprof_error::counter_overflow
740  }
741 
743  uint64_t Num, uint64_t Weight = 1) {
744  return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(
745  Num, Weight);
746  }
747 
749  uint32_t Discriminator,
750  StringRef FName, uint64_t Num,
751  uint64_t Weight = 1) {
752  return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(
753  FName, Num, Weight);
754  }
755 
756  // Remove a call target and decrease the body sample correspondingly. Return
757  // the number of body samples actually decreased.
759  uint32_t Discriminator,
760  StringRef FName) {
761  uint64_t Count = 0;
762  auto I = BodySamples.find(LineLocation(LineOffset, Discriminator));
763  if (I != BodySamples.end()) {
764  Count = I->second.removeCalledTarget(FName);
765  Count = I->second.removeSamples(Count);
766  if (!I->second.getSamples())
767  BodySamples.erase(I);
768  }
769  return Count;
770  }
771 
773  uint64_t Weight = 1) {
774  SampleRecord S;
775  S.addSamples(Num, Weight);
776  return BodySamples[LineLocation(Index, 0)].merge(S, Weight);
777  }
778 
779  // Accumulate all call target samples to update the body samples.
781  for (auto &I : BodySamples) {
782  uint64_t TargetSamples = I.second.getCallTargetSum();
783  // It's possible that the body sample count can be greater than the call
784  // target sum. E.g, if some call targets are external targets, they won't
785  // be considered valid call targets, but the body sample count which is
786  // from lbr ranges can actually include them.
787  if (TargetSamples > I.second.getSamples())
788  I.second.addSamples(TargetSamples - I.second.getSamples());
789  }
790  }
791 
792  // Accumulate all body samples to set total samples.
794  setTotalSamples(0);
795  for (const auto &I : BodySamples)
796  addTotalSamples(I.second.getSamples());
797 
798  for (auto &I : CallsiteSamples) {
799  for (auto &CS : I.second) {
800  CS.second.updateTotalSamples();
801  addTotalSamples(CS.second.getTotalSamples());
802  }
803  }
804  }
805 
806  // Set current context and all callee contexts to be synthetic.
808  Context.setState(SyntheticContext);
809  for (auto &I : CallsiteSamples) {
810  for (auto &CS : I.second) {
811  CS.second.SetContextSynthetic();
812  }
813  }
814  }
815 
816  /// Return the number of samples collected at the given location.
817  /// Each location is specified by \p LineOffset and \p Discriminator.
818  /// If the location is not found in profile, return error.
820  uint32_t Discriminator) const {
821  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
822  if (ret == BodySamples.end())
823  return std::error_code();
824  return ret->second.getSamples();
825  }
826 
827  /// Returns the call target map collected at a given location.
828  /// Each location is specified by \p LineOffset and \p Discriminator.
829  /// If the location is not found in profile, return error.
831  findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const {
832  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
833  if (ret == BodySamples.end())
834  return std::error_code();
835  return ret->second.getCallTargets();
836  }
837 
838  /// Returns the call target map collected at a given location specified by \p
839  /// CallSite. If the location is not found in profile, return error.
841  findCallTargetMapAt(const LineLocation &CallSite) const {
842  const auto &Ret = BodySamples.find(CallSite);
843  if (Ret == BodySamples.end())
844  return std::error_code();
845  return Ret->second.getCallTargets();
846  }
847 
848  /// Return the function samples at the given callsite location.
850  return CallsiteSamples[Loc];
851  }
852 
853  /// Returns the FunctionSamplesMap at the given \p Loc.
854  const FunctionSamplesMap *
856  auto iter = CallsiteSamples.find(Loc);
857  if (iter == CallsiteSamples.end())
858  return nullptr;
859  return &iter->second;
860  }
861 
862  /// Returns a pointer to FunctionSamples at the given callsite location
863  /// \p Loc with callee \p CalleeName. If no callsite can be found, relax
864  /// the restriction to return the FunctionSamples at callsite location
865  /// \p Loc with the maximum total sample count. If \p Remapper is not
866  /// nullptr, use \p Remapper to find FunctionSamples with equivalent name
867  /// as \p CalleeName.
868  const FunctionSamples *
869  findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName,
870  SampleProfileReaderItaniumRemapper *Remapper) const;
871 
872  bool empty() const { return TotalSamples == 0; }
873 
874  /// Return the total number of samples collected inside the function.
875  uint64_t getTotalSamples() const { return TotalSamples; }
876 
877  /// Return the total number of branch samples that have the function as the
878  /// branch target. This should be equivalent to the sample of the first
879  /// instruction of the symbol. But as we directly get this info for raw
880  /// profile without referring to potentially inaccurate debug info, this
881  /// gives more accurate profile data and is preferred for standalone symbols.
882  uint64_t getHeadSamples() const { return TotalHeadSamples; }
883 
884  /// Return the sample count of the first instruction of the function.
885  /// The function can be either a standalone symbol or an inlined function.
888  // For CS profile, if we already have more accurate head samples
889  // counted by branch sample from caller, use them as entry samples.
890  return getHeadSamples();
891  }
892  uint64_t Count = 0;
893  // Use either BodySamples or CallsiteSamples which ever has the smaller
894  // lineno.
895  if (!BodySamples.empty() &&
896  (CallsiteSamples.empty() ||
897  BodySamples.begin()->first < CallsiteSamples.begin()->first))
898  Count = BodySamples.begin()->second.getSamples();
899  else if (!CallsiteSamples.empty()) {
900  // An indirect callsite may be promoted to several inlined direct calls.
901  // We need to get the sum of them.
902  for (const auto &N_FS : CallsiteSamples.begin()->second)
903  Count += N_FS.second.getEntrySamples();
904  }
905  // Return at least 1 if total sample is not 0.
906  return Count ? Count : TotalSamples > 0;
907  }
908 
909  /// Return all the samples collected in the body of the function.
910  const BodySampleMap &getBodySamples() const { return BodySamples; }
911 
912  /// Return all the callsite samples collected in the body of the function.
914  return CallsiteSamples;
915  }
916 
917  /// Return the maximum of sample counts in a function body including functions
918  /// inlined in it.
920  uint64_t MaxCount = 0;
921  for (const auto &L : getBodySamples())
922  MaxCount = std::max(MaxCount, L.second.getSamples());
923  for (const auto &C : getCallsiteSamples())
924  for (const FunctionSamplesMap::value_type &F : C.second)
925  MaxCount = std::max(MaxCount, F.second.getMaxCountInside());
926  return MaxCount;
927  }
928 
929  /// Merge the samples in \p Other into this one.
930  /// Optionally scale samples by \p Weight.
931  sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) {
933  if (!GUIDToFuncNameMap)
934  GUIDToFuncNameMap = Other.GUIDToFuncNameMap;
935  if (Context.getName().empty())
936  Context = Other.getContext();
937  if (FunctionHash == 0) {
938  // Set the function hash code for the target profile.
939  FunctionHash = Other.getFunctionHash();
940  } else if (FunctionHash != Other.getFunctionHash()) {
941  // The two profiles coming with different valid hash codes indicates
942  // either:
943  // 1. They are same-named static functions from different compilation
944  // units (without using -unique-internal-linkage-names), or
945  // 2. They are really the same function but from different compilations.
946  // Let's bail out in either case for now, which means one profile is
947  // dropped.
949  }
950 
951  MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight));
952  MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight));
953  for (const auto &I : Other.getBodySamples()) {
954  const LineLocation &Loc = I.first;
955  const SampleRecord &Rec = I.second;
956  MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
957  }
958  for (const auto &I : Other.getCallsiteSamples()) {
959  const LineLocation &Loc = I.first;
961  for (const auto &Rec : I.second)
962  MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight));
963  }
964  return Result;
965  }
966 
967  /// Recursively traverses all children, if the total sample count of the
968  /// corresponding function is no less than \p Threshold, add its corresponding
969  /// GUID to \p S. Also traverse the BodySamples to add hot CallTarget's GUID
970  /// to \p S.
973  uint64_t Threshold) const {
974  if (TotalSamples <= Threshold)
975  return;
976  auto isDeclaration = [](const Function *F) {
977  return !F || F->isDeclaration();
978  };
979  if (isDeclaration(SymbolMap.lookup(getFuncName()))) {
980  // Add to the import list only when it's defined out of module.
981  S.insert(getGUID(getName()));
982  }
983  // Import hot CallTargets, which may not be available in IR because full
984  // profile annotation cannot be done until backend compilation in ThinLTO.
985  for (const auto &BS : BodySamples)
986  for (const auto &TS : BS.second.getCallTargets())
987  if (TS.getValue() > Threshold) {
988  const Function *Callee = SymbolMap.lookup(getFuncName(TS.getKey()));
989  if (isDeclaration(Callee))
990  S.insert(getGUID(TS.getKey()));
991  }
992  for (const auto &CS : CallsiteSamples)
993  for (const auto &NameFS : CS.second)
994  NameFS.second.findInlinedFunctions(S, SymbolMap, Threshold);
995  }
996 
997  /// Set the name of the function.
998  void setName(StringRef FunctionName) { Context.setName(FunctionName); }
999 
1000  /// Return the function name.
1001  StringRef getName() const { return Context.getName(); }
1002 
1003  /// Return the original function name.
1004  StringRef getFuncName() const { return getFuncName(getName()); }
1005 
1006  void setFunctionHash(uint64_t Hash) { FunctionHash = Hash; }
1007 
1008  uint64_t getFunctionHash() const { return FunctionHash; }
1009 
1010  /// Return the canonical name for a function, taking into account
1011  /// suffix elision policy attributes.
1013  auto AttrName = "sample-profile-suffix-elision-policy";
1014  auto Attr = F.getFnAttribute(AttrName).getValueAsString();
1015  return getCanonicalFnName(F.getName(), Attr);
1016  }
1017 
1018  /// Name suffixes which canonicalization should handle to avoid
1019  /// profile mismatch.
1020  static constexpr const char *LLVMSuffix = ".llvm.";
1021  static constexpr const char *PartSuffix = ".part.";
1022  static constexpr const char *UniqSuffix = ".__uniq.";
1023 
1025  StringRef Attr = "selected") {
1026  // Note the sequence of the suffixes in the knownSuffixes array matters.
1027  // If suffix "A" is appended after the suffix "B", "A" should be in front
1028  // of "B" in knownSuffixes.
1029  const char *knownSuffixes[] = {LLVMSuffix, PartSuffix, UniqSuffix};
1030  if (Attr == "" || Attr == "all") {
1031  return FnName.split('.').first;
1032  } else if (Attr == "selected") {
1033  StringRef Cand(FnName);
1034  for (const auto &Suf : knownSuffixes) {
1035  StringRef Suffix(Suf);
1036  // If the profile contains ".__uniq." suffix, don't strip the
1037  // suffix for names in the IR.
1038  if (Suffix == UniqSuffix && FunctionSamples::HasUniqSuffix)
1039  continue;
1040  auto It = Cand.rfind(Suffix);
1041  if (It == StringRef::npos)
1042  continue;
1043  auto Dit = Cand.rfind('.');
1044  if (Dit == It + Suffix.size() - 1)
1045  Cand = Cand.substr(0, It);
1046  }
1047  return Cand;
1048  } else if (Attr == "none") {
1049  return FnName;
1050  } else {
1051  assert(false && "internal error: unknown suffix elision policy");
1052  }
1053  return FnName;
1054  }
1055 
1056  /// Translate \p Name into its original name.
1057  /// When profile doesn't use MD5, \p Name needs no translation.
1058  /// When profile uses MD5, \p Name in current FunctionSamples
1059  /// is actually GUID of the original function name. getFuncName will
1060  /// translate \p Name in current FunctionSamples into its original name
1061  /// by looking up in the function map GUIDToFuncNameMap.
1062  /// If the original name doesn't exist in the map, return empty StringRef.
1064  if (!UseMD5)
1065  return Name;
1066 
1067  assert(GUIDToFuncNameMap && "GUIDToFuncNameMap needs to be populated first");
1068  return GUIDToFuncNameMap->lookup(std::stoull(Name.data()));
1069  }
1070 
1071  /// Returns the line offset to the start line of the subprogram.
1072  /// We assume that a single function will not exceed 65535 LOC.
1073  static unsigned getOffset(const DILocation *DIL);
1074 
1075  /// Returns a unique call site identifier for a given debug location of a call
1076  /// instruction. This is wrapper of two scenarios, the probe-based profile and
1077  /// regular profile, to hide implementation details from the sample loader and
1078  /// the context tracker.
1079  static LineLocation getCallSiteIdentifier(const DILocation *DIL,
1080  bool ProfileIsFS = false);
1081 
1082  /// Returns a unique hash code for a combination of a callsite location and
1083  /// the callee function name.
1084  static uint64_t getCallSiteHash(StringRef CalleeName,
1085  const LineLocation &Callsite);
1086 
1087  /// Get the FunctionSamples of the inline instance where DIL originates
1088  /// from.
1089  ///
1090  /// The FunctionSamples of the instruction (Machine or IR) associated to
1091  /// \p DIL is the inlined instance in which that instruction is coming from.
1092  /// We traverse the inline stack of that instruction, and match it with the
1093  /// tree nodes in the profile.
1094  ///
1095  /// \returns the FunctionSamples pointer to the inlined instance.
1096  /// If \p Remapper is not nullptr, it will be used to find matching
1097  /// FunctionSamples with not exactly the same but equivalent name.
1099  const DILocation *DIL,
1100  SampleProfileReaderItaniumRemapper *Remapper = nullptr) const;
1101 
1102  static bool ProfileIsProbeBased;
1103 
1104  static bool ProfileIsCS;
1105 
1106  static bool ProfileIsPreInlined;
1107 
1108  SampleContext &getContext() const { return Context; }
1109 
1110  void setContext(const SampleContext &FContext) { Context = FContext; }
1111 
1112  /// Whether the profile uses MD5 to represent string.
1113  static bool UseMD5;
1114 
1115  /// Whether the profile contains any ".__uniq." suffix in a name.
1116  static bool HasUniqSuffix;
1117 
1118  /// If this profile uses flow sensitive discriminators.
1119  static bool ProfileIsFS;
1120 
1121  /// GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for
1122  /// all the function symbols defined or declared in current module.
1124 
1125  // Assume the input \p Name is a name coming from FunctionSamples itself.
1126  // If UseMD5 is true, the name is already a GUID and we
1127  // don't want to return the GUID of GUID.
1129  return UseMD5 ? std::stoull(Name.data()) : Function::getGUID(Name);
1130  }
1131 
1132  // Find all the names in the current FunctionSamples including names in
1133  // all the inline instances and names of call targets.
1134  void findAllNames(DenseSet<StringRef> &NameSet) const;
1135 
1136 private:
1137  /// CFG hash value for the function.
1138  uint64_t FunctionHash = 0;
1139 
1140  /// Calling context for function profile
1141  mutable SampleContext Context;
1142 
1143  /// Total number of samples collected inside this function.
1144  ///
1145  /// Samples are cumulative, they include all the samples collected
1146  /// inside this function and all its inlined callees.
1147  uint64_t TotalSamples = 0;
1148 
1149  /// Total number of samples collected at the head of the function.
1150  /// This is an approximation of the number of calls made to this function
1151  /// at runtime.
1152  uint64_t TotalHeadSamples = 0;
1153 
1154  /// Map instruction locations to collected samples.
1155  ///
1156  /// Each entry in this map contains the number of samples
1157  /// collected at the corresponding line offset. All line locations
1158  /// are an offset from the start of the function.
1159  BodySampleMap BodySamples;
1160 
1161  /// Map call sites to collected samples for the called function.
1162  ///
1163  /// Each entry in this map corresponds to all the samples
1164  /// collected for the inlined function call at the given
1165  /// location. For example, given:
1166  ///
1167  /// void foo() {
1168  /// 1 bar();
1169  /// ...
1170  /// 8 baz();
1171  /// }
1172  ///
1173  /// If the bar() and baz() calls were inlined inside foo(), this
1174  /// map will contain two entries. One for all the samples collected
1175  /// in the call to bar() at line offset 1, the other for all the samples
1176  /// collected in the call to baz() at line offset 8.
1177  CallsiteSampleMap CallsiteSamples;
1178 };
1179 
1181 
1182 using SampleProfileMap =
1183  std::unordered_map<SampleContext, FunctionSamples, SampleContext::Hash>;
1184 
1185 using NameFunctionSamples = std::pair<SampleContext, const FunctionSamples *>;
1186 
1187 void sortFuncProfiles(const SampleProfileMap &ProfileMap,
1188  std::vector<NameFunctionSamples> &SortedProfiles);
1189 
1190 /// Sort a LocationT->SampleT map by LocationT.
1191 ///
1192 /// It produces a sorted list of <LocationT, SampleT> records by ascending
1193 /// order of LocationT.
1194 template <class LocationT, class SampleT> class SampleSorter {
1195 public:
1196  using SamplesWithLoc = std::pair<const LocationT, SampleT>;
1198 
1199  SampleSorter(const std::map<LocationT, SampleT> &Samples) {
1200  for (const auto &I : Samples)
1201  V.push_back(&I);
1202  llvm::stable_sort(V, [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
1203  return A->first < B->first;
1204  });
1205  }
1206 
1207  const SamplesWithLocList &get() const { return V; }
1208 
1209 private:
1211 };
1212 
1213 /// SampleContextTrimmer impelements helper functions to trim, merge cold
1214 /// context profiles. It also supports context profile canonicalization to make
1215 /// sure ProfileMap's key is consistent with FunctionSample's name/context.
1217 public:
1218  SampleContextTrimmer(SampleProfileMap &Profiles) : ProfileMap(Profiles){};
1219  // Trim and merge cold context profile when requested. TrimBaseProfileOnly
1220  // should only be effective when TrimColdContext is true. On top of
1221  // TrimColdContext, TrimBaseProfileOnly can be used to specify to trim all
1222  // cold profiles or only cold base profiles. Trimming base profiles only is
1223  // mainly to honor the preinliner decsion. Note that when MergeColdContext is
1224  // true, preinliner decsion is not honored anyway so TrimBaseProfileOnly will
1225  // be ignored.
1227  bool TrimColdContext,
1228  bool MergeColdContext,
1229  uint32_t ColdContextFrameLength,
1230  bool TrimBaseProfileOnly);
1231  // Canonicalize context profile name and attributes.
1233 
1234 private:
1235  SampleProfileMap &ProfileMap;
1236 };
1237 
1238 // CSProfileConverter converts a full context-sensitive flat sample profile into
1239 // a nested context-sensitive sample profile.
1241 public:
1243  void convertProfiles();
1244  struct FrameNode {
1246  FunctionSamples *FSamples = nullptr,
1247  LineLocation CallLoc = {0, 0})
1248  : FuncName(FName), FuncSamples(FSamples), CallSiteLoc(CallLoc){};
1249 
1250  // Map line+discriminator location to child frame
1251  std::map<uint64_t, FrameNode> AllChildFrames;
1252  // Function name for current frame
1254  // Function Samples for current frame
1256  // Callsite location in parent context
1258 
1259  FrameNode *getOrCreateChildFrame(const LineLocation &CallSite,
1260  StringRef CalleeName);
1261  };
1262 
1263 private:
1264  // Nest all children profiles into the profile of Node.
1266  FrameNode *getOrCreateContextPath(const SampleContext &Context);
1267 
1268  SampleProfileMap &ProfileMap;
1269  FrameNode RootFrame;
1270 };
1271 
1272 /// ProfileSymbolList records the list of function symbols shown up
1273 /// in the binary used to generate the profile. It is useful to
1274 /// to discriminate a function being so cold as not to shown up
1275 /// in the profile and a function newly added.
1277 public:
1278  /// copy indicates whether we need to copy the underlying memory
1279  /// for the input Name.
1280  void add(StringRef Name, bool copy = false) {
1281  if (!copy) {
1282  Syms.insert(Name);
1283  return;
1284  }
1285  Syms.insert(Name.copy(Allocator));
1286  }
1287 
1288  bool contains(StringRef Name) { return Syms.count(Name); }
1289 
1290  void merge(const ProfileSymbolList &List) {
1291  for (auto Sym : List.Syms)
1292  add(Sym, true);
1293  }
1294 
1295  unsigned size() { return Syms.size(); }
1296 
1297  void setToCompress(bool TC) { ToCompress = TC; }
1298  bool toCompress() { return ToCompress; }
1299 
1300  std::error_code read(const uint8_t *Data, uint64_t ListSize);
1301  std::error_code write(raw_ostream &OS);
1302  void dump(raw_ostream &OS = dbgs()) const;
1303 
1304 private:
1305  // Determine whether or not to compress the symbol list when
1306  // writing it into profile. The variable is unused when the symbol
1307  // list is read from an existing profile.
1308  bool ToCompress = false;
1309  DenseSet<StringRef> Syms;
1310  BumpPtrAllocator Allocator;
1311 };
1312 
1313 } // end namespace sampleprof
1314 
1315 using namespace sampleprof;
1316 // Provide DenseMapInfo for SampleContext.
1317 template <> struct DenseMapInfo<SampleContext> {
1318  static inline SampleContext getEmptyKey() { return SampleContext(); }
1319 
1320  static inline SampleContext getTombstoneKey() { return SampleContext("@"); }
1321 
1322  static unsigned getHashValue(const SampleContext &Val) {
1323  return Val.getHashCode();
1324  }
1325 
1326  static bool isEqual(const SampleContext &LHS, const SampleContext &RHS) {
1327  return LHS == RHS;
1328  }
1329 };
1330 } // end namespace llvm
1331 
1332 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H
llvm::sampleprof::CSProfileConverter
Definition: SampleProf.h:1240
llvm::sampleprof::SampleRecord::getSamples
uint64_t getSamples() const
Definition: SampleProf.h:384
llvm::sampleprof_error::uncompress_failed
@ uncompress_failed
llvm::sampleprof::FunctionSamples::getBodySamples
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:910
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:290
llvm::ArrayRef::drop_back
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:209
llvm::sampleprof::SecHdrTableEntry::Offset
uint64_t Offset
Definition: SampleProf.h:161
MathExtras.h
llvm::sampleprof::CSProfileConverter::FrameNode::FuncSamples
FunctionSamples * FuncSamples
Definition: SampleProf.h:1255
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::sampleprof::SecFuncMetadataFlags::SecFlagHasAttribute
@ SecFlagHasAttribute
llvm::sampleprof::BodySampleMap
std::map< LineLocation, SampleRecord > BodySampleMap
Definition: SampleProf.h:699
llvm::sampleprof::SecProfSummaryFlags::SecFlagFSDiscriminator
@ SecFlagFSDiscriminator
SecFlagFSDiscriminator means this profile uses flow-sensitive discriminators.
llvm::sampleprof::SampleSorter::get
const SamplesWithLocList & get() const
Definition: SampleProf.h:1207
llvm::sampleprof::SampleContext::decodeContextString
static void decodeContextString(StringRef ContextStr, StringRef &FName, LineLocation &LineLoc)
Definition: SampleProf.h:558
llvm::sampleprof::ContextDuplicatedIntoBase
@ ContextDuplicatedIntoBase
Definition: SampleProf.h:443
llvm::sampleprof::SampleContext::operator!=
bool operator!=(const SampleContext &That) const
Definition: SampleProf.h:636
llvm::sampleprof::FunctionSamples::ProfileIsProbeBased
static bool ProfileIsProbeBased
Definition: SampleProf.h:1102
llvm::sampleprof::hash_value
static hash_code hash_value(const SampleContextFrame &arg)
Definition: SampleProf.h:477
llvm::sampleprof::FunctionSamples::dump
void dump() const
Definition: SampleProf.cpp:325
llvm::sampleprof::FunctionSamples::ProfileIsCS
static bool ProfileIsCS
Definition: SampleProf.h:1104
llvm::sampleprof::RawContext
@ RawContext
Definition: SampleProf.h:432
llvm::sampleprof::LineLocation::dump
void dump() const
Definition: SampleProf.cpp:132
llvm::sampleprof::SampleRecord::CallTargetComparator::operator()
bool operator()(const CallTarget &LHS, const CallTarget &RHS) const
Definition: SampleProf.h:321
llvm::sampleprof::SampleContext::hasAttribute
bool hasAttribute(ContextAttributeMask A)
Definition: SampleProf.h:580
llvm::StringRef::rfind
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:379
llvm::Function
Definition: Function.h:60
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:199
StringRef.h
llvm::sampleprof::SecHdrTableEntry
Definition: SampleProf.h:158
llvm::sampleprof::FunctionSamples::removeTotalSamples
void removeTotalSamples(uint64_t Num)
Definition: SampleProf.h:725
llvm::sampleprof::CallsiteSampleMap
std::map< LineLocation, FunctionSamplesMap > CallsiteSampleMap
Definition: SampleProf.h:703
llvm::sampleprof::LineLocation::LineLocation
LineLocation(uint32_t L, uint32_t D)
Definition: SampleProf.h:283
llvm::sampleprof::SampleSorter::SamplesWithLocList
SmallVector< const SamplesWithLoc *, 20 > SamplesWithLocList
Definition: SampleProf.h:1197
llvm::sampleprof::SampleRecord::hasCalls
bool hasCalls() const
Return true if this sample record contains function calls.
Definition: SampleProf.h:382
llvm::sampleprof_error::hash_mismatch
@ hash_mismatch
llvm::sampleprof::SecNameTableFlags::SecFlagFixedLengthMD5
@ SecFlagFixedLengthMD5
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:60
llvm::sampleprof::FunctionSamples::getFuncName
StringRef getFuncName(StringRef Name) const
Translate Name into its original name.
Definition: SampleProf.h:1063
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::sampleprof::NameFunctionSamples
std::pair< SampleContext, const FunctionSamples * > NameFunctionSamples
Definition: SampleProf.h:1185
llvm::sampleprof::ProfileSymbolList::size
unsigned size()
Definition: SampleProf.h:1295
llvm::sampleprof::SampleContext::setContext
void setContext(SampleContextFrames Context, ContextStateMask CState=RawContext)
Definition: SampleProf.h:623
llvm::sampleprof::SampleContext::SampleContext
SampleContext(SampleContextFrames Context, ContextStateMask CState=RawContext)
Definition: SampleProf.h:508
llvm::sampleprof::SampleContextFrame::toString
std::string toString(bool OutputLineLocation) const
Definition: SampleProf.h:465
llvm::sampleprof::SecProfSummaryFlags::SecFlagFullContext
@ SecFlagFullContext
SecFlagContext means this is context-sensitive flat profile for CSSPGO.
Allocator.h
llvm::sampleprof::SampleContext::toString
std::string toString() const
Definition: SampleProf.h:605
llvm::sampleprof_error::unsupported_version
@ unsupported_version
llvm::sampleprof::SampleProfileMap
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
Definition: SampleProf.h:1183
llvm::sampleprof::SampleContext::operator==
bool operator==(const SampleContext &That) const
Definition: SampleProf.h:631
llvm::sampleprof::hasSecFlag
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:266
llvm::DILocation
Debug location.
Definition: DebugInfoMetadata.h:1557
llvm::sampleprof::LineLocation::operator<
bool operator<(const LineLocation &O) const
Definition: SampleProf.h:288
llvm::sampleprof::SPF_GCC
@ SPF_GCC
Definition: SampleProf.h:93
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::sampleprof::ContextShouldBeInlined
@ ContextShouldBeInlined
Definition: SampleProf.h:442
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::sampleprof::SecFuncOffsetTable
@ SecFuncOffsetTable
Definition: SampleProf.h:125
llvm::sampleprof::addSecFlag
static void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:250
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1668
llvm::sampleprof_error::not_implemented
@ not_implemented
FMAInstKind::Accumulator
@ Accumulator
llvm::sampleprof::SecCommonFlags::SecFlagCompress
@ SecFlagCompress
ret
to esp esp setne al movzbw ax esp setg cl movzbw cx cmove cx cl jne LBB1_2 esp ret(also really horrible code on ppc). This is due to the expand code for 64-bit compares. GCC produces multiple branches
llvm::sampleprof::SampleContext::getAllAttributes
uint32_t getAllAttributes()
Definition: SampleProf.h:582
llvm::sampleprof::SecNameTableFlags::SecFlagMD5Name
@ SecFlagMD5Name
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::sampleprof::FunctionSamples::findInlinedFunctions
void findInlinedFunctions(DenseSet< GlobalValue::GUID > &S, const StringMap< Function * > &SymbolMap, uint64_t Threshold) const
Recursively traverses all children, if the total sample count of the corresponding function is no les...
Definition: SampleProf.h:971
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::sampleprof::SampleRecord::SampleRecord
SampleRecord()=default
llvm::sampleprof::FunctionSamples::findAllNames
void findAllNames(DenseSet< StringRef > &NameSet) const
Definition: SampleProf.cpp:274
llvm::sampleprof::FunctionSamples::getName
StringRef getName() const
Return the function name.
Definition: SampleProf.h:1001
llvm::sampleprof::FunctionSamplesMap
std::map< std::string, FunctionSamples, std::less<> > FunctionSamplesMap
Definition: SampleProf.h:702
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::sampleprof::MergedContext
@ MergedContext
Definition: SampleProf.h:435
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
llvm::sampleprof::SPF_None
@ SPF_None
Definition: SampleProf.h:90
llvm::sampleprof::CSProfileConverter::FrameNode::getOrCreateChildFrame
FrameNode * getOrCreateChildFrame(const LineLocation &CallSite, StringRef CalleeName)
Definition: SampleProf.cpp:467
llvm::sampleprof::FunctionSamples::getCanonicalFnName
static StringRef getCanonicalFnName(StringRef FnName, StringRef Attr="selected")
Definition: SampleProf.h:1024
llvm::sampleprof::SecHdrTableEntry::Size
uint64_t Size
Definition: SampleProf.h:162
llvm::sampleprof::SPF_Binary
@ SPF_Binary
Definition: SampleProf.h:95
llvm::sampleprof::LineLocation::Discriminator
uint32_t Discriminator
Definition: SampleProf.h:302
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::sampleprof_error::counter_overflow
@ counter_overflow
llvm::sampleprof::LineLocation::LineOffset
uint32_t LineOffset
Definition: SampleProf.h:301
llvm::sampleprof::FunctionSamples::SetContextSynthetic
void SetContextSynthetic()
Definition: SampleProf.h:807
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::DenseMapInfo< SampleContext >::isEqual
static bool isEqual(const SampleContext &LHS, const SampleContext &RHS)
Definition: SampleProf.h:1326
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:615
llvm::sampleprof::FunctionSamples::HasUniqSuffix
static bool HasUniqSuffix
Whether the profile contains any ".__uniq." suffix in a name.
Definition: SampleProf.h:1116
llvm::sampleprof::removeSecFlag
static void removeSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:258
llvm::sampleprof::ProfileSymbolList::setToCompress
void setToCompress(bool TC)
Definition: SampleProf.h:1297
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::sampleprof::SecFuncMetadataFlags::SecFlagIsProbeBased
@ SecFlagIsProbeBased
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
llvm::sampleprof::FunctionSamples::getFuncName
StringRef getFuncName() const
Return the original function name.
Definition: SampleProf.h:1004
GlobalValue.h
llvm::sampleprof_error::unsupported_writing_format
@ unsupported_writing_format
llvm::sampleprof::SampleProfileFormat
SampleProfileFormat
Definition: SampleProf.h:89
llvm::sampleprof::getRepInFormat
static StringRef getRepInFormat(StringRef Name, bool UseMD5, std::string &GUIDBuf)
Get the proper representation of a string according to whether the current Format uses MD5 to represe...
Definition: SampleProf.h:107
llvm::sampleprof::FunctionSamples::PartSuffix
static constexpr const char * PartSuffix
Definition: SampleProf.h:1021
llvm::sampleprof::FunctionSamples::removeCalledTargetAndBodySample
uint64_t removeCalledTargetAndBodySample(uint32_t LineOffset, uint32_t Discriminator, StringRef FName)
Definition: SampleProf.h:758
llvm::sampleprof::FunctionSamples::setTotalSamples
void setTotalSamples(uint64_t Num)
Definition: SampleProf.h:732
llvm::sampleprof::FunctionSamples::getTotalSamples
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:875
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::sampleprof::SampleContextFrameHash
Definition: SampleProf.h:485
llvm::sampleprof::FunctionSamples::updateTotalSamples
void updateTotalSamples()
Definition: SampleProf.h:793
llvm::sampleprof::SampleContext::hasState
bool hasState(ContextStateMask S)
Definition: SampleProf.h:584
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:753
llvm::sampleprof::SampleRecord::CallTarget
std::pair< StringRef, uint64_t > CallTarget
Definition: SampleProf.h:319
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::sampleprof::SampleContext::Hash
Definition: SampleProf.h:661
llvm::sampleprof::CSProfileConverter::FrameNode
Definition: SampleProf.h:1244
llvm::sampleprof::SampleContext::getHashCode
uint64_t getHashCode() const
Definition: SampleProf.h:611
llvm::sampleprof::FunctionSamples::FunctionSamples
FunctionSamples()=default
llvm::sampleprof::SampleRecord::removeSamples
uint64_t removeSamples(uint64_t S)
Decrease the number of samples for this record by S.
Definition: SampleProf.h:347
llvm::sampleprof::SyntheticContext
@ SyntheticContext
Definition: SampleProf.h:433
llvm::sampleprof::SampleContextFrame::SampleContextFrame
SampleContextFrame()
Definition: SampleProf.h:452
llvm::sampleprof::SecProfSummaryFlags::SecFlagPartial
@ SecFlagPartial
SecFlagPartial means the profile is for common/shared code.
llvm::sampleprof::FunctionSamples::findCallTargetMapAt
ErrorOr< SampleRecord::CallTargetMap > findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const
Returns the call target map collected at a given location.
Definition: SampleProf.h:831
llvm::sampleprof::SampleContext::createCtxVectorFromStr
static void createCtxVectorFromStr(StringRef ContextStr, SampleContextFrameVector &Context)
Create a context vector from a given context string and save it in Context.
Definition: SampleProf.h:539
DenseSet.h
llvm::orc::SymbolMap
DenseMap< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:113
llvm::sampleprof::FunctionSamples::getGUID
static uint64_t getGUID(StringRef Name)
Definition: SampleProf.h:1128
llvm::sampleprof::SampleContext::operator<
bool operator<(const SampleContext &That) const
Definition: SampleProf.h:638
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::sampleprof_category
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:103
llvm::DenseMapInfo< SampleContext >::getHashValue
static unsigned getHashValue(const SampleContext &Val)
Definition: SampleProf.h:1322
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
ColdCountThreshold
static cl::opt< unsigned > ColdCountThreshold("mfs-count-threshold", cl::desc("Minimum number of times a block must be executed to be retained."), cl::init(1), cl::Hidden)
llvm::sampleprof::SampleSorter::SampleSorter
SampleSorter(const std::map< LocationT, SampleT > &Samples)
Definition: SampleProf.h:1199
llvm::sampleprof::SPMagic
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:98
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::sampleprof::SecNameTableFlags::SecFlagInValid
@ SecFlagInValid
llvm::sampleprof::ContextStateMask
ContextStateMask
Definition: SampleProf.h:430
StringMap.h
llvm::sampleprof::ProfileSymbolList::read
std::error_code read(const uint8_t *Data, uint64_t ListSize)
Definition: SampleProf.cpp:328
llvm::sampleprof::SampleContextFrames
ArrayRef< SampleContextFrame > SampleContextFrames
Definition: SampleProf.h:483
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:249
llvm::sampleprof::CSProfileConverter::FrameNode::AllChildFrames
std::map< uint64_t, FrameNode > AllChildFrames
Definition: SampleProf.h:1248
llvm::sampleprof::SampleContext
Definition: SampleProf.h:501
llvm::sampleprof::SecProfSummaryFlags::SecFlagInValid
@ SecFlagInValid
llvm::sampleprof::FunctionSamples::getHeadSamples
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Definition: SampleProf.h:882
llvm::sampleprof::SampleContext::isBaseContext
bool isBaseContext() const
Definition: SampleProf.h:588
llvm::sampleprof::sortFuncProfiles
void sortFuncProfiles(const SampleProfileMap &ProfileMap, std::vector< NameFunctionSamples > &SortedProfiles)
Definition: SampleProf.cpp:203
llvm::StringMap< uint64_t >
llvm::sampleprof::FunctionSamples::ProfileIsFS
static bool ProfileIsFS
If this profile uses flow sensitive discriminators.
Definition: SampleProf.h:1119
llvm::sampleprof::FunctionSamples::empty
bool empty() const
Definition: SampleProf.h:872
llvm::sampleprof::SecHdrTableEntry::Type
SecType Type
Definition: SampleProf.h:159
llvm::sampleprof::LineLocation::operator!=
bool operator!=(const LineLocation &O) const
Definition: SampleProf.h:297
llvm::sampleprof::SampleRecord::getCallTargets
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:385
llvm::sampleprof::SampleRecord::SortedCallTargetSet
std::set< CallTarget, CallTargetComparator > SortedCallTargetSet
Definition: SampleProf.h:329
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::sampleprof::SampleContext::SampleContext
SampleContext(StringRef ContextStr, std::list< SampleContextFrameVector > &CSNameTable, ContextStateMask CState=RawContext)
Definition: SampleProf.h:518
llvm::sampleprof::ProfileSymbolList::contains
bool contains(StringRef Name)
Definition: SampleProf.h:1288
llvm::sampleprof::SampleContext::getContextFrames
SampleContextFrames getContextFrames() const
Definition: SampleProf.h:590
llvm::sampleprof::FunctionSamples::getCallsiteSamples
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Definition: SampleProf.h:913
llvm::sampleprof::SampleContext::setName
void setName(StringRef FunctionName)
Set the name of the function and clear the current context.
Definition: SampleProf.h:617
llvm::sampleprof::SecHdrTableEntry::Flags
uint64_t Flags
Definition: SampleProf.h:160
llvm::sampleprof::SampleRecord::CallTargetComparator
Definition: SampleProf.h:320
llvm::sampleprof::UnknownContext
@ UnknownContext
Definition: SampleProf.h:431
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
llvm::sampleprof::SecProfSummary
@ SecProfSummary
Definition: SampleProf.h:122
llvm::sampleprof::SPF_Compact_Binary
@ SPF_Compact_Binary
Definition: SampleProf.h:92
llvm::sampleprof::ProfileSymbolList
ProfileSymbolList records the list of function symbols shown up in the binary used to generate the pr...
Definition: SampleProf.h:1276
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::sampleprof::SampleContext::setAttribute
void setAttribute(ContextAttributeMask A)
Definition: SampleProf.h:581
llvm::StringMap::erase
void erase(iterator I)
Definition: StringMap.h:370
llvm::sampleprof::FunctionSamples::addHeadSamples
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:734
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::sampleprof::SampleContext::SampleContext
SampleContext()
Definition: SampleProf.h:503
llvm::sampleprof::FunctionSamples::getEntrySamples
uint64_t getEntrySamples() const
Return the sample count of the first instruction of the function.
Definition: SampleProf.h:886
llvm::sampleprof::FunctionSamples::GUIDToFuncNameMap
DenseMap< uint64_t, StringRef > * GUIDToFuncNameMap
GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for all the function symbols define...
Definition: SampleProf.h:1123
llvm::sampleprof::LineLocation::operator==
bool operator==(const LineLocation &O) const
Definition: SampleProf.h:293
llvm::sampleprof::SecFuncOffsetFlags
SecFuncOffsetFlags
Definition: SampleProf.h:214
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:710
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
llvm::sampleprof::SampleContextFrame::Location
LineLocation Location
Definition: SampleProf.h:450
llvm::sampleprof::SecFuncMetadataFlags::SecFlagInvalid
@ SecFlagInvalid
llvm::sampleprof::SPF_Ext_Binary
@ SPF_Ext_Binary
Definition: SampleProf.h:94
llvm::DenseMap
Definition: DenseMap.h:716
llvm::sampleprof::FunctionSamples::functionSamplesAt
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
Definition: SampleProf.h:849
ErrorOr.h
llvm::DenseMapInfo< SampleContext >::getTombstoneKey
static SampleContext getTombstoneKey()
Definition: SampleProf.h:1320
llvm::sampleprof::SampleContext::clearState
void clearState(ContextStateMask S)
Definition: SampleProf.h:586
llvm::sampleprof::SecLBRProfile
@ SecLBRProfile
Definition: SampleProf.h:130
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::sampleprof::FunctionSamples::getCallSiteHash
static uint64_t getCallSiteHash(StringRef CalleeName, const LineLocation &Callsite)
Returns a unique hash code for a combination of a callsite location and the callee function name.
Definition: SampleProf.cpp:240
llvm::SaturatingMultiplyAdd
std::enable_if_t< std::is_unsigned< T >::value, 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:900
llvm::sampleprof::FunctionSamples::setContext
void setContext(const SampleContext &FContext)
Definition: SampleProf.h:1110
llvm::sampleprof::SampleRecord::getCallTargetSum
uint64_t getCallTargetSum() const
Definition: SampleProf.h:390
llvm::sampleprof::FunctionSamples::getFunctionHash
uint64_t getFunctionHash() const
Definition: SampleProf.h:1008
llvm::sampleprof::FunctionSamples::addCalledTargetSamples
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:748
llvm::sampleprof::SampleContextFrame::FuncName
StringRef FuncName
Definition: SampleProf.h:449
llvm::sampleprof::SampleRecord::adjustCallTargets
static const CallTargetMap adjustCallTargets(const CallTargetMap &Targets, float DistributionFactor)
Prorate call targets by a distribution factor.
Definition: SampleProf.h:407
llvm::sampleprof::SampleRecord::getSortedCallTargets
const SortedCallTargetSet getSortedCallTargets() const
Definition: SampleProf.h:386
llvm::sampleprof::SampleContext::setAllAttributes
void setAllAttributes(uint32_t A)
Definition: SampleProf.h:583
llvm::sampleprof_error::malformed
@ malformed
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::sampleprof::SecCommonFlags::SecFlagInValid
@ SecFlagInValid
llvm::sampleprof::SampleContext::getName
StringRef getName() const
Definition: SampleProf.h:589
llvm::X86AS::FS
@ FS
Definition: X86.h:192
llvm::sampleprof::SampleContext::setState
void setState(ContextStateMask S)
Definition: SampleProf.h:585
llvm::sampleprof::operator<<
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:113
llvm::sampleprof::FunctionSamples::UseMD5
static bool UseMD5
Whether the profile uses MD5 to represent string.
Definition: SampleProf.h:1113
llvm::sampleprof::CSProfileConverter::FrameNode::FuncName
StringRef FuncName
Definition: SampleProf.h:1253
llvm::sampleprof::SecHdrTableEntry::LayoutIndex
uint32_t LayoutIndex
Definition: SampleProf.h:165
llvm::sampleprof::ProfileSymbolList::merge
void merge(const ProfileSymbolList &List)
Definition: SampleProf.h:1290
llvm::sampleprof::CSProfileConverter::FrameNode::CallSiteLoc
LineLocation CallSiteLoc
Definition: SampleProf.h:1257
llvm::sampleprof::FunctionSamples::getOffset
static unsigned getOffset(const DILocation *DIL)
Returns the line offset to the start line of the subprogram.
Definition: SampleProf.cpp:218
llvm::sampleprof::LineLocation
Represents the relative location of an instruction.
Definition: SampleProf.h:282
llvm::MergeResult
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
Definition: SampleProf.h:67
llvm::ArrayRef< SampleContextFrame >
llvm::sampleprof::SampleContext::IsPrefixOf
bool IsPrefixOf(const SampleContext &That) const
Definition: SampleProf.h:667
llvm::sampleprof::SampleContext::SampleContext
SampleContext(StringRef Name)
Definition: SampleProf.h:505
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::sampleprof::SecProfSummaryFlags
SecProfSummaryFlags
Definition: SampleProf.h:191
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::sampleprof::SampleSorter
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:1194
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::sampleprof_error::unrecognized_format
@ unrecognized_format
llvm::sampleprof::SecInValid
@ SecInValid
Definition: SampleProf.h:121
uint32_t
llvm::sampleprof::FunctionSamples::addBodySamples
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:742
llvm::sampleprof::SampleContextFrame::operator==
bool operator==(const SampleContextFrame &That) const
Definition: SampleProf.h:457
llvm::sampleprof::ProfileSymbolList::add
void add(StringRef Name, bool copy=false)
copy indicates whether we need to copy the underlying memory for the input Name.
Definition: SampleProf.h:1280
llvm::sampleprof::FunctionSamples::print
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:157
llvm::sampleprof::FunctionSamples::findFunctionSamplesMapAt
const FunctionSamplesMap * findFunctionSamplesMapAt(const LineLocation &Loc) const
Returns the FunctionSamplesMap at the given Loc.
Definition: SampleProf.h:855
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::sampleprof::FunctionSamples::findSamplesAt
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
Definition: SampleProf.h:819
llvm::sampleprof::SampleRecord::CallTargetMap
StringMap< uint64_t > CallTargetMap
Definition: SampleProf.h:330
Node
Definition: ItaniumDemangle.h:155
llvm::sampleprof::SPF_Text
@ SPF_Text
Definition: SampleProf.h:91
llvm::sampleprof::SecFuncOffsetFlags::SecFlagOrdered
@ SecFlagOrdered
llvm::sampleprof::getSecName
static std::string getSecName(SecType Type)
Definition: SampleProf.h:133
llvm::sampleprof::FunctionSamples::findFunctionSamplesAt
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName, SampleProfileReaderItaniumRemapper *Remapper) const
Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName.
Definition: SampleProf.cpp:288
llvm::sampleprof::ProfileSymbolList::toCompress
bool toCompress()
Definition: SampleProf.h:1298
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:271
llvm::sampleprof::InlinedContext
@ InlinedContext
Definition: SampleProf.h:434
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:186
llvm::sampleprof_error::ostream_seek_unsupported
@ ostream_seek_unsupported
llvm::sampleprof::CSProfileConverter::convertProfiles
void convertProfiles()
Definition: SampleProf.cpp:549
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::stable_sort
void stable_sort(R &&Range)
Definition: STLExtras.h:1761
llvm::sampleprof::ProfileSymbolList::write
std::error_code write(raw_ostream &OS)
Definition: SampleProf.cpp:441
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
std
Definition: BitVector.h:851
llvm::sampleprof::LineLocation::print
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:107
llvm::sampleprof::SampleRecord::print
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:136
llvm::sampleprof::SPVersion
static uint64_t SPVersion()
Definition: SampleProf.h:115
llvm::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:227
llvm::sampleprof::verifySecFlag
static void verifySecFlag(SecType Type, SecFlagType Flag)
Definition: SampleProf.h:223
llvm::GlobalValue::getGUID
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:559
llvm::sampleprof::SecProfileSymbolList
@ SecProfileSymbolList
Definition: SampleProf.h:124
Function.h
llvm::sampleprof::FunctionSamples::findCallTargetMapAt
ErrorOr< SampleRecord::CallTargetMap > findCallTargetMapAt(const LineLocation &CallSite) const
Returns the call target map collected at a given location specified by CallSite.
Definition: SampleProf.h:841
llvm::sampleprof::FunctionSamples::addTotalSamples
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:717
llvm::sampleprof::SecType
SecType
Definition: SampleProf.h:120
llvm::sampleprof::SampleRecord
Representation of a single sample record.
Definition: SampleProf.h:317
llvm::sampleprof::CSProfileConverter::FrameNode::FrameNode
FrameNode(StringRef FName=StringRef(), FunctionSamples *FSamples=nullptr, LineLocation CallLoc={0, 0})
Definition: SampleProf.h:1245
llvm::sampleprof_error
sampleprof_error
Definition: SampleProf.h:45
llvm::sampleprof::FunctionSamples::merge
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
Definition: SampleProf.h:931
llvm::sampleprof::FunctionSamples::LLVMSuffix
static constexpr const char * LLVMSuffix
Name suffixes which canonicalization should handle to avoid profile mismatch.
Definition: SampleProf.h:1020
llvm::sampleprof::ContextNone
@ ContextNone
Definition: SampleProf.h:440
llvm::sampleprof_error::zlib_unavailable
@ zlib_unavailable
llvm::sampleprof::SampleContext::hasContext
bool hasContext() const
Definition: SampleProf.h:587
llvm::sampleprof::SecFuncProfileFirst
@ SecFuncProfileFirst
Definition: SampleProf.h:129
llvm::sampleprof_error::too_large
@ too_large
llvm::sampleprof::SampleContextTrimmer::trimAndMergeColdContextProfiles
void trimAndMergeColdContextProfiles(uint64_t ColdCountThreshold, bool TrimColdContext, bool MergeColdContext, uint32_t ColdContextFrameLength, bool TrimBaseProfileOnly)
Definition: SampleProf.cpp:344
llvm::sampleprof::SampleRecord::addCalledTarget
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
Definition: SampleProf.h:359
llvm::sampleprof::FunctionSamples::getContext
SampleContext & getContext() const
Definition: SampleProf.h:1108
llvm::sampleprof_error::success
@ success
llvm::sampleprof::SecNameTableFlags::SecFlagUniqSuffix
@ SecFlagUniqSuffix
llvm::sampleprof::FunctionSamples::getMaxCountInside
uint64_t getMaxCountInside() const
Return the maximum of sample counts in a function body including functions inlined in it.
Definition: SampleProf.h:919
llvm::sampleprof::ContextAttributeMask
ContextAttributeMask
Definition: SampleProf.h:439
llvm::sampleprof::SampleContextFrame::SampleContextFrame
SampleContextFrame(StringRef FuncName, LineLocation Location)
Definition: SampleProf.h:454
llvm::sampleprof::SampleContextTrimmer::SampleContextTrimmer
SampleContextTrimmer(SampleProfileMap &Profiles)
Definition: SampleProf.h:1218
llvm::sampleprof::SampleRecord::removeCalledTarget
uint64_t removeCalledTarget(StringRef F)
Remove called function from the call target map.
Definition: SampleProf.h:371
llvm::sampleprof::SecNameTableFlags
SecNameTableFlags
Definition: SampleProf.h:181
llvm::sampleprof::ContextWasInlined
@ ContextWasInlined
Definition: SampleProf.h:441
llvm::sampleprof::FunctionSamples::getCanonicalFnName
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
Definition: SampleProf.h:1012
llvm::sampleprof::SampleContextTrimmer::canonicalizeContextProfiles
void canonicalizeContextProfiles()
Definition: SampleProf.cpp:405
llvm::StringMapImpl::empty
bool empty() const
Definition: StringMap.h:94
SmallVector.h
llvm::sampleprof::SampleRecord::dump
void dump() const
Definition: SampleProf.cpp:147
llvm::DenseMapInfo< SampleContext >::getEmptyKey
static SampleContext getEmptyKey()
Definition: SampleProf.h:1318
llvm::sampleprof::SampleRecord::SortCallTargets
static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets)
Sort call targets in descending order of call frequency.
Definition: SampleProf.h:398
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:605
llvm::sampleprof::SampleContextFrame::operator!=
bool operator!=(const SampleContextFrame &That) const
Definition: SampleProf.h:461
llvm::sampleprof::SecCommonFlags::SecFlagFlat
@ SecFlagFlat
llvm::sampleprof::SecCommonFlags
SecCommonFlags
Definition: SampleProf.h:171
List
const NodeList & List
Definition: RDFGraph.cpp:199
llvm::sampleprof::FunctionSamples::UniqSuffix
static constexpr const char * UniqSuffix
Definition: SampleProf.h:1022
llvm::sampleprof::SampleRecord::merge
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight=1)
Merge the samples in Other into this record.
Definition: SampleProf.cpp:121
llvm::sampleprof::SecCSNameTable
@ SecCSNameTable
Definition: SampleProf.h:127
llvm::hash_combine_range
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:483
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:85
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::sampleprof::SecProfSummaryFlags::SecFlagIsPreInlined
@ SecFlagIsPreInlined
SecFlagIsPreInlined means this profile contains ShouldBeInlined contexts thus this is CS preinliner c...
llvm::sampleprof::SampleContext::getContextString
static std::string getContextString(SampleContextFrames Context, bool IncludeLeafLineLocation=false)
Definition: SampleProf.h:592
llvm::sampleprof::SecNameTable
@ SecNameTable
Definition: SampleProf.h:123
llvm::sampleprof::FunctionSamples::addBodySamplesForProbe
sampleprof_error addBodySamplesForProbe(uint32_t Index, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:772
llvm::sampleprof::SampleProfileReaderItaniumRemapper
SampleProfileReaderItaniumRemapper remaps the profile data from a sample profile data reader,...
Definition: SampleProfReader.h:261
llvm::sampleprof::SecFuncMetadata
@ SecFuncMetadata
Definition: SampleProf.h:126
llvm::sampleprof::SampleContextFrame
Definition: SampleProf.h:448
llvm::sampleprof_error::truncated_name_table
@ truncated_name_table
llvm::sampleprof::SampleSorter::SamplesWithLoc
std::pair< const LocationT, SampleT > SamplesWithLoc
Definition: SampleProf.h:1196
llvm::sampleprof::SampleContextTrimmer
SampleContextTrimmer impelements helper functions to trim, merge cold context profiles.
Definition: SampleProf.h:1216
llvm::sampleprof::ProfileSymbolList::dump
void dump(raw_ostream &OS=dbgs()) const
Definition: SampleProf.cpp:457
llvm::sampleprof::FunctionSamples::updateCallsiteSamples
void updateCallsiteSamples()
Definition: SampleProf.h:780
error_category
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:78
llvm::sampleprof::SecFuncOffsetFlags::SecFlagInvalid
@ SecFlagInvalid
llvm::sampleprof_error::truncated
@ truncated
llvm::StringRef::compare
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:201
Debug.h
llvm::sampleprof::SampleRecord::addSamples
sampleprof_error addSamples(uint64_t S, uint64_t Weight=1)
Increment the number of samples for this record by S.
Definition: SampleProf.h:338
llvm::sampleprof::SampleContextFrameHash::operator()
uint64_t operator()(const SampleContextFrameVector &S) const
Definition: SampleProf.h:486
llvm::sampleprof::SecFuncMetadataFlags
SecFuncMetadataFlags
Definition: SampleProf.h:208
llvm::sampleprof_error::bad_magic
@ bad_magic
llvm::sampleprof::CSProfileConverter::CSProfileConverter
CSProfileConverter(SampleProfileMap &Profiles)
Definition: SampleProf.cpp:481
llvm::sampleprof::FunctionSamples::setFunctionHash
void setFunctionHash(uint64_t Hash)
Definition: SampleProf.h:1006
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1236
llvm::sampleprof::FunctionSamples::getCallSiteIdentifier
static LineLocation getCallSiteIdentifier(const DILocation *DIL, bool ProfileIsFS=false)
Returns a unique call site identifier for a given debug location of a call instruction.
Definition: SampleProf.cpp:223
llvm::sampleprof::FunctionSamples::findFunctionSamples
const FunctionSamples * findFunctionSamples(const DILocation *DIL, SampleProfileReaderItaniumRemapper *Remapper=nullptr) const
Get the FunctionSamples of the inline instance where DIL originates from.
Definition: SampleProf.cpp:248
llvm::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:73
llvm::sampleprof::FunctionSamples::ProfileIsPreInlined
static bool ProfileIsPreInlined
Definition: SampleProf.h:1106
llvm::sampleprof::SampleContext::Hash::operator()
uint64_t operator()(const SampleContext &Context) const
Definition: SampleProf.h:662
llvm::sampleprof::FunctionSamples::setName
void setName(StringRef FunctionName)
Set the name of the function.
Definition: SampleProf.h:998