Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:44

0001 ////===- SampleProfileLoadBaseUtil.h - Profile loader util func --*- C++-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// This file provides the utility functions for the sampled PGO loader base
0011 /// implementation.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
0016 #define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
0017 
0018 #include "llvm/ADT/DenseMap.h"
0019 #include "llvm/ProfileData/SampleProf.h"
0020 #include "llvm/Support/CommandLine.h"
0021 
0022 namespace llvm {
0023 using namespace sampleprof;
0024 
0025 class ProfileSummaryInfo;
0026 class Module;
0027 
0028 extern cl::opt<unsigned> SampleProfileMaxPropagateIterations;
0029 extern cl::opt<unsigned> SampleProfileRecordCoverage;
0030 extern cl::opt<unsigned> SampleProfileSampleCoverage;
0031 extern cl::opt<bool> NoWarnSampleUnused;
0032 
0033 namespace sampleprofutil {
0034 
0035 class SampleCoverageTracker {
0036 public:
0037   bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset,
0038                        uint32_t Discriminator, uint64_t Samples);
0039   unsigned computeCoverage(unsigned Used, unsigned Total) const;
0040   unsigned countUsedRecords(const FunctionSamples *FS,
0041                             ProfileSummaryInfo *PSI) const;
0042   unsigned countBodyRecords(const FunctionSamples *FS,
0043                             ProfileSummaryInfo *PSI) const;
0044   uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
0045   uint64_t countBodySamples(const FunctionSamples *FS,
0046                             ProfileSummaryInfo *PSI) const;
0047 
0048   void clear() {
0049     SampleCoverage.clear();
0050     TotalUsedSamples = 0;
0051   }
0052   void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
0053 
0054 private:
0055   using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
0056   using FunctionSamplesCoverageMap =
0057       DenseMap<const FunctionSamples *, BodySampleCoverageMap>;
0058 
0059   /// Coverage map for sampling records.
0060   ///
0061   /// This map keeps a record of sampling records that have been matched to
0062   /// an IR instruction. This is used to detect some form of staleness in
0063   /// profiles (see flag -sample-profile-check-coverage).
0064   ///
0065   /// Each entry in the map corresponds to a FunctionSamples instance.  This is
0066   /// another map that counts how many times the sample record at the
0067   /// given location has been used.
0068   FunctionSamplesCoverageMap SampleCoverage;
0069 
0070   /// Number of samples used from the profile.
0071   ///
0072   /// When a sampling record is used for the first time, the samples from
0073   /// that record are added to this accumulator.  Coverage is later computed
0074   /// based on the total number of samples available in this function and
0075   /// its callsites.
0076   ///
0077   /// Note that this accumulator tracks samples used from a single function
0078   /// and all the inlined callsites. Strictly, we should have a map of counters
0079   /// keyed by FunctionSamples pointers, but these stats are cleared after
0080   /// every function, so we just need to keep a single counter.
0081   uint64_t TotalUsedSamples = 0;
0082 
0083   // For symbol in profile symbol list, whether to regard their profiles
0084   // to be accurate. This is passed from the SampleLoader instance.
0085   bool ProfAccForSymsInList = false;
0086 };
0087 
0088 /// Return true if the given callsite is hot wrt to hot cutoff threshold.
0089 bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
0090                    bool ProfAccForSymsInList);
0091 
0092 /// Create a global variable to flag FSDiscriminators are used.
0093 void createFSDiscriminatorVariable(Module *M);
0094 
0095 } // end of namespace sampleprofutil
0096 } // end of namespace llvm
0097 
0098 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H