SampleProfileLoaderBaseUtil.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/Analysis/ProfileSummaryInfo.h"
  23. #include "llvm/IR/BasicBlock.h"
  24. #include "llvm/IR/CFG.h"
  25. #include "llvm/IR/Constants.h"
  26. #include "llvm/IR/DebugLoc.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/ProfileData/SampleProf.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Transforms/Utils/ModuleUtils.h"
  31. namespace llvm {
  32. using namespace sampleprof;
  33. class ProfileSummaryInfo;
  34. extern cl::opt<unsigned> SampleProfileMaxPropagateIterations;
  35. extern cl::opt<unsigned> SampleProfileRecordCoverage;
  36. extern cl::opt<unsigned> SampleProfileSampleCoverage;
  37. extern cl::opt<bool> NoWarnSampleUnused;
  38. namespace sampleprofutil {
  39. class SampleCoverageTracker {
  40. public:
  41. bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset,
  42. uint32_t Discriminator, uint64_t Samples);
  43. unsigned computeCoverage(unsigned Used, unsigned Total) const;
  44. unsigned countUsedRecords(const FunctionSamples *FS,
  45. ProfileSummaryInfo *PSI) const;
  46. unsigned countBodyRecords(const FunctionSamples *FS,
  47. ProfileSummaryInfo *PSI) const;
  48. uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
  49. uint64_t countBodySamples(const FunctionSamples *FS,
  50. ProfileSummaryInfo *PSI) const;
  51. void clear() {
  52. SampleCoverage.clear();
  53. TotalUsedSamples = 0;
  54. }
  55. void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
  56. private:
  57. using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
  58. using FunctionSamplesCoverageMap =
  59. DenseMap<const FunctionSamples *, BodySampleCoverageMap>;
  60. /// Coverage map for sampling records.
  61. ///
  62. /// This map keeps a record of sampling records that have been matched to
  63. /// an IR instruction. This is used to detect some form of staleness in
  64. /// profiles (see flag -sample-profile-check-coverage).
  65. ///
  66. /// Each entry in the map corresponds to a FunctionSamples instance. This is
  67. /// another map that counts how many times the sample record at the
  68. /// given location has been used.
  69. FunctionSamplesCoverageMap SampleCoverage;
  70. /// Number of samples used from the profile.
  71. ///
  72. /// When a sampling record is used for the first time, the samples from
  73. /// that record are added to this accumulator. Coverage is later computed
  74. /// based on the total number of samples available in this function and
  75. /// its callsites.
  76. ///
  77. /// Note that this accumulator tracks samples used from a single function
  78. /// and all the inlined callsites. Strictly, we should have a map of counters
  79. /// keyed by FunctionSamples pointers, but these stats are cleared after
  80. /// every function, so we just need to keep a single counter.
  81. uint64_t TotalUsedSamples = 0;
  82. // For symbol in profile symbol list, whether to regard their profiles
  83. // to be accurate. This is passed from the SampleLoader instance.
  84. bool ProfAccForSymsInList = false;
  85. };
  86. /// Return true if the given callsite is hot wrt to hot cutoff threshold.
  87. bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
  88. bool ProfAccForSymsInList);
  89. /// Create a global variable to flag FSDiscriminators are used.
  90. void createFSDiscriminatorVariable(Module *M);
  91. } // end of namespace sampleprofutil
  92. } // end of namespace llvm
  93. #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
  94. #ifdef __GNUC__
  95. #pragma GCC diagnostic pop
  96. #endif