Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- ProfileCommon.h - Common profiling APIs. -----------------*- 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 // This file contains data structures and functions common to both instrumented
0010 // and sample profiling.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_PROFILEDATA_PROFILECOMMON_H
0015 #define LLVM_PROFILEDATA_PROFILECOMMON_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/IR/ProfileSummary.h"
0019 #include "llvm/ProfileData/InstrProf.h"
0020 #include "llvm/ProfileData/SampleProf.h"
0021 #include "llvm/Support/Error.h"
0022 #include <algorithm>
0023 #include <cstdint>
0024 #include <functional>
0025 #include <map>
0026 #include <memory>
0027 #include <vector>
0028 
0029 namespace llvm {
0030 
0031 extern cl::opt<bool> UseContextLessSummary;
0032 extern cl::opt<int> ProfileSummaryCutoffHot;
0033 extern cl::opt<int> ProfileSummaryCutoffCold;
0034 extern cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold;
0035 extern cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold;
0036 extern cl::opt<uint64_t> ProfileSummaryHotCount;
0037 extern cl::opt<uint64_t> ProfileSummaryColdCount;
0038 
0039 namespace sampleprof {
0040 
0041 class FunctionSamples;
0042 
0043 } // end namespace sampleprof
0044 
0045 class ProfileSummaryBuilder {
0046 private:
0047   /// We keep track of the number of times a count (block count or samples)
0048   /// appears in the profile. The map is kept sorted in the descending order of
0049   /// counts.
0050   std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
0051   std::vector<uint32_t> DetailedSummaryCutoffs;
0052 
0053 protected:
0054   SummaryEntryVector DetailedSummary;
0055   uint64_t TotalCount = 0;
0056   uint64_t MaxCount = 0;
0057   uint64_t MaxFunctionCount = 0;
0058   uint32_t NumCounts = 0;
0059   uint32_t NumFunctions = 0;
0060 
0061   ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
0062       : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
0063   ~ProfileSummaryBuilder() = default;
0064 
0065   inline void addCount(uint64_t Count);
0066   void computeDetailedSummary();
0067 
0068 public:
0069   /// A vector of useful cutoff values for detailed summary.
0070   static const ArrayRef<uint32_t> DefaultCutoffs;
0071 
0072   /// Find the summary entry for a desired percentile of counts.
0073   static const ProfileSummaryEntry &
0074   getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile);
0075   static uint64_t getHotCountThreshold(const SummaryEntryVector &DS);
0076   static uint64_t getColdCountThreshold(const SummaryEntryVector &DS);
0077 };
0078 
0079 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
0080   uint64_t MaxInternalBlockCount = 0;
0081 
0082 public:
0083   InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
0084       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
0085 
0086   void addEntryCount(uint64_t Count);
0087   void addInternalCount(uint64_t Count);
0088 
0089   void addRecord(const InstrProfRecord &);
0090   std::unique_ptr<ProfileSummary> getSummary();
0091 };
0092 
0093 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
0094 public:
0095   SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
0096       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
0097 
0098   void addRecord(const sampleprof::FunctionSamples &FS,
0099                  bool isCallsiteSample = false);
0100   std::unique_ptr<ProfileSummary>
0101   computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles);
0102   std::unique_ptr<ProfileSummary> getSummary();
0103 };
0104 
0105 /// This is called when a count is seen in the profile.
0106 void ProfileSummaryBuilder::addCount(uint64_t Count) {
0107   TotalCount += Count;
0108   if (Count > MaxCount)
0109     MaxCount = Count;
0110   NumCounts++;
0111   CountFrequencies[Count]++;
0112 }
0113 
0114 } // end namespace llvm
0115 
0116 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H