1 ////===- SampleProfileLoadBaseUtil.h - Profile loader util func --*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file provides the utility functions for the sampled PGO loader base
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
16 #define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/Analysis/ProfileSummaryInfo.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CFG.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DebugLoc.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/ProfileData/SampleProf.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Transforms/Utils/ModuleUtils.h"
30 using namespace sampleprof;
32 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;
39 namespace sampleprofutil {
41 class SampleCoverageTracker {
43 bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset,
44 uint32_t Discriminator, uint64_t Samples);
45 unsigned computeCoverage(unsigned Used, unsigned Total) const;
46 unsigned countUsedRecords(const FunctionSamples *FS,
47 ProfileSummaryInfo *PSI) const;
48 unsigned countBodyRecords(const FunctionSamples *FS,
49 ProfileSummaryInfo *PSI) const;
50 uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
51 uint64_t countBodySamples(const FunctionSamples *FS,
52 ProfileSummaryInfo *PSI) const;
55 SampleCoverage.clear();
58 void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
61 using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
62 using FunctionSamplesCoverageMap =
63 DenseMap<const FunctionSamples *, BodySampleCoverageMap>;
65 /// Coverage map for sampling records.
67 /// This map keeps a record of sampling records that have been matched to
68 /// an IR instruction. This is used to detect some form of staleness in
69 /// profiles (see flag -sample-profile-check-coverage).
71 /// Each entry in the map corresponds to a FunctionSamples instance. This is
72 /// another map that counts how many times the sample record at the
73 /// given location has been used.
74 FunctionSamplesCoverageMap SampleCoverage;
76 /// Number of samples used from the profile.
78 /// When a sampling record is used for the first time, the samples from
79 /// that record are added to this accumulator. Coverage is later computed
80 /// based on the total number of samples available in this function and
83 /// Note that this accumulator tracks samples used from a single function
84 /// and all the inlined callsites. Strictly, we should have a map of counters
85 /// keyed by FunctionSamples pointers, but these stats are cleared after
86 /// every function, so we just need to keep a single counter.
87 uint64_t TotalUsedSamples = 0;
89 // For symbol in profile symbol list, whether to regard their profiles
90 // to be accurate. This is passed from the SampleLoader instance.
91 bool ProfAccForSymsInList = false;
94 /// Return true if the given callsite is hot wrt to hot cutoff threshold.
95 bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
96 bool ProfAccForSymsInList);
98 /// Create a global variable to flag FSDiscriminators are used.
99 void createFSDiscriminatorVariable(Module *M);
101 } // end of namespace sampleprofutil
102 } // end of namespace llvm
104 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H