1 ////===- SampleProfileLoadBaseUtil.h - Profile loader util func --*- 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 /// \file
10 /// This file provides the utility functions for the sampled PGO loader base
11 /// implementation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
16 #define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
17 
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/CommandLine.h"
21 
22 namespace llvm {
23 using namespace sampleprof;
24 
25 class ProfileSummaryInfo;
26 class Module;
27 
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 
33 namespace sampleprofutil {
34 
35 class SampleCoverageTracker {
36 public:
37   bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset,
38                        uint32_t Discriminator, uint64_t Samples);
39   unsigned computeCoverage(unsigned Used, unsigned Total) const;
40   unsigned countUsedRecords(const FunctionSamples *FS,
41                             ProfileSummaryInfo *PSI) const;
42   unsigned countBodyRecords(const FunctionSamples *FS,
43                             ProfileSummaryInfo *PSI) const;
44   uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
45   uint64_t countBodySamples(const FunctionSamples *FS,
46                             ProfileSummaryInfo *PSI) const;
47 
48   void clear() {
49     SampleCoverage.clear();
50     TotalUsedSamples = 0;
51   }
52   void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
53 
54 private:
55   using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
56   using FunctionSamplesCoverageMap =
57       DenseMap<const FunctionSamples *, BodySampleCoverageMap>;
58 
59   /// Coverage map for sampling records.
60   ///
61   /// This map keeps a record of sampling records that have been matched to
62   /// an IR instruction. This is used to detect some form of staleness in
63   /// profiles (see flag -sample-profile-check-coverage).
64   ///
65   /// Each entry in the map corresponds to a FunctionSamples instance.  This is
66   /// another map that counts how many times the sample record at the
67   /// given location has been used.
68   FunctionSamplesCoverageMap SampleCoverage;
69 
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 
83   // For symbol in profile symbol list, whether to regard their profiles
84   // to be accurate. This is passed from the SampleLoader instance.
85   bool ProfAccForSymsInList = false;
86 };
87 
88 /// Return true if the given callsite is hot wrt to hot cutoff threshold.
89 bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
90                    bool ProfAccForSymsInList);
91 
92 /// Create a global variable to flag FSDiscriminators are used.
93 void createFSDiscriminatorVariable(Module *M);
94 
95 } // end of namespace sampleprofutil
96 } // end of namespace llvm
97 
98 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
99