SampleProfileLoaderBaseUtil.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. ////===- SampleProfileLoadBaseUtil.h - Profile loader util func --*- C++-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// This file provides the utility functions for the sampled PGO loader base
  16. /// implementation.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
  20. #define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ProfileData/SampleProf.h"
  23. #include "llvm/Support/CommandLine.h"
  24. namespace llvm {
  25. using namespace sampleprof;
  26. class ProfileSummaryInfo;
  27. class Module;
  28. extern cl::opt<unsigned> SampleProfileMaxPropagateIterations;
  29. extern cl::opt<unsigned> SampleProfileRecordCoverage;
  30. extern cl::opt<unsigned> SampleProfileSampleCoverage;
  31. extern cl::opt<bool> NoWarnSampleUnused;
  32. namespace sampleprofutil {
  33. class SampleCoverageTracker {
  34. public:
  35. bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset,
  36. uint32_t Discriminator, uint64_t Samples);
  37. unsigned computeCoverage(unsigned Used, unsigned Total) const;
  38. unsigned countUsedRecords(const FunctionSamples *FS,
  39. ProfileSummaryInfo *PSI) const;
  40. unsigned countBodyRecords(const FunctionSamples *FS,
  41. ProfileSummaryInfo *PSI) const;
  42. uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
  43. uint64_t countBodySamples(const FunctionSamples *FS,
  44. ProfileSummaryInfo *PSI) const;
  45. void clear() {
  46. SampleCoverage.clear();
  47. TotalUsedSamples = 0;
  48. }
  49. void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
  50. private:
  51. using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
  52. using FunctionSamplesCoverageMap =
  53. DenseMap<const FunctionSamples *, BodySampleCoverageMap>;
  54. /// Coverage map for sampling records.
  55. ///
  56. /// This map keeps a record of sampling records that have been matched to
  57. /// an IR instruction. This is used to detect some form of staleness in
  58. /// profiles (see flag -sample-profile-check-coverage).
  59. ///
  60. /// Each entry in the map corresponds to a FunctionSamples instance. This is
  61. /// another map that counts how many times the sample record at the
  62. /// given location has been used.
  63. FunctionSamplesCoverageMap SampleCoverage;
  64. /// Number of samples used from the profile.
  65. ///
  66. /// When a sampling record is used for the first time, the samples from
  67. /// that record are added to this accumulator. Coverage is later computed
  68. /// based on the total number of samples available in this function and
  69. /// its callsites.
  70. ///
  71. /// Note that this accumulator tracks samples used from a single function
  72. /// and all the inlined callsites. Strictly, we should have a map of counters
  73. /// keyed by FunctionSamples pointers, but these stats are cleared after
  74. /// every function, so we just need to keep a single counter.
  75. uint64_t TotalUsedSamples = 0;
  76. // For symbol in profile symbol list, whether to regard their profiles
  77. // to be accurate. This is passed from the SampleLoader instance.
  78. bool ProfAccForSymsInList = false;
  79. };
  80. /// Return true if the given callsite is hot wrt to hot cutoff threshold.
  81. bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
  82. bool ProfAccForSymsInList);
  83. /// Create a global variable to flag FSDiscriminators are used.
  84. void createFSDiscriminatorVariable(Module *M);
  85. } // end of namespace sampleprofutil
  86. } // end of namespace llvm
  87. #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif