Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- ProfileSummary.h - Profile summary data structure. -------*- 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 defines the profile summary data structure.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_PROFILESUMMARY_H
0014 #define LLVM_IR_PROFILESUMMARY_H
0015 
0016 #include <algorithm>
0017 #include <cassert>
0018 #include <cstdint>
0019 #include <vector>
0020 
0021 namespace llvm {
0022 
0023 class LLVMContext;
0024 class Metadata;
0025 class raw_ostream;
0026 
0027 // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.
0028 // The semantics of counts depend on the type of profile. For instrumentation
0029 // profile, counts are block counts and for sample profile, counts are
0030 // per-line samples. Given a target counts percentile, we compute the minimum
0031 // number of counts needed to reach this target and the minimum among these
0032 // counts.
0033 struct ProfileSummaryEntry {
0034   const uint32_t Cutoff;    ///< The required percentile of counts.
0035   const uint64_t MinCount;  ///< The minimum count for this percentile.
0036   const uint64_t NumCounts; ///< Number of counts >= the minimum count.
0037 
0038   ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
0039                       uint64_t TheNumCounts)
0040       : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
0041 };
0042 
0043 using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
0044 
0045 class ProfileSummary {
0046 public:
0047   enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
0048 
0049 private:
0050   const Kind PSK;
0051   const SummaryEntryVector DetailedSummary;
0052   const uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
0053   const uint32_t NumCounts, NumFunctions;
0054   /// If 'Partial' is false, it means the profile being used to optimize
0055   /// a target is collected from the same target.
0056   /// If 'Partial' is true, it means the profile is for common/shared
0057   /// code. The common profile is usually merged from profiles collected
0058   /// from running other targets.
0059   bool Partial = false;
0060   /// This approximately represents the ratio of the number of profile counters
0061   /// of the program being built to the number of profile counters in the
0062   /// partial sample profile. When 'Partial' is false, it is undefined. This is
0063   /// currently only available under thin LTO mode.
0064   double PartialProfileRatio = 0.0;
0065   /// Return detailed summary as metadata.
0066   Metadata *getDetailedSummaryMD(LLVMContext &Context);
0067 
0068 public:
0069   static const int Scale = 1000000;
0070 
0071   ProfileSummary(Kind K, const SummaryEntryVector &DetailedSummary,
0072                  uint64_t TotalCount, uint64_t MaxCount,
0073                  uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
0074                  uint32_t NumCounts, uint32_t NumFunctions,
0075                  bool Partial = false, double PartialProfileRatio = 0)
0076       : PSK(K), DetailedSummary(DetailedSummary), TotalCount(TotalCount),
0077         MaxCount(MaxCount), MaxInternalCount(MaxInternalCount),
0078         MaxFunctionCount(MaxFunctionCount), NumCounts(NumCounts),
0079         NumFunctions(NumFunctions), Partial(Partial),
0080         PartialProfileRatio(PartialProfileRatio) {}
0081 
0082   Kind getKind() const { return PSK; }
0083   /// Return summary information as metadata.
0084   Metadata *getMD(LLVMContext &Context, bool AddPartialField = true,
0085                   bool AddPartialProfileRatioField = true);
0086   /// Construct profile summary from metdata.
0087   static ProfileSummary *getFromMD(Metadata *MD);
0088   const SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
0089   uint32_t getNumFunctions() const { return NumFunctions; }
0090   uint64_t getMaxFunctionCount() const { return MaxFunctionCount; }
0091   uint32_t getNumCounts() const { return NumCounts; }
0092   uint64_t getTotalCount() const { return TotalCount; }
0093   uint64_t getMaxCount() const { return MaxCount; }
0094   uint64_t getMaxInternalCount() const { return MaxInternalCount; }
0095   void setPartialProfile(bool PP) { Partial = PP; }
0096   bool isPartialProfile() const { return Partial; }
0097   double getPartialProfileRatio() const { return PartialProfileRatio; }
0098   void setPartialProfileRatio(double R) {
0099     assert(isPartialProfile() && "Unexpected when not partial profile");
0100     PartialProfileRatio = R;
0101   }
0102   void printSummary(raw_ostream &OS) const;
0103   void printDetailedSummary(raw_ostream &OS) const;
0104 };
0105 
0106 } // end namespace llvm
0107 
0108 #endif // LLVM_IR_PROFILESUMMARY_H