LLVM  10.0.0svn
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/ADT/StringSet.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorOr.h"
29 #include <algorithm>
30 #include <cstdint>
31 #include <map>
32 #include <set>
33 #include <string>
34 #include <system_error>
35 #include <utility>
36 
37 namespace llvm {
38 
39 class raw_ostream;
40 
42 
43 enum class sampleprof_error {
44  success = 0,
45  bad_magic,
47  too_large,
48  truncated,
49  malformed,
59 };
60 
61 inline std::error_code make_error_code(sampleprof_error E) {
62  return std::error_code(static_cast<int>(E), sampleprof_category());
63 }
64 
66  sampleprof_error Result) {
67  // Prefer first error encountered as later errors may be secondary effects of
68  // the initial problem.
69  if (Accumulator == sampleprof_error::success &&
70  Result != sampleprof_error::success)
71  Accumulator = Result;
72  return Accumulator;
73 }
74 
75 } // end namespace llvm
76 
77 namespace std {
78 
79 template <>
80 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
81 
82 } // end namespace std
83 
84 namespace llvm {
85 namespace sampleprof {
86 
88  SPF_None = 0,
89  SPF_Text = 0x1,
91  SPF_GCC = 0x3,
93  SPF_Binary = 0xff
94 };
95 
96 static inline uint64_t SPMagic(SampleProfileFormat Format = SPF_Binary) {
97  return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
98  uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
99  uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
100  uint64_t('2') << (64 - 56) | uint64_t(Format);
101 }
102 
103 // Get the proper representation of a string in the input Format.
106  std::string &GUIDBuf) {
107  if (Name.empty())
108  return Name;
109  GUIDBuf = std::to_string(Function::getGUID(Name));
110  return (Format == SPF_Compact_Binary) ? StringRef(GUIDBuf) : Name;
111 }
112 
113 static inline uint64_t SPVersion() { return 103; }
114 
115 // Section Type used by SampleProfileExtBinaryBaseReader and
116 // SampleProfileExtBinaryBaseWriter. Never change the existing
117 // value of enum. Only append new ones.
118 enum SecType {
124  // marker for the first type of profile.
127 };
128 
129 static inline std::string getSecName(SecType Type) {
130  switch (Type) {
131  case SecInValid:
132  return "InvalidSection";
133  case SecProfSummary:
134  return "ProfileSummarySection";
135  case SecNameTable:
136  return "NameTableSection";
138  return "ProfileSymbolListSection";
139  case SecFuncOffsetTable:
140  return "FuncOffsetTableSection";
141  case SecLBRProfile:
142  return "LBRProfileSection";
143  }
144  llvm_unreachable("A SecType has no name for output");
145 }
146 
147 // Entry type of section header table used by SampleProfileExtBinaryBaseReader
148 // and SampleProfileExtBinaryBaseWriter.
151  uint64_t Flags;
152  uint64_t Offset;
153  uint64_t Size;
154 };
155 
156 enum SecFlags { SecFlagInValid = 0, SecFlagCompress = (1 << 0) };
157 
158 static inline void addSecFlags(SecHdrTableEntry &Entry, uint64_t Flags) {
159  Entry.Flags |= Flags;
160 }
161 
162 static inline void removeSecFlags(SecHdrTableEntry &Entry, uint64_t Flags) {
163  Entry.Flags &= ~Flags;
164 }
165 
167  return Entry.Flags & Flag;
168 }
169 
170 /// Represents the relative location of an instruction.
171 ///
172 /// Instruction locations are specified by the line offset from the
173 /// beginning of the function (marked by the line where the function
174 /// header is) and the discriminator value within that line.
175 ///
176 /// The discriminator value is useful to distinguish instructions
177 /// that are on the same line but belong to different basic blocks
178 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
179 struct LineLocation {
180  LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
181 
182  void print(raw_ostream &OS) const;
183  void dump() const;
184 
185  bool operator<(const LineLocation &O) const {
186  return LineOffset < O.LineOffset ||
187  (LineOffset == O.LineOffset && Discriminator < O.Discriminator);
188  }
189 
192 };
193 
195 
196 /// Representation of a single sample record.
197 ///
198 /// A sample record is represented by a positive integer value, which
199 /// indicates how frequently was the associated line location executed.
200 ///
201 /// Additionally, if the associated location contains a function call,
202 /// the record will hold a list of all the possible called targets. For
203 /// direct calls, this will be the exact function being invoked. For
204 /// indirect calls (function pointers, virtual table dispatch), this
205 /// will be a list of one or more functions.
207 public:
208  using CallTarget = std::pair<StringRef, uint64_t>;
210  bool operator()(const CallTarget &LHS, const CallTarget &RHS) const {
211  if (LHS.second != RHS.second)
212  return LHS.second > RHS.second;
213 
214  return LHS.first < RHS.first;
215  }
216  };
217 
218  using SortedCallTargetSet = std::set<CallTarget, CallTargetComparator>;
220  SampleRecord() = default;
221 
222  /// Increment the number of samples for this record by \p S.
223  /// Optionally scale sample count \p S by \p Weight.
224  ///
225  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
226  /// around unsigned integers.
227  sampleprof_error addSamples(uint64_t S, uint64_t Weight = 1) {
228  bool Overflowed;
229  NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed);
230  return Overflowed ? sampleprof_error::counter_overflow
232  }
233 
234  /// Add called function \p F with samples \p S.
235  /// Optionally scale sample count \p S by \p Weight.
236  ///
237  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
238  /// around unsigned integers.
240  uint64_t Weight = 1) {
241  uint64_t &TargetSamples = CallTargets[F];
242  bool Overflowed;
243  TargetSamples =
244  SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed);
245  return Overflowed ? sampleprof_error::counter_overflow
247  }
248 
249  /// Return true if this sample record contains function calls.
250  bool hasCalls() const { return !CallTargets.empty(); }
251 
252  uint64_t getSamples() const { return NumSamples; }
253  const CallTargetMap &getCallTargets() const { return CallTargets; }
255  return SortCallTargets(CallTargets);
256  }
257 
258  /// Sort call targets in descending order of call frequency.
259  static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets) {
260  SortedCallTargetSet SortedTargets;
261  for (const auto &I : Targets) {
262  SortedTargets.emplace(I.first(), I.second);
263  }
264  return SortedTargets;
265  }
266 
267  /// Merge the samples in \p Other into this record.
268  /// Optionally scale sample counts by \p Weight.
269  sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1) {
270  sampleprof_error Result = addSamples(Other.getSamples(), Weight);
271  for (const auto &I : Other.getCallTargets()) {
272  MergeResult(Result, addCalledTarget(I.first(), I.second, Weight));
273  }
274  return Result;
275  }
276 
277  void print(raw_ostream &OS, unsigned Indent) const;
278  void dump() const;
279 
280 private:
281  uint64_t NumSamples = 0;
282  CallTargetMap CallTargets;
283 };
284 
285 raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
286 
287 class FunctionSamples;
288 
289 using BodySampleMap = std::map<LineLocation, SampleRecord>;
290 // NOTE: Using a StringMap here makes parsed profiles consume around 17% more
291 // memory, which is *very* significant for large profiles.
292 using FunctionSamplesMap = std::map<std::string, FunctionSamples, std::less<>>;
293 using CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>;
294 
295 /// Representation of the samples collected for a function.
296 ///
297 /// This data structure contains all the collected samples for the body
298 /// of a function. Each sample corresponds to a LineLocation instance
299 /// within the body of the function.
301 public:
302  FunctionSamples() = default;
303 
304  void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
305  void dump() const;
306 
307  sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight = 1) {
308  bool Overflowed;
309  TotalSamples =
310  SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed);
311  return Overflowed ? sampleprof_error::counter_overflow
313  }
314 
315  sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight = 1) {
316  bool Overflowed;
317  TotalHeadSamples =
318  SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed);
319  return Overflowed ? sampleprof_error::counter_overflow
321  }
322 
324  uint64_t Num, uint64_t Weight = 1) {
325  return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(
326  Num, Weight);
327  }
328 
330  uint32_t Discriminator,
331  StringRef FName, uint64_t Num,
332  uint64_t Weight = 1) {
333  return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(
334  FName, Num, Weight);
335  }
336 
337  /// Return the number of samples collected at the given location.
338  /// Each location is specified by \p LineOffset and \p Discriminator.
339  /// If the location is not found in profile, return error.
341  uint32_t Discriminator) const {
342  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
343  if (ret == BodySamples.end())
344  return std::error_code();
345  else
346  return ret->second.getSamples();
347  }
348 
349  /// Returns the call target map collected at a given location.
350  /// Each location is specified by \p LineOffset and \p Discriminator.
351  /// If the location is not found in profile, return error.
353  findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const {
354  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
355  if (ret == BodySamples.end())
356  return std::error_code();
357  return ret->second.getCallTargets();
358  }
359 
360  /// Return the function samples at the given callsite location.
362  return CallsiteSamples[Loc];
363  }
364 
365  /// Returns the FunctionSamplesMap at the given \p Loc.
366  const FunctionSamplesMap *
368  auto iter = CallsiteSamples.find(Loc);
369  if (iter == CallsiteSamples.end())
370  return nullptr;
371  return &iter->second;
372  }
373 
374  /// Returns a pointer to FunctionSamples at the given callsite location \p Loc
375  /// with callee \p CalleeName. If no callsite can be found, relax the
376  /// restriction to return the FunctionSamples at callsite location \p Loc
377  /// with the maximum total sample count.
379  StringRef CalleeName) const {
380  std::string CalleeGUID;
381  CalleeName = getRepInFormat(CalleeName, Format, CalleeGUID);
382 
383  auto iter = CallsiteSamples.find(Loc);
384  if (iter == CallsiteSamples.end())
385  return nullptr;
386  auto FS = iter->second.find(CalleeName);
387  if (FS != iter->second.end())
388  return &FS->second;
389  // If we cannot find exact match of the callee name, return the FS with
390  // the max total count.
391  uint64_t MaxTotalSamples = 0;
392  const FunctionSamples *R = nullptr;
393  for (const auto &NameFS : iter->second)
394  if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
395  MaxTotalSamples = NameFS.second.getTotalSamples();
396  R = &NameFS.second;
397  }
398  return R;
399  }
400 
401  bool empty() const { return TotalSamples == 0; }
402 
403  /// Return the total number of samples collected inside the function.
404  uint64_t getTotalSamples() const { return TotalSamples; }
405 
406  /// Return the total number of branch samples that have the function as the
407  /// branch target. This should be equivalent to the sample of the first
408  /// instruction of the symbol. But as we directly get this info for raw
409  /// profile without referring to potentially inaccurate debug info, this
410  /// gives more accurate profile data and is preferred for standalone symbols.
411  uint64_t getHeadSamples() const { return TotalHeadSamples; }
412 
413  /// Return the sample count of the first instruction of the function.
414  /// The function can be either a standalone symbol or an inlined function.
415  uint64_t getEntrySamples() const {
416  // Use either BodySamples or CallsiteSamples which ever has the smaller
417  // lineno.
418  if (!BodySamples.empty() &&
419  (CallsiteSamples.empty() ||
420  BodySamples.begin()->first < CallsiteSamples.begin()->first))
421  return BodySamples.begin()->second.getSamples();
422  if (!CallsiteSamples.empty()) {
423  uint64_t T = 0;
424  // An indirect callsite may be promoted to several inlined direct calls.
425  // We need to get the sum of them.
426  for (const auto &N_FS : CallsiteSamples.begin()->second)
427  T += N_FS.second.getEntrySamples();
428  return T;
429  }
430  return 0;
431  }
432 
433  /// Return all the samples collected in the body of the function.
434  const BodySampleMap &getBodySamples() const { return BodySamples; }
435 
436  /// Return all the callsite samples collected in the body of the function.
438  return CallsiteSamples;
439  }
440 
441  /// Merge the samples in \p Other into this one.
442  /// Optionally scale samples by \p Weight.
443  sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) {
445  Name = Other.getName();
446  MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight));
447  MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight));
448  for (const auto &I : Other.getBodySamples()) {
449  const LineLocation &Loc = I.first;
450  const SampleRecord &Rec = I.second;
451  MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
452  }
453  for (const auto &I : Other.getCallsiteSamples()) {
454  const LineLocation &Loc = I.first;
455  FunctionSamplesMap &FSMap = functionSamplesAt(Loc);
456  for (const auto &Rec : I.second)
457  MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight));
458  }
459  return Result;
460  }
461 
462  /// Recursively traverses all children, if the total sample count of the
463  /// corresponding function is no less than \p Threshold, add its corresponding
464  /// GUID to \p S. Also traverse the BodySamples to add hot CallTarget's GUID
465  /// to \p S.
467  uint64_t Threshold) const {
468  if (TotalSamples <= Threshold)
469  return;
470  S.insert(getGUID(Name));
471  // Import hot CallTargets, which may not be available in IR because full
472  // profile annotation cannot be done until backend compilation in ThinLTO.
473  for (const auto &BS : BodySamples)
474  for (const auto &TS : BS.second.getCallTargets())
475  if (TS.getValue() > Threshold) {
476  const Function *Callee =
477  M->getFunction(getNameInModule(TS.getKey(), M));
478  if (!Callee || !Callee->getSubprogram())
479  S.insert(getGUID(TS.getKey()));
480  }
481  for (const auto &CS : CallsiteSamples)
482  for (const auto &NameFS : CS.second)
483  NameFS.second.findInlinedFunctions(S, M, Threshold);
484  }
485 
486  /// Set the name of the function.
487  void setName(StringRef FunctionName) { Name = FunctionName; }
488 
489  /// Return the function name.
490  StringRef getName() const { return Name; }
491 
492  /// Return the original function name if it exists in Module \p M.
494  return getNameInModule(Name, M);
495  }
496 
497  /// Return the canonical name for a function, taking into account
498  /// suffix elision policy attributes.
500  static const char *knownSuffixes[] = { ".llvm.", ".part." };
501  auto AttrName = "sample-profile-suffix-elision-policy";
502  auto Attr = F.getFnAttribute(AttrName).getValueAsString();
503  if (Attr == "" || Attr == "all") {
504  return F.getName().split('.').first;
505  } else if (Attr == "selected") {
506  StringRef Cand(F.getName());
507  for (const auto &Suf : knownSuffixes) {
508  StringRef Suffix(Suf);
509  auto It = Cand.rfind(Suffix);
510  if (It == StringRef::npos)
511  return Cand;
512  auto Dit = Cand.rfind('.');
513  if (Dit == It + Suffix.size() - 1)
514  Cand = Cand.substr(0, It);
515  }
516  return Cand;
517  } else if (Attr == "none") {
518  return F.getName();
519  } else {
520  assert(false && "internal error: unknown suffix elision policy");
521  }
522  return F.getName();
523  }
524 
525  /// Translate \p Name into its original name in Module.
526  /// When the Format is not SPF_Compact_Binary, \p Name needs no translation.
527  /// When the Format is SPF_Compact_Binary, \p Name in current FunctionSamples
528  /// is actually GUID of the original function name. getNameInModule will
529  /// translate \p Name in current FunctionSamples into its original name.
530  /// If the original name doesn't exist in \p M, return empty StringRef.
532  if (Format != SPF_Compact_Binary)
533  return Name;
534 
535  assert(GUIDToFuncNameMap && "GUIDToFuncNameMap needs to be popluated first");
536  auto iter = GUIDToFuncNameMap->find(std::stoull(Name.data()));
537  if (iter == GUIDToFuncNameMap->end())
538  return StringRef();
539  return iter->second;
540  }
541 
542  /// Returns the line offset to the start line of the subprogram.
543  /// We assume that a single function will not exceed 65535 LOC.
544  static unsigned getOffset(const DILocation *DIL);
545 
546  /// Get the FunctionSamples of the inline instance where DIL originates
547  /// from.
548  ///
549  /// The FunctionSamples of the instruction (Machine or IR) associated to
550  /// \p DIL is the inlined instance in which that instruction is coming from.
551  /// We traverse the inline stack of that instruction, and match it with the
552  /// tree nodes in the profile.
553  ///
554  /// \returns the FunctionSamples pointer to the inlined instance.
555  const FunctionSamples *findFunctionSamples(const DILocation *DIL) const;
556 
558 
559  /// GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for
560  /// all the function symbols defined or declared in current module.
561  DenseMap<uint64_t, StringRef> *GUIDToFuncNameMap = nullptr;
562 
563  // Assume the input \p Name is a name coming from FunctionSamples itself.
564  // If the format is SPF_Compact_Binary, the name is already a GUID and we
565  // don't want to return the GUID of GUID.
566  static uint64_t getGUID(StringRef Name) {
567  return (Format == SPF_Compact_Binary) ? std::stoull(Name.data())
568  : Function::getGUID(Name);
569  }
570 
571 private:
572  /// Mangled name of the function.
573  StringRef Name;
574 
575  /// Total number of samples collected inside this function.
576  ///
577  /// Samples are cumulative, they include all the samples collected
578  /// inside this function and all its inlined callees.
579  uint64_t TotalSamples = 0;
580 
581  /// Total number of samples collected at the head of the function.
582  /// This is an approximation of the number of calls made to this function
583  /// at runtime.
584  uint64_t TotalHeadSamples = 0;
585 
586  /// Map instruction locations to collected samples.
587  ///
588  /// Each entry in this map contains the number of samples
589  /// collected at the corresponding line offset. All line locations
590  /// are an offset from the start of the function.
591  BodySampleMap BodySamples;
592 
593  /// Map call sites to collected samples for the called function.
594  ///
595  /// Each entry in this map corresponds to all the samples
596  /// collected for the inlined function call at the given
597  /// location. For example, given:
598  ///
599  /// void foo() {
600  /// 1 bar();
601  /// ...
602  /// 8 baz();
603  /// }
604  ///
605  /// If the bar() and baz() calls were inlined inside foo(), this
606  /// map will contain two entries. One for all the samples collected
607  /// in the call to bar() at line offset 1, the other for all the samples
608  /// collected in the call to baz() at line offset 8.
609  CallsiteSampleMap CallsiteSamples;
610 };
611 
613 
614 /// Sort a LocationT->SampleT map by LocationT.
615 ///
616 /// It produces a sorted list of <LocationT, SampleT> records by ascending
617 /// order of LocationT.
618 template <class LocationT, class SampleT> class SampleSorter {
619 public:
620  using SamplesWithLoc = std::pair<const LocationT, SampleT>;
622 
623  SampleSorter(const std::map<LocationT, SampleT> &Samples) {
624  for (const auto &I : Samples)
625  V.push_back(&I);
626  llvm::stable_sort(V, [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
627  return A->first < B->first;
628  });
629  }
630 
631  const SamplesWithLocList &get() const { return V; }
632 
633 private:
635 };
636 
637 /// ProfileSymbolList records the list of function symbols shown up
638 /// in the binary used to generate the profile. It is useful to
639 /// to discriminate a function being so cold as not to shown up
640 /// in the profile and a function newly added.
642 public:
643  /// copy indicates whether we need to copy the underlying memory
644  /// for the input Name.
645  void add(StringRef Name, bool copy = false) {
646  if (!copy) {
647  Syms.insert(Name);
648  return;
649  }
650  Syms.insert(Name.copy(Allocator));
651  }
652 
653  bool contains(StringRef Name) { return Syms.count(Name); }
654 
655  void merge(const ProfileSymbolList &List) {
656  for (auto Sym : List.Syms)
657  add(Sym, true);
658  }
659 
660  unsigned size() { return Syms.size(); }
661 
662  void setToCompress(bool TC) { ToCompress = TC; }
663  bool toCompress() { return ToCompress; }
664 
665  std::error_code read(const uint8_t *Data, uint64_t ListSize);
666  std::error_code write(raw_ostream &OS);
667  void dump(raw_ostream &OS = dbgs()) const;
668 
669 private:
670  // Determine whether or not to compress the symbol list when
671  // writing it into profile. The variable is unused when the symbol
672  // list is read from an existing profile.
673  bool ToCompress = false;
674  DenseSet<StringRef> Syms;
676 };
677 
678 } // end namespace sampleprof
679 } // end namespace llvm
680 
681 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H
const FunctionSamplesMap * findFunctionSamplesMapAt(const LineLocation &Loc) const
Returns the FunctionSamplesMap at the given Loc.
Definition: SampleProf.h:367
static uint64_t getGUID(StringRef Name)
Definition: SampleProf.h:566
Represents either an error or a value T.
Definition: ErrorOr.h:56
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static StringRef getRepInFormat(StringRef Name, SampleProfileFormat Format, std::string &GUIDBuf)
Definition: SampleProf.h:104
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight=1)
Merge the samples in Other into this record.
Definition: SampleProf.h:269
bool contains(StringRef Name)
Definition: SampleProf.h:653
LineLocation(uint32_t L, uint32_t D)
Definition: SampleProf.h:180
SampleSorter(const std::map< LocationT, SampleT > &Samples)
Definition: SampleProf.h:623
static void removeSecFlags(SecHdrTableEntry &Entry, uint64_t Flags)
Definition: SampleProf.h:162
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
Definition: SampleProf.h:340
static void addSecFlags(SecHdrTableEntry &Entry, uint64_t Flags)
Definition: SampleProf.h:158
F(f)
std::pair< const LocationT, SampleT > SamplesWithLoc
Definition: SampleProf.h:620
void merge(const ProfileSymbolList &List)
Definition: SampleProf.h:655
void findInlinedFunctions(DenseSet< GlobalValue::GUID > &S, const Module *M, uint64_t Threshold) const
Recursively traverses all children, if the total sample count of the corresponding function is no les...
Definition: SampleProf.h:466
bool operator<(const LineLocation &O) const
Definition: SampleProf.h:185
static SampleProfileFormat Format
Definition: SampleProf.h:557
Representation of the samples collected for a function.
Definition: SampleProf.h:300
static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets)
Sort call targets in descending order of call frequency.
Definition: SampleProf.h:259
Definition: BitVector.h:937
static std::string getSecName(SecType Type)
Definition: SampleProf.h:129
LLVM_NODISCARD StringRef copy(Allocator &A) const
Definition: StringRef.h:162
static bool hasSecFlag(SecHdrTableEntry &Entry, SecFlags Flag)
Definition: SampleProf.h:166
std::error_code make_error_code(BitcodeError E)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
Definition: SampleProf.h:239
Representation of a single sample record.
Definition: SampleProf.h:206
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
void setName(StringRef FunctionName)
Set the name of the function.
Definition: SampleProf.h:487
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
std::map< LineLocation, SampleRecord > BodySampleMap
Definition: SampleProf.h:289
Debug location.
static uint64_t SPVersion()
Definition: SampleProf.h:113
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:618
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Definition: SampleProf.h:411
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:131
uint64_t getSamples() const
Definition: SampleProf.h:252
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
sampleprof_error
Definition: SampleProf.h:43
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1522
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:434
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:323
sampleprof_error addSamples(uint64_t S, uint64_t Weight=1)
Increment the number of samples for this record by S.
Definition: SampleProf.h:227
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes...
Definition: SampleProf.h:499
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static void write(bool isBE, void *P, T V)
std::map< std::string, FunctionSamples, std::less<> > FunctionSamplesMap
Definition: SampleProf.h:292
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StringRef getFuncNameInModule(const Module *M) const
Return the original function name if it exists in Module M.
Definition: SampleProf.h:493
std::enable_if< std::is_unsigned< T >::value, T >::type 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:851
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName) const
Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName...
Definition: SampleProf.h:378
Basic Register Allocator
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:307
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Definition: FileCheck.cpp:215
Module.h This file contains the declarations for the Module class.
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:710
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
Definition: SampleProf.h:361
ErrorOr< SampleRecord::CallTargetMap > findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const
Returns the call target map collected at a given location.
Definition: SampleProf.h:353
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:86
amdgpu Simplify well known AMD library false FunctionCallee Callee
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:510
bool hasCalls() const
Return true if this sample record contains function calls.
Definition: SampleProf.h:250
std::map< LineLocation, FunctionSamplesMap > CallsiteSampleMap
Definition: SampleProf.h:293
ProfileSymbolList records the list of function symbols shown up in the binary used to generate the pr...
Definition: SampleProf.h:641
static const size_t npos
Definition: StringRef.h:50
Represents the relative location of an instruction.
Definition: SampleProf.h:179
StringRef getNameInModule(StringRef Name, const Module *M) const
Translate Name into its original name in Module.
Definition: SampleProf.h:531
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:220
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
const NodeList & List
Definition: RDFGraph.cpp:201
#define I(x, y, z)
Definition: MD5.cpp:58
StringRef getName() const
Return the function name.
Definition: SampleProf.h:490
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:253
std::set< CallTarget, CallTargetComparator > SortedCallTargetSet
Definition: SampleProf.h:218
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2047
static GlobalValue::GUID getGUID(GlobalValue::GUID G)
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:96
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:315
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
Provides ErrorOr<T> smart pointer.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
std::pair< StringRef, uint64_t > CallTarget
Definition: SampleProf.h:208
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void stable_sort(R &&Range)
Definition: STLExtras.h:1289
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:404
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Definition: SampleProf.h:437
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:333
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
uint64_t getEntrySamples() const
Return the sample count of the first instruction of the function.
Definition: SampleProf.h:415
const SortedCallTargetSet getSortedCallTargets() const
Definition: SampleProf.h:254
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
bool operator()(const CallTarget &LHS, const CallTarget &RHS) const
Definition: SampleProf.h:210
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1217
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
Definition: SampleProf.h:443
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:329
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
Definition: SampleProf.h:65
void add(StringRef Name, bool copy=false)
copy indicates whether we need to copy the underlying memory for the input Name.
Definition: SampleProf.h:645